-
-
Notifications
You must be signed in to change notification settings - Fork 455
/
collection.js
137 lines (118 loc) · 3.17 KB
/
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
import { Events } from './events.js';
/**
* @name Two.Collection
* @class
* @extends Two.Events
* @description An `Array` like object with additional event propagation on actions. `pop`, `shift`, and `splice` trigger `removed` events. `push`, `unshift`, and `splice` with more than 2 arguments trigger 'inserted'. Finally, `sort` and `reverse` trigger `order` events.
*/
export class Collection extends Array {
// Warning: Multiple inheritance hack
/**
* @private
*/
_events = new Events();
// Getters and setters aren't enumerable
get _bound() {
return this._events._bound;
}
set _bound(v) {
this._events._bound = v;
}
addEventListener() {
return this._events.addEventListener.apply(this, arguments);
}
on() {
return this._events.on.apply(this, arguments);
}
bind() {
return this._events.bind.apply(this, arguments);
}
removeEventListener() {
return this._events.removeEventListener.apply(this, arguments);
}
off() {
return this._events.off.apply(this, arguments);
}
unbind() {
return this._events.unbind.apply(this, arguments);
}
dispatchEvent() {
return this._events.dispatchEvent.apply(this, arguments);
}
trigger() {
return this._events.trigger.apply(this, arguments);
}
listen() {
return this._events.listen.apply(this, arguments);
}
ignore() {
return this._events.ignore.apply(this, arguments);
}
constructor() {
super();
if (arguments[0] && Array.isArray(arguments[0])) {
if (arguments[0].length > 0) {
this.push.apply(this, arguments[0]);
}
} else if (arguments.length > 0) {
this.push.apply(this, arguments);
}
}
pop() {
const popped = super.pop.apply(this, arguments);
this.trigger(Events.Types.remove, [popped]);
return popped;
}
shift() {
const shifted = super.shift.apply(this, arguments);
this.trigger(Events.Types.remove, [shifted]);
return shifted;
}
push() {
const pushed = super.push.apply(this, arguments);
this.trigger(Events.Types.insert, arguments);
return pushed;
}
unshift() {
const unshifted = super.unshift.apply(this, arguments);
this.trigger(Events.Types.insert, arguments);
return unshifted;
}
splice() {
const spliced = super.splice.apply(this, arguments);
this.trigger(Events.Types.remove, spliced);
if (arguments.length > 2) {
const inserted = this.slice(arguments[0], arguments[0] + arguments.length - 2);
this.trigger(Events.Types.insert, inserted);
this.trigger(Events.Types.order);
}
return spliced;
}
sort() {
super.sort.apply(this, arguments);
this.trigger(Events.Types.order);
return this;
}
reverse() {
super.reverse.apply(this, arguments);
this.trigger(Events.Types.order);
return this;
}
indexOf() {
return super.indexOf.apply(this, arguments);
}
map(func, scope) {
const results = [];
for (let key = 0; key < this.length; key++) {
const value = this[key];
let result;
if (scope) {
result = func.call(scope, value, key);
} else {
result = func(value, key);
}
results.push(result);
}
return results;
}
}