-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathclusterize-cluster.vue
107 lines (100 loc) · 2.57 KB
/
clusterize-cluster.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
// out: ..
<template lang="pug">
.clusterize-cluster(
:class="{loading:loading}",
:style="{height:height+'px',overflow:'visible',position:'relative',margin:0,padding:0}"
)
.clusterize-cluster-loading(
ref="loading",
v-show="loading"
)
slot loading...
</template>
<script lang="coffee">
module.exports =
mixins: [
require("vue-mixins/vue")
]
props:
"bindingName":
type: String
default: "data"
"loading":
type: Number
default: 0
"nr":
type: Number
"index":
type: Number
"height":
type: Number
"data":
type: Array
default: -> []
"rowWatchers":
type: Object
"parentVm":
type: Object
data: ->
isCluster: true
factory: null
Vue: null
end: null
frags: []
mounted: ->
@end = @Vue.util.createAnchor('clusterize-cluster-end')
@$el.appendChild(@end)
for key,val of @rowWatchers
@initRowWatchers(key,val)
methods:
createFrag: (i) ->
parentScope = @parentVm
scope = Object.create(parentScope)
scope.$refs = Object.create(parentScope.$refs)
scope.$parent = parentScope
scope.$forContext = @
@Vue.util.defineReactive(scope,@bindingName,@data[i])
@Vue.util.defineReactive(scope,"loading",@loading)
@Vue.util.defineReactive(scope,"index",@index+i)
for key,val of @rowWatchers
@Vue.util.defineReactive(scope,key,val.vm[val.prop])
scope[key] = val.vm[val.prop]
frag = @factory.create(@, scope, @$options._frag)
frag.before(@end)
@frags[i] = frag
updateIndex: ->
for frag,i in @frags
frag.scope.index = @index+i
destroyFrag: (i) ->
@frags[i].remove()
initRowWatchers: (key,obj) ->
self = @
obj.vm.$watch obj.prop, (val) ->
for frag in self.frags
frag.scope[key] = val
redraw: ->
if @frags.length > 0
for i in [[email protected]]
@destroyFrag(i)
@createFrag(i)
watch:
"index": "updateIndex"
"factory": "redraw"
"rowWatchers": (newRW,oldRW) ->
for key,val of newRW
unless oldRW[key]?
@initRowWatchers(key,val)
data: (newData, oldData)->
diff = newData.length-oldData.length
if diff > 0
for i in [0...diff]
@createFrag(oldData.length+i)
else if diff < 0
for i in [diff...0]
@destroyFrag(oldData.length+i)
for frag,index in @frags
frag.scope.data = newData[index]
loading: (newLoading) ->
for frag in @frags
frag.scope.loading = newLoading
</script>