2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
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
20 var FACADE, FACADE_KEYS, CEProto = Y.CustomEvent.prototype;
23 * Wraps and protects a custom event for use when emitFacade is set to true.
24 * Requires the event-custom-complex module
26 * @param e {Event} the custom event
27 * @param currentTarget {HTMLElement} the element the listener was attached to
30 Y.EventFacade = function(e, currentTarget) {
35 * The arguments passed to fire
39 this.details = e.details;
48 //////////////////////////////////////////////////////
51 * Node reference for the targeted eventtarget
55 this.target = e.target;
58 * Node reference for the element that the listener was attached to.
59 * @propery currentTarget
62 this.currentTarget = currentTarget;
65 * Node reference to the relatedTarget
66 * @propery relatedTarget
69 this.relatedTarget = e.relatedTarget;
72 * Stops the propagation to the next bubble target
73 * @method stopPropagation
75 this.stopPropagation = function() {
80 * Stops the propagation to the next bubble target and
81 * prevents any additional listeners from being exectued
82 * on the current target.
83 * @method stopImmediatePropagation
85 this.stopImmediatePropagation = function() {
86 e.stopImmediatePropagation();
90 * Prevents the event's default behavior
91 * @method preventDefault
93 this.preventDefault = function() {
98 * Stops the event propagation and prevents the default
101 * @param immediate {boolean} if true additional listeners
102 * on the current target will not be executed
104 this.halt = function(immediate) {
110 CEProto.fireComplex = function(args) {
111 var es = Y.Env._eventstack, ef, q, queue, ce, ret, events;
114 // queue this event if the current item in the queue bubbles
115 if (this.queuable && this.type != es.next.type) {
116 this.log('queue ' + this.type);
117 es.queue.push([this, args]);
121 Y.Env._eventstack = {
122 // id of the first event in the stack
130 es = Y.Env._eventstack;
135 this.target = this.target || this.host;
137 events = new Y.EventTarget({
142 this.events = events;
144 if (this.preventedFn) {
145 events.on('prevented', this.preventedFn);
148 if (this.stoppedFn) {
149 events.on('stopped', this.stoppedFn);
152 this.currentTarget = this.host || this.currentTarget;
154 this.details = args.slice(); // original arguments in the details
156 // this.log("Firing " + this + ", " + "args: " + args);
157 this.log("Firing " + this.type);
159 this._facade = null; // kill facade to eliminate stale properties
161 ef = this._getFacade(args);
163 if (Y.Lang.isObject(args[0])) {
169 if (this.hasSubscribers) {
170 this._procSubs(Y.merge(this.subscribers), args, ef);
173 // bubble if this is hosted in an event target and propagation has not been stopped
174 if (this.bubbles && this.host && this.host.bubble && !this.stopped) {
177 ret = this.host.bubble(this);
179 this.stopped = Math.max(this.stopped, es.stopped);
180 this.prevented = Math.max(this.prevented, es.prevented);
184 // execute the default behavior if not prevented
185 if (this.defaultFn && !this.prevented) {
186 this.defaultFn.apply(this.host || this, args);
189 // broadcast listeners are fired as discreet events on the
190 // YUI instance and potentially the YUI global.
191 this._broadcast(args);
193 // process after listeners. If the default behavior was
194 // prevented, the after events don't fire.
195 if (this.hasAfters && !this.prevented && this.stopped < 2) {
196 this._procSubs(Y.merge(this.afters), args, ef);
199 if (es.id === this.id) {
202 while (queue.length) {
207 // set up stack to allow the next item to be processed
209 ce.fire.apply(ce, q[1]);
212 Y.Env._eventstack = null;
215 return this.stopped ? false : true;
218 CEProto._getFacade = function() {
220 var ef = this._facade, o, o2,
224 ef = new Y.EventFacade(this, this.currentTarget);
227 // if the first argument is an object literal, apply the
228 // properties to the event facade
231 if (Y.Lang.isObject(o, true)) {
235 // protect the event facade properties
236 Y.mix(o2, ef, true, FACADE_KEYS);
242 Y.mix(ef, o2, true, FACADE_KEYS);
245 // update the details field with the arguments
246 // ef.type = this.type;
247 ef.details = this.details;
248 ef.target = this.target;
249 ef.currentTarget = this.currentTarget;
259 * Stop propagation to bubble targets
261 * @method stopPropagation
263 CEProto.stopPropagation = function() {
265 Y.Env._eventstack.stopped = 1;
266 this.events.fire('stopped', this);
270 * Stops propagation to bubble targets, and prevents any remaining
271 * subscribers on the current target from executing.
272 * @method stopImmediatePropagation
274 CEProto.stopImmediatePropagation = function() {
276 Y.Env._eventstack.stopped = 2;
277 this.events.fire('stopped', this);
281 * Prevents the execution of this event's defaultFn
282 * @method preventDefault
284 CEProto.preventDefault = function() {
285 if (this.preventable) {
287 Y.Env._eventstack.prevented = 1;
288 this.events.fire('prevented', this);
293 * Stops the event propagation and prevents the default
296 * @param immediate {boolean} if true additional listeners
297 * on the current target will not be executed
299 CEProto.halt = function(immediate) {
301 this.stopImmediatePropagation();
303 this.stopPropagation();
305 this.preventDefault();
309 * Propagate an event. Requires the event-custom-complex module.
311 * @param evt {Event.Custom} the custom event to propagate
312 * @return {boolean} the aggregated return value from Event.Custom.fire
315 Y.EventTarget.prototype.bubble = function(evt, args, target) {
317 var targs = this._yuievt.targets, ret = true,
320 if (!evt || ((!evt.stopped) && targs)) {
323 if (targs.hasOwnProperty(i)) {
325 type = evt && evt.type;
326 ce = t.getEvent(type, true);
328 // if this event was not published on the bubble target,
329 // publish it with sensible default properties
332 if (t._yuievt.hasTargets) {
333 t.bubble.call(t, evt, args, target);
337 ce.target = target || (evt && evt.target) || this;
338 ce.currentTarget = t;
341 ce.broadcast = false;
342 ret = ret && ce.fire.apply(ce, args || evt.details);
345 // stopPropagation() was called
357 FACADE = new Y.EventFacade();
358 FACADE_KEYS = Y.Object.keys(FACADE);
363 }, '3.0.0' ,{requires:['event-custom-base']});