Debian Codegraph @

mixmark-io-domino/lib/EventTarget.js

raw symbols SCIP
1 "use strict";
2 var Event = require('./Event');
3 var MouseEvent = require('./MouseEvent');
4 var utils = require('./utils');
5
6 module.exports = EventTarget;
7
8 function EventTarget() {}
9
10 EventTarget.prototype = {
11 // XXX
12 // See WebIDL §4.8 for details on object event handlers
13 // and how they should behave. We actually have to accept
14 // any object to addEventListener... Can't type check it.
15 // on registration.
16
17 // XXX:
18 // Capturing event listeners are sort of rare. I think I can optimize
19 // them so that dispatchEvent can skip the capturing phase (or much of
20 // it). Each time a capturing listener is added, increment a flag on
21 // the target node and each of its ancestors. Decrement when removed.
22 // And update the counter when nodes are added and removed from the
23 // tree as well. Then, in dispatch event, the capturing phase can
24 // abort if it sees any node with a zero count.
25 addEventListener: function addEventListener(type, listener, capture) {
26 if (!listener) return;
27 if (capture === undefined) capture = false;
28 if (!this._listeners) this._listeners = Object.create(null);
29 if (!this._listeners[type]) this._listeners[type] = [];
30 var list = this._listeners[type];
31
32 // If this listener has already been registered, just return
33 for(var i = 0, n = list.length; i < n; i++) {
34 var l = list[i];
35 if (l.listener === listener && l.capture === capture)
36 return;
37 }
38
39 // Add an object to the list of listeners
40 var obj = { listener: listener, capture: capture };
41 if (typeof listener === 'function') obj.f = listener;
42 list.push(obj);
43 },
44
45 removeEventListener: function removeEventListener(type,
46 listener,
47 capture) {
48 if (capture === undefined) capture = false;
49 if (this._listeners) {
50 var list = this._listeners[type];
51 if (list) {
52 // Find the listener in the list and remove it
53 for(var i = 0, n = list.length; i < n; i++) {
54 var l = list[i];
55 if (l.listener === listener && l.capture === capture) {
56 if (list.length === 1) {
57 this._listeners[type] = undefined;
58 }
59 else {
60 list.splice(i, 1);
61 }
62 return;
63 }
64 }
65 }
66 }
67 },
68
69 // This is the public API for dispatching untrusted public events.
70 // See _dispatchEvent for the implementation
71 dispatchEvent: function dispatchEvent(event) {
72 // Dispatch an untrusted event
73 return this._dispatchEvent(event, false);
74 },
75
76 //
77 // See DOMCore §4.4
78 // XXX: I'll probably need another version of this method for
79 // internal use, one that does not set isTrusted to false.
80 // XXX: see Document._dispatchEvent: perhaps that and this could
81 // call a common internal function with different settings of
82 // a trusted boolean argument
83 //
84 // XXX:
85 // The spec has changed in how to deal with handlers registered
86 // on idl or content attributes rather than with addEventListener.
87 // Used to say that they always ran first. That's how webkit does it
88 // Spec now says that they run in a position determined by
89 // when they were first set. FF does it that way. See:
90 // http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#event-handlers
91 //
92 _dispatchEvent: function _dispatchEvent(event, trusted) {
93 if (typeof trusted !== 'boolean') trusted = false;
94 function invoke(target, event) {
95 var type = event.type, phase = event.eventPhase;
96 event.currentTarget = target;
97
98 // If there was an individual handler defined, invoke it first
99 // XXX: see comment above: this shouldn't always be first.
100 if (phase !== Event.CAPTURING_PHASE &&
101 target._handlers && target._handlers[type])
102 {
103 var handler = target._handlers[type];
104 var rv;
105 if (typeof handler === 'function') {
106 rv=handler.call(event.currentTarget, event);
107 }
108 else {
109 var f = handler.handleEvent;
110 if (typeof f !== 'function')
111 throw new TypeError('handleEvent property of ' +
112 'event handler object is' +
113 'not a function.');
114 rv=f.call(handler, event);
115 }
116
117 switch(event.type) {
118 case 'mouseover':
119 if (rv === true) // Historical baggage
120 event.preventDefault();
121 break;
122 case 'beforeunload':
123 // XXX: eventually we need a special case here
124 /* falls through */
125 default:
126 if (rv === false)
127 event.preventDefault();
128 break;
129 }
130 }
131
132 // Now invoke list list of listeners for this target and type
133 var list = target._listeners && target._listeners[type];
134 if (!list) return;
135 list = list.slice();
136 for(var i = 0, n = list.length; i < n; i++) {
137 if (event._immediatePropagationStopped) return;
138 var l = list[i];
139 if ((phase === Event.CAPTURING_PHASE && !l.capture) ||
140 (phase === Event.BUBBLING_PHASE && l.capture))
141 continue;
142 if (l.f) {
143 l.f.call(event.currentTarget, event);
144 }
145 else {
146 var fn = l.listener.handleEvent;
147 if (typeof fn !== 'function')
148 throw new TypeError('handleEvent property of event listener object is not a function.');
149 fn.call(l.listener, event);
150 }
151 }
152 }
153
154 if (!event._initialized || event._dispatching) utils.InvalidStateError();
155 event.isTrusted = trusted;
156
157 // Begin dispatching the event now
158 event._dispatching = true;
159 event.target = this;
160
161 // Build the list of targets for the capturing and bubbling phases
162 // XXX: we'll eventually have to add Window to this list.
163 var ancestors = [];
164 for(var n = this.parentNode; n; n = n.parentNode)
165 ancestors.push(n);
166
167 // Capturing phase
168 event.eventPhase = Event.CAPTURING_PHASE;
169 for(var i = ancestors.length-1; i >= 0; i--) {
170 invoke(ancestors[i], event);
171 if (event._propagationStopped) break;
172 }
173
174 // At target phase
175 if (!event._propagationStopped) {
176 event.eventPhase = Event.AT_TARGET;
177 invoke(this, event);
178 }
179
180 // Bubbling phase
181 if (event.bubbles && !event._propagationStopped) {
182 event.eventPhase = Event.BUBBLING_PHASE;
183 for(var ii = 0, nn = ancestors.length; ii < nn; ii++) {
184 invoke(ancestors[ii], event);
185 if (event._propagationStopped) break;
186 }
187 }
188
189 event._dispatching = false;
190 event.eventPhase = Event.AT_TARGET;
191 event.currentTarget = null;
192
193 // Deal with mouse events and figure out when
194 // a click has happened
195 if (trusted && !event.defaultPrevented && event instanceof MouseEvent) {
196 switch(event.type) {
197 case 'mousedown':
198 this._armed = {
199 x: event.clientX,
200 y: event.clientY,
201 t: event.timeStamp
202 };
203 break;
204 case 'mouseout':
205 case 'mouseover':
206 this._armed = null;
207 break;
208 case 'mouseup':
209 if (this._isClick(event)) this._doClick(event);
210 this._armed = null;
211 break;
212 }
213 }
214
215
216
217 return !event.defaultPrevented;
218 },
219
220 // Determine whether a click occurred
221 // XXX We don't support double clicks for now
222 _isClick: function(event) {
223 return (this._armed !== null &&
224 event.type === 'mouseup' &&
225 event.isTrusted &&
226 event.button === 0 &&
227 event.timeStamp - this._armed.t < 1000 &&
228 Math.abs(event.clientX - this._armed.x) < 10 &&
229 Math.abs(event.clientY - this._armed.Y) < 10);
230 },
231
232 // Clicks are handled like this:
233 // http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#interactive-content-0
234 //
235 // Note that this method is similar to the HTMLElement.click() method
236 // The event argument must be the trusted mouseup event
237 _doClick: function(event) {
238 if (this._click_in_progress) return;
239 this._click_in_progress = true;
240
241 // Find the nearest enclosing element that is activatable
242 // An element is activatable if it has a
243 // _post_click_activation_steps hook
244 var activated = this;
245 while(activated && !activated._post_click_activation_steps)
246 activated = activated.parentNode;
247
248 if (activated && activated._pre_click_activation_steps) {
249 activated._pre_click_activation_steps();
250 }
251
252 var click = this.ownerDocument.createEvent('MouseEvent');
253 click.initMouseEvent('click', true, true,
254 this.ownerDocument.defaultView, 1,
255 event.screenX, event.screenY,
256 event.clientX, event.clientY,
257 event.ctrlKey, event.altKey,
258 event.shiftKey, event.metaKey,
259 event.button, null);
260
261 var result = this._dispatchEvent(click, true);
262
263 if (activated) {
264 if (result) {
265 // This is where hyperlinks get followed, for example.
266 if (activated._post_click_activation_steps)
267 activated._post_click_activation_steps(click);
268 }
269 else {
270 if (activated._cancelled_activation_steps)
271 activated._cancelled_activation_steps();
272 }
273 }
274 },
275
276 //
277 // An event handler is like an event listener, but it registered
278 // by setting an IDL or content attribute like onload or onclick.
279 // There can only be one of these at a time for any event type.
280 // This is an internal method for the attribute accessors and
281 // content attribute handlers that need to register events handlers.
282 // The type argument is the same as in addEventListener().
283 // The handler argument is the same as listeners in addEventListener:
284 // it can be a function or an object. Pass null to remove any existing
285 // handler. Handlers are always invoked before any listeners of
286 // the same type. They are not invoked during the capturing phase
287 // of event dispatch.
288 //
289 _setEventHandler: function _setEventHandler(type, handler) {
290 if (!this._handlers) this._handlers = Object.create(null);
291 this._handlers[type] = handler;
292 },
293
294 _getEventHandler: function _getEventHandler(type) {
295 return (this._handlers && this._handlers[type]) || null;
296 }
297
298 };