2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('event-custom-complex', function(Y) {
12 * Adds event facades, preventable default behavior, and bubbling.
14 * @module event-custom
15 * @submodule event-custom-complex
21 CEProto = Y.CustomEvent.prototype,
22 ETProto = Y.EventTarget.prototype;
25 * Wraps and protects a custom event for use when emitFacade is set to true.
26 * Requires the event-custom-complex module
28 * @param e {Event} the custom event
29 * @param currentTarget {HTMLElement} the element the listener was attached to
32 Y.EventFacade = function(e, currentTarget) {
39 * The arguments passed to fire
43 this.details = e.details;
46 * The event type, this can be overridden by the fire() payload
59 //////////////////////////////////////////////////////
62 * Node reference for the targeted eventtarget
66 this.target = e.target;
69 * Node reference for the element that the listener was attached to.
70 * @propery currentTarget
73 this.currentTarget = currentTarget;
76 * Node reference to the relatedTarget
77 * @propery relatedTarget
80 this.relatedTarget = e.relatedTarget;
84 Y.extend(Y.EventFacade, Object, {
87 * Stops the propagation to the next bubble target
88 * @method stopPropagation
90 stopPropagation: function() {
91 this._event.stopPropagation();
96 * Stops the propagation to the next bubble target and
97 * prevents any additional listeners from being exectued
98 * on the current target.
99 * @method stopImmediatePropagation
101 stopImmediatePropagation: function() {
102 this._event.stopImmediatePropagation();
107 * Prevents the event's default behavior
108 * @method preventDefault
110 preventDefault: function() {
111 this._event.preventDefault();
116 * Stops the event propagation and prevents the default
119 * @param immediate {boolean} if true additional listeners
120 * on the current target will not be executed
122 halt: function(immediate) {
123 this._event.halt(immediate);
125 this.stopped = (immediate) ? 2 : 1;
130 CEProto.fireComplex = function(args) {
132 var es, ef, q, queue, ce, ret, events, subs, postponed,
133 self = this, host = self.host || self, next, oldbubble;
136 // queue this event if the current item in the queue bubbles
137 if (self.queuable && self.type != self.stack.next.type) {
138 self.log('queue ' + self.type);
139 self.stack.queue.push([self, args]);
145 // id of the first event in the stack
153 // defaultFnQueue: new Y.Queue(),
154 afterQueue: new Y.Queue(),
155 defaultTargetOnly: self.defaultTargetOnly,
159 subs = self.getSubs();
161 self.stopped = (self.type !== es.type) ? 0 : es.stopped;
162 self.prevented = (self.type !== es.type) ? 0 : es.prevented;
164 self.target = self.target || host;
166 events = new Y.EventTarget({
171 self.events = events;
173 if (self.preventedFn) {
174 events.on('prevented', self.preventedFn);
177 if (self.stoppedFn) {
178 events.on('stopped', self.stoppedFn);
181 self.currentTarget = host;
183 self.details = args.slice(); // original arguments in the details
185 // self.log("Firing " + self + ", " + "args: " + args);
186 self.log("Firing " + self.type);
188 self._facade = null; // kill facade to eliminate stale properties
190 ef = self._getFacade(args);
192 if (Y.Lang.isObject(args[0])) {
200 // self._procSubs(Y.merge(self.subscribers), args, ef);
201 self._procSubs(subs[0], args, ef);
204 // bubble if this is hosted in an event target and propagation has not been stopped
205 if (self.bubbles && host.bubble && !self.stopped) {
207 oldbubble = es.bubbling;
209 // self.bubbling = true;
210 es.bubbling = self.type;
212 // if (host !== ef.target || es.type != self.type) {
213 if (es.type != self.type) {
218 ret = host.bubble(self, args, null, es);
220 self.stopped = Math.max(self.stopped, es.stopped);
221 self.prevented = Math.max(self.prevented, es.prevented);
223 // self.bubbling = false;
224 es.bubbling = oldbubble;
228 if (self.defaultFn &&
230 ((!self.defaultTargetOnly && !es.defaultTargetOnly) || host === ef.target)) {
232 self.defaultFn.apply(host, args);
235 // broadcast listeners are fired as discreet events on the
236 // YUI instance and potentially the YUI global.
237 self._broadcast(args);
240 if (subs[1] && !self.prevented && self.stopped < 2) {
241 if (es.id === self.id || self.type != host._yuievt.bubbling) {
242 self._procSubs(subs[1], args, ef);
243 while ((next = es.afterQueue.last())) {
248 if (es.execDefaultCnt) {
249 postponed = Y.merge(postponed);
250 Y.each(postponed, function(s) {
255 es.afterQueue.add(function() {
256 self._procSubs(postponed, args, ef);
263 if (es.id === self.id) {
266 while (queue.length) {
269 // set up stack to allow the next item to be processed
271 ce.fire.apply(ce, q[1]);
277 ret = !(self.stopped);
279 if (self.type != host._yuievt.bubbling) {
289 CEProto._getFacade = function() {
291 var ef = this._facade, o, o2,
295 ef = new Y.EventFacade(this, this.currentTarget);
298 // if the first argument is an object literal, apply the
299 // properties to the event facade
302 if (Y.Lang.isObject(o, true)) {
306 // protect the event facade properties
307 Y.mix(o2, ef, true, FACADE_KEYS);
313 Y.mix(ef, o2, true, FACADE_KEYS);
315 // Allow the event type to be faked
316 // http://yuilibrary.com/projects/yui3/ticket/2528376
317 ef.type = o.type || ef.type;
320 // update the details field with the arguments
321 // ef.type = this.type;
322 ef.details = this.details;
324 // use the original target when the event bubbled to this target
325 ef.target = this.originalTarget || this.target;
327 ef.currentTarget = this.currentTarget;
337 * Stop propagation to bubble targets
339 * @method stopPropagation
341 CEProto.stopPropagation = function() {
344 this.stack.stopped = 1;
346 this.events.fire('stopped', this);
350 * Stops propagation to bubble targets, and prevents any remaining
351 * subscribers on the current target from executing.
352 * @method stopImmediatePropagation
354 CEProto.stopImmediatePropagation = function() {
357 this.stack.stopped = 2;
359 this.events.fire('stopped', this);
363 * Prevents the execution of this event's defaultFn
364 * @method preventDefault
366 CEProto.preventDefault = function() {
367 if (this.preventable) {
370 this.stack.prevented = 1;
372 this.events.fire('prevented', this);
377 * Stops the event propagation and prevents the default
380 * @param immediate {boolean} if true additional listeners
381 * on the current target will not be executed
383 CEProto.halt = function(immediate) {
385 this.stopImmediatePropagation();
387 this.stopPropagation();
389 this.preventDefault();
393 * Registers another EventTarget as a bubble target. Bubble order
394 * is determined by the order registered. Multiple targets can
397 * Events can only bubble if emitFacade is true.
399 * Included in the event-custom-complex submodule.
402 * @param o {EventTarget} the target to add
405 ETProto.addTarget = function(o) {
406 this._yuievt.targets[Y.stamp(o)] = o;
407 this._yuievt.hasTargets = true;
411 * Returns an array of bubble targets for this object.
413 * @return EventTarget[]
415 ETProto.getTargets = function() {
416 return Y.Object.values(this._yuievt.targets);
420 * Removes a bubble target
421 * @method removeTarget
422 * @param o {EventTarget} the target to remove
425 ETProto.removeTarget = function(o) {
426 delete this._yuievt.targets[Y.stamp(o)];
430 * Propagate an event. Requires the event-custom-complex module.
432 * @param evt {CustomEvent} the custom event to propagate
433 * @return {boolean} the aggregated return value from Event.Custom.fire
436 ETProto.bubble = function(evt, args, target, es) {
438 var targs = this._yuievt.targets, ret = true,
439 t, type = evt && evt.type, ce, i, bc, ce2,
440 originalTarget = target || (evt && evt.target) || this,
443 if (!evt || ((!evt.stopped) && targs)) {
446 if (targs.hasOwnProperty(i)) {
448 ce = t.getEvent(type, true);
449 ce2 = t.getSibling(type, ce);
452 ce = t.publish(type);
455 oldbubble = t._yuievt.bubbling;
456 t._yuievt.bubbling = type;
458 // if this event was not published on the bubble target,
459 // continue propagating the event.
461 if (t._yuievt.hasTargets) {
462 t.bubble(evt, args, originalTarget, es);
468 // set the original target to that the target payload on the
469 // facade is correct.
470 ce.target = originalTarget;
471 ce.originalTarget = originalTarget;
472 ce.currentTarget = t;
474 ce.broadcast = false;
476 // default publish may not have emitFacade true -- that
477 // shouldn't be what the implementer meant to do
478 ce.emitFacade = true;
482 ret = ret && ce.fire.apply(ce, args || evt.details || []);
484 ce.originalTarget = null;
487 // stopPropagation() was called
493 t._yuievt.bubbling = oldbubble;
501 FACADE = new Y.EventFacade();
502 FACADE_KEYS = Y.Object.keys(FACADE);
506 }, '3.3.0' ,{requires:['event-custom-base']});