event-facade.js revision a4ef7d2a4e172cb1efd9c548d3f239364bcc3dc0
486N/A
486N/A/**
486N/A * Adds event facades, preventable default behavior, and bubbling.
486N/A * events.
486N/A * @module event-custom
486N/A * @submodule event-custom-complex
486N/A */
486N/A
486N/A(function() {
486N/A
486N/Avar FACADE, FACADE_KEYS, CEProto = Y.CustomEvent.prototype,
486N/A ETProto = Y.EventTarget.prototype;
486N/A
486N/A/**
486N/A * Wraps and protects a custom event for use when emitFacade is set to true.
486N/A * Requires the event-custom-complex module
486N/A * @class EventFacade
486N/A * @param e {Event} the custom event
486N/A * @param currentTarget {HTMLElement} the element the listener was attached to
873N/A */
486N/A
486N/AY.EventFacade = function(e, currentTarget) {
486N/A
486N/A e = e || {};
486N/A
3233N/A /**
486N/A * The arguments passed to fire
486N/A * @property details
619N/A * @type Array
619N/A */
619N/A this.details = e.details;
619N/A
619N/A /**
3488N/A * The event type, this can be overridden by the fire() payload
619N/A * @property type
619N/A * @type string
619N/A */
619N/A this.type = e.type;
619N/A
619N/A /**
619N/A * The real event type
619N/A * @property type
486N/A * @type string
486N/A */
486N/A this._type = e.type;
486N/A
486N/A //////////////////////////////////////////////////////
486N/A
486N/A /**
486N/A * Node reference for the targeted eventtarget
536N/A * @propery target
2976N/A * @type Node
2976N/A */
536N/A this.target = e.target;
2976N/A
2976N/A /**
2976N/A * Node reference for the element that the listener was attached to.
2976N/A * @propery currentTarget
2976N/A * @type Node
2976N/A */
1181N/A this.currentTarget = currentTarget;
2976N/A
1181N/A /**
536N/A * Node reference to the relatedTarget
2976N/A * @propery relatedTarget
830N/A * @type Node
830N/A */
830N/A this.relatedTarget = e.relatedTarget;
830N/A
830N/A /**
830N/A * Stops the propagation to the next bubble target
2650N/A * @method stopPropagation
830N/A */
830N/A this.stopPropagation = function() {
830N/A e.stopPropagation();
2296N/A this.stopped = 1;
830N/A };
961N/A
961N/A /**
961N/A * Stops the propagation to the next bubble target and
486N/A * prevents any additional listeners from being exectued
2976N/A * on the current target.
486N/A * @method stopImmediatePropagation
486N/A */
486N/A this.stopImmediatePropagation = function() {
486N/A e.stopImmediatePropagation();
2296N/A this.stopped = 2;
2296N/A };
2650N/A
2296N/A /**
2296N/A * Prevents the event's default behavior
2650N/A * @method preventDefault
2296N/A */
2296N/A this.preventDefault = function() {
2402N/A e.preventDefault();
2402N/A this.prevented = 1;
2402N/A };
2402N/A
2650N/A /**
2402N/A * Stops the event propagation and prevents the default
2422N/A * event behavior.
2402N/A * @method halt
2402N/A * @param immediate {boolean} if true additional listeners
2402N/A * on the current target will not be executed
2402N/A */
2402N/A this.halt = function(immediate) {
2402N/A e.halt(immediate);
2402N/A this.prevented = 1;
2402N/A this.stopped = (immediate) ? 2 : 1;
2402N/A };
486N/A
961N/A};
961N/A
961N/ACEProto.fireComplex = function(args) {
961N/A var es = Y.Env._eventstack, ef, q, queue, ce, ret, events, subs,
961N/A self = this, host = self.host || self, next, oldbubble;
961N/A
961N/A if (es) {
1024N/A // queue this event if the current item in the queue bubbles
2976N/A if (self.queuable && self.type != es.next.type) {
961N/A self.log('queue ' + self.type);
961N/A es.queue.push([self, args]);
961N/A return true;
961N/A }
961N/A } else {
961N/A Y.Env._eventstack = {
961N/A // id of the first event in the stack
961N/A id: self.id,
961N/A next: self,
961N/A silent: self.silent,
486N/A stopped: 0,
prevented: 0,
bubbling: null,
type: self.type,
// defaultFnQueue: new Y.Queue(),
afterQueue: new Y.Queue(),
defaultTargetOnly: self.defaultTargetOnly,
queue: []
};
es = Y.Env._eventstack;
}
subs = self.getSubs();
self.stopped = (self.type !== es.type) ? 0 : es.stopped;
self.prevented = (self.type !== es.type) ? 0 : es.prevented;
self.target = self.target || host;
events = new Y.EventTarget({
fireOnce: true,
context: host
});
self.events = events;
if (self.preventedFn) {
events.on('prevented', self.preventedFn);
}
if (self.stoppedFn) {
events.on('stopped', self.stoppedFn);
}
self.currentTarget = host;
self.details = args.slice(); // original arguments in the details
// self.log("Firing " + self + ", " + "args: " + args);
self.log("Firing " + self.type);
self._facade = null; // kill facade to eliminate stale properties
ef = self._getFacade(args);
if (Y.Lang.isObject(args[0])) {
args[0] = ef;
} else {
args.unshift(ef);
}
// if (subCount) {
if (subs[0]) {
// self._procSubs(Y.merge(self.subscribers), args, ef);
self._procSubs(subs[0], args, ef);
}
// bubble if this is hosted in an event target and propagation has not been stopped
if (self.bubbles && host.bubble && !self.stopped) {
oldbubble = es.bubbling;
// self.bubbling = true;
es.bubbling = self.type;
// if (host !== ef.target || es.type != self.type) {
if (es.type != self.type) {
es.stopped = 0;
es.prevented = 0;
}
ret = host.bubble(self);
self.stopped = Math.max(self.stopped, es.stopped);
self.prevented = Math.max(self.prevented, es.prevented);
// self.bubbling = false;
es.bubbling = oldbubble;
}
// execute the default behavior if not prevented
// console.log('defaultTargetOnly: ' + self.defaultTargetOnly);
// console.log('host === target: ' + (host === ef.target));
// if (self.defaultFn && !self.prevented && ((!self.defaultTargetOnly) || host === es.id === self.id)) {
if (self.defaultFn && !self.prevented && ((!self.defaultTargetOnly && !es.defaultTargetOnly) || host === ef.target)) {
// if (es.id === self.id) {
// self.defaultFn.apply(host, args);
// while ((next = es.defaultFnQueue.last())) {
// next();
// }
// } else {
// es.defaultFnQueue.add(function() {
// self.defaultFn.apply(host, args);
// });
// }
self.defaultFn.apply(host, args);
}
// broadcast listeners are fired as discreet events on the
// YUI instance and potentially the YUI global.
self._broadcast(args);
// process after listeners. If the default behavior was
// prevented, the after events don't fire.
// if (self.afterCount && !self.prevented && self.stopped < 2) {
// if (subs[1] && !self.prevented && self.stopped < 2) {
// // self._procSubs(Y.merge(self.afters), args, ef);
// self._procSubs(subs[1], args, ef);
// }
// Queue the after
if (subs[1] && !self.prevented && self.stopped < 2) {
if (es.id === self.id || self.type != host._yuievt.bubbling) {
self._procSubs(subs[1], args, ef);
while ((next = es.afterQueue.last())) {
next();
}
} else {
es.afterQueue.add(function() {
self._procSubs(subs[1], args, ef);
});
}
}
self.target = null;
// es.stopped = 0;
// es.prevented = 0;
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]);
// es.stopped = 0;
// es.prevented = 0;
}
Y.Env._eventstack = 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;
Y.Env._eventstack.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;
Y.Env._eventstack.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;
Y.Env._eventstack.prevented = 1;
this.events.fire('prevented', this);
}
};
/**
* 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 {Event.Custom} the custom event to propagate
* @return {boolean} the aggregated return value from Event.Custom.fire
* @for EventTarget
*/
ETProto.bubble = function(evt, args, target) {
var targs = this._yuievt.targets, ret = true,
t, type = evt && evt.type, ce, i, bc, ce2,
originalTarget = target || (evt && evt.target) || this,
es = Y.Env._eventstack, 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);
}
} 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;
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);
})();