This repository has been archived by the owner on Jul 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 396
/
EventDispatcher.js
132 lines (111 loc) · 3.48 KB
/
EventDispatcher.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
var EventDispatcherDefinition = (function () {
var CUSTOM_DOM_EVENT_PREFIX = 'shumway.';
var def = {
__class__: 'flash.events.EventDispatcher',
initialize: function () {
this._captureHandlers = { };
this._control = null;
this._handlers = { };
},
ctor: function (target) {
this._target = target;
},
addEventListener: function (type, listener, useCapture, prio, useWeakReference) {
if (typeof listener !== 'function')
if (typeof listener !== 'function' || !type)
throw ArgumentError();
if (prio === undefined)
prio = 0;
var handlers = useCapture ? this._captureHandlers : this._handlers;
var handler = handlers[type];
if (!handler) {
var target = this;
handler = {
queue: [],
handleEvent: function (evt) {
if (evt instanceof CustomEvent) {
var domEvent = evt;
evt = domEvent.detail;
evt._eventPhase = domEvent.eventPhase;
}
evt._currentTarget = this;
var queue = this.queue;
for (var i = 0, n = queue.length; i < n; i++) {
var entry = queue[i];
entry.listener(evt);
}
}
};
if (this._control)
this._control.addEventListener(CUSTOM_DOM_EVENT_PREFIX + type, handler, useCapture);
handlers[type] = handler;
}
var queue = handler.queue;
var index = queue.length;
while (index > 0) {
var entry = queue[index - 1];
if (prio < entry.prio)
break;
index--;
}
queue.splice(index, 0, { listener: listener, prio: prio });
},
ctor: function (target) {
},
dispatchEvent: function (evt) {
evt._target = this;
if (this._control) {
var domEvent = document.createEvent('CustomEvent');
domEvent.initCustomEvent(CUSTOM_DOM_EVENT_PREFIX + evt.type, evt.bubbles, evt.cancelable, evt);
this._control.dispatchEvent(domEvent);
} else {
var handler = this._handlers[evt.type];
if (handler)
handler.handleEvent(evt);
}
return !!evt.isDefaultPrevented;
},
hasEventListener: function (type) {
return type in this._captureHandlers || type in this._handlers;
},
removeEventListener: function (type, listener, useCapture) {
var handlers = useCapture ? this._captureHandlers : this._handlers;
var handler = handlers[type];
if (handler) {
var queue = handler.queue;
for (var i = 0; i < queue.length; i++) {
if (queue[i].listener === listener) {
queue.splice(i, 1);
break;
}
}
if (!queue.length) {
if (this._control)
this._control.removeEventListener(CUSTOM_DOM_EVENT_PREFIX + type, handler, useCapture);
delete handlers[type];
}
}
},
willTrigger: function (type) {
var dispatcher = this;
do {
if (dispatcher.hasEventListener(type))
return true;
} while (dispatcher = dispatcher.parent);
return false;
}
};
def.__glue__ = {
native: {
instance: {
ctor: def.ctor,
addEventListener: def.addEventListener,
removeEventListener: def.removeEventListener,
hasEventListener: def.hasEventListener,
willTrigger: def.willTrigger,
dispatchEventFunction: def.dispatchEvent
}
}
};
return def;
}).call(this);