event-facade.js revision e281a15571f07a6b4da34d2ffd674dbeb5dfd612
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp/**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Adds event facades, preventable default behavior, and bubbling.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * events.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @module event-custom
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @submodule event-custom-complex
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippvar FACADE,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp FACADE_KEYS,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp EMPTY = {},
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp CEProto = Y.CustomEvent.prototype,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp ETProto = Y.EventTarget.prototype;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp/**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Wraps and protects a custom event for use when emitFacade is set to true.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Requires the event-custom-complex module
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @class EventFacade
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param e {Event} the custom event
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param currentTarget {HTMLElement} the element the listener was attached to
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippY.EventFacade = function(e, currentTarget) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp e = e || EMPTY;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this._event = e;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The arguments passed to fire
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property details
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Array
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this.details = e.details;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The event type, this can be overridden by the fire() payload
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property type
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type string
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this.type = e.type;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * The real event type
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @property type
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type string
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this._type = e.type;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp //////////////////////////////////////////////////////
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Node reference for the targeted eventtarget
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @propery target
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this.target = e.target;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Node reference for the element that the listener was attached to.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @propery currentTarget
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this.currentTarget = currentTarget;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Node reference to the relatedTarget
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @propery relatedTarget
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @type Node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this.relatedTarget = e.relatedTarget;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp};
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippY.extend(Y.EventFacade, Object, {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Stops the propagation to the next bubble target
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method stopPropagation
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp stopPropagation: function() {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this._event.stopPropagation();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this.stopped = 1;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp },
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Stops the propagation to the next bubble target and
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * prevents any additional listeners from being exectued
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * on the current target.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method stopImmediatePropagation
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp stopImmediatePropagation: function() {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this._event.stopImmediatePropagation();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this.stopped = 2;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp },
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Prevents the event's default behavior
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method preventDefault
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp preventDefault: function() {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this._event.preventDefault();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this.prevented = 1;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp },
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /**
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Stops the event propagation and prevents the default
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * event behavior.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @method halt
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @param immediate {boolean} if true additional listeners
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * on the current target will not be executed
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp halt: function(immediate) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this._event.halt(immediate);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this.prevented = 1;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp this.stopped = (immediate) ? 2 : 1;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp});
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippCEProto.fireComplex = function(args) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp var es, ef, q, queue, ce, ret, events, subs, postponed,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self = this, host = self.host || self, next, oldbubble;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (self.stack) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // queue this event if the current item in the queue bubbles
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (self.queuable && self.type != self.stack.next.type) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self.log('queue ' + self.type);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self.stack.queue.push([self, args]);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp return true;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp es = self.stack || {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // id of the first event in the stack
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp id: self.id,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp next: self,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp silent: self.silent,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp stopped: 0,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp prevented: 0,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp bubbling: null,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp type: self.type,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // defaultFnQueue: new Y.Queue(),
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp afterQueue: new Y.Queue(),
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp defaultTargetOnly: self.defaultTargetOnly,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp queue: []
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp };
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp subs = self.getSubs();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self.stopped = (self.type !== es.type) ? 0 : es.stopped;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self.prevented = (self.type !== es.type) ? 0 : es.prevented;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self.target = self.target || host;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp events = new Y.EventTarget({
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp fireOnce: true,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp context: host
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp });
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self.events = events;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (self.stoppedFn) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp events.on('stopped', self.stoppedFn);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self.currentTarget = host;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self.details = args.slice(); // original arguments in the details
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // self.log("Firing " + self + ", " + "args: " + args);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self.log("Firing " + self.type);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self._facade = null; // kill facade to eliminate stale properties
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp ef = self._getFacade(args);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (Y.Lang.isObject(args[0])) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp args[0] = ef;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp } else {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp args.unshift(ef);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // if (subCount) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (subs[0]) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // self._procSubs(Y.merge(self.subscribers), args, ef);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self._procSubs(subs[0], args, ef);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // bubble if this is hosted in an event target and propagation has not been stopped
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (self.bubbles && host.bubble && !self.stopped) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp oldbubble = es.bubbling;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // self.bubbling = true;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp es.bubbling = self.type;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // if (host !== ef.target || es.type != self.type) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (es.type != self.type) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp es.stopped = 0;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp es.prevented = 0;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp ret = host.bubble(self, args, null, es);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self.stopped = Math.max(self.stopped, es.stopped);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self.prevented = Math.max(self.prevented, es.prevented);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // self.bubbling = false;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp es.bubbling = oldbubble;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (self.prevented) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (self.preventedFn) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self.preventedFn.apply(host, args);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp } else if (self.defaultFn &&
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp ((!self.defaultTargetOnly && !es.defaultTargetOnly) ||
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp host === ef.target)) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self.defaultFn.apply(host, args);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // broadcast listeners are fired as discreet events on the
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // YUI instance and potentially the YUI global.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self._broadcast(args);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // Queue the after
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (subs[1] && !self.prevented && self.stopped < 2) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (es.id === self.id || self.type != host._yuievt.bubbling) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self._procSubs(subs[1], args, ef);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp while ((next = es.afterQueue.last())) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp next();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp } else {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp postponed = subs[1];
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (es.execDefaultCnt) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp postponed = Y.merge(postponed);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Y.each(postponed, function(s) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp s.postponed = true;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp });
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp es.afterQueue.add(function() {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp self._procSubs(postponed, args, ef);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp });
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp
self.target = null;
if (es.id === self.id) {
queue = es.queue;
while (queue.length) {
q = queue.pop();
ce = q[0];
// set up stack to allow the next item to be processed
es.next = ce;
ce.fire.apply(ce, q[1]);
}
self.stack = null;
}
ret = !(self.stopped);
if (self.type != host._yuievt.bubbling) {
es.stopped = 0;
es.prevented = 0;
self.stopped = 0;
self.prevented = 0;
}
return ret;
};
CEProto._getFacade = function() {
var ef = this._facade, o, o2,
args = this.details;
if (!ef) {
ef = new Y.EventFacade(this, this.currentTarget);
}
// if the first argument is an object literal, apply the
// properties to the event facade
o = args && args[0];
if (Y.Lang.isObject(o, true)) {
o2 = {};
// protect the event facade properties
Y.mix(o2, ef, true, FACADE_KEYS);
// mix the data
Y.mix(ef, o, true);
// restore ef
Y.mix(ef, o2, true, FACADE_KEYS);
// Allow the event type to be faked
// http://yuilibrary.com/projects/yui3/ticket/2528376
ef.type = o.type || ef.type;
}
// update the details field with the arguments
// ef.type = this.type;
ef.details = this.details;
// use the original target when the event bubbled to this target
ef.target = this.originalTarget || this.target;
ef.currentTarget = this.currentTarget;
ef.stopped = 0;
ef.prevented = 0;
this._facade = ef;
return this._facade;
};
/**
* Stop propagation to bubble targets
* @for CustomEvent
* @method stopPropagation
*/
CEProto.stopPropagation = function() {
this.stopped = 1;
if (this.stack) {
this.stack.stopped = 1;
}
this.events.fire('stopped', this);
};
/**
* Stops propagation to bubble targets, and prevents any remaining
* subscribers on the current target from executing.
* @method stopImmediatePropagation
*/
CEProto.stopImmediatePropagation = function() {
this.stopped = 2;
if (this.stack) {
this.stack.stopped = 2;
}
this.events.fire('stopped', this);
};
/**
* Prevents the execution of this event's defaultFn
* @method preventDefault
*/
CEProto.preventDefault = function() {
if (this.preventable) {
this.prevented = 1;
if (this.stack) {
this.stack.prevented = 1;
}
}
};
/**
* Stops the event propagation and prevents the default
* event behavior.
* @method halt
* @param immediate {boolean} if true additional listeners
* on the current target will not be executed
*/
CEProto.halt = function(immediate) {
if (immediate) {
this.stopImmediatePropagation();
} else {
this.stopPropagation();
}
this.preventDefault();
};
/**
* Registers another EventTarget as a bubble target. Bubble order
* is determined by the order registered. Multiple targets can
* be specified.
*
* Events can only bubble if emitFacade is true.
*
* Included in the event-custom-complex submodule.
*
* @method addTarget
* @param o {EventTarget} the target to add
* @for EventTarget
*/
ETProto.addTarget = function(o) {
this._yuievt.targets[Y.stamp(o)] = o;
this._yuievt.hasTargets = true;
};
/**
* Returns an array of bubble targets for this object.
* @method getTargets
* @return EventTarget[]
*/
ETProto.getTargets = function() {
return Y.Object.values(this._yuievt.targets);
};
/**
* Removes a bubble target
* @method removeTarget
* @param o {EventTarget} the target to remove
* @for EventTarget
*/
ETProto.removeTarget = function(o) {
delete this._yuievt.targets[Y.stamp(o)];
};
/**
* Propagate an event. Requires the event-custom-complex module.
* @method bubble
* @param evt {CustomEvent} the custom event to propagate
* @return {boolean} the aggregated return value from Event.Custom.fire
* @for EventTarget
*/
ETProto.bubble = function(evt, args, target, es) {
var targs = this._yuievt.targets, ret = true,
t, type = evt && evt.type, ce, i, bc, ce2,
originalTarget = target || (evt && evt.target) || this,
oldbubble;
if (!evt || ((!evt.stopped) && targs)) {
// Y.log('Bubbling ' + evt.type);
for (i in targs) {
if (targs.hasOwnProperty(i)) {
t = targs[i];
ce = t.getEvent(type, true);
ce2 = t.getSibling(type, ce);
if (ce2 && !ce) {
ce = t.publish(type);
}
oldbubble = t._yuievt.bubbling;
t._yuievt.bubbling = type;
// if this event was not published on the bubble target,
// continue propagating the event.
if (!ce) {
if (t._yuievt.hasTargets) {
t.bubble(evt, args, originalTarget, es);
}
} else {
ce.sibling = ce2;
// set the original target to that the target payload on the
// facade is correct.
ce.target = originalTarget;
ce.originalTarget = originalTarget;
ce.currentTarget = t;
bc = ce.broadcast;
ce.broadcast = false;
// default publish may not have emitFacade true -- that
// shouldn't be what the implementer meant to do
ce.emitFacade = true;
ce.stack = es;
ret = ret && ce.fire.apply(ce, args || evt.details || []);
ce.broadcast = bc;
ce.originalTarget = null;
// stopPropagation() was called
if (ce.stopped) {
break;
}
}
t._yuievt.bubbling = oldbubble;
}
}
}
return ret;
};
FACADE = new Y.EventFacade();
FACADE_KEYS = Y.Object.keys(FACADE);