]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/event/event.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / event / event.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 var GLOBAL_ENV = YUI.Env;
9
10 if (!GLOBAL_ENV._ready) {
11     GLOBAL_ENV._ready = function() {
12         GLOBAL_ENV.DOMReady = true;
13         GLOBAL_ENV.remove(YUI.config.doc, 'DOMContentLoaded', GLOBAL_ENV._ready);
14     };
15
16     // if (!YUI.UA.ie) {
17         GLOBAL_ENV.add(YUI.config.doc, 'DOMContentLoaded', GLOBAL_ENV._ready);
18     // }
19 }
20
21 YUI.add('event-base', function(Y) {
22
23 /*
24  * DOM event listener abstraction layer
25  * @module event
26  * @submodule event-base
27  */
28
29 /**
30  * The domready event fires at the moment the browser's DOM is
31  * usable. In most cases, this is before images are fully
32  * downloaded, allowing you to provide a more responsive user
33  * interface.
34  *
35  * In YUI 3, domready subscribers will be notified immediately if
36  * that moment has already passed when the subscription is created.
37  *
38  * One exception is if the yui.js file is dynamically injected into
39  * the page.  If this is done, you must tell the YUI instance that
40  * you did this in order for DOMReady (and window load events) to
41  * fire normally.  That configuration option is 'injected' -- set
42  * it to true if the yui.js script is not included inline.
43  *
44  * This method is part of the 'event-ready' module, which is a
45  * submodule of 'event'.
46  *
47  * @event domready
48  * @for YUI
49  */
50 Y.publish('domready', {
51     fireOnce: true,
52     async: true
53 });
54
55 if (GLOBAL_ENV.DOMReady) {
56     Y.fire('domready');
57 } else {
58     Y.Do.before(function() { Y.fire('domready'); }, YUI.Env, '_ready');
59 }
60
61 /**
62  * Custom event engine, DOM event listener abstraction layer, synthetic DOM
63  * events.
64  * @module event
65  * @submodule event-base
66  */
67
68 /**
69  * Wraps a DOM event, properties requiring browser abstraction are
70  * fixed here.  Provids a security layer when required.
71  * @class DOMEventFacade
72  * @param ev {Event} the DOM event
73  * @param currentTarget {HTMLElement} the element the listener was attached to
74  * @param wrapper {Event.Custom} the custom event wrapper for this DOM event
75  */
76
77     var ua = Y.UA,
78
79     EMPTY = {},
80
81     /**
82      * webkit key remapping required for Safari < 3.1
83      * @property webkitKeymap
84      * @private
85      */
86     webkitKeymap = {
87         63232: 38, // up
88         63233: 40, // down
89         63234: 37, // left
90         63235: 39, // right
91         63276: 33, // page up
92         63277: 34, // page down
93         25:     9, // SHIFT-TAB (Safari provides a different key code in
94                    // this case, even though the shiftKey modifier is set)
95         63272: 46, // delete
96         63273: 36, // home
97         63275: 35  // end
98     },
99
100     /**
101      * Returns a wrapped node.  Intended to be used on event targets,
102      * so it will return the node's parent if the target is a text
103      * node.
104      *
105      * If accessing a property of the node throws an error, this is
106      * probably the anonymous div wrapper Gecko adds inside text
107      * nodes.  This likely will only occur when attempting to access
108      * the relatedTarget.  In this case, we now return null because
109      * the anonymous div is completely useless and we do not know
110      * what the related target was because we can't even get to
111      * the element's parent node.
112      *
113      * @method resolve
114      * @private
115      */
116     resolve = function(n) {
117         if (!n) {
118             return n;
119         }
120         try {
121             if (n && 3 == n.nodeType) {
122                 n = n.parentNode;
123             }
124         } catch(e) {
125             return null;
126         }
127
128         return Y.one(n);
129     },
130
131     DOMEventFacade = function(ev, currentTarget, wrapper) {
132         this._event = ev;
133         this._currentTarget = currentTarget;
134         this._wrapper = wrapper || EMPTY;
135
136         // if not lazy init
137         this.init();
138     };
139
140 Y.extend(DOMEventFacade, Object, {
141
142     init: function() {
143
144         var e = this._event,
145             overrides = this._wrapper.overrides,
146             x = e.pageX,
147             y = e.pageY,
148             c,
149             currentTarget = this._currentTarget;
150
151         this.altKey   = e.altKey;
152         this.ctrlKey  = e.ctrlKey;
153         this.metaKey  = e.metaKey;
154         this.shiftKey = e.shiftKey;
155         this.type     = (overrides && overrides.type) || e.type;
156         this.clientX  = e.clientX;
157         this.clientY  = e.clientY;
158
159         this.pageX = x;
160         this.pageY = y;
161
162         c = e.keyCode || e.charCode;
163
164         if (ua.webkit && (c in webkitKeymap)) {
165             c = webkitKeymap[c];
166         }
167
168         this.keyCode = c;
169         this.charCode = c;
170         this.which = e.which || e.charCode || c;
171         // this.button = e.button;
172         this.button = this.which;
173
174         this.target = resolve(e.target);
175         this.currentTarget = resolve(currentTarget);
176         this.relatedTarget = resolve(e.relatedTarget);
177
178         if (e.type == "mousewheel" || e.type == "DOMMouseScroll") {
179             this.wheelDelta = (e.detail) ? (e.detail * -1) : Math.round(e.wheelDelta / 80) || ((e.wheelDelta < 0) ? -1 : 1);
180         }
181
182         if (this._touch) {
183             this._touch(e, currentTarget, this._wrapper);
184         }
185     },
186
187     stopPropagation: function() {
188         this._event.stopPropagation();
189         this._wrapper.stopped = 1;
190         this.stopped = 1;
191     },
192
193     stopImmediatePropagation: function() {
194         var e = this._event;
195         if (e.stopImmediatePropagation) {
196             e.stopImmediatePropagation();
197         } else {
198             this.stopPropagation();
199         }
200         this._wrapper.stopped = 2;
201         this.stopped = 2;
202     },
203
204     preventDefault: function(returnValue) {
205         var e = this._event;
206         e.preventDefault();
207         e.returnValue = returnValue || false;
208         this._wrapper.prevented = 1;
209         this.prevented = 1;
210     },
211
212     halt: function(immediate) {
213         if (immediate) {
214             this.stopImmediatePropagation();
215         } else {
216             this.stopPropagation();
217         }
218
219         this.preventDefault();
220     }
221
222 });
223
224 DOMEventFacade.resolve = resolve;
225 Y.DOM2EventFacade = DOMEventFacade;
226 Y.DOMEventFacade = DOMEventFacade;
227
228     /**
229      * The native event
230      * @property _event
231      */
232
233     /**
234      * The X location of the event on the page (including scroll)
235      * @property pageX
236      * @type int
237      */
238
239     /**
240      * The Y location of the event on the page (including scroll)
241      * @property pageY
242      * @type int
243      */
244
245     /**
246      * The keyCode for key events.  Uses charCode if keyCode is not available
247      * @property keyCode
248      * @type int
249      */
250
251     /**
252      * The charCode for key events.  Same as keyCode
253      * @property charCode
254      * @type int
255      */
256
257     /**
258      * The button that was pushed.
259      * @property button
260      * @type int
261      */
262
263     /**
264      * The button that was pushed.  Same as button.
265      * @property which
266      * @type int
267      */
268
269     /**
270      * Node reference for the targeted element
271      * @propery target
272      * @type Node
273      */
274
275     /**
276      * Node reference for the element that the listener was attached to.
277      * @propery currentTarget
278      * @type Node
279      */
280
281     /**
282      * Node reference to the relatedTarget
283      * @propery relatedTarget
284      * @type Node
285      */
286
287     /**
288      * Number representing the direction and velocity of the movement of the mousewheel.
289      * Negative is down, the higher the number, the faster.  Applies to the mousewheel event.
290      * @property wheelDelta
291      * @type int
292      */
293
294     /**
295      * Stops the propagation to the next bubble target
296      * @method stopPropagation
297      */
298
299     /**
300      * Stops the propagation to the next bubble target and
301      * prevents any additional listeners from being exectued
302      * on the current target.
303      * @method stopImmediatePropagation
304      */
305
306     /**
307      * Prevents the event's default behavior
308      * @method preventDefault
309      * @param returnValue {string} sets the returnValue of the event to this value
310      * (rather than the default false value).  This can be used to add a customized
311      * confirmation query to the beforeunload event).
312      */
313
314     /**
315      * Stops the event propagation and prevents the default
316      * event behavior.
317      * @method halt
318      * @param immediate {boolean} if true additional listeners
319      * on the current target will not be executed
320      */
321 (function() {
322 /**
323  * DOM event listener abstraction layer
324  * @module event
325  * @submodule event-base
326  */
327
328 /**
329  * The event utility provides functions to add and remove event listeners,
330  * event cleansing.  It also tries to automatically remove listeners it
331  * registers during the unload event.
332  *
333  * @class Event
334  * @static
335  */
336
337 Y.Env.evt.dom_wrappers = {};
338 Y.Env.evt.dom_map = {};
339
340 var _eventenv = Y.Env.evt,
341     config = Y.config,
342     win = config.win,
343     add = YUI.Env.add,
344     remove = YUI.Env.remove,
345
346     onLoad = function() {
347         YUI.Env.windowLoaded = true;
348         Y.Event._load();
349         remove(win, "load", onLoad);
350     },
351
352     onUnload = function() {
353         Y.Event._unload();
354     },
355
356     EVENT_READY = 'domready',
357
358     COMPAT_ARG = '~yui|2|compat~',
359
360     shouldIterate = function(o) {
361         try {
362             return (o && typeof o !== "string" && Y.Lang.isNumber(o.length) &&
363                     !o.tagName && !o.alert);
364         } catch(ex) {
365             return false;
366         }
367
368     },
369
370 Event = function() {
371
372     /**
373      * True after the onload event has fired
374      * @property _loadComplete
375      * @type boolean
376      * @static
377      * @private
378      */
379     var _loadComplete =  false,
380
381     /**
382      * The number of times to poll after window.onload.  This number is
383      * increased if additional late-bound handlers are requested after
384      * the page load.
385      * @property _retryCount
386      * @static
387      * @private
388      */
389     _retryCount = 0,
390
391     /**
392      * onAvailable listeners
393      * @property _avail
394      * @static
395      * @private
396      */
397     _avail = [],
398
399     /**
400      * Custom event wrappers for DOM events.  Key is
401      * 'event:' + Element uid stamp + event type
402      * @property _wrappers
403      * @type Y.Event.Custom
404      * @static
405      * @private
406      */
407     _wrappers = _eventenv.dom_wrappers,
408
409     _windowLoadKey = null,
410
411     /**
412      * Custom event wrapper map DOM events.  Key is
413      * Element uid stamp.  Each item is a hash of custom event
414      * wrappers as provided in the _wrappers collection.  This
415      * provides the infrastructure for getListeners.
416      * @property _el_events
417      * @static
418      * @private
419      */
420     _el_events = _eventenv.dom_map;
421
422     return {
423
424         /**
425          * The number of times we should look for elements that are not
426          * in the DOM at the time the event is requested after the document
427          * has been loaded.  The default is 1000@amp;40 ms, so it will poll
428          * for 40 seconds or until all outstanding handlers are bound
429          * (whichever comes first).
430          * @property POLL_RETRYS
431          * @type int
432          * @static
433          * @final
434          */
435         POLL_RETRYS: 1000,
436
437         /**
438          * The poll interval in milliseconds
439          * @property POLL_INTERVAL
440          * @type int
441          * @static
442          * @final
443          */
444         POLL_INTERVAL: 40,
445
446         /**
447          * addListener/removeListener can throw errors in unexpected scenarios.
448          * These errors are suppressed, the method returns false, and this property
449          * is set
450          * @property lastError
451          * @static
452          * @type Error
453          */
454         lastError: null,
455
456
457         /**
458          * poll handle
459          * @property _interval
460          * @static
461          * @private
462          */
463         _interval: null,
464
465         /**
466          * document readystate poll handle
467          * @property _dri
468          * @static
469          * @private
470          */
471          _dri: null,
472
473         /**
474          * True when the document is initially usable
475          * @property DOMReady
476          * @type boolean
477          * @static
478          */
479         DOMReady: false,
480
481         /**
482          * @method startInterval
483          * @static
484          * @private
485          */
486         startInterval: function() {
487             if (!Event._interval) {
488 Event._interval = setInterval(Event._poll, Event.POLL_INTERVAL);
489             }
490         },
491
492         /**
493          * Executes the supplied callback when the item with the supplied
494          * id is found.  This is meant to be used to execute behavior as
495          * soon as possible as the page loads.  If you use this after the
496          * initial page load it will poll for a fixed time for the element.
497          * The number of times it will poll and the frequency are
498          * configurable.  By default it will poll for 10 seconds.
499          *
500          * <p>The callback is executed with a single parameter:
501          * the custom object parameter, if provided.</p>
502          *
503          * @method onAvailable
504          *
505          * @param {string||string[]}   id the id of the element, or an array
506          * of ids to look for.
507          * @param {function} fn what to execute when the element is found.
508          * @param {object}   p_obj an optional object to be passed back as
509          *                   a parameter to fn.
510          * @param {boolean|object}  p_override If set to true, fn will execute
511          *                   in the context of p_obj, if set to an object it
512          *                   will execute in the context of that object
513          * @param checkContent {boolean} check child node readiness (onContentReady)
514          * @static
515          * @deprecated Use Y.on("available")
516          */
517         // @TODO fix arguments
518         onAvailable: function(id, fn, p_obj, p_override, checkContent, compat) {
519
520             var a = Y.Array(id), i, availHandle;
521
522
523             for (i=0; i<a.length; i=i+1) {
524                 _avail.push({
525                     id:         a[i],
526                     fn:         fn,
527                     obj:        p_obj,
528                     override:   p_override,
529                     checkReady: checkContent,
530                     compat:     compat
531                 });
532             }
533             _retryCount = this.POLL_RETRYS;
534
535             // We want the first test to be immediate, but async
536             setTimeout(Event._poll, 0);
537
538             availHandle = new Y.EventHandle({
539
540                 _delete: function() {
541                     // set by the event system for lazy DOM listeners
542                     if (availHandle.handle) {
543                         availHandle.handle.detach();
544                         return;
545                     }
546
547                     var i, j;
548
549                     // otherwise try to remove the onAvailable listener(s)
550                     for (i = 0; i < a.length; i++) {
551                         for (j = 0; j < _avail.length; j++) {
552                             if (a[i] === _avail[j].id) {
553                                 _avail.splice(j, 1);
554                             }
555                         }
556                     }
557                 }
558
559             });
560
561             return availHandle;
562         },
563
564         /**
565          * Works the same way as onAvailable, but additionally checks the
566          * state of sibling elements to determine if the content of the
567          * available element is safe to modify.
568          *
569          * <p>The callback is executed with a single parameter:
570          * the custom object parameter, if provided.</p>
571          *
572          * @method onContentReady
573          *
574          * @param {string}   id the id of the element to look for.
575          * @param {function} fn what to execute when the element is ready.
576          * @param {object}   obj an optional object to be passed back as
577          *                   a parameter to fn.
578          * @param {boolean|object}  override If set to true, fn will execute
579          *                   in the context of p_obj.  If an object, fn will
580          *                   exectute in the context of that object
581          *
582          * @static
583          * @deprecated Use Y.on("contentready")
584          */
585         // @TODO fix arguments
586         onContentReady: function(id, fn, obj, override, compat) {
587             return Event.onAvailable(id, fn, obj, override, true, compat);
588         },
589
590         /**
591          * Adds an event listener
592          *
593          * @method attach
594          *
595          * @param {String}   type     The type of event to append
596          * @param {Function} fn        The method the event invokes
597          * @param {String|HTMLElement|Array|NodeList} el An id, an element
598          *  reference, or a collection of ids and/or elements to assign the
599          *  listener to.
600          * @param {Object}   context optional context object
601          * @param {Boolean|object}  args 0..n arguments to pass to the callback
602          * @return {EventHandle} an object to that can be used to detach the listener
603          *
604          * @static
605          */
606
607         attach: function(type, fn, el, context) {
608             return Event._attach(Y.Array(arguments, 0, true));
609         },
610
611         _createWrapper: function (el, type, capture, compat, facade) {
612
613             var cewrapper,
614                 ek  = Y.stamp(el),
615                 key = 'event:' + ek + type;
616
617             if (false === facade) {
618                 key += 'native';
619             }
620             if (capture) {
621                 key += 'capture';
622             }
623
624
625             cewrapper = _wrappers[key];
626
627
628             if (!cewrapper) {
629                 // create CE wrapper
630                 cewrapper = Y.publish(key, {
631                     silent: true,
632                     bubbles: false,
633                     contextFn: function() {
634                         if (compat) {
635                             return cewrapper.el;
636                         } else {
637                             cewrapper.nodeRef = cewrapper.nodeRef || Y.one(cewrapper.el);
638                             return cewrapper.nodeRef;
639                         }
640                     }
641                 });
642
643                 cewrapper.overrides = {};
644
645                 // for later removeListener calls
646                 cewrapper.el = el;
647                 cewrapper.key = key;
648                 cewrapper.domkey = ek;
649                 cewrapper.type = type;
650                 cewrapper.fn = function(e) {
651                     cewrapper.fire(Event.getEvent(e, el, (compat || (false === facade))));
652                 };
653                 cewrapper.capture = capture;
654
655                 if (el == win && type == "load") {
656                     // window load happens once
657                     cewrapper.fireOnce = true;
658                     _windowLoadKey = key;
659                 }
660
661                 _wrappers[key] = cewrapper;
662                 _el_events[ek] = _el_events[ek] || {};
663                 _el_events[ek][key] = cewrapper;
664
665                 add(el, type, cewrapper.fn, capture);
666             }
667
668             return cewrapper;
669
670         },
671
672         _attach: function(args, conf) {
673
674             var compat,
675                 handles, oEl, cewrapper, context,
676                 fireNow = false, ret,
677                 type = args[0],
678                 fn = args[1],
679                 el = args[2] || win,
680                 facade = conf && conf.facade,
681                 capture = conf && conf.capture,
682                 overrides = conf && conf.overrides;
683
684             if (args[args.length-1] === COMPAT_ARG) {
685                 compat = true;
686                 // trimmedArgs.pop();
687             }
688
689             if (!fn || !fn.call) {
690 // throw new TypeError(type + " attach call failed, callback undefined");
691                 return false;
692             }
693
694             // The el argument can be an array of elements or element ids.
695             if (shouldIterate(el)) {
696
697                 handles=[];
698
699                 Y.each(el, function(v, k) {
700                     args[2] = v;
701                     handles.push(Event._attach(args, conf));
702                 });
703
704                 // return (handles.length === 1) ? handles[0] : handles;
705                 return new Y.EventHandle(handles);
706
707             // If the el argument is a string, we assume it is
708             // actually the id of the element.  If the page is loaded
709             // we convert el to the actual element, otherwise we
710             // defer attaching the event until the element is
711             // ready
712             } else if (Y.Lang.isString(el)) {
713
714                 // oEl = (compat) ? Y.DOM.byId(el) : Y.Selector.query(el);
715
716                 if (compat) {
717                     oEl = Y.DOM.byId(el);
718                 } else {
719
720                     oEl = Y.Selector.query(el);
721
722                     switch (oEl.length) {
723                         case 0:
724                             oEl = null;
725                             break;
726                         case 1:
727                             oEl = oEl[0];
728                             break;
729                         default:
730                             args[2] = oEl;
731                             return Event._attach(args, conf);
732                     }
733                 }
734
735                 if (oEl) {
736
737                     el = oEl;
738
739                 // Not found = defer adding the event until the element is available
740                 } else {
741
742                     ret = Event.onAvailable(el, function() {
743
744                         ret.handle = Event._attach(args, conf);
745
746                     }, Event, true, false, compat);
747
748                     return ret;
749
750                 }
751             }
752
753             // Element should be an html element or node
754             if (!el) {
755                 return false;
756             }
757
758             if (Y.Node && Y.instanceOf(el, Y.Node)) {
759                 el = Y.Node.getDOMNode(el);
760             }
761
762             cewrapper = Event._createWrapper(el, type, capture, compat, facade);
763             if (overrides) {
764                 Y.mix(cewrapper.overrides, overrides);
765             }
766
767             if (el == win && type == "load") {
768
769                 // if the load is complete, fire immediately.
770                 // all subscribers, including the current one
771                 // will be notified.
772                 if (YUI.Env.windowLoaded) {
773                     fireNow = true;
774                 }
775             }
776
777             if (compat) {
778                 args.pop();
779             }
780
781             context = args[3];
782
783             // set context to the Node if not specified
784             // ret = cewrapper.on.apply(cewrapper, trimmedArgs);
785             ret = cewrapper._on(fn, context, (args.length > 4) ? args.slice(4) : null);
786
787             if (fireNow) {
788                 cewrapper.fire();
789             }
790
791             return ret;
792
793         },
794
795         /**
796          * Removes an event listener.  Supports the signature the event was bound
797          * with, but the preferred way to remove listeners is using the handle
798          * that is returned when using Y.on
799          *
800          * @method detach
801          *
802          * @param {String} type the type of event to remove.
803          * @param {Function} fn the method the event invokes.  If fn is
804          * undefined, then all event handlers for the type of event are
805          * removed.
806          * @param {String|HTMLElement|Array|NodeList|EventHandle} el An
807          * event handle, an id, an element reference, or a collection
808          * of ids and/or elements to remove the listener from.
809          * @return {boolean} true if the unbind was successful, false otherwise.
810          * @static
811          */
812         detach: function(type, fn, el, obj) {
813
814             var args=Y.Array(arguments, 0, true), compat, l, ok, i,
815                 id, ce;
816
817             if (args[args.length-1] === COMPAT_ARG) {
818                 compat = true;
819                 // args.pop();
820             }
821
822             if (type && type.detach) {
823                 return type.detach();
824             }
825
826             // The el argument can be a string
827             if (typeof el == "string") {
828
829                 // el = (compat) ? Y.DOM.byId(el) : Y.all(el);
830                 if (compat) {
831                     el = Y.DOM.byId(el);
832                 } else {
833                     el = Y.Selector.query(el);
834                     l = el.length;
835                     if (l < 1) {
836                         el = null;
837                     } else if (l == 1) {
838                         el = el[0];
839                     }
840                 }
841                 // return Event.detach.apply(Event, args);
842             }
843
844             if (!el) {
845                 return false;
846             }
847
848             if (el.detach) {
849                 args.splice(2, 1);
850                 return el.detach.apply(el, args);
851             // The el argument can be an array of elements or element ids.
852             } else if (shouldIterate(el)) {
853                 ok = true;
854                 for (i=0, l=el.length; i<l; ++i) {
855                     args[2] = el[i];
856                     ok = ( Y.Event.detach.apply(Y.Event, args) && ok );
857                 }
858
859                 return ok;
860             }
861
862             if (!type || !fn || !fn.call) {
863                 return Event.purgeElement(el, false, type);
864             }
865
866             id = 'event:' + Y.stamp(el) + type;
867             ce = _wrappers[id];
868
869             if (ce) {
870                 return ce.detach(fn);
871             } else {
872                 return false;
873             }
874
875         },
876
877         /**
878          * Finds the event in the window object, the caller's arguments, or
879          * in the arguments of another method in the callstack.  This is
880          * executed automatically for events registered through the event
881          * manager, so the implementer should not normally need to execute
882          * this function at all.
883          * @method getEvent
884          * @param {Event} e the event parameter from the handler
885          * @param {HTMLElement} el the element the listener was attached to
886          * @return {Event} the event
887          * @static
888          */
889         getEvent: function(e, el, noFacade) {
890             var ev = e || win.event;
891
892             return (noFacade) ? ev :
893                 new Y.DOMEventFacade(ev, el, _wrappers['event:' + Y.stamp(el) + e.type]);
894         },
895
896         /**
897          * Generates an unique ID for the element if it does not already
898          * have one.
899          * @method generateId
900          * @param el the element to create the id for
901          * @return {string} the resulting id of the element
902          * @static
903          */
904         generateId: function(el) {
905             return Y.DOM.generateID(el);
906         },
907
908         /**
909          * We want to be able to use getElementsByTagName as a collection
910          * to attach a group of events to.  Unfortunately, different
911          * browsers return different types of collections.  This function
912          * tests to determine if the object is array-like.  It will also
913          * fail if the object is an array, but is empty.
914          * @method _isValidCollection
915          * @param o the object to test
916          * @return {boolean} true if the object is array-like and populated
917          * @deprecated was not meant to be used directly
918          * @static
919          * @private
920          */
921         _isValidCollection: shouldIterate,
922
923         /**
924          * hook up any deferred listeners
925          * @method _load
926          * @static
927          * @private
928          */
929         _load: function(e) {
930             if (!_loadComplete) {
931                 _loadComplete = true;
932
933                 // Just in case DOMReady did not go off for some reason
934                 // E._ready();
935                 if (Y.fire) {
936                     Y.fire(EVENT_READY);
937                 }
938
939                 // Available elements may not have been detected before the
940                 // window load event fires. Try to find them now so that the
941                 // the user is more likely to get the onAvailable notifications
942                 // before the window load notification
943                 Event._poll();
944             }
945         },
946
947         /**
948          * Polling function that runs before the onload event fires,
949          * attempting to attach to DOM Nodes as soon as they are
950          * available
951          * @method _poll
952          * @static
953          * @private
954          */
955         _poll: function() {
956             if (Event.locked) {
957                 return;
958             }
959
960             if (Y.UA.ie && !YUI.Env.DOMReady) {
961                 // Hold off if DOMReady has not fired and check current
962                 // readyState to protect against the IE operation aborted
963                 // issue.
964                 Event.startInterval();
965                 return;
966             }
967
968             Event.locked = true;
969
970             // keep trying until after the page is loaded.  We need to
971             // check the page load state prior to trying to bind the
972             // elements so that we can be certain all elements have been
973             // tested appropriately
974             var i, len, item, el, notAvail, executeItem,
975                 tryAgain = !_loadComplete;
976
977             if (!tryAgain) {
978                 tryAgain = (_retryCount > 0);
979             }
980
981             // onAvailable
982             notAvail = [];
983
984             executeItem = function (el, item) {
985                 var context, ov = item.override;
986                 if (item.compat) {
987                     if (item.override) {
988                         if (ov === true) {
989                             context = item.obj;
990                         } else {
991                             context = ov;
992                         }
993                     } else {
994                         context = el;
995                     }
996                     item.fn.call(context, item.obj);
997                 } else {
998                     context = item.obj || Y.one(el);
999                     item.fn.apply(context, (Y.Lang.isArray(ov)) ? ov : []);
1000                 }
1001             };
1002
1003             // onAvailable
1004             for (i=0,len=_avail.length; i<len; ++i) {
1005                 item = _avail[i];
1006                 if (item && !item.checkReady) {
1007
1008                     // el = (item.compat) ? Y.DOM.byId(item.id) : Y.one(item.id);
1009                     el = (item.compat) ? Y.DOM.byId(item.id) : Y.Selector.query(item.id, null, true);
1010
1011                     if (el) {
1012                         executeItem(el, item);
1013                         _avail[i] = null;
1014                     } else {
1015                         notAvail.push(item);
1016                     }
1017                 }
1018             }
1019
1020             // onContentReady
1021             for (i=0,len=_avail.length; i<len; ++i) {
1022                 item = _avail[i];
1023                 if (item && item.checkReady) {
1024
1025                     // el = (item.compat) ? Y.DOM.byId(item.id) : Y.one(item.id);
1026                     el = (item.compat) ? Y.DOM.byId(item.id) : Y.Selector.query(item.id, null, true);
1027
1028                     if (el) {
1029                         // The element is available, but not necessarily ready
1030                         // @todo should we test parentNode.nextSibling?
1031                         if (_loadComplete || (el.get && el.get('nextSibling')) || el.nextSibling) {
1032                             executeItem(el, item);
1033                             _avail[i] = null;
1034                         }
1035                     } else {
1036                         notAvail.push(item);
1037                     }
1038                 }
1039             }
1040
1041             _retryCount = (notAvail.length === 0) ? 0 : _retryCount - 1;
1042
1043             if (tryAgain) {
1044                 // we may need to strip the nulled out items here
1045                 Event.startInterval();
1046             } else {
1047                 clearInterval(Event._interval);
1048                 Event._interval = null;
1049             }
1050
1051             Event.locked = false;
1052
1053             return;
1054
1055         },
1056
1057         /**
1058          * Removes all listeners attached to the given element via addListener.
1059          * Optionally, the node's children can also be purged.
1060          * Optionally, you can specify a specific type of event to remove.
1061          * @method purgeElement
1062          * @param {HTMLElement} el the element to purge
1063          * @param {boolean} recurse recursively purge this element's children
1064          * as well.  Use with caution.
1065          * @param {string} type optional type of listener to purge. If
1066          * left out, all listeners will be removed
1067          * @static
1068          */
1069         purgeElement: function(el, recurse, type) {
1070             // var oEl = (Y.Lang.isString(el)) ? Y.one(el) : el,
1071             var oEl = (Y.Lang.isString(el)) ?  Y.Selector.query(el, null, true) : el,
1072                 lis = Event.getListeners(oEl, type), i, len, props, children, child;
1073
1074             if (recurse && oEl) {
1075                 lis = lis || [];
1076                 children = Y.Selector.query('*', oEl);
1077                 i = 0;
1078                 len = children.length;
1079                 for (; i < len; ++i) {
1080                     child = Event.getListeners(children[i], type);
1081                     if (child) {
1082                         lis = lis.concat(child);
1083                     }
1084                 }
1085             }
1086
1087             if (lis) {
1088                 i = 0;
1089                 len = lis.length;
1090                 for (; i < len; ++i) {
1091                     props = lis[i];
1092                     props.detachAll();
1093                     remove(props.el, props.type, props.fn, props.capture);
1094                     delete _wrappers[props.key];
1095                     delete _el_events[props.domkey][props.key];
1096                 }
1097             }
1098
1099         },
1100
1101
1102         /**
1103          * Returns all listeners attached to the given element via addListener.
1104          * Optionally, you can specify a specific type of event to return.
1105          * @method getListeners
1106          * @param el {HTMLElement|string} the element or element id to inspect
1107          * @param type {string} optional type of listener to return. If
1108          * left out, all listeners will be returned
1109          * @return {Y.Custom.Event} the custom event wrapper for the DOM event(s)
1110          * @static
1111          */
1112         getListeners: function(el, type) {
1113             var ek = Y.stamp(el, true), evts = _el_events[ek],
1114                 results=[] , key = (type) ? 'event:' + ek + type : null,
1115                 adapters = _eventenv.plugins;
1116
1117             if (!evts) {
1118                 return null;
1119             }
1120
1121             if (key) {
1122                 // look for synthetic events
1123                 if (adapters[type] && adapters[type].eventDef) {
1124                     key += '_synth';
1125                 }
1126
1127                 if (evts[key]) {
1128                     results.push(evts[key]);
1129                 }
1130
1131                 // get native events as well
1132                 key += 'native';
1133                 if (evts[key]) {
1134                     results.push(evts[key]);
1135                 }
1136
1137             } else {
1138                 Y.each(evts, function(v, k) {
1139                     results.push(v);
1140                 });
1141             }
1142
1143             return (results.length) ? results : null;
1144         },
1145
1146         /**
1147          * Removes all listeners registered by pe.event.  Called
1148          * automatically during the unload event.
1149          * @method _unload
1150          * @static
1151          * @private
1152          */
1153         _unload: function(e) {
1154             Y.each(_wrappers, function(v, k) {
1155                 v.detachAll();
1156                 remove(v.el, v.type, v.fn, v.capture);
1157                 delete _wrappers[k];
1158                 delete _el_events[v.domkey][k];
1159             });
1160             remove(win, "unload", onUnload);
1161         },
1162
1163         /**
1164          * Adds a DOM event directly without the caching, cleanup, context adj, etc
1165          *
1166          * @method nativeAdd
1167          * @param {HTMLElement} el      the element to bind the handler to
1168          * @param {string}      type   the type of event handler
1169          * @param {function}    fn      the callback to invoke
1170          * @param {boolen}      capture capture or bubble phase
1171          * @static
1172          * @private
1173          */
1174         nativeAdd: add,
1175
1176         /**
1177          * Basic remove listener
1178          *
1179          * @method nativeRemove
1180          * @param {HTMLElement} el      the element to bind the handler to
1181          * @param {string}      type   the type of event handler
1182          * @param {function}    fn      the callback to invoke
1183          * @param {boolen}      capture capture or bubble phase
1184          * @static
1185          * @private
1186          */
1187         nativeRemove: remove
1188     };
1189
1190 }();
1191
1192 Y.Event = Event;
1193
1194 if (config.injected || YUI.Env.windowLoaded) {
1195     onLoad();
1196 } else {
1197     add(win, "load", onLoad);
1198 }
1199
1200 // Process onAvailable/onContentReady items when when the DOM is ready in IE
1201 if (Y.UA.ie) {
1202     Y.on(EVENT_READY, Event._poll);
1203 }
1204
1205 add(win, "unload", onUnload);
1206
1207 Event.Custom = Y.CustomEvent;
1208 Event.Subscriber = Y.Subscriber;
1209 Event.Target = Y.EventTarget;
1210 Event.Handle = Y.EventHandle;
1211 Event.Facade = Y.EventFacade;
1212
1213 Event._poll();
1214
1215 })();
1216
1217 /**
1218  * DOM event listener abstraction layer
1219  * @module event
1220  * @submodule event-base
1221  */
1222
1223 /**
1224  * Executes the callback as soon as the specified element
1225  * is detected in the DOM.  This function expects a selector
1226  * string for the element(s) to detect.  If you already have
1227  * an element reference, you don't need this event.
1228  * @event available
1229  * @param type {string} 'available'
1230  * @param fn {function} the callback function to execute.
1231  * @param el {string} an selector for the element(s) to attach
1232  * @param context optional argument that specifies what 'this' refers to.
1233  * @param args* 0..n additional arguments to pass on to the callback function.
1234  * These arguments will be added after the event object.
1235  * @return {EventHandle} the detach handle
1236  * @for YUI
1237  */
1238 Y.Env.evt.plugins.available = {
1239     on: function(type, fn, id, o) {
1240         var a = arguments.length > 4 ?  Y.Array(arguments, 4, true) : null;
1241         return Y.Event.onAvailable.call(Y.Event, id, fn, o, a);
1242     }
1243 };
1244
1245 /**
1246  * Executes the callback as soon as the specified element
1247  * is detected in the DOM with a nextSibling property
1248  * (indicating that the element's children are available).
1249  * This function expects a selector
1250  * string for the element(s) to detect.  If you already have
1251  * an element reference, you don't need this event.
1252  * @event contentready
1253  * @param type {string} 'contentready'
1254  * @param fn {function} the callback function to execute.
1255  * @param el {string} an selector for the element(s) to attach.
1256  * @param context optional argument that specifies what 'this' refers to.
1257  * @param args* 0..n additional arguments to pass on to the callback function.
1258  * These arguments will be added after the event object.
1259  * @return {EventHandle} the detach handle
1260  * @for YUI
1261  */
1262 Y.Env.evt.plugins.contentready = {
1263     on: function(type, fn, id, o) {
1264         var a = arguments.length > 4 ? Y.Array(arguments, 4, true) : null;
1265         return Y.Event.onContentReady.call(Y.Event, id, fn, o, a);
1266     }
1267 };
1268
1269
1270 }, '3.3.0' ,{requires:['event-custom-base']});
1271 YUI.add('event-delegate', function(Y) {
1272
1273 /**
1274  * Adds event delegation support to the library.
1275  * 
1276  * @module event
1277  * @submodule event-delegate
1278  */
1279
1280 var toArray          = Y.Array,
1281     YLang            = Y.Lang,
1282     isString         = YLang.isString,
1283     isObject         = YLang.isObject,
1284     isArray          = YLang.isArray,
1285     selectorTest     = Y.Selector.test,
1286     detachCategories = Y.Env.evt.handles;
1287
1288 /**
1289  * <p>Sets up event delegation on a container element.  The delegated event
1290  * will use a supplied selector or filtering function to test if the event
1291  * references at least one node that should trigger the subscription
1292  * callback.</p>
1293  *
1294  * <p>Selector string filters will trigger the callback if the event originated
1295  * from a node that matches it or is contained in a node that matches it.
1296  * Function filters are called for each Node up the parent axis to the
1297  * subscribing container node, and receive at each level the Node and the event
1298  * object.  The function should return true (or a truthy value) if that Node
1299  * should trigger the subscription callback.  Note, it is possible for filters
1300  * to match multiple Nodes for a single event.  In this case, the delegate
1301  * callback will be executed for each matching Node.</p>
1302  *
1303  * <p>For each matching Node, the callback will be executed with its 'this'
1304  * object set to the Node matched by the filter (unless a specific context was
1305  * provided during subscription), and the provided event's
1306  * <code>currentTarget</code> will also be set to the matching Node.  The
1307  * containing Node from which the subscription was originally made can be
1308  * referenced as <code>e.container</code>.
1309  *
1310  * @method delegate
1311  * @param type {String} the event type to delegate
1312  * @param fn {Function} the callback function to execute.  This function
1313  *              will be provided the event object for the delegated event.
1314  * @param el {String|node} the element that is the delegation container
1315  * @param spec {string|Function} a selector that must match the target of the
1316  *              event or a function to test target and its parents for a match
1317  * @param context optional argument that specifies what 'this' refers to.
1318  * @param args* 0..n additional arguments to pass on to the callback function.
1319  *              These arguments will be added after the event object.
1320  * @return {EventHandle} the detach handle
1321  * @for YUI
1322  */
1323 function delegate(type, fn, el, filter) {
1324     var args     = toArray(arguments, 0, true),
1325         query    = isString(el) ? el : null,
1326         typeBits, synth, container, categories, cat, i, len, handles, handle;
1327
1328     // Support Y.delegate({ click: fnA, key: fnB }, context, filter, ...);
1329     // and Y.delegate(['click', 'key'], fn, context, filter, ...);
1330     if (isObject(type)) {
1331         handles = [];
1332
1333         if (isArray(type)) {
1334             for (i = 0, len = type.length; i < len; ++i) {
1335                 args[0] = type[i];
1336                 handles.push(Y.delegate.apply(Y, args));
1337             }
1338         } else {
1339             // Y.delegate({'click', fn}, context, filter) =>
1340             // Y.delegate('click', fn, context, filter)
1341             args.unshift(null); // one arg becomes two; need to make space
1342
1343             for (i in type) {
1344                 if (type.hasOwnProperty(i)) {
1345                     args[0] = i;
1346                     args[1] = type[i];
1347                     handles.push(Y.delegate.apply(Y, args));
1348                 }
1349             }
1350         }
1351
1352         return new Y.EventHandle(handles);
1353     }
1354
1355     typeBits = type.split(/\|/);
1356
1357     if (typeBits.length > 1) {
1358         cat  = typeBits.shift();
1359         type = typeBits.shift();
1360     }
1361
1362     synth = Y.Node.DOM_EVENTS[type];
1363
1364     if (isObject(synth) && synth.delegate) {
1365         handle = synth.delegate.apply(synth, arguments);
1366     }
1367
1368     if (!handle) {
1369         if (!type || !fn || !el || !filter) {
1370             return;
1371         }
1372
1373         container = (query) ? Y.Selector.query(query, null, true) : el;
1374
1375         if (!container && isString(el)) {
1376             handle = Y.on('available', function () {
1377                 Y.mix(handle, Y.delegate.apply(Y, args), true);
1378             }, el);
1379         }
1380
1381         if (!handle && container) {
1382             args.splice(2, 2, container); // remove the filter
1383
1384             handle = Y.Event._attach(args, { facade: false });
1385             handle.sub.filter  = filter;
1386             handle.sub._notify = delegate.notifySub;
1387         }
1388     }
1389
1390     if (handle && cat) {
1391         categories = detachCategories[cat]  || (detachCategories[cat] = {});
1392         categories = categories[type] || (categories[type] = []);
1393         categories.push(handle);
1394     }
1395
1396     return handle;
1397 }
1398
1399 /**
1400  * Overrides the <code>_notify</code> method on the normal DOM subscription to
1401  * inject the filtering logic and only proceed in the case of a match.
1402  * 
1403  * @method delegate.notifySub
1404  * @param thisObj {Object} default 'this' object for the callback
1405  * @param args {Array} arguments passed to the event's <code>fire()</code>
1406  * @param ce {CustomEvent} the custom event managing the DOM subscriptions for
1407  *              the subscribed event on the subscribing node.
1408  * @return {Boolean} false if the event was stopped
1409  * @private
1410  * @static
1411  * @since 3.2.0
1412  */
1413 delegate.notifySub = function (thisObj, args, ce) {
1414     // Preserve args for other subscribers
1415     args = args.slice();
1416     if (this.args) {
1417         args.push.apply(args, this.args);
1418     }
1419
1420     // Only notify subs if the event occurred on a targeted element
1421     var currentTarget = delegate._applyFilter(this.filter, args, ce),
1422         //container     = e.currentTarget,
1423         e, i, len, ret;
1424
1425     if (currentTarget) {
1426         // Support multiple matches up the the container subtree
1427         currentTarget = toArray(currentTarget);
1428
1429         // The second arg is the currentTarget, but we'll be reusing this
1430         // facade, replacing the currentTarget for each use, so it doesn't
1431         // matter what element we seed it with.
1432         e = args[0] = new Y.DOMEventFacade(args[0], ce.el, ce);
1433
1434         e.container = Y.one(ce.el);
1435     
1436         for (i = 0, len = currentTarget.length; i < len && !e.stopped; ++i) {
1437             e.currentTarget = Y.one(currentTarget[i]);
1438
1439             ret = this.fn.apply(this.context || e.currentTarget, args);
1440
1441             if (ret === false) { // stop further notifications
1442                 break;
1443             }
1444         }
1445
1446         return ret;
1447     }
1448 };
1449
1450 /**
1451  * <p>Compiles a selector string into a filter function to identify whether
1452  * Nodes along the parent axis of an event's target should trigger event
1453  * notification.</p>
1454  *
1455  * <p>This function is memoized, so previously compiled filter functions are
1456  * returned if the same selector string is provided.</p>
1457  *
1458  * <p>This function may be useful when defining synthetic events for delegate
1459  * handling.</p>
1460  *
1461  * @method delegate.compileFilter
1462  * @param selector {String} the selector string to base the filtration on
1463  * @return {Function}
1464  * @since 3.2.0
1465  * @static
1466  */
1467 delegate.compileFilter = Y.cached(function (selector) {
1468     return function (target, e) {
1469         return selectorTest(target._node, selector, e.currentTarget._node);
1470     };
1471 });
1472
1473 /**
1474  * Walks up the parent axis of an event's target, and tests each element
1475  * against a supplied filter function.  If any Nodes, including the container,
1476  * satisfy the filter, the delegated callback will be triggered for each.
1477  *
1478  * @method delegate._applyFilter
1479  * @param filter {Function} boolean function to test for inclusion in event
1480  *                  notification
1481  * @param args {Array} the arguments that would be passed to subscribers
1482  * @param ce   {CustomEvent} the DOM event wrapper
1483  * @return {Node|Node[]|undefined} The Node or Nodes that satisfy the filter
1484  * @protected
1485  */
1486 delegate._applyFilter = function (filter, args, ce) {
1487     var e         = args[0],
1488         container = ce.el, // facadeless events in IE, have no e.currentTarget
1489         target    = e.target || e.srcElement,
1490         match     = [],
1491         isContainer = false;
1492
1493     // Resolve text nodes to their containing element
1494     if (target.nodeType === 3) {
1495         target = target.parentNode;
1496     }
1497
1498     // passing target as the first arg rather than leaving well enough alone
1499     // making 'this' in the filter function refer to the target.  This is to
1500     // support bound filter functions.
1501     args.unshift(target);
1502
1503     if (isString(filter)) {
1504         while (target) {
1505             isContainer = (target === container);
1506             if (selectorTest(target, filter, (isContainer ?null: container))) {
1507                 match.push(target);
1508             }
1509
1510             if (isContainer) {
1511                 break;
1512             }
1513
1514             target = target.parentNode;
1515         }
1516     } else {
1517         // filter functions are implementer code and should receive wrappers
1518         args[0] = Y.one(target);
1519         args[1] = new Y.DOMEventFacade(e, container, ce);
1520
1521         while (target) {
1522             // filter(target, e, extra args...) - this === target
1523             if (filter.apply(args[0], args)) {
1524                 match.push(target);
1525             }
1526
1527             if (target === container) {
1528                 break;
1529             }
1530
1531             target = target.parentNode;
1532             args[0] = Y.one(target);
1533         }
1534         args[1] = e; // restore the raw DOM event
1535     }
1536
1537     if (match.length <= 1) {
1538         match = match[0]; // single match or undefined
1539     }
1540
1541     // remove the target
1542     args.shift();
1543
1544     return match;
1545 };
1546
1547 /**
1548  * Sets up event delegation on a container element.  The delegated event
1549  * will use a supplied filter to test if the callback should be executed.
1550  * This filter can be either a selector string or a function that returns
1551  * a Node to use as the currentTarget for the event.
1552  *
1553  * The event object for the delegated event is supplied to the callback
1554  * function.  It is modified slightly in order to support all properties
1555  * that may be needed for event delegation.  'currentTarget' is set to
1556  * the element that matched the selector string filter or the Node returned
1557  * from the filter function.  'container' is set to the element that the
1558  * listener is delegated from (this normally would be the 'currentTarget').
1559  *
1560  * Filter functions will be called with the arguments that would be passed to
1561  * the callback function, including the event object as the first parameter.
1562  * The function should return false (or a falsey value) if the success criteria
1563  * aren't met, and the Node to use as the event's currentTarget and 'this'
1564  * object if they are.
1565  *
1566  * @method delegate
1567  * @param type {string} the event type to delegate
1568  * @param fn {function} the callback function to execute.  This function
1569  * will be provided the event object for the delegated event.
1570  * @param el {string|node} the element that is the delegation container
1571  * @param filter {string|function} a selector that must match the target of the
1572  * event or a function that returns a Node or false.
1573  * @param context optional argument that specifies what 'this' refers to.
1574  * @param args* 0..n additional arguments to pass on to the callback function.
1575  * These arguments will be added after the event object.
1576  * @return {EventHandle} the detach handle
1577  * @for YUI
1578  */
1579 Y.delegate = Y.Event.delegate = delegate;
1580
1581
1582 }, '3.3.0' ,{requires:['node-base']});
1583 YUI.add('event-synthetic', function(Y) {
1584
1585 /**
1586  * Define new DOM events that can be subscribed to from Nodes.
1587  *
1588  * @module event
1589  * @submodule event-synthetic
1590  */
1591 var DOMMap   = Y.Env.evt.dom_map,
1592     toArray  = Y.Array,
1593     YLang    = Y.Lang,
1594     isObject = YLang.isObject,
1595     isString = YLang.isString,
1596     query    = Y.Selector.query,
1597     noop     = function () {};
1598
1599 /**
1600  * <p>The triggering mechanism used by SyntheticEvents.</p>
1601  *
1602  * <p>Implementers should not instantiate these directly.  Use the Notifier
1603  * provided to the event's implemented <code>on(node, sub, notifier)</code> or
1604  * <code>delegate(node, sub, notifier, filter)</code> methods.</p>
1605  *
1606  * @class SyntheticEvent.Notifier
1607  * @constructor
1608  * @param handle {EventHandle} the detach handle for the subscription to an
1609  *              internal custom event used to execute the callback passed to
1610  *              on(..) or delegate(..)
1611  * @param emitFacade {Boolean} take steps to ensure the first arg received by
1612  *              the subscription callback is an event facade
1613  * @private
1614  * @since 3.2.0
1615  */
1616 function Notifier(handle, emitFacade) {
1617     this.handle     = handle;
1618     this.emitFacade = emitFacade;
1619 }
1620
1621 /**
1622  * <p>Executes the subscription callback, passing the firing arguments as the
1623  * first parameters to that callback. For events that are configured with
1624  * emitFacade=true, it is common practice to pass the triggering DOMEventFacade
1625  * as the first parameter.  Barring a proper DOMEventFacade or EventFacade
1626  * (from a CustomEvent), a new EventFacade will be generated.  In that case, if
1627  * fire() is called with a simple object, it will be mixed into the facade.
1628  * Otherwise, the facade will be prepended to the callback parameters.</p>
1629  *
1630  * <p>For notifiers provided to delegate logic, the first argument should be an
1631  * object with a &quot;currentTarget&quot; property to identify what object to
1632  * default as 'this' in the callback.  Typically this is gleaned from the
1633  * DOMEventFacade or EventFacade, but if configured with emitFacade=false, an
1634  * object must be provided.  In that case, the object will be removed from the
1635  * callback parameters.</p>
1636  *
1637  * <p>Additional arguments passed during event subscription will be
1638  * automatically added after those passed to fire().</p>
1639  *
1640  * @method fire
1641  * @param e {EventFacade|DOMEventFacade|Object|any} (see description)
1642  * @param arg* {any} additional arguments received by all subscriptions
1643  * @private
1644  */
1645 Notifier.prototype.fire = function (e) {
1646     // first arg to delegate notifier should be an object with currentTarget
1647     var args     = toArray(arguments, 0, true),
1648         handle   = this.handle,
1649         ce       = handle.evt,
1650         sub      = handle.sub,
1651         thisObj  = sub.context,
1652         delegate = sub.filter,
1653         event    = e || {};
1654
1655     if (this.emitFacade) {
1656         if (!e || !e.preventDefault) {
1657             event = ce._getFacade();
1658
1659             if (isObject(e) && !e.preventDefault) {
1660                 Y.mix(event, e, true);
1661                 args[0] = event;
1662             } else {
1663                 args.unshift(event);
1664             }
1665         }
1666
1667         event.type    = ce.type;
1668         event.details = args.slice();
1669
1670         if (delegate) {
1671             event.container = ce.host;
1672         }
1673     } else if (delegate && isObject(e) && e.currentTarget) {
1674         args.shift();
1675     }
1676
1677     sub.context = thisObj || event.currentTarget || ce.host;
1678     ce.fire.apply(ce, args);
1679     sub.context = thisObj; // reset for future firing
1680 };
1681
1682
1683 /**
1684  * <p>Wrapper class for the integration of new events into the YUI event
1685  * infrastructure.  Don't instantiate this object directly, use
1686  * <code>Y.Event.define(type, config)</code>.  See that method for details.</p>
1687  *
1688  * <p>Properties that MAY or SHOULD be specified in the configuration are noted
1689  * below and in the description of <code>Y.Event.define</code>.</p>
1690  *
1691  * @class SyntheticEvent
1692  * @constructor
1693  * @param cfg {Object} Implementation pieces and configuration
1694  * @since 3.1.0
1695  * @in event-synthetic
1696  */
1697 function SyntheticEvent() {
1698     this._init.apply(this, arguments);
1699 }
1700
1701 Y.mix(SyntheticEvent, {
1702     Notifier: Notifier,
1703
1704     /**
1705      * Returns the array of subscription handles for a node for the given event
1706      * type.  Passing true as the third argument will create a registry entry
1707      * in the event system's DOM map to host the array if one doesn't yet exist.
1708      *
1709      * @method getRegistry
1710      * @param node {Node} the node
1711      * @param type {String} the event
1712      * @param create {Boolean} create a registration entry to host a new array
1713      *                  if one doesn't exist.
1714      * @return {Array}
1715      * @static
1716      * @protected
1717      * @since 3.2.0
1718      */
1719     getRegistry: function (node, type, create) {
1720         var el     = node._node,
1721             yuid   = Y.stamp(el),
1722             key    = 'event:' + yuid + type + '_synth',
1723             events = DOMMap[yuid] || (DOMMap[yuid] = {});
1724
1725         if (!events[key] && create) {
1726             events[key] = {
1727                 type      : '_synth',
1728                 fn        : noop,
1729                 capture   : false,
1730                 el        : el,
1731                 key       : key,
1732                 domkey    : yuid,
1733                 notifiers : [],
1734
1735                 detachAll : function () {
1736                     var notifiers = this.notifiers,
1737                         i = notifiers.length;
1738
1739                     while (--i >= 0) {
1740                         notifiers[i].detach();
1741                     }
1742                 }
1743             };
1744         }
1745
1746         return (events[key]) ? events[key].notifiers : null;
1747     },
1748
1749     /**
1750      * Alternate <code>_delete()</code> method for the CustomEvent object
1751      * created to manage SyntheticEvent subscriptions.
1752      *
1753      * @method _deleteSub
1754      * @param sub {Subscription} the subscription to clean up
1755      * @private
1756      * @since 3.2.0
1757      */
1758     _deleteSub: function (sub) {
1759         if (sub && sub.fn) {
1760             var synth = this.eventDef,
1761                 method = (sub.filter) ? 'detachDelegate' : 'detach';
1762
1763             this.subscribers = {};
1764             this.subCount = 0;
1765
1766             synth[method](sub.node, sub, this.notifier, sub.filter);
1767             synth._unregisterSub(sub);
1768
1769             delete sub.fn;
1770             delete sub.node;
1771             delete sub.context;
1772         }
1773     },
1774
1775     prototype: {
1776         constructor: SyntheticEvent,
1777
1778         /**
1779          * Construction logic for the event.
1780          *
1781          * @method _init
1782          * @protected
1783          */
1784         _init: function () {
1785             var config = this.publishConfig || (this.publishConfig = {});
1786
1787             // The notification mechanism handles facade creation
1788             this.emitFacade = ('emitFacade' in config) ?
1789                                 config.emitFacade :
1790                                 true;
1791             config.emitFacade  = false;
1792         },
1793
1794         /**
1795          * <p>Implementers MAY provide this method definition.</p>
1796          *
1797          * <p>Implement this function if the event supports a different
1798          * subscription signature.  This function is used by both
1799          * <code>on()</code> and <code>delegate()</code>.  The second parameter
1800          * indicates that the event is being subscribed via
1801          * <code>delegate()</code>.</p>
1802          *
1803          * <p>Implementations must remove extra arguments from the args list
1804          * before returning.  The required args for <code>on()</code>
1805          * subscriptions are</p>
1806          * <pre><code>[type, callback, target, context, argN...]</code></pre>
1807          *
1808          * <p>The required args for <code>delegate()</code>
1809          * subscriptions are</p>
1810          *
1811          * <pre><code>[type, callback, target, filter, context, argN...]</code></pre>
1812          *
1813          * <p>The return value from this function will be stored on the
1814          * subscription in the '_extra' property for reference elsewhere.</p>
1815          *
1816          * @method processArgs
1817          * @param args {Array} parmeters passed to Y.on(..) or Y.delegate(..)
1818          * @param delegate {Boolean} true if the subscription is from Y.delegate
1819          * @return {any}
1820          */
1821         processArgs: noop,
1822
1823         /**
1824          * <p>Implementers MAY override this property.</p>
1825          *
1826          * <p>Whether to prevent multiple subscriptions to this event that are
1827          * classified as being the same.  By default, this means the subscribed
1828          * callback is the same function.  See the <code>subMatch</code>
1829          * method.  Setting this to true will impact performance for high volume
1830          * events.</p>
1831          *
1832          * @property preventDups
1833          * @type {Boolean}
1834          * @default false
1835          */
1836         //preventDups  : false,
1837
1838         /**
1839          * <p>Implementers SHOULD provide this method definition.</p>
1840          *
1841          * Implementation logic for subscriptions done via <code>node.on(type,
1842          * fn)</code> or <code>Y.on(type, fn, target)</code>.  This
1843          * function should set up the monitor(s) that will eventually fire the
1844          * event.  Typically this involves subscribing to at least one DOM
1845          * event.  It is recommended to store detach handles from any DOM
1846          * subscriptions to make for easy cleanup in the <code>detach</code>
1847          * method.  Typically these handles are added to the <code>sub</code>
1848          * object.  Also for SyntheticEvents that leverage a single DOM
1849          * subscription under the hood, it is recommended to pass the DOM event
1850          * object to <code>notifier.fire(e)</code>.  (The event name on the
1851          * object will be updated).
1852          *
1853          * @method on
1854          * @param node {Node} the node the subscription is being applied to
1855          * @param sub {Subscription} the object to track this subscription
1856          * @param notifier {SyntheticEvent.Notifier} call notifier.fire(..) to
1857          *              trigger the execution of the subscribers
1858          */
1859         on: noop,
1860
1861         /**
1862          * <p>Implementers SHOULD provide this method definition.</p>
1863          *
1864          * <p>Implementation logic for detaching subscriptions done via
1865          * <code>node.on(type, fn)</code>.  This function should clean up any
1866          * subscriptions made in the <code>on()</code> phase.</p>
1867          *
1868          * @method detach
1869          * @param node {Node} the node the subscription was applied to
1870          * @param sub {Subscription} the object tracking this subscription
1871          * @param notifier {SyntheticEvent.Notifier} the Notifier used to
1872          *              trigger the execution of the subscribers
1873          */
1874         detach: noop,
1875
1876         /**
1877          * <p>Implementers SHOULD provide this method definition.</p>
1878          *
1879          * <p>Implementation logic for subscriptions done via
1880          * <code>node.delegate(type, fn, filter)</code> or
1881          * <code>Y.delegate(type, fn, container, filter)</code>.  Like with
1882          * <code>on()</code> above, this function should monitor the environment
1883          * for the event being fired, and trigger subscription execution by
1884          * calling <code>notifier.fire(e)</code>.</p>
1885          *
1886          * <p>This function receives a fourth argument, which is the filter
1887          * used to identify which Node's are of interest to the subscription.
1888          * The filter will be either a boolean function that accepts a target
1889          * Node for each hierarchy level as the event bubbles, or a selector
1890          * string.  To translate selector strings into filter functions, use
1891          * <code>Y.delegate.compileFilter(filter)</code>.</p>
1892          *
1893          * @method delegate
1894          * @param node {Node} the node the subscription is being applied to
1895          * @param sub {Subscription} the object to track this subscription
1896          * @param notifier {SyntheticEvent.Notifier} call notifier.fire(..) to
1897          *              trigger the execution of the subscribers
1898          * @param filter {String|Function} Selector string or function that
1899          *              accepts an event object and returns null, a Node, or an
1900          *              array of Nodes matching the criteria for processing.
1901          * @since 3.2.0
1902          */
1903         delegate       : noop,
1904
1905         /**
1906          * <p>Implementers SHOULD provide this method definition.</p>
1907          *
1908          * <p>Implementation logic for detaching subscriptions done via
1909          * <code>node.delegate(type, fn, filter)</code> or
1910          * <code>Y.delegate(type, fn, container, filter)</code>.  This function
1911          * should clean up any subscriptions made in the
1912          * <code>delegate()</code> phase.</p>
1913          *
1914          * @method detachDelegate
1915          * @param node {Node} the node the subscription was applied to
1916          * @param sub {Subscription} the object tracking this subscription
1917          * @param notifier {SyntheticEvent.Notifier} the Notifier used to
1918          *              trigger the execution of the subscribers
1919          * @param filter {String|Function} Selector string or function that
1920          *              accepts an event object and returns null, a Node, or an
1921          *              array of Nodes matching the criteria for processing.
1922          * @since 3.2.0
1923          */
1924         detachDelegate : noop,
1925
1926         /**
1927          * Sets up the boilerplate for detaching the event and facilitating the
1928          * execution of subscriber callbacks.
1929          *
1930          * @method _on
1931          * @param args {Array} array of arguments passed to
1932          *              <code>Y.on(...)</code> or <code>Y.delegate(...)</code>
1933          * @param delegate {Boolean} true if called from
1934          * <code>Y.delegate(...)</code>
1935          * @return {EventHandle} the detach handle for this subscription
1936          * @private
1937          * since 3.2.0
1938          */
1939         _on: function (args, delegate) {
1940             var handles  = [],
1941                 extra    = this.processArgs(args, delegate),
1942                 selector = args[2],
1943                 method   = delegate ? 'delegate' : 'on',
1944                 nodes, handle;
1945
1946             // Can't just use Y.all because it doesn't support window (yet?)
1947             nodes = (isString(selector)) ? query(selector) : toArray(selector);
1948
1949             if (!nodes.length && isString(selector)) {
1950                 handle = Y.on('available', function () {
1951                     Y.mix(handle, Y[method].apply(Y, args), true);
1952                 }, selector);
1953
1954                 return handle;
1955             }
1956
1957             Y.Array.each(nodes, function (node) {
1958                 var subArgs = args.slice(),
1959                     filter;
1960
1961                 node = Y.one(node);
1962
1963                 if (node) {
1964                     if (delegate) {
1965                         filter = subArgs.splice(3, 1)[0];
1966                     }
1967
1968                     // (type, fn, el, thisObj, ...) => (fn, thisObj, ...)
1969                     subArgs.splice(0, 4, subArgs[1], subArgs[3]);
1970
1971                     if (!this.preventDups || !this.getSubs(node, args,null,true)) {
1972                         handle = this._getNotifier(node, subArgs, extra,filter);
1973
1974                         this[method](node, handle.sub, handle.notifier, filter);
1975
1976                         handles.push(handle);
1977                     }
1978                 }
1979             }, this);
1980
1981             return (handles.length === 1) ?
1982                 handles[0] :
1983                 new Y.EventHandle(handles);
1984         },
1985
1986         /**
1987          * Creates a new Notifier object for use by this event's
1988          * <code>on(...)</code> or <code>delegate(...)</code> implementation.
1989          *
1990          * @method _getNotifier
1991          * @param node {Node} the Node hosting the event
1992          * @param args {Array} the subscription arguments passed to either
1993          *              <code>Y.on(...)</code> or <code>Y.delegate(...)</code>
1994          *              after running through <code>processArgs(args)</code> to
1995          *              normalize the argument signature
1996          * @param extra {any} Extra data parsed from
1997          *              <code>processArgs(args)</code>
1998          * @param filter {String|Function} the selector string or function
1999          *              filter passed to <code>Y.delegate(...)</code> (not
2000          *              present when called from <code>Y.on(...)</code>)
2001          * @return {SyntheticEvent.Notifier}
2002          * @private
2003          * @since 3.2.0
2004          */
2005         _getNotifier: function (node, args, extra, filter) {
2006             var dispatcher = new Y.CustomEvent(this.type, this.publishConfig),
2007                 handle     = dispatcher.on.apply(dispatcher, args),
2008                 notifier   = new Notifier(handle, this.emitFacade),
2009                 registry   = SyntheticEvent.getRegistry(node, this.type, true),
2010                 sub        = handle.sub;
2011
2012             handle.notifier   = notifier;
2013
2014             sub.node   = node;
2015             sub.filter = filter;
2016             if (extra) {
2017                 this.applyArgExtras(extra, sub);
2018             }
2019
2020             Y.mix(dispatcher, {
2021                 eventDef     : this,
2022                 notifier     : notifier,
2023                 host         : node,       // I forget what this is for
2024                 currentTarget: node,       // for generating facades
2025                 target       : node,       // for generating facades
2026                 el           : node._node, // For category detach
2027
2028                 _delete      : SyntheticEvent._deleteSub
2029             }, true);
2030
2031             registry.push(handle);
2032
2033             return handle;
2034         },
2035
2036         /**
2037          * <p>Implementers MAY provide this method definition.</p>
2038          *
2039          * <p>Implement this function if you want extra data extracted during
2040          * processArgs to be propagated to subscriptions on a per-node basis.
2041          * That is to say, if you call <code>Y.on('xyz', fn, xtra, 'div')</code>
2042          * the data returned from processArgs will be shared
2043          * across the subscription objects for all the divs.  If you want each
2044          * subscription to receive unique information, do that processing
2045          * here.</p>
2046          *
2047          * <p>The default implementation adds the data extracted by processArgs
2048          * to the subscription object as <code>sub._extra</code>.</p>
2049          *
2050          * @method applyArgExtras
2051          * @param extra {any} Any extra data extracted from processArgs
2052          * @param sub {Subscription} the individual subscription
2053          */
2054         applyArgExtras: function (extra, sub) {
2055             sub._extra = extra;
2056         },
2057
2058         /**
2059          * Removes the subscription from the Notifier registry.
2060          *
2061          * @method _unregisterSub
2062          * @param sub {Subscription} the subscription
2063          * @private
2064          * @since 3.2.0
2065          */
2066         _unregisterSub: function (sub) {
2067             var notifiers = SyntheticEvent.getRegistry(sub.node, this.type),
2068                 i;
2069
2070             if (notifiers) {
2071                 for (i = notifiers.length - 1; i >= 0; --i) {
2072                     if (notifiers[i].sub === sub) {
2073                         notifiers.splice(i, 1);
2074                         break;
2075                     }
2076                 }
2077             }
2078         },
2079
2080         /**
2081          * Removes the subscription(s) from the internal subscription dispatch
2082          * mechanism.  See <code>SyntheticEvent._deleteSub</code>.
2083          *
2084          * @method _detach
2085          * @param args {Array} The arguments passed to
2086          *                  <code>node.detach(...)</code>
2087          * @private
2088          * @since 3.2.0
2089          */
2090         _detach: function (args) {
2091             // Can't use Y.all because it doesn't support window (yet?)
2092             // TODO: Does Y.all support window now?
2093             var target = args[2],
2094                 els    = (isString(target)) ?
2095                             query(target) : toArray(target),
2096                 node, i, len, handles, j;
2097
2098             // (type, fn, el, context, filter?) => (type, fn, context, filter?)
2099             args.splice(2, 1);
2100
2101             for (i = 0, len = els.length; i < len; ++i) {
2102                 node = Y.one(els[i]);
2103
2104                 if (node) {
2105                     handles = this.getSubs(node, args);
2106
2107                     if (handles) {
2108                         for (j = handles.length - 1; j >= 0; --j) {
2109                             handles[j].detach();
2110                         }
2111                     }
2112                 }
2113             }
2114         },
2115
2116         /**
2117          * Returns the detach handles of subscriptions on a node that satisfy a
2118          * search/filter function.  By default, the filter used is the
2119          * <code>subMatch</code> method.
2120          *
2121          * @method getSubs
2122          * @param node {Node} the node hosting the event
2123          * @param args {Array} the array of original subscription args passed
2124          *              to <code>Y.on(...)</code> (before
2125          *              <code>processArgs</code>
2126          * @param filter {Function} function used to identify a subscription
2127          *              for inclusion in the returned array
2128          * @param first {Boolean} stop after the first match (used to check for
2129          *              duplicate subscriptions)
2130          * @return {Array} detach handles for the matching subscriptions
2131          */
2132         getSubs: function (node, args, filter, first) {
2133             var notifiers = SyntheticEvent.getRegistry(node, this.type),
2134                 handles = [],
2135                 i, len, handle;
2136
2137             if (notifiers) {
2138                 if (!filter) {
2139                     filter = this.subMatch;
2140                 }
2141
2142                 for (i = 0, len = notifiers.length; i < len; ++i) {
2143                     handle = notifiers[i];
2144                     if (filter.call(this, handle.sub, args)) {
2145                         if (first) {
2146                             return handle;
2147                         } else {
2148                             handles.push(notifiers[i]);
2149                         }
2150                     }
2151                 }
2152             }
2153
2154             return handles.length && handles;
2155         },
2156
2157         /**
2158          * <p>Implementers MAY override this to define what constitutes a
2159          * &quot;same&quot; subscription.  Override implementations should
2160          * consider the lack of a comparator as a match, so calling
2161          * <code>getSubs()</code> with no arguments will return all subs.</p>
2162          *
2163          * <p>Compares a set of subscription arguments against a Subscription
2164          * object to determine if they match.  The default implementation
2165          * compares the callback function against the second argument passed to
2166          * <code>Y.on(...)</code> or <code>node.detach(...)</code> etc.</p>
2167          *
2168          * @method subMatch
2169          * @param sub {Subscription} the existing subscription
2170          * @param args {Array} the calling arguments passed to
2171          *                  <code>Y.on(...)</code> etc.
2172          * @return {Boolean} true if the sub can be described by the args
2173          *                  present
2174          * @since 3.2.0
2175          */
2176         subMatch: function (sub, args) {
2177             // Default detach cares only about the callback matching
2178             return !args[1] || sub.fn === args[1];
2179         }
2180     }
2181 }, true);
2182
2183 Y.SyntheticEvent = SyntheticEvent;
2184
2185 /**
2186  * <p>Defines a new event in the DOM event system.  Implementers are
2187  * responsible for monitoring for a scenario whereby the event is fired.  A
2188  * notifier object is provided to the functions identified below.  When the
2189  * criteria defining the event are met, call notifier.fire( [args] ); to
2190  * execute event subscribers.</p>
2191  *
2192  * <p>The first parameter is the name of the event.  The second parameter is a
2193  * configuration object which define the behavior of the event system when the
2194  * new event is subscribed to or detached from.  The methods that should be
2195  * defined in this configuration object are <code>on</code>,
2196  * <code>detach</code>, <code>delegate</code>, and <code>detachDelegate</code>.
2197  * You are free to define any other methods or properties needed to define your
2198  * event.  Be aware, however, that since the object is used to subclass
2199  * SyntheticEvent, you should avoid method names used by SyntheticEvent unless
2200  * your intention is to override the default behavior.</p>
2201  *
2202  * <p>This is a list of properties and methods that you can or should specify
2203  * in the configuration object:</p>
2204  *
2205  * <dl>
2206  *   <dt><code>on</code></dt>
2207  *       <dd><code>function (node, subscription, notifier)</code> The
2208  *       implementation logic for subscription.  Any special setup you need to
2209  *       do to create the environment for the event being fired--E.g. native
2210  *       DOM event subscriptions.  Store subscription related objects and
2211  *       state on the <code>subscription</code> object.  When the
2212  *       criteria have been met to fire the synthetic event, call
2213  *       <code>notifier.fire(e)</code>.  See Notifier's <code>fire()</code>
2214  *       method for details about what to pass as parameters.</dd>
2215  *
2216  *   <dt><code>detach</code></dt>
2217  *       <dd><code>function (node, subscription, notifier)</code> The
2218  *       implementation logic for cleaning up a detached subscription. E.g.
2219  *       detach any DOM subscriptions added in <code>on</code>.</dd>
2220  *
2221  *   <dt><code>delegate</code></dt>
2222  *       <dd><code>function (node, subscription, notifier, filter)</code> The
2223  *       implementation logic for subscription via <code>Y.delegate</code> or
2224  *       <code>node.delegate</code>.  The filter is typically either a selector
2225  *       string or a function.  You can use
2226  *       <code>Y.delegate.compileFilter(selectorString)</code> to create a
2227  *       filter function from a selector string if needed.  The filter function
2228  *       expects an event object as input and should output either null, a
2229  *       matching Node, or an array of matching Nodes.  Otherwise, this acts
2230  *       like <code>on</code> DOM event subscriptions.  Store subscription
2231  *       related objects and information on the <code>subscription</code>
2232  *       object.  When the criteria have been met to fire the synthetic event,
2233  *       call <code>notifier.fire(e)</code> as noted above.</dd>
2234  *
2235  *   <dt><code>detachDelegate</code></dt>
2236  *       <dd><code>function (node, subscription, notifier)</code> The
2237  *       implementation logic for cleaning up a detached delegate subscription.
2238  *       E.g. detach any DOM delegate subscriptions added in
2239  *       <code>delegate</code>.</dd>
2240  *
2241  *   <dt><code>publishConfig</code></dt>
2242  *       <dd>(Object) The configuration object that will be used to instantiate
2243  *       the underlying CustomEvent. See Notifier's <code>fire</code> method
2244  *       for details.</dd>
2245  *
2246  *   <dt><code>processArgs</code></dt
2247  *       <dd>
2248  *          <p><code>function (argArray, fromDelegate)</code> Optional method
2249  *          to extract any additional arguments from the subscription
2250  *          signature.  Using this allows <code>on</code> or
2251  *          <code>delegate</code> signatures like
2252  *          <code>node.on(&quot;hover&quot;, overCallback,
2253  *          outCallback)</code>.</p>
2254  *          <p>When processing an atypical argument signature, make sure the
2255  *          args array is returned to the normal signature before returning
2256  *          from the function.  For example, in the &quot;hover&quot; example
2257  *          above, the <code>outCallback</code> needs to be <code>splice</code>d
2258  *          out of the array.  The expected signature of the args array for
2259  *          <code>on()</code> subscriptions is:</p>
2260  *          <pre>
2261  *              <code>[type, callback, target, contextOverride, argN...]</code>
2262  *          </pre>
2263  *          <p>And for <code>delegate()</code>:</p>
2264  *          <pre>
2265  *              <code>[type, callback, target, filter, contextOverride, argN...]</code>
2266  *          </pre>
2267  *          <p>where <code>target</code> is the node the event is being
2268  *          subscribed for.  You can see these signatures documented for
2269  *          <code>Y.on()</code> and <code>Y.delegate()</code> respectively.</p>
2270  *          <p>Whatever gets returned from the function will be stored on the
2271  *          <code>subscription</code> object under
2272  *          <code>subscription._extra</code>.</p></dd>
2273  *   <dt><code>subMatch</code></dt>
2274  *       <dd>
2275  *           <p><code>function (sub, args)</code>  Compares a set of
2276  *           subscription arguments against a Subscription object to determine
2277  *           if they match.  The default implementation compares the callback
2278  *           function against the second argument passed to
2279  *           <code>Y.on(...)</code> or <code>node.detach(...)</code> etc.</p>
2280  *       </dd>
2281  * </dl>
2282  *
2283  * @method Event.define
2284  * @param type {String} the name of the event
2285  * @param config {Object} the prototype definition for the new event (see above)
2286  * @param force {Boolean} override an existing event (use with caution)
2287  * @static
2288  * @return {SyntheticEvent} the subclass implementation instance created to
2289  *              handle event subscriptions of this type
2290  * @for Event
2291  * @since 3.1.0
2292  * @in event-synthetic
2293  */
2294 Y.Event.define = function (type, config, force) {
2295     if (!config) {
2296         config = {};
2297     }
2298
2299     var eventDef = (isObject(type)) ? type : Y.merge({ type: type }, config),
2300         Impl, synth;
2301
2302     if (force || !Y.Node.DOM_EVENTS[eventDef.type]) {
2303         Impl = function () {
2304             SyntheticEvent.apply(this, arguments);
2305         };
2306         Y.extend(Impl, SyntheticEvent, eventDef);
2307         synth = new Impl();
2308
2309         type = synth.type;
2310
2311         Y.Node.DOM_EVENTS[type] = Y.Env.evt.plugins[type] = {
2312             eventDef: synth,
2313
2314             on: function () {
2315                 return synth._on(toArray(arguments));
2316             },
2317
2318             delegate: function () {
2319                 return synth._on(toArray(arguments), true);
2320             },
2321
2322             detach: function () {
2323                 return synth._detach(toArray(arguments));
2324             }
2325         };
2326
2327     }
2328
2329     return synth;
2330 };
2331
2332
2333 }, '3.3.0' ,{requires:['node-base', 'event-custom']});
2334 YUI.add('event-mousewheel', function(Y) {
2335
2336 /**
2337  * Adds mousewheel event support
2338  * @module event
2339  * @submodule event-mousewheel
2340  */
2341 var DOM_MOUSE_SCROLL = 'DOMMouseScroll',
2342     fixArgs = function(args) {
2343         var a = Y.Array(args, 0, true), target;
2344         if (Y.UA.gecko) {
2345             a[0] = DOM_MOUSE_SCROLL;
2346             target = Y.config.win;
2347         } else {
2348             target = Y.config.doc;
2349         }
2350
2351         if (a.length < 3) {
2352             a[2] = target;
2353         } else {
2354             a.splice(2, 0, target);
2355         }
2356
2357         return a;
2358     };
2359
2360 /**
2361  * Mousewheel event.  This listener is automatically attached to the
2362  * correct target, so one should not be supplied.  Mouse wheel 
2363  * direction and velocity is stored in the 'mouseDelta' field.
2364  * @event mousewheel
2365  * @param type {string} 'mousewheel'
2366  * @param fn {function} the callback to execute
2367  * @param context optional context object
2368  * @param args 0..n additional arguments to provide to the listener.
2369  * @return {EventHandle} the detach handle
2370  * @for YUI
2371  */
2372 Y.Env.evt.plugins.mousewheel = {
2373     on: function() {
2374         return Y.Event._attach(fixArgs(arguments));
2375     },
2376
2377     detach: function() {
2378         return Y.Event.detach.apply(Y.Event, fixArgs(arguments));
2379     }
2380 };
2381
2382
2383 }, '3.3.0' ,{requires:['node-base']});
2384 YUI.add('event-mouseenter', function(Y) {
2385
2386 /**
2387  * <p>Adds subscription and delegation support for mouseenter and mouseleave
2388  * events.  Unlike mouseover and mouseout, these events aren't fired from child
2389  * elements of a subscribed node.</p>
2390  *
2391  * <p>This avoids receiving three mouseover notifications from a setup like</p>
2392  *
2393  * <pre><code>div#container > p > a[href]</code></pre>
2394  *
2395  * <p>where</p>
2396  *
2397  * <pre><code>Y.one('#container').on('mouseover', callback)</code></pre>
2398  *
2399  * <p>When the mouse moves over the link, one mouseover event is fired from
2400  * #container, then when the mouse moves over the p, another mouseover event is
2401  * fired and bubbles to #container, causing a second notification, and finally
2402  * when the mouse moves over the link, a third mouseover event is fired and
2403  * bubbles to #container for a third notification.</p>
2404  *
2405  * <p>By contrast, using mouseenter instead of mouseover, the callback would be
2406  * executed only once when the mouse moves over #container.</p>
2407  *
2408  * @module event
2409  * @submodule event-mouseenter
2410  */
2411 function notify(e, notifier) {
2412     var current = e.currentTarget,
2413         related = e.relatedTarget;
2414
2415     if (current !== related && !current.contains(related)) {
2416         notifier.fire(e);
2417     }
2418 }
2419
2420 var config = {
2421     proxyType: "mouseover",
2422
2423     on: function (node, sub, notifier) {
2424         sub.onHandle = node.on(this.proxyType, notify, null, notifier);
2425     },
2426
2427     detach: function (node, sub) {
2428         sub.onHandle.detach();
2429     },
2430
2431     delegate: function (node, sub, notifier, filter) {
2432         sub.delegateHandle =
2433             Y.delegate(this.proxyType, notify, node, filter, null, notifier);
2434     },
2435
2436     detachDelegate: function (node, sub) {
2437         sub.delegateHandle.detach();
2438     }
2439 };
2440
2441 Y.Event.define("mouseenter", config, true);
2442 Y.Event.define("mouseleave", Y.merge(config, { proxyType: "mouseout" }), true);
2443
2444
2445 }, '3.3.0' ,{requires:['event-synthetic']});
2446 YUI.add('event-key', function(Y) {
2447
2448 /**
2449  * Functionality to listen for one or more specific key combinations.
2450  * @module event
2451  * @submodule event-key
2452  */
2453
2454 /**
2455  * Add a key listener.  The listener will only be notified if the
2456  * keystroke detected meets the supplied specification.  The
2457  * spec consists of the key event type, followed by a colon,
2458  * followed by zero or more comma separated key codes, followed
2459  * by zero or more modifiers delimited by a plus sign.  Ex:
2460  * press:12,65+shift+ctrl
2461  * @event key
2462  * @for YUI
2463  * @param type {string} 'key'
2464  * @param fn {function} the function to execute
2465  * @param id {string|HTMLElement|collection} the element(s) to bind
2466  * @param spec {string} the keyCode and modifier specification
2467  * @param o optional context object
2468  * @param args 0..n additional arguments to provide to the listener.
2469  * @return {Event.Handle} the detach handle
2470  */
2471 Y.Env.evt.plugins.key = {
2472
2473     on: function(type, fn, id, spec, o) {
2474         var a = Y.Array(arguments, 0, true), parsed, etype, criteria, ename;
2475
2476         parsed = spec && spec.split(':');
2477
2478         if (!spec || spec.indexOf(':') == -1 || !parsed[1]) {
2479             a[0] = 'key' + ((parsed && parsed[0]) || 'press');
2480             return Y.on.apply(Y, a);
2481         }
2482
2483         // key event type: 'down', 'up', or 'press'
2484         etype = parsed[0];
2485
2486         // list of key codes optionally followed by modifiers
2487         criteria = (parsed[1]) ? parsed[1].split(/,|\+/) : null;
2488
2489         // the name of the custom event that will be created for the spec
2490         ename = (Y.Lang.isString(id) ? id : Y.stamp(id)) + spec;
2491
2492         ename = ename.replace(/,/g, '_');
2493
2494         if (!Y.getEvent(ename)) {
2495
2496             // subscribe spec validator to the DOM event
2497             Y.on(type + etype, function(e) {
2498
2499                 
2500                 var passed = false, failed = false, i, crit, critInt;
2501
2502                 for (i=0; i<criteria.length; i=i+1) {
2503                     crit = criteria[i]; 
2504                     critInt = parseInt(crit, 10);
2505
2506                     // pass this section if any supplied keyCode 
2507                     // is found
2508                     if (Y.Lang.isNumber(critInt)) {
2509
2510                         if (e.charCode === critInt) {
2511                             passed = true;
2512                         } else {
2513                             failed = true;
2514                         }
2515
2516                     // only check modifier if no keyCode was specified
2517                     // or the keyCode check was successful.  pass only 
2518                     // if every modifier passes
2519                     } else if (passed || !failed) {
2520                         passed = (e[crit + 'Key']);
2521                         failed = !passed;
2522                     }                    
2523                 }
2524
2525                 // fire spec custom event if spec if met
2526                 if (passed) {
2527                     Y.fire(ename, e);
2528                 }
2529
2530             }, id);
2531
2532         }
2533
2534         // subscribe supplied listener to custom event for spec validator
2535         // remove element and spec.
2536         a.splice(2, 2);
2537         a[0] = ename;
2538
2539         return Y.on.apply(Y, a);
2540     }
2541 };
2542
2543
2544 }, '3.3.0' ,{requires:['node-base']});
2545 YUI.add('event-focus', function(Y) {
2546
2547 /**
2548  * Adds bubbling and delegation support to DOM events focus and blur.
2549  * 
2550  * @module event
2551  * @submodule event-focus
2552  */
2553 var Event    = Y.Event,
2554     YLang    = Y.Lang,
2555     isString = YLang.isString,
2556     useActivate = YLang.isFunction(
2557         Y.DOM.create('<p onbeforeactivate=";"/>').onbeforeactivate);
2558
2559 function define(type, proxy, directEvent) {
2560     var nodeDataKey = '_' + type + 'Notifiers';
2561
2562     Y.Event.define(type, {
2563
2564         _attach: function (el, notifier, delegate) {
2565             if (Y.DOM.isWindow(el)) {
2566                 return Event._attach([type, function (e) {
2567                     notifier.fire(e);
2568                 }, el]);
2569             } else {
2570                 return Event._attach(
2571                     [proxy, this._proxy, el, this, notifier, delegate],
2572                     { capture: true });
2573             }
2574         },
2575
2576         _proxy: function (e, notifier, delegate) {
2577             var node       = e.target,
2578                 notifiers  = node.getData(nodeDataKey),
2579                 yuid       = Y.stamp(e.currentTarget._node),
2580                 defer      = (useActivate || e.target !== e.currentTarget),
2581                 sub        = notifier.handle.sub,
2582                 filterArgs = [node, e].concat(sub.args || []),
2583                 directSub;
2584                 
2585             notifier.currentTarget = (delegate) ? node : e.currentTarget;
2586             notifier.container     = (delegate) ? e.currentTarget : null;
2587
2588             if (!sub.filter || sub.filter.apply(node, filterArgs)) {
2589                 // Maintain a list to handle subscriptions from nested
2590                 // containers div#a>div#b>input #a.on(focus..) #b.on(focus..),
2591                 // use one focus or blur subscription that fires notifiers from
2592                 // #b then #a to emulate bubble sequence.
2593                 if (!notifiers) {
2594                     notifiers = {};
2595                     node.setData(nodeDataKey, notifiers);
2596
2597                     // only subscribe to the element's focus if the target is
2598                     // not the current target (
2599                     if (defer) {
2600                         directSub = Event._attach(
2601                             [directEvent, this._notify, node._node]).sub;
2602                         directSub.once = true;
2603                     }
2604                 }
2605
2606                 if (!notifiers[yuid]) {
2607                     notifiers[yuid] = [];
2608                 }
2609
2610                 notifiers[yuid].push(notifier);
2611
2612                 if (!defer) {
2613                     this._notify(e);
2614                 }
2615             }
2616         },
2617
2618         _notify: function (e, container) {
2619             var node        = e.currentTarget,
2620                 notifiers   = node.getData(nodeDataKey),
2621                               // document.get('ownerDocument') returns null
2622                 doc         = node.get('ownerDocument') || node,
2623                 target      = node,
2624                 nots        = [],
2625                 notifier, i, len;
2626
2627             if (notifiers) {
2628                 // Walk up the parent axis until the origin node, 
2629                 while (target && target !== doc) {
2630                     nots.push.apply(nots, notifiers[Y.stamp(target)] || []);
2631                     target = target.get('parentNode');
2632                 }
2633                 nots.push.apply(nots, notifiers[Y.stamp(doc)] || []);
2634
2635                 for (i = 0, len = nots.length; i < len; ++i) {
2636                     notifier = nots[i];
2637                     e.currentTarget = nots[i].currentTarget;
2638
2639                     if (notifier.container) {
2640                         e.container = notifier.container;
2641                     } else {
2642                         delete e.container;
2643                     }
2644
2645                     notifier.fire(e);
2646                 }
2647
2648                 // clear the notifications list (mainly for delegation)
2649                 node.clearData(nodeDataKey);
2650             }
2651         },
2652
2653         on: function (node, sub, notifier) {
2654             sub.onHandle = this._attach(node._node, notifier);
2655         },
2656
2657         detach: function (node, sub) {
2658             sub.onHandle.detach();
2659         },
2660
2661         delegate: function (node, sub, notifier, filter) {
2662             if (isString(filter)) {
2663                 sub.filter = Y.delegate.compileFilter(filter);
2664             }
2665
2666             sub.delegateHandle = this._attach(node._node, notifier, true);
2667         },
2668
2669         detachDelegate: function (node, sub) {
2670             sub.delegateHandle.detach();
2671         }
2672     }, true);
2673 }
2674
2675 // For IE, we need to defer to focusin rather than focus because
2676 // `el.focus(); doSomething();` executes el.onbeforeactivate, el.onactivate,
2677 // el.onfocusin, doSomething, then el.onfocus.  All others support capture
2678 // phase focus, which executes before doSomething.  To guarantee consistent
2679 // behavior for this use case, IE's direct subscriptions are made against
2680 // focusin so subscribers will be notified before js following el.focus() is
2681 // executed.
2682 if (useActivate) {
2683     //     name     capture phase       direct subscription
2684     define("focus", "beforeactivate",   "focusin");
2685     define("blur",  "beforedeactivate", "focusout");
2686 } else {
2687     define("focus", "focus", "focus");
2688     define("blur",  "blur",  "blur");
2689 }
2690
2691
2692 }, '3.3.0' ,{requires:['event-synthetic']});
2693 YUI.add('event-resize', function(Y) {
2694
2695 /**
2696  * Adds a window resize event that has its behavior normalized to fire at the
2697  * end of the resize rather than constantly during the resize.
2698  * @module event
2699  * @submodule event-resize
2700  */
2701 (function() {
2702
2703 var detachHandle,
2704
2705     timerHandle,
2706
2707     CE_NAME = 'window:resize',
2708
2709     handler = function(e) {
2710
2711         if (Y.UA.gecko) {
2712
2713             Y.fire(CE_NAME, e);
2714
2715         } else {
2716
2717             if (timerHandle) {
2718                 timerHandle.cancel();
2719             }
2720
2721             timerHandle = Y.later(Y.config.windowResizeDelay || 40, Y, function() {
2722                 Y.fire(CE_NAME, e);
2723             });
2724         }
2725         
2726     };
2727
2728
2729 /**
2730  * Firefox fires the window resize event once when the resize action
2731  * finishes, other browsers fire the event periodically during the
2732  * resize.  This code uses timeout logic to simulate the Firefox 
2733  * behavior in other browsers.
2734  * @event windowresize
2735  * @for YUI
2736  */
2737 Y.Env.evt.plugins.windowresize = {
2738
2739     on: function(type, fn) {
2740
2741         // check for single window listener and add if needed
2742         if (!detachHandle) {
2743             detachHandle = Y.Event._attach(['resize', handler]);
2744         }
2745
2746         var a = Y.Array(arguments, 0, true);
2747         a[0] = CE_NAME;
2748
2749         return Y.on.apply(Y, a);
2750     }
2751 };
2752
2753 })();
2754
2755
2756 }, '3.3.0' ,{requires:['node-base']});
2757 YUI.add('event-hover', function(Y) {
2758
2759 /**
2760  * Adds support for a "hover" event.  The event provides a convenience wrapper
2761  * for subscribing separately to mouseenter and mouseleave.  The signature for
2762  * subscribing to the event is</p>
2763  *
2764  * <pre><code>node.on("hover", overFn, outFn);
2765  * node.delegate("hover", overFn, outFn, ".filterSelector");
2766  * Y.on("hover", overFn, outFn, ".targetSelector");
2767  * Y.delegate("hover", overFn, outFn, "#container", ".filterSelector");
2768  * </code></pre>
2769  *
2770  * <p>Additionally, for compatibility with a more typical subscription
2771  * signature, the following are also supported:</p>
2772  *
2773  * <pre><code>Y.on("hover", overFn, ".targetSelector", outFn);
2774  * Y.delegate("hover", overFn, "#container", outFn, ".filterSelector");
2775  * </code></pre>
2776  *
2777  * @module event
2778  * @submodule event-hover
2779  */
2780 var isFunction = Y.Lang.isFunction,
2781     noop = function () {},
2782     conf = {
2783         processArgs: function (args) {
2784             // Y.delegate('hover', over, out, '#container', '.filter')
2785             // comes in as ['hover', over, out, '#container', '.filter'], but
2786             // node.delegate('hover', over, out, '.filter')
2787             // comes in as ['hover', over, containerEl, out, '.filter']
2788             var i = isFunction(args[2]) ? 2 : 3;
2789
2790             return (isFunction(args[i])) ? args.splice(i,1)[0] : noop;
2791         },
2792
2793         on: function (node, sub, notifier, filter) {
2794             sub._detach = node[(filter) ? "delegate" : "on"]({
2795                 mouseenter: Y.bind(notifier.fire, notifier),
2796                 mouseleave: sub._extra
2797             }, filter);
2798         },
2799
2800         detach: function (node, sub, notifier) {
2801             sub._detacher.detach();
2802         }
2803     };
2804
2805 conf.delegate = conf.on;
2806 conf.detachDelegate = conf.detach;
2807
2808 Y.Event.define("hover", conf);
2809
2810
2811 }, '3.3.0' ,{requires:['event-mouseenter']});
2812
2813
2814 YUI.add('event', function(Y){}, '3.3.0' ,{use:['event-base', 'event-delegate', 'event-synthetic', 'event-mousewheel', 'event-mouseenter', 'event-key', 'event-focus', 'event-resize', 'event-hover']});
2815