/
event-target.js
230 lines (211 loc) · 9.3 KB
/
event-target.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
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
const js = require('../platform/js');
const CallbacksInvoker = require('../platform/callbacks-invoker');
var fastRemove = js.array.fastRemove;
/**
* !#en
* EventTarget is an object to which an event is dispatched when something has occurred.
* Entity are the most common event targets, but other objects can be event targets too.
*
* Event targets are an important part of the Fireball event model.
* The event target serves as the focal point for how events flow through the scene graph.
* When an event such as a mouse click or a keypress occurs, Fireball dispatches an event object
* into the event flow from the root of the hierarchy. The event object then makes its way through
* the scene graph until it reaches the event target, at which point it begins its return trip through
* the scene graph. This round-trip journey to the event target is conceptually divided into three phases:
* - The capture phase comprises the journey from the root to the last node before the event target's node
* - The target phase comprises only the event target node
* - The bubbling phase comprises any subsequent nodes encountered on the return trip to the root of the tree
* See also: http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
*
* Event targets can implement the following methods:
* - _getCapturingTargets
* - _getBubblingTargets
*
* !#zh
* 事件目标是事件触发时,分派的事件对象,Node 是最常见的事件目标,
* 但是其他对象也可以是事件目标。<br/>
*
* @class EventTarget
*/
function EventTarget () {
CallbacksInvoker.call(this);
}
js.extend(EventTarget, CallbacksInvoker);
var proto = EventTarget.prototype;
/**
* !#en Checks whether the EventTarget object has any callback registered for a specific type of event.
* !#zh 检查事件目标对象是否有为特定类型的事件注册的回调。
* @method hasEventListener
* @param {String} type - The type of event.
* @return {Boolean} True if a callback of the specified type is registered; false otherwise.
*/
/**
* !#en
* Register an callback of a specific event type on the EventTarget.
* This type of event should be triggered via `emit`.
* !#zh
* 注册事件目标的特定事件类型回调。这种类型的事件应该被 `emit` 触发。
*
* @method on
* @param {String} type - A string representing the event type to listen for.
* @param {Function} callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param {any} [callback.arg1] arg1
* @param {any} [callback.arg2] arg2
* @param {any} [callback.arg3] arg3
* @param {any} [callback.arg4] arg4
* @param {any} [callback.arg5] arg5
* @param {Object} [target] - The target (this object) to invoke the callback, can be null
* @return {Function} - Just returns the incoming callback so you can save the anonymous function easier.
* @typescript
* on<T extends Function>(type: string, callback: T, target?: any, useCapture?: boolean): T
* @example
* eventTarget.on('fire', function () {
* cc.log("fire in the hole");
* }, node);
*/
proto.on = function (type, callback, target) {
if (!callback) {
cc.errorID(6800);
return;
}
if ( !this.hasEventListener(type, callback, target) ) {
this.add(type, callback, target);
if (target && target.__eventTargets)
target.__eventTargets.push(this);
}
return callback;
};
/**
* !#en
* Removes the listeners previously registered with the same type, callback, target and or useCapture,
* if only type is passed as parameter, all listeners registered with that type will be removed.
* !#zh
* 删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
*
* @method off
* @param {String} type - A string representing the event type being removed.
* @param {Function} [callback] - The callback to remove.
* @param {Object} [target] - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
* @example
* // register fire eventListener
* var callback = eventTarget.on('fire', function () {
* cc.log("fire in the hole");
* }, target);
* // remove fire event listener
* eventTarget.off('fire', callback, target);
* // remove all fire event listeners
* eventTarget.off('fire');
*/
proto.off = function (type, callback, target) {
if (!callback) {
this.removeAll(type);
}
else {
this.remove(type, callback, target);
if (target && target.__eventTargets) {
fastRemove(target.__eventTargets, this);
}
}
};
/**
* !#en Removes all callbacks previously registered with the same target (passed as parameter).
* This is not for removing all listeners in the current event target,
* and this is not for removing all listeners the target parameter have registered.
* It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter.
* !#zh 在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。
* 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。
* 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。
* @method targetOff
* @param {Object} target - The target to be searched for all related listeners
*/
proto.targetOff = proto.removeAll;
/**
* !#en
* Register an callback of a specific event type on the EventTarget,
* the callback will remove itself after the first time it is triggered.
* !#zh
* 注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。
*
* @method once
* @param {String} type - A string representing the event type to listen for.
* @param {Function} callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param {any} [callback.arg1] arg1
* @param {any} [callback.arg2] arg2
* @param {any} [callback.arg3] arg3
* @param {any} [callback.arg4] arg4
* @param {any} [callback.arg5] arg5
* @param {Object} [target] - The target (this object) to invoke the callback, can be null
* @example
* eventTarget.once('fire', function () {
* cc.log("this is the callback and will be invoked only once");
* }, node);
*/
proto.once = function (type, callback, target) {
var eventType_hasOnceListener = '__ONCE_FLAG:' + type;
var hasOnceListener = this.hasEventListener(eventType_hasOnceListener, callback, target);
if (!hasOnceListener) {
var self = this;
var onceWrapper = function (arg1, arg2, arg3, arg4, arg5) {
self.off(type, onceWrapper, target);
self.remove(eventType_hasOnceListener, callback, target);
callback.call(this, arg1, arg2, arg3, arg4, arg5);
};
this.on(type, onceWrapper, target);
this.add(eventType_hasOnceListener, callback, target);
}
};
/**
* !#en
* Trigger an event directly with the event name and necessary arguments.
* !#zh
* 通过事件名发送自定义事件
*
* @method emit
* @param {String} type - event type
* @param {*} [arg1] - First argument
* @param {*} [arg2] - Second argument
* @param {*} [arg3] - Third argument
* @param {*} [arg4] - Fourth argument
* @param {*} [arg5] - Fifth argument
* @example
*
* eventTarget.emit('fire', event);
* eventTarget.emit('fire', message, emitter);
*/
proto.emit = CallbacksInvoker.prototype.invoke;
/**
* !#en
* Send an event with the event object.
* !#zh
* 通过事件对象派发事件
*
* @method dispatchEvent
* @param {Event} event
*/
proto.dispatchEvent = function (event) {
this.invoke(event.type, event);
};
cc.EventTarget = module.exports = EventTarget;