forked from RubyLouvre/avalon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08 Collection.js
235 lines (230 loc) · 7.13 KB
/
08 Collection.js
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
/*********************************************************************
* 监控数组(与ms-each, ms-repeat配合使用) *
**********************************************************************/
function Collection(model) {
var array = []
array.$id = generateID()
array.$model = model //数据模型
array.$events = {}
array.$events[subscribers] = []
array._ = modelFactory({
length: model.length
})
array._.$watch("length", function(a, b) {
array.$fire("length", a, b)
})
for (var i in EventBus) {
array[i] = EventBus[i]
}
avalon.mix(array, CollectionPrototype)
return array
}
function mutateArray(method, pos, n, index, method2, pos2, n2) {
var oldLen = this.length, loop = 2
while (--loop) {
switch (method) {
case "add":
var array = this.$model.slice(pos, pos + n).map(function(el) {
if (rcomplexType.test(avalon.type(el))) {
return el.$id ? el : modelFactory(el, 0, el)
} else {
return el
}
})
_splice.apply(this, [pos, 0].concat(array))
this._fire("add", pos, n)
break
case "del":
var ret = this._splice(pos, n)
this._fire("del", pos, n)
break
}
if (method2) {
method = method2
pos = pos2
n = n2
loop = 2
method2 = 0
}
}
this._fire("index", index)
if (this.length !== oldLen) {
this._.length = this.length
}
return ret
}
var _splice = ap.splice
var CollectionPrototype = {
_splice: _splice,
_fire: function(method, a, b) {
notifySubscribers(this.$events[subscribers], method, a, b)
},
size: function() { //取得数组长度,这个函数可以同步视图,length不能
return this._.length
},
pushArray: function(array) {
var m = array.length, n = this.length
if (m) {
ap.push.apply(this.$model, array)
mutateArray.call(this, "add", n, m, n)
}
return m + n
},
push: function() {
//http://jsperf.com/closure-with-arguments
var array = []
var i, n = arguments.length
for (i = 0; i < n; i++) {
array[i] = arguments[i]
}
return this.pushArray(arguments)
},
unshift: function() {
var m = arguments.length, n = this.length
if (m) {
ap.unshift.apply(this.$model, arguments)
mutateArray.call(this, "add", 0, m, 0)
}
return m + n //IE67的unshift不会返回长度
},
shift: function() {
if (this.length) {
var el = this.$model.shift()
mutateArray.call(this, "del", 0, 1, 0)
return el //返回被移除的元素
}
},
pop: function() {
var m = this.length
if (m) {
var el = this.$model.pop()
mutateArray.call(this, "del", m - 1, 1, Math.max(0, m - 2))
return el //返回被移除的元素
}
},
splice: function(start) {
var m = arguments.length, args = [], change
var removed = _splice.apply(this.$model, arguments)
if (removed.length) { //如果用户删掉了元素
args.push("del", start, removed.length, 0)
change = true
}
if (m > 2) { //如果用户添加了元素
if (change) {
args.splice(3, 1, 0, "add", start, m - 2)
} else {
args.push("add", start, m - 2, 0)
}
change = true
}
if (change) { //返回被移除的元素
return mutateArray.apply(this, args)
} else {
return []
}
},
contains: function(el) { //判定是否包含
return this.indexOf(el) !== -1
},
remove: function(el) { //移除第一个等于给定值的元素
return this.removeAt(this.indexOf(el))
},
removeAt: function(index) { //移除指定索引上的元素
if (index >= 0) {
this.$model.splice(index, 1)
return mutateArray.call(this, "del", index, 1, 0)
}
return []
},
clear: function() {
this.$model.length = this.length = this._.length = 0 //清空数组
this._fire("clear", 0)
return this
},
removeAll: function(all) { //移除N个元素
if (Array.isArray(all)) {
all.forEach(function(el) {
this.remove(el)
}, this)
} else if (typeof all === "function") {
for (var i = this.length - 1; i >= 0; i--) {
var el = this[i]
if (all(el, i)) {
this.removeAt(i)
}
}
} else {
this.clear()
}
},
ensure: function(el) {
if (!this.contains(el)) { //只有不存在才push
this.push(el)
}
return this
},
set: function(index, val) {
if (index >= 0) {
var valueType = avalon.type(val)
if (val && val.$model) {
val = val.$model
}
var target = this[index]
if (valueType === "object") {
for (var i in val) {
if (target.hasOwnProperty(i)) {
target[i] = val[i]
}
}
} else if (valueType === "array") {
target.clear().push.apply(target, val)
} else if (target !== val) {
this[index] = val
this.$model[index] = val
this._fire("set", index, val)
}
}
return this
}
}
function sortByIndex(array, indexes) {
var map = {};
for (var i = 0, n = indexes.length; i < n; i++) {
map[i] = array[i] // preserve
var j = indexes[i]
if (j in map) {
array[i] = map[j]
delete map[j]
} else {
array[i] = array[j]
}
}
}
"sort,reverse".replace(rword, function(method) {
CollectionPrototype[method] = function() {
var newArray = this.$model//这是要排序的新数组
var oldArray = newArray.concat() //保持原来状态的旧数组
var mask = Math.random()
var indexes = []
var hasSort
ap[method].apply(newArray, arguments) //排序
for (var i = 0, n = oldArray.length; i < n; i++) {
var neo = newArray[i]
var old = oldArray[i]
if (isEqual(neo, old)) {
indexes.push(i)
} else {
var index = oldArray.indexOf(neo)
indexes.push(index)//得到新数组的每个元素在旧数组对应的位置
oldArray[index] = mask //屏蔽已经找过的元素
hasSort = true
}
}
if (hasSort) {
sortByIndex(this, indexes)
this._fire("move", indexes)
this._fire("index", 0)
}
return this
}
})