-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathclusterize.vue
374 lines (348 loc) · 10.9 KB
/
clusterize.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// out: ..
<template lang="pug">
.clusterize(
:style="computedStyle",
:class="{'loading':state.loading, 'not-started':!state.started}",
@scroll="onScroll"
)
.clusterize-first-row(ref="first-row" v-bind:style="{height:firstRowHeight+'px'}")
clusterize-cluster(v-bind:binding-name="bindingName" v-bind:row-watchers="rowWatchers" v-bind:parent-vm="parentVm")
slot(name="loading")
clusterize-cluster(v-bind:binding-name="bindingName" v-bind:row-watchers="rowWatchers" v-bind:parent-vm="parentVm")
slot(name="loading")
clusterize-cluster(v-bind:binding-name="bindingName" v-bind:row-watchers="rowWatchers" v-bind:parent-vm="parentVm")
slot(name="loading")
.clusterize-last-row(ref="last-row" v-bind:style="{height:lastRowHeight+'px'}")
</template>
<script lang="coffee">
module.exports =
mixins: [
require "vue-mixins/onElementResize"
require "vue-mixins/vue"
require "vue-mixins/fragToString"
]
components:
"clusterize-cluster": require "./clusterize-cluster"
props:
"bindingName":
type: String
default: "data"
"height":
type: Number
"autoHeight":
type: Boolean
default: false
"manualStart":
type: Boolean
default: false
"data":
type: Array
"scrollTop":
type: Number
default: 0
"scrollLeft":
type: Number
default: 0
"clusterSizeFac":
type: Number
default: 1.5
"rowHeight":
type: Number
"template":
type: String
"style":
type: Object
"rowWatchers":
type: Object
default: -> {height: {vm: @, prop:"rowHeight"}}
"parentVm":
type: Object
default: -> @$parent
"flex":
type: Boolean
default: false
"flexInitial":
type: Number
default: 20
"flexFac":
type: Number
default: 1
computed:
position: ->
if @autoHeight
@disposeResizeCb = @onElementResize @$el, @updateHeight unless @disposeResizeCb?
return "absolute"
else if @flex
@disposeResizeCb = @onElementResize @$el, @updateHeight unless @disposeResizeCb?
else
@disposeResizeCb?()
return null
computedStyle: ->
return null unless @state.started
style =
height: @height+'px'
position: @position
top: if @autoHeight then 0 else null
bottom: if @autoHeight then 0 else null
left: if @autoHeight then 0 else null
right: if @autoHeight then 0 else null
overflow: "auto"
if @style?
for key,val of @style
style[key] = val
return style
data: ->
clusters: []
firstRowHeight: null
lastRowHeight: null
rowCount: null
rowsCount: null
itemsPerRow: 1
clustersCount: null
clusterHeight: null
clusterSize: null
clustersBelow: 2
clusterVisible: 0
clusterVisibleLast: -1
offsetHeight: 0
itemWidth: 0
minHeight: null
lastScrollTop: @scrollTop
lastScrollLeft: @scrollLeft
state:
started: false
startFinished: false
loading: false
methods:
updateHeight: ->
process = =>
if @flex
oldData = @clusters[0].data
tmp = []
for data in @clusters[0].data
tmp = tmp.concat data
break if tmp.length >= @flexInitial
@clusters[0].data = [tmp]
@$nextTick =>
@calcRowHeight()
@processClusterChange(@$el.scrollTop,true)
else
@calcClusterSize()
@processClusterChange(@$el.scrollTop,true)
if @state.startFinished and @rowHeight > -1
changedHeight = Math.abs(@offsetHeight-@$el.offsetHeight)/@clusterHeight*@clusterSizeFac > 0.2
if @flex
changedWidth = @$el.clientWidth - @itemsPerRow*@itemWidth
if changedWidth > @itemWidth or changedWidth < 1
process()
else if changedHeight
process()
start: (top = @$el.scrollTop) ->
@state.started = true
@processTemplate()
@state.loading = true
if @data?
@$watch("data", @processData) # watch data only if static
count = 0
if @flex
count = @flexInitial
if not @rowHeight or @flex
@getData 0,count, (data) =>
@getAndProcessDataCount()
@clusters[0].index = 0
if @flex
@clusters[0].data = [data]
else
@clusters[0].data = data
@$nextTick =>
@calcRowHeight()
@processScroll(top)
@state.startFinished = true
else
@getAndProcessDataCount()
@$nextTick =>
@calcClusterSize()
@processScroll(top)
@state.startFinished = true
getData: (first,last,cb) ->
if @data?
cb(@data[first..last])
else
@$emit("get-data",first,last,cb)
getAndProcessDataCount: ->
getDataCount = (cb) =>
if @data?
cb(@data.length)
else
@$emit("get-data-count",cb)
processDataCount = (count) =>
if count > 0
@dataCount = count
@clustersCount = Math.ceil(@dataCount / @itemsPerRow / @clusterSize)
@updateLastRowHeight()
getDataCount processDataCount
calcRowHeight: ->
if @flex
maxHeights = [0]
el = @clusters[0].$el
lastTop = Number.MIN_VALUE
itemsPerRow = []
itemsPerRowLast = 0
row = el.children[1]
items = row.children.length-1
width = 0
k = 0
for i in [1..items]
child = row.children[i]
return unless child
rect = child.getBoundingClientRect()
style = window.getComputedStyle(child)
height = rect.height + parseInt(style.marginTop,10) + parseInt(style.marginBottom,10)
width += rect.width
if rect.top > lastTop + maxHeights[k]*1/3 and i > 1
j = i-1
k++
itemsPerRow.push j-itemsPerRowLast
itemsPerRowLast = j
lastTop = rect.top
maxHeights.push height
else
if lastTop < rect.top
lastTop = rect.top
if maxHeights[maxHeights.length-1] < height
maxHeights[maxHeights.length-1] = height
itemsPerRow.shift()
maxHeights.shift()
if itemsPerRow.length > 0
@itemsPerRow = Math.floor(itemsPerRow.reduce((a,b)->a+b)/itemsPerRow.length*@flexFac)
else
@itemsPerRow = items
@itemsPerRow = 1 if @itemsPerRow == 0
@itemWidth = width / items
if maxHeights.length > 0
@rowHeight = maxHeights.reduce((a,b)->a+b)/maxHeights.length
else
@rowHeight = height
else
@rowHeight = @clusters[0].$el.children[1].getBoundingClientRect().height
@calcClusterSize()
calcClusterSize: ->
@offsetHeight = @$el.offsetHeight
@clusterSize = Math.ceil(@$el.offsetHeight/@rowHeight*@clusterSizeFac)*@itemsPerRow
if @dataCount
@clustersCount = Math.ceil(@dataCount / @itemsPerRow / @clusterSize)
@clustersCount = 3 if @clustersCount < 3
@updateLastRowHeight()
@clusterHeight = @rowHeight*@clusterSize/@itemsPerRow
for cluster in @clusters
cluster.height = @clusterHeight
updateLastRowHeight: ->
if @dataCount and @clusterSize
newHeight = (@dataCount - (@clusterVisible+@clustersBelow+1)*@clusterSize)*@rowHeight / @itemsPerRow
if newHeight > 0
@lastRowHeight = newHeight
else
@lastRowHeight = 0
processScroll: (top) ->
@clusterVisible = Math.floor(top/@clusterHeight+0.5)
if @clusterVisibleLast != @clusterVisible
@processClusterChange(top)
@clusterVisibleLast = @clusterVisible
processClusterChange: (top = @$el.scrollTop, repaint = false) ->
down = @clusterVisibleLast < @clusterVisible # is scrolling down
# calculating how many clusters will be below the currently visible
if @clusterVisible == 0 # first
@clustersBelow = 2
else if @clusterVisible == @clustersCount-1 # last
@clustersBelow = 0
else
@clustersBelow = 1 #
position = @clusterVisible + @clustersBelow
# calculate absolute numbers of visible clusters
if down
absIs = [position-2..position]
else
absIs = [position..position-2]
for absI in absIs
relI = absI%3
if @clusters[relI].nr != absI or repaint# if position of cluster changed
# move the cluster
if down
@clusters[relI].$before @$els.lastRow
else
@clusters[relI].$after @$els.firstRow
@clusters[relI].nr = absI
@clusters[relI].index = absI*@clusterSize
# change data of the moving cluster
@fillClusterWithData @clusters[relI], absI*@clusterSize, (absI+1)*@clusterSize-1
@updateFirstRowHeight()
@updateLastRowHeight()
fillClusterWithData: (cluster,first,last) ->
if @state.loading
@state.loading = false
@$emit "clusterize-loaded"
cluster.loading += 1
loading = cluster.loading
@$emit "cluster-loading", cluster.nr
@getData first, last, (data) =>
if cluster.loading == loading
if data.length != @clusterSize
cluster.height = data.length * @rowHeight / @itemsPerRow
else
cluster.height = @clusterHeight
if @flex
data2 = []
currentData = []
for d,i in data
if i %% @itemsPerRow == 0
currentData = []
data2.push currentData
currentData.push d
cluster.data = data2
else
cluster.data = data
cluster.loading = 0
@$emit "cluster-loaded", cluster.nr
updateFirstRowHeight: ->
newHeight = (@clusterVisible-(2-@clustersBelow))*@clusterHeight
if newHeight > 0
@firstRowHeight = newHeight
else
@firstRowHeight = 0
onScroll: (e) ->
top = @$el.scrollTop
@$emit "scroll-y", top
@$emit "scroll-x", @$el.scrollLeft
if @lastScrollTop != top
@lastScrollTop = top
@processScroll(top)
processData: (newData, oldData) ->
if newData != oldData
@redraw()
redraw: ->
@getAndProcessDataCount()
@processClusterChange(@$el.scrollTop,true)
processTemplate: ->
if @state.started
unless @template
@template = @fragToString(@_slotContents.default)
factory = new @Vue.FragmentFactory @parentVm, @template
for cluster in @clusters
cluster.factory = factory
mounted: ->
for child in @$children
if child.isCluster
@clusters.push child
unless @manualStart
@start()
watch:
"height" : "updateHeight"
"scrollTop": (val) ->
if val != @$el.scrollTop
@$el.scrollTop = val
@processScroll(val)
"template": "processTemplate"
"rowWatchers": (val) ->
val.height = {vm: @, prop:"rowHeight"} unless val.height?
return val
</script>