-
Notifications
You must be signed in to change notification settings - Fork 8
/
ampersand-subcollection.js
232 lines (196 loc) · 7.02 KB
/
ampersand-subcollection.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
/*$AMPERSAND_VERSION*/
var Events = require('ampersand-events');
var classExtend = require('ampersand-class-extend');
var lodashMixins = require('ampersand-collection-lodash-mixin');
var slice = Array.prototype.slice;
var assign = require('lodash.assign');
var difference = require('lodash.difference');
var each = require('lodash.foreach');
var isArray = require('lodash.isarray');
var keys = require('lodash.keys');
var reduce = require('lodash.reduce');
var sortBy = require('lodash.sortby');
var union = require('lodash.union');
var includes = require('lodash.includes');
var isEqual = require('lodash.isequal');
function SubCollection(collection, spec) {
this.collection = collection;
this.models = [];
this.configure(spec || {}, true);
this.listenTo(this.collection, 'all', this._onCollectionEvent);
}
assign(SubCollection.prototype, Events, lodashMixins, {
// add a filter function directly
addFilter: function (filter) {
this.swapFilters([filter], []);
},
// remove filter function directly
removeFilter: function (filter) {
this.swapFilters([], [filter]);
},
// clears filters fires events for changes
clearFilters: function () {
this._resetFilters();
this._runFilters();
},
// Swap out a set of old filters with a set of
// new filters
swapFilters: function (newFilters, oldFilters) {
var self = this;
if (!oldFilters) {
oldFilters = this._filters;
} else if (!isArray(oldFilters)) {
oldFilters = [oldFilters];
}
if (!newFilters) {
newFilters = [];
} else if (!isArray(newFilters)) {
newFilters = [newFilters];
}
oldFilters.forEach(function (filter) {
self._removeFilter(filter);
});
newFilters.forEach(function (filter) {
self._addFilter(filter);
});
this._runFilters();
},
// Update sub collection config, if `clear`
// then clear existing spec before start.
// This takes all the same filter arguments
// as the init function. So you can pass:
// {
// where: {
// name: 'something'
// },
// limit: 20
// }
configure: function (opts, clear) {
if (clear) this._resetFilters(clear);
//assign(this._spec, opts);
this._parseSpec(opts);
this._runFilters();
},
// gets a model at a given index
at: function (index) {
return this.models[index];
},
// proxy `get` method to the underlying collection
get: function (query, indexName) {
var model = this.collection.get(query, indexName);
if (model && this.includes(model)) return model;
},
// remove filter if found
_removeFilter: function (filter) {
var index = this._filters.indexOf(filter);
if (index !== -1) {
this._filters.splice(index, 1);
}
},
// clear all filters, reset everything
reset: function () {
this.configure({}, true);
},
// just reset filters, no model changes
_resetFilters: function (resetComparator) {
this._filters = [];
this._watched = [];
this.limit = undefined;
this.offset = undefined;
if (resetComparator) this.comparator = undefined;
},
// internal method registering new filter function
_addFilter: function (filter) {
this._filters.push(filter);
},
// adds a property or array of properties to watch, ensures uniqueness.
_watch: function (item) {
this._watched = union(this._watched, isArray(item) ? item : [item]);
},
// removes a watched property
_unwatch: function (item) {
this._watched = difference(this._watched, isArray(item) ? item : [item]);
},
_parseSpec: function (spec) {
if (spec.watched) this._watch(spec.watched);
if (spec.comparator) this.comparator = spec.comparator;
if (spec.where) {
each(spec.where, function (value, item) {
this._addFilter(function (model) {
return (model.get ? model.get(item) : model[item]) === value;
});
}, this);
// also make sure we watch all `where` keys
this._watch(keys(spec.where));
}
if (spec.hasOwnProperty('limit')) this.limit = spec.limit;
if (spec.hasOwnProperty('offset')) this.offset = spec.offset;
if (spec.filter) {
this._addFilter(spec.filter);
}
if (spec.filters) {
spec.filters.forEach(this._addFilter, this);
}
},
_runFilters: function () {
// make a copy of the array for comparisons
var existingModels = slice.call(this.models);
var rootModels = slice.call(this.collection.models);
var offset = (this.offset || 0);
var newModels, toAdd, toRemove;
// reduce base model set by applying filters
if (this._filters.length) {
newModels = reduce(this._filters, function (startingArray, filterFunc) {
return startingArray.filter(filterFunc);
}, rootModels);
} else {
newModels = slice.call(rootModels);
}
// sort it
if (this.comparator) newModels = sortBy(newModels, this.comparator);
// trim it to length
if (this.limit || this.offset) {
// Cache a reference to the full filtered set to allow this.filtered.length. Ref: #6
this.filtered = newModels;
newModels = newModels.slice(offset, this.limit + offset);
}
// now we've got our new models time to compare
toAdd = difference(newModels, existingModels);
toRemove = difference(existingModels, newModels);
// save 'em
this.models = newModels;
each(toRemove, function (model) {
this.trigger('remove', model, this);
}, this);
each(toAdd, function (model) {
this.trigger('add', model, this);
}, this);
// unless we have the same models in same order trigger `sort`
if (!isEqual(existingModels, newModels) && this.comparator) {
this.trigger('sort', this);
}
},
_onCollectionEvent: function (eventName, model) {
var propName = eventName.split(':')[1];
// conditions under which we should re-run filters
if (propName === this.comparator || includes(this._watched, propName) || includes(['add', 'remove', 'reset', 'sync'], eventName)) {
this._runFilters();
}
// conditions under which we should proxy the events
if (!includes(['add', 'remove'], eventName) && this.includes(model)) {
this.trigger.apply(this, arguments);
}
}
});
Object.defineProperty(SubCollection.prototype, 'length', {
get: function () {
return this.models.length;
}
});
Object.defineProperty(SubCollection.prototype, 'isCollection', {
get: function () {
return true;
}
});
SubCollection.extend = classExtend;
module.exports = SubCollection;