-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
array_proxy.js
384 lines (311 loc) · 10.3 KB
/
array_proxy.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
382
383
384
import { assert } from 'ember-metal/debug';
import { get } from 'ember-metal/property_get';
import {
isArray
} from 'ember-runtime/utils';
import { computed } from 'ember-metal/computed';
import { observer } from 'ember-metal/mixin';
import {
beginPropertyChanges,
endPropertyChanges
} from 'ember-metal/property_events';
import EmberError from 'ember-metal/error';
import EmberObject from 'ember-runtime/system/object';
import MutableArray from 'ember-runtime/mixins/mutable_array';
import Enumerable from 'ember-runtime/mixins/enumerable';
import alias from 'ember-metal/alias';
/**
@module ember
@submodule ember-runtime
*/
var OUT_OF_RANGE_EXCEPTION = 'Index out of range';
var EMPTY = [];
function K() { return this; }
/**
An ArrayProxy wraps any other object that implements `Ember.Array` and/or
`Ember.MutableArray,` forwarding all requests. This makes it very useful for
a number of binding use cases or other cases where being able to swap
out the underlying array is useful.
A simple example of usage:
```javascript
var pets = ['dog', 'cat', 'fish'];
var ap = Ember.ArrayProxy.create({ content: Ember.A(pets) });
ap.get('firstObject'); // 'dog'
ap.set('content', ['amoeba', 'paramecium']);
ap.get('firstObject'); // 'amoeba'
```
This class can also be useful as a layer to transform the contents of
an array, as they are accessed. This can be done by overriding
`objectAtContent`:
```javascript
var pets = ['dog', 'cat', 'fish'];
var ap = Ember.ArrayProxy.create({
content: Ember.A(pets),
objectAtContent: function(idx) {
return this.get('content').objectAt(idx).toUpperCase();
}
});
ap.get('firstObject'); // . 'DOG'
```
@class ArrayProxy
@namespace Ember
@extends Ember.Object
@uses Ember.MutableArray
@public
*/
var ArrayProxy = EmberObject.extend(MutableArray, {
/**
The content array. Must be an object that implements `Ember.Array` and/or
`Ember.MutableArray.`
@property content
@type Ember.Array
@private
*/
content: computed({
get() {
return this._content;
},
set(k, v) {
if (this._didInitArrayProxy) {
var oldContent = this._content;
var len = oldContent ? get(oldContent, 'length') : 0;
this.arrangedContentArrayWillChange(this, 0, len, undefined);
this.arrangedContentWillChange(this);
}
this._content = v;
return v;
}
}),
/**
The array that the proxy pretends to be. In the default `ArrayProxy`
implementation, this and `content` are the same. Subclasses of `ArrayProxy`
can override this property to provide things like sorting and filtering.
@property arrangedContent
@private
*/
arrangedContent: alias('content'),
/**
Should actually retrieve the object at the specified index from the
content. You can override this method in subclasses to transform the
content item to something new.
This method will only be called if content is non-`null`.
@method objectAtContent
@param {Number} idx The index to retrieve.
@return {Object} the value or undefined if none found
@private
*/
objectAtContent(idx) {
return get(this, 'arrangedContent').objectAt(idx);
},
/**
Should actually replace the specified objects on the content array.
You can override this method in subclasses to transform the content item
into something new.
This method will only be called if content is non-`null`.
@method replaceContent
@param {Number} idx The starting index
@param {Number} amt The number of items to remove from the content.
@param {Array} objects Optional array of objects to insert or null if no
objects.
@return {void}
@private
*/
replaceContent(idx, amt, objects) {
get(this, 'content').replace(idx, amt, objects);
},
_teardownContent(content) {
if (content) {
content.removeArrayObserver(this, {
willChange: 'contentArrayWillChange',
didChange: 'contentArrayDidChange'
});
}
},
/**
Override to implement content array `willChange` observer.
@method contentArrayWillChange
@param {Ember.Array} contentArray the content array
@param {Number} start starting index of the change
@param {Number} removeCount count of items removed
@param {Number} addCount count of items added
@private
*/
contentArrayWillChange: K,
/**
Override to implement content array `didChange` observer.
@method contentArrayDidChange
@param {Ember.Array} contentArray the content array
@param {Number} start starting index of the change
@param {Number} removeCount count of items removed
@param {Number} addCount count of items added
@private
*/
contentArrayDidChange: K,
/**
Invoked when the content property changes. Notifies observers that the
entire array content has changed.
@private
@method _contentDidChange
*/
_contentDidChange: observer('content', function() {
var content = get(this, 'content');
this._teardownContent(this._prevContent);
assert('Can\'t set ArrayProxy\'s content to itself', content !== this);
this._setupContent();
}),
_setupContent() {
var content = get(this, 'content');
this._prevContent = content;
if (content) {
assert(`ArrayProxy expects an Array or Ember.ArrayProxy, but you passed ${typeof content}`, isArray(content) || content.isDestroyed);
content.addArrayObserver(this, {
willChange: 'contentArrayWillChange',
didChange: 'contentArrayDidChange'
});
}
},
_arrangedContentDidChange: observer('arrangedContent', function() {
this._teardownArrangedContent(this._prevArrangedContent);
var arrangedContent = get(this, 'arrangedContent');
var len = arrangedContent ? get(arrangedContent, 'length') : 0;
assert('Can\'t set ArrayProxy\'s content to itself', arrangedContent !== this);
this._setupArrangedContent();
this.arrangedContentDidChange(this);
this.arrangedContentArrayDidChange(this, 0, undefined, len);
}),
_setupArrangedContent() {
var arrangedContent = get(this, 'arrangedContent');
this._prevArrangedContent = arrangedContent;
if (arrangedContent) {
assert(`ArrayProxy expects an Array or Ember.ArrayProxy, but you passed ${typeof arrangedContent}`,
isArray(arrangedContent) || arrangedContent.isDestroyed);
arrangedContent.addArrayObserver(this, {
willChange: 'arrangedContentArrayWillChange',
didChange: 'arrangedContentArrayDidChange'
});
}
},
_teardownArrangedContent() {
var arrangedContent = get(this, 'arrangedContent');
if (arrangedContent) {
arrangedContent.removeArrayObserver(this, {
willChange: 'arrangedContentArrayWillChange',
didChange: 'arrangedContentArrayDidChange'
});
}
},
arrangedContentWillChange: K,
arrangedContentDidChange: K,
objectAt(idx) {
return get(this, 'content') && this.objectAtContent(idx);
},
length: computed(function() {
var arrangedContent = get(this, 'arrangedContent');
return arrangedContent ? get(arrangedContent, 'length') : 0;
// No dependencies since Enumerable notifies length of change
}),
_replace(idx, amt, objects) {
var content = get(this, 'content');
assert('The content property of ' + this.constructor + ' should be set before modifying it', content);
if (content) {
this.replaceContent(idx, amt, objects);
}
return this;
},
replace() {
if (get(this, 'arrangedContent') === get(this, 'content')) {
this._replace(...arguments);
} else {
throw new EmberError('Using replace on an arranged ArrayProxy is not allowed.');
}
},
_insertAt(idx, object) {
if (idx > get(this, 'content.length')) {
throw new EmberError(OUT_OF_RANGE_EXCEPTION);
}
this._replace(idx, 0, [object]);
return this;
},
insertAt(idx, object) {
if (get(this, 'arrangedContent') === get(this, 'content')) {
return this._insertAt(idx, object);
} else {
throw new EmberError('Using insertAt on an arranged ArrayProxy is not allowed.');
}
},
removeAt(start, len) {
if ('number' === typeof start) {
var content = get(this, 'content');
var arrangedContent = get(this, 'arrangedContent');
var indices = [];
var i;
if ((start < 0) || (start >= get(this, 'length'))) {
throw new EmberError(OUT_OF_RANGE_EXCEPTION);
}
if (len === undefined) {
len = 1;
}
// Get a list of indices in original content to remove
for (i = start; i < start + len; i++) {
// Use arrangedContent here so we avoid confusion with objects transformed by objectAtContent
indices.push(content.indexOf(arrangedContent.objectAt(i)));
}
// Replace in reverse order since indices will change
indices.sort(function(a, b) { return b - a; });
beginPropertyChanges();
for (i = 0; i < indices.length; i++) {
this._replace(indices[i], 1, EMPTY);
}
endPropertyChanges();
}
return this;
},
pushObject(obj) {
this._insertAt(get(this, 'content.length'), obj);
return obj;
},
pushObjects(objects) {
if (!(Enumerable.detect(objects) || isArray(objects))) {
throw new TypeError('Must pass Ember.Enumerable to Ember.MutableArray#pushObjects');
}
this._replace(get(this, 'length'), 0, objects);
return this;
},
setObjects(objects) {
if (objects.length === 0) {
return this.clear();
}
var len = get(this, 'length');
this._replace(0, len, objects);
return this;
},
unshiftObject(obj) {
this._insertAt(0, obj);
return obj;
},
unshiftObjects(objects) {
this._replace(0, 0, objects);
return this;
},
slice() {
var arr = this.toArray();
return arr.slice(...arguments);
},
arrangedContentArrayWillChange(item, idx, removedCnt, addedCnt) {
this.arrayContentWillChange(idx, removedCnt, addedCnt);
},
arrangedContentArrayDidChange(item, idx, removedCnt, addedCnt) {
this.arrayContentDidChange(idx, removedCnt, addedCnt);
},
init() {
this._didInitArrayProxy = true;
this._super(...arguments);
this._setupContent();
this._setupArrangedContent();
},
willDestroy() {
this._teardownArrangedContent();
this._teardownContent(this.get('content'));
}
});
export default ArrayProxy;