-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
381 lines (322 loc) · 7.47 KB
/
index.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
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
375
376
377
378
379
380
381
/**
* Module dependencies.
*/
var Enumerable = require('enumerable')
, Emitter = require('emitter')
, bind = require('bind')
, each = require('each')
, inherit = require('inherit');
/**
* Module exports.
*/
module.exports = Collection;
/**
* Initialize new collection.
*/
function Collection(models, type) {
if (typeof models === 'function') {
type = models;
models = [];
}
/**
* Private variables.
*/
var self = this;
var dirty = 0;
// flag temporarily set by .reset() to indicate that no actions
// should be generated
//
// TODO: turn this into something less ugly
var silent = false;
// track actions on the collection to be able to reset it
var actions = [];
models = models || [];
/**
* Properties.
*/
// rescue old code
// deprecated
Object.defineProperty(this, '_models', {
get: function(){
console.log('Collection()._models is deprecated, use Collection().items');
return this.items;
}
});
// deprecated
Object.defineProperty(this, 'items', {
get: function(){
return models;
}
});
Object.defineProperty(this, 'dirty', {
get: function(){
return dirty !== 0;
}
});
Object.defineProperty(this, 'length', {
get: function(){
return models.length;
}
});
Object.defineProperty(this, 'type', {
value: type || null,
writable: false
});
Object.defineProperty(this, 'added', {
value: [],
writable: false
});
Object.defineProperty(this, 'removed', {
value: [],
writable: false
});
/**
* Public methods.
*/
this.insert = insert;
this.remove = remove;
this.move = move;
this.push = push;
this.pop = pop;
this.reset = reset;
this.pin = this.resetDirty = pin;
this.clear = clear;
this.toJSON = toJSON;
this.update = update;
this.__iterate__ = iterate;
// ensure types of models
each(models, function(m, i){
m = ensureType(m);
models[i] = m;
bind(m);
});
/**
* Insert a new model at `index`.
*/
function insert(model, index) {
model = ensureType(model);
action('remove', [ index ]);
_insert(model, index);
bind(model);
this.emit('insert', model, index);
return model;
};
/**
* Remove the model at `index`.
*/
function remove(index) {
var model = _remove(index);
action('insert', [ model, index ]);
unbind(model);
this.emit('remove', index, model);
return model;
};
function move(from, to) {
if (from === to) return;
action('move', [ to, from ]);
var model = _remove(from);
_insert(model, to);
this.emit('move', from, to);
return model;
};
function push(model) {
if (model.each) return model.each(bind(this, this.push));
var index = models.length;
return this.insert(model, index);
};
function pop() {
var index = this.length - 1;
return this.remove(index);
};
/**
* Reset the collection to its original state.
*/
function reset() {
silent = true;
try {
for (var action; actions.length > 0;) {
action = actions.pop();
this[action.method].apply(this, action.args);
}
this.each(function(model){
model.reset();
});
} catch (e) {
// be sure that silent is reset
silent = false;
throw e;
}
silent = false;
}
/**
* Pin the collection to the current state.
*/
function pin() {
var wasDirty = dirty > 0;
this.each(function(child){
child.resetDirty();
});
this.added.length = [];
this.removed.length = [];
actions.length = 0;
dirty = 0;
if (wasDirty) emitDirty(false, true);
};
/**
* Clear the collection.
*/
function clear() {
this.each(function(model){
setTimeout(function(){
self.remove(self.indexOf(model));
}, 0);
});
return this;
};
function toJSON() {
var a = [];
this.each(function(model){
if (typeof model.toJSON == 'function') model = model.toJSON();
a.push(model);
});
return a;
};
function update(collection) {
/**
* Remove old models from collection.
*/
var remove = [];
this.each(function(model, index){
if (model == null) return;
if (!~collection.indexOf(model)) {
remove.push({
index: index,
model: model
});
}
});
for (var i = remove.length-1; i >= 0; --i) {
this.remove(remove[i].index);
}
/**
* Insert new models, if not already in.
*/
collection.each(function(model, index){
var old = self.indexOf(model);
if (!~old) self.push(model);
});
/**
* Sort collection as the new col.
*/
collection.each(function(model, index){
var old = self.indexOf(model);
self.move(old, index);
});
};
/**
* Satisfy iteration API.
*/
function iterate() {
return {
length: function(){ return models.length; },
get: function(i){ return models[i]; }
};
};
function action(method, args) {
if (silent) return;
actions.push({ method: method, args: args });
}
function _insert(model, index) {
models.splice(index, 0, model);
if (~self.removed.indexOf(model)) {
self.removed.splice(self.removed.indexOf(model), 1);
updateDirty(-1);
} else {
self.added.push(model);
updateDirty(1);
}
}
function _remove(index) {
var model = models.splice(index, 1)[0];
if (~self.added.indexOf(model)) {
self.added.splice(self.added.indexOf(model), 1);
updateDirty(-1);
if (model.dirty) updateDirty(-1);
} else {
// TODO save index were it was removed
self.removed.push(model);
updateDirty(1);
}
return model;
}
function handleDestroy() {
var index = models.indexOf(this);
self.remove(index);
};
/**
* Is bound as event handler to the destroying of a model.
*/
function handleChange(name, value) {
if (name == 'dirty') {
if (value == true) return updateDirty(1);
if (value == false) return updateDirty(-1);
}
var args = [].slice.call(arguments, 1);
var index = models.indexOf(this);
name = 'items.' + index + '.' + name;
args.unshift(name);
args.unshift('change');
self.emit.apply(self, args);
};
function updateDirty(v) {
dirty += v;
if (dirty === 1 && v === 1) return emitDirty(true, false);
if (dirty == 0) return emitDirty(false, true);
}
function emitDirty(value, old) {
self.emit('change', 'dirty', value, old);
self.emit('change dirty', value, old);
}
/**
* Bind the events of a newly inserted model.
*/
function bind(model) {
var self = this;
model.on('destroy', handleDestroy);
model.on('change', handleChange);
}
function unbind(model) {
model.off('destroy', handleDestroy);
model.off('change', handleChange);
}
function ensureType(model) {
var type = self.type;
if (typeof type !== 'function') return model;
if (!(model instanceof type)) {
return new type(model);
}
return model;
}
}
/**
* Creates a constructor which encloses `type`.
*/
Collection.type = function(type){
function TypedCollection(models) {
Collection.call(this, models, type);
}
inherit(TypedCollection, Collection);
TypedCollection.use = Collection.use;
return TypedCollection;
};
/**
* Syntactic sugar to insert a plugin.
*/
Collection.use = function(fn){
fn(this);
return this;
};
// emitter mixin
Emitter(Collection.prototype);
// enumerable mixin
Enumerable(Collection.prototype);