-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
array.js
536 lines (435 loc) · 14.9 KB
/
array.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/**
@module ember
@submodule ember-runtime
*/
// ..........................................................
// HELPERS
//
import Ember from 'ember-metal/core'; // ES6TODO: Ember.A
import { get } from 'ember-metal/property_get';
import {
computed,
cacheFor
} from 'ember-metal/computed';
import isNone from 'ember-metal/is_none';
import Enumerable from 'ember-runtime/mixins/enumerable';
import { Mixin } from 'ember-metal/mixin';
import {
propertyWillChange,
propertyDidChange
} from 'ember-metal/property_events';
import {
addListener,
removeListener,
sendEvent,
hasListeners
} from 'ember-metal/events';
import EachProxy from 'ember-runtime/system/each_proxy';
function arrayObserversHelper(obj, target, opts, operation, notify) {
var willChange = (opts && opts.willChange) || 'arrayWillChange';
var didChange = (opts && opts.didChange) || 'arrayDidChange';
var hasObservers = get(obj, 'hasArrayObservers');
if (hasObservers === notify) {
propertyWillChange(obj, 'hasArrayObservers');
}
operation(obj, '@array:before', target, willChange);
operation(obj, '@array:change', target, didChange);
if (hasObservers === notify) {
propertyDidChange(obj, 'hasArrayObservers');
}
return obj;
}
// ..........................................................
// ARRAY
//
/**
This mixin implements Observer-friendly Array-like behavior. It is not a
concrete implementation, but it can be used up by other classes that want
to appear like arrays.
For example, ArrayProxy is a concrete classes that can
be instantiated to implement array-like behavior. Both of these classes use
the Array Mixin by way of the MutableArray mixin, which allows observable
changes to be made to the underlying array.
Unlike `Ember.Enumerable,` this mixin defines methods specifically for
collections that provide index-ordered access to their contents. When you
are designing code that needs to accept any kind of Array-like object, you
should use these methods instead of Array primitives because these will
properly notify observers of changes to the array.
Although these methods are efficient, they do add a layer of indirection to
your application so it is a good idea to use them only when you need the
flexibility of using both true JavaScript arrays and "virtual" arrays such
as controllers and collections.
You can use the methods defined in this module to access and modify array
contents in a KVO-friendly way. You can also be notified whenever the
membership of an array changes by using `.observes('myArray.[]')`.
To support `Ember.Array` in your own class, you must override two
primitives to use it: `length()` and `objectAt()`.
Note that the Ember.Array mixin also incorporates the `Ember.Enumerable`
mixin. All `Ember.Array`-like objects are also enumerable.
@class Array
@namespace Ember
@uses Ember.Enumerable
@since Ember 0.9.0
@public
*/
export default Mixin.create(Enumerable, {
/**
__Required.__ You must implement this method to apply this mixin.
Your array must support the `length` property. Your replace methods should
set this property whenever it changes.
@property {Number} length
@public
*/
length: null,
/**
Returns the object at the given `index`. If the given `index` is negative
or is greater or equal than the array length, returns `undefined`.
This is one of the primitives you must implement to support `Ember.Array`.
If your object supports retrieving the value of an array item using `get()`
(i.e. `myArray.get(0)`), then you do not need to implement this method
yourself.
```javascript
var arr = ['a', 'b', 'c', 'd'];
arr.objectAt(0); // 'a'
arr.objectAt(3); // 'd'
arr.objectAt(-1); // undefined
arr.objectAt(4); // undefined
arr.objectAt(5); // undefined
```
@method objectAt
@param {Number} idx The index of the item to return.
@return {*} item at index or undefined
@public
*/
objectAt(idx) {
if (idx < 0 || idx >= get(this, 'length')) {
return undefined;
}
return get(this, idx);
},
/**
This returns the objects at the specified indexes, using `objectAt`.
```javascript
var arr = ['a', 'b', 'c', 'd'];
arr.objectsAt([0, 1, 2]); // ['a', 'b', 'c']
arr.objectsAt([2, 3, 4]); // ['c', 'd', undefined]
```
@method objectsAt
@param {Array} indexes An array of indexes of items to return.
@return {Array}
@public
*/
objectsAt(indexes) {
var self = this;
return indexes.map(function(idx) {
return self.objectAt(idx);
});
},
// overrides Ember.Enumerable version
nextObject(idx) {
return this.objectAt(idx);
},
/**
This is the handler for the special array content property. If you get
this property, it will return this. If you set this property to a new
array, it will replace the current content.
This property overrides the default property defined in `Ember.Enumerable`.
@property []
@return this
@public
*/
'[]': computed({
get(key) {
return this;
},
set(key, value) {
this.replace(0, get(this, 'length'), value);
return this;
}
}),
firstObject: computed(function() {
return this.objectAt(0);
}),
lastObject: computed(function() {
return this.objectAt(get(this, 'length') - 1);
}),
// optimized version from Enumerable
contains(obj) {
return this.indexOf(obj) >= 0;
},
// Add any extra methods to Ember.Array that are native to the built-in Array.
/**
Returns a new array that is a slice of the receiver. This implementation
uses the observable array methods to retrieve the objects for the new
slice.
```javascript
var arr = ['red', 'green', 'blue'];
arr.slice(0); // ['red', 'green', 'blue']
arr.slice(0, 2); // ['red', 'green']
arr.slice(1, 100); // ['green', 'blue']
```
@method slice
@param {Number} beginIndex (Optional) index to begin slicing from.
@param {Number} endIndex (Optional) index to end the slice at (but not included).
@return {Array} New array with specified slice
@public
*/
slice(beginIndex, endIndex) {
var ret = Ember.A();
var length = get(this, 'length');
if (isNone(beginIndex)) {
beginIndex = 0;
}
if (isNone(endIndex) || (endIndex > length)) {
endIndex = length;
}
if (beginIndex < 0) {
beginIndex = length + beginIndex;
}
if (endIndex < 0) {
endIndex = length + endIndex;
}
while (beginIndex < endIndex) {
ret[ret.length] = this.objectAt(beginIndex++);
}
return ret;
},
/**
Returns the index of the given object's first occurrence.
If no `startAt` argument is given, the starting location to
search is 0. If it's negative, will count backward from
the end of the array. Returns -1 if no match is found.
```javascript
var arr = ['a', 'b', 'c', 'd', 'a'];
arr.indexOf('a'); // 0
arr.indexOf('z'); // -1
arr.indexOf('a', 2); // 4
arr.indexOf('a', -1); // 4
arr.indexOf('b', 3); // -1
arr.indexOf('a', 100); // -1
```
@method indexOf
@param {Object} object the item to search for
@param {Number} startAt optional starting location to search, default 0
@return {Number} index or -1 if not found
@public
*/
indexOf(object, startAt) {
var len = get(this, 'length');
var idx;
if (startAt === undefined) {
startAt = 0;
}
if (startAt < 0) {
startAt += len;
}
for (idx = startAt; idx < len; idx++) {
if (this.objectAt(idx) === object) {
return idx;
}
}
return -1;
},
/**
Returns the index of the given object's last occurrence.
If no `startAt` argument is given, the search starts from
the last position. If it's negative, will count backward
from the end of the array. Returns -1 if no match is found.
```javascript
var arr = ['a', 'b', 'c', 'd', 'a'];
arr.lastIndexOf('a'); // 4
arr.lastIndexOf('z'); // -1
arr.lastIndexOf('a', 2); // 0
arr.lastIndexOf('a', -1); // 4
arr.lastIndexOf('b', 3); // 1
arr.lastIndexOf('a', 100); // 4
```
@method lastIndexOf
@param {Object} object the item to search for
@param {Number} startAt optional starting location to search, default 0
@return {Number} index or -1 if not found
@public
*/
lastIndexOf(object, startAt) {
var len = get(this, 'length');
var idx;
if (startAt === undefined || startAt >= len) {
startAt = len - 1;
}
if (startAt < 0) {
startAt += len;
}
for (idx = startAt; idx >= 0; idx--) {
if (this.objectAt(idx) === object) {
return idx;
}
}
return -1;
},
// ..........................................................
// ARRAY OBSERVERS
//
/**
Adds an array observer to the receiving array. The array observer object
normally must implement two methods:
* `arrayWillChange(observedObj, start, removeCount, addCount)` - This method will be
called just before the array is modified.
* `arrayDidChange(observedObj, start, removeCount, addCount)` - This method will be
called just after the array is modified.
Both callbacks will be passed the observed object, starting index of the
change as well a a count of the items to be removed and added. You can use
these callbacks to optionally inspect the array during the change, clear
caches, or do any other bookkeeping necessary.
In addition to passing a target, you can also include an options hash
which you can use to override the method names that will be invoked on the
target.
@method addArrayObserver
@param {Object} target The observer object.
@param {Object} opts Optional hash of configuration options including
`willChange` and `didChange` option.
@return {Ember.Array} receiver
@public
*/
addArrayObserver(target, opts) {
return arrayObserversHelper(this, target, opts, addListener, false);
},
/**
Removes an array observer from the object if the observer is current
registered. Calling this method multiple times with the same object will
have no effect.
@method removeArrayObserver
@param {Object} target The object observing the array.
@param {Object} opts Optional hash of configuration options including
`willChange` and `didChange` option.
@return {Ember.Array} receiver
@public
*/
removeArrayObserver(target, opts) {
return arrayObserversHelper(this, target, opts, removeListener, true);
},
/**
Becomes true whenever the array currently has observers watching changes
on the array.
@property {Boolean} hasArrayObservers
@public
*/
hasArrayObservers: computed(function() {
return hasListeners(this, '@array:change') || hasListeners(this, '@array:before');
}),
/**
If you are implementing an object that supports `Ember.Array`, call this
method just before the array content changes to notify any observers and
invalidate any related properties. Pass the starting index of the change
as well as a delta of the amounts to change.
@method arrayContentWillChange
@param {Number} startIdx The starting index in the array that will change.
@param {Number} removeAmt The number of items that will be removed. If you
pass `null` assumes 0
@param {Number} addAmt The number of items that will be added. If you
pass `null` assumes 0.
@return {Ember.Array} receiver
@public
*/
arrayContentWillChange(startIdx, removeAmt, addAmt) {
var removing, lim;
// if no args are passed assume everything changes
if (startIdx === undefined) {
startIdx = 0;
removeAmt = addAmt = -1;
} else {
if (removeAmt === undefined) {
removeAmt = -1;
}
if (addAmt === undefined) {
addAmt = -1;
}
}
if (this.__each) {
this.__each.arrayWillChange(this, startIdx, removeAmt, addAmt);
}
sendEvent(this, '@array:before', [this, startIdx, removeAmt, addAmt]);
if (startIdx >= 0 && removeAmt >= 0 && get(this, 'hasEnumerableObservers')) {
removing = [];
lim = startIdx + removeAmt;
for (var idx = startIdx; idx < lim; idx++) {
removing.push(this.objectAt(idx));
}
} else {
removing = removeAmt;
}
this.enumerableContentWillChange(removing, addAmt);
return this;
},
/**
If you are implementing an object that supports `Ember.Array`, call this
method just after the array content changes to notify any observers and
invalidate any related properties. Pass the starting index of the change
as well as a delta of the amounts to change.
@method arrayContentDidChange
@param {Number} startIdx The starting index in the array that did change.
@param {Number} removeAmt The number of items that were removed. If you
pass `null` assumes 0
@param {Number} addAmt The number of items that were added. If you
pass `null` assumes 0.
@return {Ember.Array} receiver
@public
*/
arrayContentDidChange(startIdx, removeAmt, addAmt) {
var adding, lim;
// if no args are passed assume everything changes
if (startIdx === undefined) {
startIdx = 0;
removeAmt = addAmt = -1;
} else {
if (removeAmt === undefined) {
removeAmt = -1;
}
if (addAmt === undefined) {
addAmt = -1;
}
}
if (startIdx >= 0 && addAmt >= 0 && get(this, 'hasEnumerableObservers')) {
adding = [];
lim = startIdx + addAmt;
for (var idx = startIdx; idx < lim; idx++) {
adding.push(this.objectAt(idx));
}
} else {
adding = addAmt;
}
this.enumerableContentDidChange(removeAmt, adding);
if (this.__each) {
this.__each.arrayDidChange(this, startIdx, removeAmt, addAmt);
}
sendEvent(this, '@array:change', [this, startIdx, removeAmt, addAmt]);
var length = get(this, 'length');
var cachedFirst = cacheFor(this, 'firstObject');
var cachedLast = cacheFor(this, 'lastObject');
if (this.objectAt(0) !== cachedFirst) {
propertyWillChange(this, 'firstObject');
propertyDidChange(this, 'firstObject');
}
if (this.objectAt(length - 1) !== cachedLast) {
propertyWillChange(this, 'lastObject');
propertyDidChange(this, 'lastObject');
}
return this;
},
/**
Returns a special object that can be used to observe individual properties
on the array. Just get an equivalent property on this object and it will
return an enumerable that maps automatically to the named key on the
member objects.
If you merely want to watch for any items being added or removed to the array,
use the `[]` property instead of `@each`.
@property @each
@public
*/
'@each': computed(function() {
// TODO use Symbol or add to meta
if (!this.__each) {
this.__each = new EachProxy(this);
}
return this.__each;
}).volatile()
});