2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('event-custom-complex', function (Y, NAME) {
12 * Adds event facades, preventable default behavior, and bubbling.
14 * @module event-custom
15 * @submodule event-custom-complex
23 CEProto = Y.CustomEvent.prototype,
24 ETProto = Y.EventTarget.prototype,
26 mixFacadeProps = function(facade, payload) {
30 if (!(FACADE_KEYS.hasOwnProperty(p))) {
31 facade[p] = payload[p];
37 * Wraps and protects a custom event for use when emitFacade is set to true.
38 * Requires the event-custom-complex module
40 * @param e {Event} the custom event
41 * @param currentTarget {HTMLElement} the element the listener was attached to
44 Y.EventFacade = function(e, currentTarget) {
53 * The arguments passed to fire
57 this.details = e.details;
60 * The event type, this can be overridden by the fire() payload
74 //////////////////////////////////////////////////////
77 * Node reference for the targeted eventtarget
81 this.target = e.target;
84 * Node reference for the element that the listener was attached to.
85 * @property currentTarget
88 this.currentTarget = currentTarget;
91 * Node reference to the relatedTarget
92 * @property relatedTarget
95 this.relatedTarget = e.relatedTarget;
99 Y.mix(Y.EventFacade.prototype, {
102 * Stops the propagation to the next bubble target
103 * @method stopPropagation
105 stopPropagation: function() {
106 this._event.stopPropagation();
111 * Stops the propagation to the next bubble target and
112 * prevents any additional listeners from being exectued
113 * on the current target.
114 * @method stopImmediatePropagation
116 stopImmediatePropagation: function() {
117 this._event.stopImmediatePropagation();
122 * Prevents the event's default behavior
123 * @method preventDefault
125 preventDefault: function() {
126 this._event.preventDefault();
131 * Stops the event propagation and prevents the default
134 * @param immediate {boolean} if true additional listeners
135 * on the current target will not be executed
137 halt: function(immediate) {
138 this._event.halt(immediate);
140 this.stopped = (immediate) ? 2 : 1;
145 CEProto.fireComplex = function(args) {
163 host = self.host || self,
167 yuievt = host._yuievt,
168 hasPotentialSubscribers;
172 // queue this event if the current item in the queue bubbles
173 if (self.queuable && self.type !== stack.next.type) {
174 self.log('queue ' + self.type);
179 stack.queue.push([self, args]);
185 hasPotentialSubscribers = self.hasSubs() || yuievt.hasTargets || self.broadcast;
187 self.target = self.target || host;
188 self.currentTarget = host;
190 self.details = args.concat();
192 if (hasPotentialSubscribers) {
196 id: self.id, // id of the first event in the stack
203 // defaultFnQueue: new Y.Queue(),
204 defaultTargetOnly: self.defaultTargetOnly
208 subs = self.getSubs();
212 self.stopped = (self.type !== es.type) ? 0 : es.stopped;
213 self.prevented = (self.type !== es.type) ? 0 : es.prevented;
215 if (self.stoppedFn) {
216 // PERF TODO: Can we replace with callback, like preventedFn. Look into history
217 events = new Y.EventTarget({
221 self.events = events;
222 events.on('stopped', self.stoppedFn);
225 // self.log("Firing " + self + ", " + "args: " + args);
226 self.log("Firing " + self.type);
228 self._facade = null; // kill facade to eliminate stale properties
230 ef = self._createFacade(args);
233 self._procSubs(ons, args, ef);
236 // bubble if this is hosted in an event target and propagation has not been stopped
237 if (self.bubbles && host.bubble && !self.stopped) {
238 oldbubble = es.bubbling;
240 es.bubbling = self.type;
242 if (es.type !== self.type) {
247 ret = host.bubble(self, args, null, es);
249 self.stopped = Math.max(self.stopped, es.stopped);
250 self.prevented = Math.max(self.prevented, es.prevented);
252 es.bubbling = oldbubble;
255 prevented = self.prevented;
258 preventedFn = self.preventedFn;
260 preventedFn.apply(host, args);
263 defaultFn = self.defaultFn;
265 if (defaultFn && ((!self.defaultTargetOnly && !es.defaultTargetOnly) || host === ef.target)) {
266 defaultFn.apply(host, args);
270 // broadcast listeners are fired as discreet events on the
271 // YUI instance and potentially the YUI global.
272 if (self.broadcast) {
273 self._broadcast(args);
276 if (afters && !self.prevented && self.stopped < 2) {
279 afterQueue = es.afterQueue;
281 if (es.id === self.id || self.type !== yuievt.bubbling) {
283 self._procSubs(afters, args, ef);
286 while ((next = afterQueue.last())) {
293 if (es.execDefaultCnt) {
294 postponed = Y.merge(postponed);
296 Y.each(postponed, function(s) {
302 es.afterQueue = new Y.Queue();
305 es.afterQueue.add(function() {
306 self._procSubs(postponed, args, ef);
314 if (es.id === self.id) {
319 while (queue.length) {
322 // set up stack to allow the next item to be processed
331 ret = !(self.stopped);
333 if (self.type !== yuievt.bubbling) {
341 defaultFn = self.defaultFn;
344 ef = self._createFacade(args);
346 if ((!self.defaultTargetOnly) || (host === ef.target)) {
347 defaultFn.apply(host, args);
352 // Kill the cached facade to free up memory.
353 // Otherwise we have the facade from the last fire, sitting around forever.
360 * @method _hasPotentialSubscribers
363 * @return {boolean} Whether the event has potential subscribers or not
365 CEProto._hasPotentialSubscribers = function() {
366 return this.hasSubs() || this.host._yuievt.hasTargets || this.broadcast;
370 * Internal utility method to create a new facade instance and
371 * insert it into the fire argument list, accounting for any payload
372 * merging which needs to happen.
374 * This used to be called `_getFacade`, but the name seemed inappropriate
375 * when it was used without a need for the return value.
377 * @method _createFacade
379 * @param fireArgs {Array} The arguments passed to "fire", which need to be
380 * shifted (and potentially merged) when the facade is added.
381 * @return {EventFacade} The event facade created.
384 // TODO: Remove (private) _getFacade alias, once synthetic.js is updated.
385 CEProto._createFacade = CEProto._getFacade = function(fireArgs) {
387 var userArgs = this.details,
388 firstArg = userArgs && userArgs[0],
389 firstArgIsObj = (firstArg && (typeof firstArg === "object")),
393 ef = new Y.EventFacade(this, this.currentTarget);
397 // protect the event facade properties
398 mixFacadeProps(ef, firstArg);
400 // Allow the event type to be faked http://yuilibrary.com/projects/yui3/ticket/2528376
402 ef.type = firstArg.type;
410 fireArgs.unshift(ef);
414 // update the details field with the arguments
415 ef.details = this.details;
417 // use the original target when the event bubbled to this target
418 ef.target = this.originalTarget || this.target;
420 ef.currentTarget = this.currentTarget;
430 * Utility method to manipulate the args array passed in, to add the event facade,
431 * if it's not already the first arg.
433 * @method _addFacadeToArgs
435 * @param {Array} The arguments to manipulate
437 CEProto._addFacadeToArgs = function(args) {
440 // Trying not to use instanceof, just to avoid potential cross Y edge case issues.
441 if (!(e && e.halt && e.stopImmediatePropagation && e.stopPropagation && e._event)) {
442 this._createFacade(args);
447 * Stop propagation to bubble targets
449 * @method stopPropagation
451 CEProto.stopPropagation = function() {
454 this.stack.stopped = 1;
457 this.events.fire('stopped', this);
462 * Stops propagation to bubble targets, and prevents any remaining
463 * subscribers on the current target from executing.
464 * @method stopImmediatePropagation
466 CEProto.stopImmediatePropagation = function() {
469 this.stack.stopped = 2;
472 this.events.fire('stopped', this);
477 * Prevents the execution of this event's defaultFn
478 * @method preventDefault
480 CEProto.preventDefault = function() {
481 if (this.preventable) {
484 this.stack.prevented = 1;
490 * Stops the event propagation and prevents the default
493 * @param immediate {boolean} if true additional listeners
494 * on the current target will not be executed
496 CEProto.halt = function(immediate) {
498 this.stopImmediatePropagation();
500 this.stopPropagation();
502 this.preventDefault();
506 * Registers another EventTarget as a bubble target. Bubble order
507 * is determined by the order registered. Multiple targets can
510 * Events can only bubble if emitFacade is true.
512 * Included in the event-custom-complex submodule.
516 * @param o {EventTarget} the target to add
519 ETProto.addTarget = function(o) {
520 var etState = this._yuievt;
522 if (!etState.targets) {
523 etState.targets = {};
526 etState.targets[Y.stamp(o)] = o;
527 etState.hasTargets = true;
533 * Returns an array of bubble targets for this object.
535 * @return EventTarget[]
537 ETProto.getTargets = function() {
538 var targets = this._yuievt.targets;
539 return targets ? YObject.values(targets) : [];
543 * Removes a bubble target
544 * @method removeTarget
546 * @param o {EventTarget} the target to remove
549 ETProto.removeTarget = function(o) {
550 var targets = this._yuievt.targets;
553 delete targets[Y.stamp(o, true)];
555 if (YObject.size(targets) === 0) {
556 this._yuievt.hasTargets = false;
564 * Propagate an event. Requires the event-custom-complex module.
566 * @param evt {CustomEvent} the custom event to propagate
567 * @return {boolean} the aggregated return value from Event.Custom.fire
570 ETProto.bubble = function(evt, args, target, es) {
572 var targs = this._yuievt.targets,
579 type = evt && evt.type,
580 originalTarget = target || (evt && evt.target) || this,
583 if (!evt || ((!evt.stopped) && targs)) {
586 if (targs.hasOwnProperty(i)) {
590 ce = t._yuievt.events[type];
592 if (t._hasSiblings) {
593 ce2 = t.getSibling(type, ce);
597 ce = t.publish(type);
600 oldbubble = t._yuievt.bubbling;
601 t._yuievt.bubbling = type;
603 // if this event was not published on the bubble target,
604 // continue propagating the event.
606 if (t._yuievt.hasTargets) {
607 t.bubble(evt, args, originalTarget, es);
615 // set the original target to that the target payload on the facade is correct.
616 ce.target = originalTarget;
617 ce.originalTarget = originalTarget;
618 ce.currentTarget = t;
620 ce.broadcast = false;
622 // default publish may not have emitFacade true -- that
623 // shouldn't be what the implementer meant to do
624 ce.emitFacade = true;
628 // TODO: See what's getting in the way of changing this to use
629 // the more performant ce._fire(args || evt.details || []).
631 // Something in Widget Parent/Child tests is not happy if we
632 // change it - maybe evt.details related?
633 ret = ret && ce.fire.apply(ce, args || evt.details || []);
636 ce.originalTarget = null;
638 // stopPropagation() was called
644 t._yuievt.bubbling = oldbubble;
653 * @method _hasPotentialSubscribers
656 * @param {String} fullType The fully prefixed type name
657 * @return {boolean} Whether the event has potential subscribers or not
659 ETProto._hasPotentialSubscribers = function(fullType) {
661 var etState = this._yuievt,
662 e = etState.events[fullType];
665 return e.hasSubs() || etState.hasTargets || e.broadcast;
671 FACADE = new Y.EventFacade();
675 for (key in FACADE) {
676 FACADE_KEYS[key] = true;
680 }, '3.13.0', {"requires": ["event-custom-base"]});