forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.js
More file actions
222 lines (193 loc) · 7.37 KB
/
events.js
File metadata and controls
222 lines (193 loc) · 7.37 KB
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
/**
* @name pc.events
* @namespace
* @description global namespace that allows to extend other objects with events
* Additionally it can handle global events it self.
* @example
* var obj = { };
* pc.events.attach(obj);
*
* // subscribe to an event
* obj.on('hello', function(str) {
* console.log('event hello is fired', str);
* });
*
* // fire event
* obj.fire('hello', 'world');
*/
pc.events = {
/**
* @function
* @name pc.events.attach
* @description Attach event methods 'on', 'off', 'fire', 'once' and 'hasEvent' to the target object
* @param {Object} target The object to add events to.
* @return {Object} The target object
* @example
* var obj = { };
* pc.events.attach(obj);
*/
attach: function (target) {
var ev = pc.events;
target.on = ev.on;
target.off = ev.off;
target.fire = ev.fire;
target.once = ev.once;
target.hasEvent = ev.hasEvent;
target._callbackActive = { };
return target;
},
/**
* @function
* @name pc.events.on
* @description Attach an event handler to an event
* @param {String} name Name of the event to bind the callback to
* @param {Function} callback Function that is called when event is fired. Note the callback is limited to 8 arguments.
* @param {Object} [scope] Object to use as 'this' when the event is fired, defaults to current this
* @example
* obj.on('test', function (a, b) {
* console.log(a + b);
* });
* obj.fire('test', 1, 2); // prints 3 to the console
*/
on: function (name, callback, scope) {
if (! name || typeof(name) !== 'string' || ! callback)
return this;
if (! this._callbacks)
this._callbacks = { };
if (! this._callbacks[name])
this._callbacks[name] = [ ];
if (! this._callbackActive)
this._callbackActive = { };
if (this._callbackActive[name] && this._callbackActive[name] === this._callbacks[name])
this._callbackActive[name] = this._callbackActive[name].slice();
this._callbacks[name].push({
callback: callback,
scope: scope || this
});
return this;
},
/**
* @function
* @name pc.events.off
* @description Detach an event handler from an event. If callback is not provided then all callbacks are unbound from the event,
* if scope is not provided then all events with the callback will be unbound.
* @param {String} [name] Name of the event to unbind
* @param {Function} [callback] Function to be unbound
* @param {Object} [scope] Scope that was used as the this when the event is fired
* @example
* var handler = function () {
* };
* obj.on('test', handler);
*
* obj.off(); // Removes all events
* obj.off('test'); // Removes all events called 'test'
* obj.off('test', handler); // Removes all handler functions, called 'test'
* obj.off('test', handler, this); // Removes all hander functions, called 'test' with scope this
*/
off: function (name, callback, scope) {
if (! this._callbacks)
return this;
if (this._callbackActive) {
if (name) {
if (this._callbackActive[name] && this._callbackActive[name] === this._callbacks[name])
this._callbackActive[name] = this._callbackActive[name].slice();
} else {
for(var key in this._callbackActive) {
if (! this._callbacks[key])
continue;
if (this._callbacks[key] !== this._callbackActive[key])
continue;
this._callbackActive[key] = this._callbackActive[key].slice();
}
}
}
if (! name) {
this._callbacks = null;
} else if (! callback) {
if (this._callbacks[name])
delete this._callbacks[name];
} else {
var events = this._callbacks[name];
if (! events)
return this;
var i = events.length;
while(i--) {
if (events[i].callback !== callback)
continue;
if (scope && events[i].scope !== scope)
continue;
events.splice(i, 1);
}
}
return this;
},
/**
* @function
* @name pc.events.fire
* @description Fire an event, all additional arguments are passed on to the event listener
* @param {Object} name Name of event to fire
* @param {*} [...] Arguments that are passed to the event handler
* @example
* obj.fire('test', 'This is the message');
*/
fire: function (name, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) {
if (! name || ! this._callbacks || ! this._callbacks[name])
return this;
var callbacks;
if (! this._callbackActive)
this._callbackActive = { };
if (! this._callbackActive[name]) {
this._callbackActive[name] = this._callbacks[name];
} else {
if (this._callbackActive[name] === this._callbacks[name])
this._callbackActive[name] = this._callbackActive[name].slice();
callbacks = this._callbacks[name].slice();
}
for(var i = 0; (callbacks || this._callbackActive[name]) && (i < (callbacks || this._callbackActive[name]).length); i++) {
var evt = (callbacks || this._callbackActive[name])[i];
evt.callback.call(evt.scope, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
if (evt.callback.once) {
var ind = this._callbacks[name].indexOf(evt);
if (ind !== -1) {
if (this._callbackActive[name] === this._callbacks[name])
this._callbackActive[name] = this._callbackActive[name].slice();
this._callbacks[name].splice(ind, 1);
}
}
}
if (! callbacks)
this._callbackActive[name] = null;
return this;
},
/**
* @function
* @name pc.events.once
* @description Attach an event handler to an event. This handler will be removed after being fired once.
* @param {String} name Name of the event to bind the callback to
* @param {Function} callback Function that is called when event is fired. Note the callback is limited to 8 arguments.
* @param {Object} [scope] Object to use as 'this' when the event is fired, defaults to current this
* @example
* obj.once('test', function (a, b) {
* console.log(a + b);
* });
* obj.fire('test', 1, 2); // prints 3 to the console
* obj.fire('test', 1, 2); // not going to get handled
*/
once: function (name, callback, scope) {
callback.once = true;
this.on(name, callback, scope);
return this;
},
/**
* @function
* @name pc.events.hasEvent
* @description Test if there are any handlers bound to an event name
* @param {String} name The name of the event to test
* @example
* obj.on('test', function () { }); // bind an event to 'test'
* obj.hasEvent('test'); // returns true
*/
hasEvent: function (name) {
return (this._callbacks && this._callbacks[name] && this._callbacks[name].length !== 0) || false;
}
};