event-custom-complex-debug.js revision 0771d781138a507b3e657573703f511291640bf3
127N/AYUI.add('event-custom-complex', function(Y) {
127N/A
127N/A(function() {
127N/A
127N/Avar FACADE, FACADE_KEYS, CEProto = Y.CustomEvent.prototype;
127N/A
127N/A/**
127N/A * Wraps and protects a custom event for use when emitFacade is set to true.
127N/A * Requires the event-custom-complex module
127N/A * @class EventFacade
127N/A * @param e {Event} the custom event
127N/A * @param currentTarget {HTMLElement} the element the listener was attached to
127N/A */
127N/A
127N/AY.EventFacade = function(e, currentTarget) {
127N/A
127N/A e = e || {};
127N/A
127N/A /**
127N/A * The arguments passed to fire
127N/A * @property details
127N/A * @type Array
729N/A */
127N/A this.details = e.details;
127N/A
127N/A /**
127N/A * The event type
127N/A * @property type
729N/A * @type string
618N/A */
127N/A this.type = e.type;
127N/A
844N/A //////////////////////////////////////////////////////
844N/A
127N/A /**
127N/A * Node reference for the targeted eventtarget
729N/A * @propery target
729N/A * @type Node
729N/A */
729N/A this.target = e.target;
729N/A
729N/A /**
844N/A * Node reference for the element that the listener was attached to.
844N/A * @propery currentTarget
729N/A * @type Node
729N/A */
729N/A this.currentTarget = currentTarget;
127N/A
127N/A /**
127N/A * Node reference to the relatedTarget
127N/A * @propery relatedTarget
127N/A * @type Node
127N/A */
127N/A this.relatedTarget = e.relatedTarget;
127N/A
127N/A /**
729N/A * Stops the propagation to the next bubble target
729N/A * @method stopPropagation
751N/A */
751N/A this.stopPropagation = function() {
751N/A e.stopPropagation();
751N/A };
751N/A
751N/A /**
751N/A * Stops the propagation to the next bubble target and
751N/A * prevents any additional listeners from being exectued
751N/A * on the current target.
751N/A * @method stopImmediatePropagation
729N/A */
729N/A this.stopImmediatePropagation = function() {
729N/A e.stopImmediatePropagation();
729N/A };
729N/A
729N/A /**
729N/A * Prevents the event's default behavior
729N/A * @method preventDefault
729N/A */
729N/A this.preventDefault = function() {
729N/A e.preventDefault();
729N/A };
729N/A
729N/A /**
729N/A * Stops the event propagation and prevents the default
729N/A * event behavior.
729N/A * @method halt
729N/A * @param immediate {boolean} if true additional listeners
729N/A * on the current target will not be executed
729N/A */
729N/A this.halt = function(immediate) {
729N/A e.halt(immediate);
729N/A };
729N/A
729N/A};
729N/A
729N/ACEProto.fireComplex = function(args) {
729N/A var es = Y.Env._eventstack, ef, q, queue, ce, ret, events;
729N/A
729N/A if (es) {
729N/A // queue this event if the current item in the queue bubbles
729N/A if (this.queuable && this.type != es.next.type) {
729N/A this.log('queue ' + this.type);
729N/A es.queue.push([this, args]);
127N/A return true;
127N/A }
127N/A } else {
127N/A Y.Env._eventstack = {
127N/A // id of the first event in the stack
181N/A id: this.id,
181N/A next: this,
127N/A silent: this.silent,
127N/A stopped: 0,
127N/A prevented: 0,
queue: []
};
es = Y.Env._eventstack;
}
this.stopped = 0;
this.prevented = 0;
this.target = this.target || this.host;
events = new Y.EventTarget({
fireOnce: true,
context: this.host
});
this.events = events;
if (this.preventedFn) {
events.on('prevented', this.preventedFn);
}
if (this.stoppedFn) {
events.on('stopped', this.stoppedFn);
}
this.currentTarget = this.host || this.currentTarget;
this.details = args.slice(); // original arguments in the details
// this.log("Firing " + this + ", " + "args: " + args);
this.log("Firing " + this.type);
this._facade = null; // kill facade to eliminate stale properties
ef = this._getFacade(args);
if (Y.Lang.isObject(args[0])) {
args[0] = ef;
} else {
args.unshift(ef);
}
if (this.hasSubscribers) {
this._procSubs(Y.merge(this.subscribers), args, ef);
}
// bubble if this is hosted in an event target and propagation has not been stopped
if (this.bubbles && this.host && this.host.bubble && !this.stopped) {
es.stopped = 0;
es.prevented = 0;
ret = this.host.bubble(this);
this.stopped = Math.max(this.stopped, es.stopped);
this.prevented = Math.max(this.prevented, es.prevented);
}
// execute the default behavior if not prevented
if (this.defaultFn && !this.prevented) {
this.defaultFn.apply(this.host || this, args);
}
// broadcast listeners are fired as discreet events on the
// YUI instance and potentially the YUI global.
this._broadcast(args);
// process after listeners. If the default behavior was
// prevented, the after events don't fire.
if (this.hasAfters && !this.prevented && this.stopped < 2) {
this._procSubs(Y.merge(this.afters), args, ef);
}
if (es.id === this.id) {
queue = es.queue;
while (queue.length) {
q = queue.pop();
ce = q[0];
es.stopped = 0;
es.prevented = 0;
// set up stack to allow the next item to be processed
es.next = ce;
ce.fire.apply(ce, q[1]);
}
Y.Env._eventstack = null;
}
return this.stopped ? false : true;
};
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);
}
// update the details field with the arguments
// ef.type = this.type;
ef.details = this.details;
ef.target = this.target;
ef.currentTarget = this.currentTarget;
ef.stopped = 0;
ef.prevented = 0;
this._facade = ef;
return this._facade;
};
/**
* Stop propagation to bubble targets
* @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();
};
/**
* 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
*/
Y.EventTarget.prototype.bubble = function(evt, args, target) {
var targs = this._yuievt.targets, ret = true,
t, type, ce, i;
if (!evt || ((!evt.stopped) && targs)) {
// Y.log('Bubbling ' + evt.type);
for (i in targs) {
if (targs.hasOwnProperty(i)) {
t = targs[i];
type = evt && evt.type;
ce = t.getEvent(type);
// if this event was not published on the bubble target,
// publish it with sensible default properties
if (!ce) {
if (t._yuievt.hasTargets) {
t.bubble.call(t, evt, args, target);
}
} else {
ce.target = target || (evt && evt.target) || this;
ce.currentTarget = t;
ret = ret && ce.fire.apply(ce, args || evt.details);
// stopPropagation() was called
if (ce.stopped) {
break;
}
}
}
}
}
return ret;
};
FACADE = new Y.EventFacade();
FACADE_KEYS = Y.Object.keys(FACADE);
})();
}, '@VERSION@' ,{requires:['event-custom-base']});