event.js revision 49aeff0977f7697801747d30bbd74b749891c2d4
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith/**
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith * DOM event listener abstraction layer
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith * @module event
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith */
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith(function() {
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith// Unlike most of the library, this code has to be executed as soon as it is
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith// introduced into the page -- and it should only be executed one time
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith// regardless of the number of instances that use it.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smithvar GLOBAL_ENV = YUI.Env,
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith C = YUI.config,
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith D = C.doc,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith POLL_INTERVAL = C.pollInterval || 40,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith _ready = function(e) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith GLOBAL_ENV._ready();
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith };
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith if (!GLOBAL_ENV._ready) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith GLOBAL_ENV._ready = function() {
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith if (!GLOBAL_ENV.DOMReady) {
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith GLOBAL_ENV.DOMReady=true;
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith // Remove the DOMContentLoaded (FF/Opera/Safari)
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith if (D.removeEventListener) {
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith D.removeEventListener("DOMContentLoaded", _ready, false);
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith }
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith }
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith };
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // create custom event
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith/*! DOMReady: based on work by: Dean Edwards/John Resig/Matthias Miller/Diego Perini */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith // Internet Explorer: use the readyState of a defered script.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // This isolates what appears to be a safe moment to manipulate
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith // the DOM prior to when the document's readyState suggests
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith // it is safe to do so.
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith if (navigator.userAgent.match(/MSIE/)) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith GLOBAL_ENV._dri = setInterval(function() {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith try {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // throws an error if doc is not ready
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith document.documentElement.doScroll('left');
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith clearInterval(GLOBAL_ENV._dri);
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith GLOBAL_ENV._dri = null;
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith _ready();
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith } catch (ex) {
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith }
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith }, POLL_INTERVAL);
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // FireFox, Opera, Safari 3+: These browsers provide a event for this
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // moment.
f012d18e08e5c256ec59c1fce1262f86e1d4a75dLuke Smith } else {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith D.addEventListener("DOMContentLoaded", _ready, false);
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith /////////////////////////////////////////////////////////////
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith})();
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke SmithYUI.add('event', function(Y) {
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith(function() {
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith/**
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith * DOM event listener abstraction layer
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith * @module event
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith */
4ec6c08988d4f78cd72251f55b27f018823a5eeeLuke Smith
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smithvar GLOBAL_ENV = YUI.Env,
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith adapt = Y.Env.evt.plugins,
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith yready = function() {
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith Y.fire('domready');
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith };
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke SmithY.mix(adapt, {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith /**
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * Executes the supplied callback when the DOM is first usable. This
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * will execute immediately if called after the DOMReady event has
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * fired. @todo the DOMContentReady event does not fire when the
9327ef7ad1fee11b0e494b97cc07386565326c03Luke Smith * script is dynamically injected into the page. This means the
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * DOMReady custom event will never fire in FireFox or Opera when the
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * library is injected. It _will_ fire in Safari, and the IE
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * implementation would allow for us to fire it if the defered script
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * is not available. We want this to behave the same in all browsers.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Is there a way to identify when the script has been injected
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * instead of included inline? Is there a way to know whether the
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * window onload event has fired without having had a listener attached
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * to it when it did so?
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith *
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * <p>The callback is a Event.Custom, so the signature is:</p>
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * <p>type &lt;string&gt;, args &lt;array&gt;, customobject &lt;object&gt;</p>
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * <p>For DOMReady events, there are no fire argments, so the
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * signature is:</p>
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * <p>"DOMReady", [], obj</p>
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith *
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith *
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @event domready
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @for YUI
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith *
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param {function} fn what to execute when the element is found.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @optional context execution context
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @optional args 0..n arguments to send to the listener
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith *
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith domready: {},
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * Use domready event instead. @see domready
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @event event:ready
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @for YUI
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @deprecated use 'domready' instead
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith 'event:ready': {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith on: function() {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith var a = Y.Array(arguments, 0, true);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith a[0] = 'domready';
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith return Y.subscribe.apply(Y, a);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith },
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith detach: function() {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var a = Y.Array(arguments, 0, true);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith a[0] = 'domready';
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return Y.unsubscribe.apply(Y, a);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith});
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke SmithY.publish('domready', {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith fireOnce: true
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith});
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smithif (GLOBAL_ENV.DOMReady) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // console.log('DOMReady already fired', 'info', 'event');
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith yready();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith} else {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // console.log('setting up before listener', 'info', 'event');
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // console.log('env: ' + YUI.Env.windowLoaded, 'info', 'event');
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith Y.before(yready, GLOBAL_ENV, "_ready");
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith}
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith})();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith(function() {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith/**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Custom event engine, DOM event listener abstraction layer, synthetic DOM
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * events.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @module event
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith/**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Wraps a DOM event, properties requiring browser abstraction are
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * fixed here. Provids a security layer when required.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @class DOMEventFacade
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param ev {Event} the DOM event
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param currentTarget {HTMLElement} the element the listener was attached to
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param wrapper {Event.Custom} the custom event wrapper for this DOM event
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith/*
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @TODO constants? LEFTBUTTON, MIDDLEBUTTON, RIGHTBUTTON, keys
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith/*
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smithvar whitelist = {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith altKey : 1,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "button" : 1, // we supply
970d3605073835415b62245302838340763d086fLuke Smith // "bubbles" : 1, // needed?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "cancelable" : 1, // needed?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "charCode" : 1, // we supply
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith cancelBubble : 1,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "currentTarget" : 1, // we supply
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith ctrlKey : 1,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith clientX : 1, // needed?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith clientY : 1, // needed?
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith detail : 1, // not fully implemented
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // "fromElement" : 1,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith keyCode : 1,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // "height" : 1, // needed?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "initEvent" : 1, // need the init events?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "initMouseEvent" : 1,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // "initUIEvent" : 1,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "layerX" : 1, // needed?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "layerY" : 1, // needed?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith metaKey : 1,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "modifiers" : 1, // needed?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "offsetX" : 1, // needed?
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // "offsetY" : 1, // needed?
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // "preventDefault" : 1, // we supply
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "reason" : 1, // IE proprietary
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // "relatedTarget" : 1,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "returnValue" : 1, // needed?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith shiftKey : 1,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // "srcUrn" : 1, // IE proprietary
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "srcElement" : 1,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "srcFilter" : 1, IE proprietary
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "stopPropagation" : 1, // we supply
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "target" : 1,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "timeStamp" : 1, // needed?
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // "toElement" : 1,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith type : 1,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "view" : 1,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // "which" : 1, // we supply
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "width" : 1, // needed?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith x : 1,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith y : 1
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith},
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith*/
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var ua = Y.UA,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * webkit key remapping required for Safari < 3.1
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @property webkitKeymap
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @private
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith webkitKeymap = {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith 63232: 38, // up
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith 63233: 40, // down
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith 63234: 37, // left
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith 63235: 39, // right
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith 63276: 33, // page up
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith 63277: 34, // page down
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith 25: 9 // SHIFT-TAB (Safari provides a different key code in
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // this case, even though the shiftKey modifier is set)
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith },
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Returns a wrapped node. Intended to be used on event targets,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * so it will return the node's parent if the target is a text
16479f9c396537f36d0d9c5633b24df618eee1e6Luke Smith * node
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method resolve
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @private
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith resolve = function(n) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (!n) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return null;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith try {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (ua.webkit && 3 == n.nodeType) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith n = n.parentNode;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith } catch(ex) { }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return Y.Node.get(n);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith };
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith// provide a single event with browser abstractions resolved
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith//
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith// include all properties for both browers?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith// include only DOM2 spec properties?
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith// provide browser-specific facade?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke SmithY.DOMEventFacade = function(ev, currentTarget, wrapper) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var e = ev, ot = currentTarget, d = Y.config.doc, b = d.body,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith x = e.pageX, y = e.pageY, c, t;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.altKey = e.altKey;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith this.ctrlKey = e.ctrlKey;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.metaKey = e.metaKey;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith this.shiftKey = e.shiftKey;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.type = e.type;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith //////////////////////////////////////////////////////
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (!x && 0 !== x) {
c31ae0e3c3d2726907b1876b5fe81cfd94527d5dLuke Smith x = e.clientX || 0;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith y = e.clientY || 0;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (ua.ie) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith x += Math.max(d.documentElement.scrollLeft, b.scrollLeft);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith y += Math.max(d.documentElement.scrollTop, b.scrollTop);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this._yuifacade = true;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * The X location of the event on the page (including scroll)
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @property pageX
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @type int
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith this.pageX = x;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * The Y location of the event on the page (including scroll)
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property pageY
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @type int
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.pageY = y;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith //////////////////////////////////////////////////////
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith c = e.keyCode || e.charCode || 0;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (ua.webkit && (c in webkitKeymap)) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith c = webkitKeymap[c];
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * The keyCode for key events. Uses charCode if keyCode is not available
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @property keyCode
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @type int
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.keyCode = c;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * The charCode for key events. Same as keyCode
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property charCode
16479f9c396537f36d0d9c5633b24df618eee1e6Luke Smith * @type int
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
16479f9c396537f36d0d9c5633b24df618eee1e6Luke Smith this.charCode = c;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
16479f9c396537f36d0d9c5633b24df618eee1e6Luke Smith //////////////////////////////////////////////////////
16479f9c396537f36d0d9c5633b24df618eee1e6Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * The button that was pushed.
16479f9c396537f36d0d9c5633b24df618eee1e6Luke Smith * @property button
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @type int
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.button = e.which || e.button;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
88c285485a7919c95181acfc6ed882304dc31187Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * The button that was pushed. Same as button.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property which
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @type int
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith this.which = this.button;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith //////////////////////////////////////////////////////
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Node reference for the targeted element
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @propery target
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @type Node
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.target = resolve(e.target || e.srcElement);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
970d3605073835415b62245302838340763d086fLuke Smith * Node reference for the element that the listener was attached to.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @propery currentTarget
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @type Node
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith this.currentTarget = resolve(ot);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith t = e.relatedTarget;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith if (!t) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (e.type == "mouseout") {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith t = e.toElement;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith } else if (e.type == "mouseover") {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith t = e.fromElement;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * Node reference to the relatedTarget
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @propery relatedTarget
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith * @type Node
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.relatedTarget = resolve(t);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith //////////////////////////////////////////////////////
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // methods
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
5800f4bbed853824dd8797caae32f78bcc9ef09bLuke Smith * Stops the propagation to the next bubble target
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method stopPropagation
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
0b911fcfd6406910a95696c8d583a698e6304e5fSatyen Desai this.stopPropagation = function() {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (e.stopPropagation) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith e.stopPropagation();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith } else {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith e.cancelBubble = true;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (wrapper) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith wrapper.stopPropagation();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith };
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Stops the propagation to the next bubble target and
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * prevents any additional listeners from being exectued
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * on the current target.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @method stopImmediatePropagation
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.stopImmediatePropagation = function() {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (e.stopImmediatePropagation) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith e.stopImmediatePropagation();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith } else {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.stopPropagation();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith if (wrapper) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith wrapper.stopImmediatePropagation();
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith };
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith /**
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * Prevents the event's default behavior
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method preventDefault
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith this.preventDefault = function() {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (e.preventDefault) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith e.preventDefault();
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith } else {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith e.returnValue = false;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (wrapper) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith wrapper.preventDefault();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith };
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Stops the event propagation and prevents the default
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * event behavior.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method halt
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param immediate {boolean} if true additional listeners
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * on the current target will not be executed
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.halt = function(immediate) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (immediate) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.stopImmediatePropagation();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith } else {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.stopPropagation();
5800f4bbed853824dd8797caae32f78bcc9ef09bLuke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.preventDefault();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith };
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith};
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith})();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith(function() {
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith/**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * DOM event listener abstraction layer
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @module event
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith/**
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith * The event utility provides functions to add and remove event listeners,
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith * event cleansing. It also tries to automatically remove listeners it
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith * registers during the unload event.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith *
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith * @class Event
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith * @static
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith */
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smithvar add = YUI.Env.add,
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smithremove = YUI.Env.remove,
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke SmithonLoad = function() {
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith YUI.Env.windowLoaded = true;
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith Y.Event._load();
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith remove(window, "load", onLoad);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith},
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke SmithonUnload = function() {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith Y.Event._unload();
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith remove(window, "unload", onUnload);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith},
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke SmithEVENT_READY = 'domready',
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke SmithCOMPAT_ARG = '~yui|2|compat~',
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke SmithshouldIterate = function(o) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith try {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
c36390194c43d30adc1059a74fdf9f51a235b8a9Luke Smith // if (o instanceof Y.Node) {
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith // o.tagName ="adsf";
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith return ( o && // o is something
abdfe7cf11d34f89f17b26e4779bf6079d22a910Adam Moore typeof o !== "string" && // o is not a string
abdfe7cf11d34f89f17b26e4779bf6079d22a910Adam Moore // o.length && // o is indexed
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith (o.length && ((!o.size) || (o.size() > 1))) && // o is indexed
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith !o.tagName && // o is not an HTML element
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith !o.alert && // o is not a window
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith (o.item || typeof o[0] !== "undefined") );
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith } catch(ex) {
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith return false;
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith }
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith},
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke SmithEvent = function() {
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith /**
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith * True after the onload event has fired
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property _loadComplete
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @type boolean
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @static
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @private
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith var _loadComplete = false,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith /**
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * The number of times to poll after window.onload. This number is
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * increased if additional late-bound handlers are requested after
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * the page load.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property _retryCount
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @static
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @private
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith */
c36390194c43d30adc1059a74fdf9f51a235b8a9Luke Smith _retryCount = 0,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith /**
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * onAvailable listeners
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property _avail
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @static
d7a66fe2de41baa56b41e8b7b254d1f6a50a5135Luke Smith * @private
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith _avail = [],
c31ae0e3c3d2726907b1876b5fe81cfd94527d5dLuke Smith
c31ae0e3c3d2726907b1876b5fe81cfd94527d5dLuke Smith /**
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * Custom event wrappers for DOM events. Key is
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * 'event:' + Element uid stamp + event type
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property _wrappers
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @type Y.Event.Custom
c36390194c43d30adc1059a74fdf9f51a235b8a9Luke Smith * @static
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @private
12193f069c735c9fa68aa22d6b4a8ff1fc5b83f1Luke Smith */
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith _wrappers = {},
12193f069c735c9fa68aa22d6b4a8ff1fc5b83f1Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith _windowLoadKey = null,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
12193f069c735c9fa68aa22d6b4a8ff1fc5b83f1Luke Smith /**
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * Custom event wrapper map DOM events. Key is
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * Element uid stamp. Each item is a hash of custom event
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * wrappers as provided in the _wrappers collection. This
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * provides the infrastructure for getListeners.
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * @property _el_events
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * @static
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * @private
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith */
12193f069c735c9fa68aa22d6b4a8ff1fc5b83f1Luke Smith _el_events = {};
12193f069c735c9fa68aa22d6b4a8ff1fc5b83f1Luke Smith
12193f069c735c9fa68aa22d6b4a8ff1fc5b83f1Luke Smith return {
12193f069c735c9fa68aa22d6b4a8ff1fc5b83f1Luke Smith
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith /**
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * The number of times we should look for elements that are not
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * in the DOM at the time the event is requested after the document
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * has been loaded. The default is 2000@amp;20 ms, so it will poll
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * for 40 seconds or until all outstanding handlers are bound
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * (whichever comes first).
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * @property POLL_RETRYS
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * @type int
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * @static
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * @final
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith */
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith POLL_RETRYS: 1000,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith /**
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * The poll interval in milliseconds
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property POLL_INTERVAL
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @type int
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @static
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @final
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith POLL_INTERVAL: 40,
f9918f6b658319940d633dd8c9a2c16d05b4c881Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith /**
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * addListener/removeListener can throw errors in unexpected scenarios.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * These errors are suppressed, the method returns false, and this property
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * is set
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property lastError
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @static
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @type Error
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith lastError: null,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith /**
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * poll handle
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property _interval
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @static
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @private
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith _interval: null,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith /**
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * document readystate poll handle
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * @property _dri
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * @static
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * @private
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith */
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith _dri: null,
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith /**
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * True when the document is initially usable
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @property DOMReady
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * @type boolean
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @static
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith DOMReady: false,
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith /**
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @method startInterval
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * @static
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * @private
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith */
c31ae0e3c3d2726907b1876b5fe81cfd94527d5dLuke Smith startInterval: function() {
c31ae0e3c3d2726907b1876b5fe81cfd94527d5dLuke Smith var E = Y.Event;
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith if (!E._interval) {
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke SmithE._interval = setInterval(Y.bind(E._poll, E), E.POLL_INTERVAL);
8951c05b281fd712464388c237e1f0be6b6487a7Luke Smith }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith },
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
c31ae0e3c3d2726907b1876b5fe81cfd94527d5dLuke Smith /**
c31ae0e3c3d2726907b1876b5fe81cfd94527d5dLuke Smith * Executes the supplied callback when the item with the supplied
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * id is found. This is meant to be used to execute behavior as
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * soon as possible as the page loads. If you use this after the
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * initial page load it will poll for a fixed time for the element.
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * The number of times it will poll and the frequency are
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * configurable. By default it will poll for 10 seconds.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith *
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * <p>The callback is executed with a single parameter:
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * the custom object parameter, if provided.</p>
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith *
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @method onAvailable
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith *
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @param {string||string[]} id the id of the element, or an array
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * of ids to look for.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @param {function} fn what to execute when the element is found.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @param {object} p_obj an optional object to be passed back as
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * a parameter to fn.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @param {boolean|object} p_override If set to true, fn will execute
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * in the context of p_obj, if set to an object it
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * will execute in the context of that object
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @param checkContent {boolean} check child node readiness (onContentReady)
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @static
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @deprecated Use Y.on("available")
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // @TODO fix arguments
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith onAvailable: function(id, fn, p_obj, p_override, checkContent, compat) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith var a = Y.Array(id), i;
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith for (i=0; i<a.length; i=i+1) {
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith _avail.push({
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith id: a[i],
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith fn: fn,
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith obj: p_obj,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith override: p_override,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith checkReady: checkContent,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith compat: compat
08299549a01ea998d0fc3bfbf97c5ff4d351f543Luke Smith });
08299549a01ea998d0fc3bfbf97c5ff4d351f543Luke Smith }
08299549a01ea998d0fc3bfbf97c5ff4d351f543Luke Smith _retryCount = this.POLL_RETRYS;
08299549a01ea998d0fc3bfbf97c5ff4d351f543Luke Smith
08299549a01ea998d0fc3bfbf97c5ff4d351f543Luke Smith // We want the first test to be immediate, but async
08299549a01ea998d0fc3bfbf97c5ff4d351f543Luke Smith setTimeout(Y.bind(Y.Event._poll, Y.Event), 0);
08299549a01ea998d0fc3bfbf97c5ff4d351f543Luke Smith
08299549a01ea998d0fc3bfbf97c5ff4d351f543Luke Smith return new Y.EventHandle(); // @TODO by id needs a defered handle
08299549a01ea998d0fc3bfbf97c5ff4d351f543Luke Smith },
08299549a01ea998d0fc3bfbf97c5ff4d351f543Luke Smith
08299549a01ea998d0fc3bfbf97c5ff4d351f543Luke Smith /**
08299549a01ea998d0fc3bfbf97c5ff4d351f543Luke Smith * Works the same way as onAvailable, but additionally checks the
08299549a01ea998d0fc3bfbf97c5ff4d351f543Luke Smith * state of sibling elements to determine if the content of the
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * available element is safe to modify.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith *
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * <p>The callback is executed with a single parameter:
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * the custom object parameter, if provided.</p>
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith *
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @method onContentReady
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith *
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @param {string} id the id of the element to look for.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @param {function} fn what to execute when the element is ready.
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * @param {object} p_obj an optional object to be passed back as
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * a parameter to fn.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @param {boolean|object} p_override If set to true, fn will execute
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * in the context of p_obj. If an object, fn will
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * exectute in the context of that object
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith *
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @static
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @deprecated Use Y.on("contentready")
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // @TODO fix arguments
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith onContentReady: function(id, fn, p_obj, p_override, compat) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith return this.onAvailable(id, fn, p_obj, p_override, true, compat);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith },
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith /**
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * Appends an event handler
c36390194c43d30adc1059a74fdf9f51a235b8a9Luke Smith *
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * @method attach
c36390194c43d30adc1059a74fdf9f51a235b8a9Luke Smith *
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * @param {String} type The type of event to append
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * @param {Function} fn The method the event invokes
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * @param {String|HTMLElement|Array|NodeList} el An id, an element
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * reference, or a collection of ids and/or elements to assign the
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * listener to.
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * @param {Object} obj An arbitrary object that will be
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * passed as a parameter to the handler
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @param {Boolean|object} args 0..n arguments to pass to the callback
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith * @return {Boolean} True if the action was successful or defered,
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith * false if one or more of the elements
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith * could not have the listener attached,
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith * or if the operation throws an exception.
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith * @static
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith */
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith attach: function(type, fn, el, obj) {
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith return Y.Event._attach(Y.Array(arguments, 0, true));
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith },
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith _createWrapper: function (el, type, capture, compat, facade) {
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith var ek = Y.stamp(el),
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith key = 'event:' + ek + type,
c36390194c43d30adc1059a74fdf9f51a235b8a9Luke Smith cewrapper;
c36390194c43d30adc1059a74fdf9f51a235b8a9Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith if (false === facade) {
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith key += 'native';
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith if (capture) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith key += 'capture';
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith cewrapper = _wrappers[key];
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith if (!cewrapper) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // create CE wrapper
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith cewrapper = Y.publish(key, {
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith //silent: true,
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith // host: this,
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith bubbles: false,
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith contextFn: function() {
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith cewrapper.nodeRef = cewrapper.nodeRef || Y.get(cewrapper.el);
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith return cewrapper.nodeRef;
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith }
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith });
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith // for later removeListener calls
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith cewrapper.el = el;
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith cewrapper.type = type;
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith cewrapper.fn = function(e) {
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith cewrapper.fire(Y.Event.getEvent(e, el, (compat || (false === facade))));
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith };
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith if (el == Y.config.win && type == "load") {
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith // window load happens once
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith cewrapper.fireOnce = true;
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith _windowLoadKey = key;
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith }
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith _wrappers[key] = cewrapper;
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith _el_events[ek] = _el_events[ek] || {};
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith _el_events[ek][key] = cewrapper;
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith add(el, type, cewrapper.fn, capture);
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith }
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith return cewrapper;
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith },
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith _attach: function(args, config) {
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith var trimmedArgs=args.slice(1),
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith compat, E=Y.Event,
970d3605073835415b62245302838340763d086fLuke Smith handles, oEl, cewrapper, context,
970d3605073835415b62245302838340763d086fLuke Smith fireNow = false, ret,
970d3605073835415b62245302838340763d086fLuke Smith type = args[0],
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith fn = args[1],
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith el = args[2] || Y.config.win,
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith facade = config && config.facade,
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith capture = config && config.capture;
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith if (trimmedArgs[trimmedArgs.length-1] === COMPAT_ARG) {
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith compat = true;
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith trimmedArgs.pop();
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith }
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith if (!fn || !fn.call) {
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith// throw new TypeError(type + " attach call failed, callback undefined");
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith return false;
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith }
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // The el argument can be an array of elements or element ids.
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith if (shouldIterate(el)) {
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
ee2325dc6d26d8f385007b70eef20ba8890138e8Satyen Desai
ee2325dc6d26d8f385007b70eef20ba8890138e8Satyen Desai handles=[];
ee2325dc6d26d8f385007b70eef20ba8890138e8Satyen Desai
ee2325dc6d26d8f385007b70eef20ba8890138e8Satyen Desai Y.each(el, function(v, k) {
ee2325dc6d26d8f385007b70eef20ba8890138e8Satyen Desai args[2] = v;
ee2325dc6d26d8f385007b70eef20ba8890138e8Satyen Desai handles.push(E._attach(args, config));
ee2325dc6d26d8f385007b70eef20ba8890138e8Satyen Desai });
ee2325dc6d26d8f385007b70eef20ba8890138e8Satyen Desai
ee2325dc6d26d8f385007b70eef20ba8890138e8Satyen Desai return (handles.length === 1) ? handles[0] : handles;
ee2325dc6d26d8f385007b70eef20ba8890138e8Satyen Desai
ee2325dc6d26d8f385007b70eef20ba8890138e8Satyen Desai // If the el argument is a string, we assume it is
ee2325dc6d26d8f385007b70eef20ba8890138e8Satyen Desai // actually the id of the element. If the page is loaded
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // we convert el to the actual element, otherwise we
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // defer attaching the event until the element is
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // ready
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith } else if (Y.Lang.isString(el)) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // @TODO switch to using DOM directly here
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // oEl = (compat) ? Y.DOM.byId(el) : Y.all(el);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith oEl = (compat) ? Y.DOM.byId(el) : Y.Selector.query(el);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith if (oEl) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith if (Y.Lang.isArray(oEl)) {
e4c11ebec5d4604079c3d53d2138ee3f772f91f7Luke Smith if (oEl.length == 1) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith el = oEl[0];
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith } else {
970d3605073835415b62245302838340763d086fLuke Smith args[2] = oEl;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith return E._attach(args, config);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
970d3605073835415b62245302838340763d086fLuke Smith // HTMLElement
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith } else {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith el = oEl;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // Not found = defer adding the event until the element is available
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith } else {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
c36390194c43d30adc1059a74fdf9f51a235b8a9Luke Smith return this.onAvailable(el, function() {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith E._attach(args, config);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }, E, true, false, compat);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // Element should be an html element or an array if we get here.
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith if (!el) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith return false;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // the custom event key is the uid for the element + type
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // allow a node reference to Y.on to work with load time addEventListener check
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // (Node currently only has the addEventListener interface and that may be
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith // removed).
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith if (Y.Node && el instanceof Y.Node) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith return el.on.apply(el, args);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith cewrapper = this._createWrapper(el, type, capture, compat, facade);
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith if (el == Y.config.win && type == "load") {
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // if the load is complete, fire immediately.
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // all subscribers, including the current one
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // will be notified.
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith if (YUI.Env.windowLoaded) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith fireNow = true;
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith }
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith }
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // switched from obj to trimmedArgs[2] to deal with appened compat param
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // context = trimmedArgs[2] || ((compat) ? el : Y.get(el));
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith context = trimmedArgs[2];
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // set the context as the second arg to subscribe
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith trimmedArgs[1] = context;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // remove the 'obj' param
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith trimmedArgs.splice(2, 1);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // set context to the Node if not specified
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith ret = cewrapper.subscribe.apply(cewrapper, trimmedArgs);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith if (fireNow) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith cewrapper.fire();
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith return ret;
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith },
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith /**
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * Removes an event listener. Supports the signature the event was bound
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * with, but the preferred way to remove listeners is using the handle
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * that is returned when using Y.on
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith *
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith * @method detach
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith *
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith * @param {String|HTMLElement|Array|NodeList} el An id, an element
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith * reference, or a collection of ids and/or elements to remove
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith * the listener from.
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith * @param {String} type the type of event to remove.
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith * @param {Function} fn the method the event invokes. If fn is
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith * undefined, then all event handlers for the type of event are * removed.
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith * @return {boolean} true if the unbind was successful, false * otherwise.
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith * @static
60f5c306297c0d0dc6d239ee618ee333f4c40746Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith detach: function(type, fn, el, obj) {
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith var args=Y.Array(arguments, 0, true), compat, i, len, ok,
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith id, ce;
60f5c306297c0d0dc6d239ee618ee333f4c40746Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith if (args[args.length-1] === COMPAT_ARG) {
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith compat = true;
970d3605073835415b62245302838340763d086fLuke Smith // args.pop();
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith }
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
60f5c306297c0d0dc6d239ee618ee333f4c40746Luke Smith if (type && type.detach) {
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith return type.detach();
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith }
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // The el argument can be a string
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith if (typeof el == "string") {
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // el = (compat) ? Y.DOM.byId(el) : Y.all(el);
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith el = (compat) ? Y.DOM.byId(el) : Y.Selector.query(el);
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith return Y.Event.detach.apply(Y.Event, args);
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // The el argument can be an array of elements or element ids.
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith } else if (shouldIterate(el)) {
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith ok = true;
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith for (i=0, len=el.length; i<len; ++i) {
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith args[2] = el[i];
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith ok = ( Y.Event.detach.apply(Y.Event, args) && ok );
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith }
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith return ok;
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith }
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith if (!type || !fn || !fn.call) {
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith return this.purgeElement(el, false, type);
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith }
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith id = 'event:' + Y.stamp(el) + type;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith ce = _wrappers[id];
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith if (ce) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith return ce.detach(fn);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith } else {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith return false;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith },
abdfe7cf11d34f89f17b26e4779bf6079d22a910Adam Moore
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith /**
abdfe7cf11d34f89f17b26e4779bf6079d22a910Adam Moore * Finds the event in the window object, the caller's arguments, or
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * in the arguments of another method in the callstack. This is
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * executed automatically for events registered through the event
abdfe7cf11d34f89f17b26e4779bf6079d22a910Adam Moore * manager, so the implementer should not normally need to execute
abdfe7cf11d34f89f17b26e4779bf6079d22a910Adam Moore * this function at all.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @method getEvent
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @param {Event} e the event parameter from the handler
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @param {HTMLElement} el the element the listener was attached to
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith * @return {Event} the event
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @static
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith getEvent: function(e, el, noFacade) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith var ev = e || window.event;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith return (noFacade) ? ev :
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith new Y.DOMEventFacade(ev, el, _wrappers['event:' + Y.stamp(el) + e.type]);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith },
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith /**
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * Generates an unique ID for the element if it does not already
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * have one.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @method generateId
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @param el the element to create the id for
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @return {string} the resulting id of the element
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @static
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith */
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith generateId: function(el) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith var id = el.id;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith if (!id) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith id = Y.stamp(el);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith el.id = id;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith return id;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith },
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith /**
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * We want to be able to use getElementsByTagName as a collection
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * to attach a group of events to. Unfortunately, different
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * browsers return different types of collections. This function
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * tests to determine if the object is array-like. It will also
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * fail if the object is an array, but is empty.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method _isValidCollection
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param o the object to test
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @return {boolean} true if the object is array-like and populated
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @deprecated was not meant to be used directly
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @static
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @private
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith _isValidCollection: shouldIterate,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * hook up any deferred listeners
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method _load
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @static
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @private
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith _load: function(e) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith if (!_loadComplete) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith _loadComplete = true;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // Just in case DOMReady did not go off for some reason
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // E._ready();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (Y.fire) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith Y.fire(EVENT_READY);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith // Available elements may not have been detected before the
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // window load event fires. Try to find them now so that the
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // the user is more likely to get the onAvailable notifications
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // before the window load notification
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith Y.Event._poll();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith },
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Polling function that runs before the onload event fires,
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith * attempting to attach to DOM Nodes as soon as they are
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * available
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method _poll
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @static
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @private
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith _poll: function() {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (this.locked) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (Y.UA.ie && !YUI.Env.DOMReady) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // Hold off if DOMReady has not fired and check current
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // readyState to protect against the IE operation aborted
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // issue.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.startInterval();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith this.locked = true;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // keep trying until after the page is loaded. We need to
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // check the page load state prior to trying to bind the
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // elements so that we can be certain all elements have been
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // tested appropriately
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var tryAgain = !_loadComplete, notAvail, executeItem,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith i, len, item, el;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (!tryAgain) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith tryAgain = (_retryCount > 0);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // onAvailable
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith notAvail = [];
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith executeItem = function (el, item) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var context, ov = item.override;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (item.compat) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (item.override) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (ov === true) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith context = item.obj;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith } else {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith context = ov;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith } else {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith context = el;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith item.fn.call(context, item.obj);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith } else {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith context = item.obj || Y.get(el);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith item.fn.apply(context, (Y.Lang.isArray(ov)) ? ov : []);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith };
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // onAvailable
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith for (i=0,len=_avail.length; i<len; ++i) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith item = _avail[i];
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (item && !item.checkReady) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // el = (item.compat) ? Y.DOM.byId(item.id) : Y.get(item.id);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith el = (item.compat) ? Y.DOM.byId(item.id) : Y.Selector.query(item.id, null, true);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (el) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith executeItem(el, item);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith _avail[i] = null;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith } else {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith notAvail.push(item);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // onContentReady
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith for (i=0,len=_avail.length; i<len; ++i) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith item = _avail[i];
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (item && item.checkReady) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith // el = (item.compat) ? Y.DOM.byId(item.id) : Y.get(item.id);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith el = (item.compat) ? Y.DOM.byId(item.id) : Y.Selector.query(item.id, null, true);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (el) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // The element is available, but not necessarily ready
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // @todo should we test parentNode.nextSibling?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (_loadComplete || (el.get && el.get('nextSibling')) || el.nextSibling) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith executeItem(el, item);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith _avail[i] = null;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith } else {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith notAvail.push(item);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith _retryCount = (notAvail.length === 0) ? 0 : _retryCount - 1;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith if (tryAgain) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // we may need to strip the nulled out items here
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.startInterval();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith } else {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith clearInterval(this._interval);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this._interval = null;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.locked = false;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith },
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Removes all listeners attached to the given element via addListener.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Optionally, the node's children can also be purged.
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith * Optionally, you can specify a specific type of event to remove.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method purgeElement
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param {HTMLElement} el the element to purge
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param {boolean} recurse recursively purge this element's children
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * as well. Use with caution.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param {string} type optional type of listener to purge. If
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * left out, all listeners will be removed
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @static
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith purgeElement: function(el, recurse, type) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // var oEl = (Y.Lang.isString(el)) ? Y.get(el) : el,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var oEl = (Y.Lang.isString(el)) ? Y.Selector.query(el, null, true) : el,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith lis = this.getListeners(oEl, type), i, len;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (lis) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith for (i=0,len=lis.length; i<len ; ++i) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith lis[i].detachAll();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (recurse && oEl && oEl.childNodes) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith for (i=0,len=oEl.childNodes.length; i<len ; ++i) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.purgeElement(oEl.childNodes[i], recurse, type);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith },
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Returns all listeners attached to the given element via addListener.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Optionally, you can specify a specific type of event to return.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method getListeners
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param el {HTMLElement|string} the element or element id to inspect
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param type {string} optional type of listener to return. If
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * left out, all listeners will be returned
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @return {Y.Custom.Event} the custom event wrapper for the DOM event(s)
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith * @static
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith getListeners: function(el, type) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var ek = Y.stamp(el, true), evts = _el_events[ek],
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith results=[] , key = (type) ? 'event:' + ek + type : null;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (!evts) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return null;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith if (key) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (evts[key]) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith results.push(evts[key]);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith } else {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith Y.each(evts, function(v, k) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith results.push(v);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith });
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return (results.length) ? results : null;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith },
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Removes all listeners registered by pe.event. Called
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * automatically during the unload event.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method _unload
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @static
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @private
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith _unload: function(e) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var E = Y.Event;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith Y.each(_wrappers, function(v, k) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith v.detachAll();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith remove(v.el, v.type, v.fn);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith delete _wrappers[k];
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith });
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith remove(window, "load", E._load);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith remove(window, "unload", E._unload);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith },
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Adds a DOM event directly without the caching, cleanup, context adj, etc
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith *
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method nativeAdd
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param {HTMLElement} el the element to bind the handler to
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param {string} type the type of event handler
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param {function} fn the callback to invoke
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param {boolen} capture capture or bubble phase
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @static
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @private
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith nativeAdd: add,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith /**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Basic remove listener
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith *
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method nativeRemove
b2cd00278b38b2f4565812f25e8d9b532e013735Satyen Desai * @param {HTMLElement} el the element to bind the handler to
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param {string} type the type of event handler
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param {function} fn the callback to invoke
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param {boolen} capture capture or bubble phase
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @static
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @private
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith nativeRemove: remove
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith };
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith}();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke SmithY.Event = Event;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smithif (Y.config.injected || YUI.Env.windowLoaded) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith onLoad();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith} else {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith add(window, "load", onLoad);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith}
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith// Process onAvailable/onContentReady items when when the DOM is ready in IE
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smithif (Y.UA.ie) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith Y.on(EVENT_READY, Event._poll, Event, true);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith}
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smithadd(window, "unload", onUnload);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke SmithEvent.Custom = Y.CustomEvent;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke SmithEvent.Subscriber = Y.Subscriber;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke SmithEvent.Target = Y.EventTarget;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke SmithEvent.Handle = Y.EventHandle;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke SmithEvent.Facade = Y.EventFacade;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke SmithEvent._poll();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith})();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith/**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Executes the callback as soon as the specified element
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * is detected in the DOM.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @for YUI
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @event available
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke SmithY.Env.evt.plugins.available = {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith on: function(type, fn, id, o) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var a = arguments.length > 4 ? Y.Array(arguments, 4, true) : [];
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return Y.Event.onAvailable.call(Y.Event, id, fn, o, a);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith};
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith/**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Executes the callback as soon as the specified element
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * is detected in the DOM with a nextSibling property
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * (indicating that the element's children are available)
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @for YUI
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @event contentready
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke SmithY.Env.evt.plugins.contentready = {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith on: function(type, fn, id, o) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var a = arguments.length > 4 ? Y.Array(arguments, 4, true) : [];
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return Y.Event.onContentReady.call(Y.Event, id, fn, o, a);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith};
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith(function() {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smithvar adapt = Y.Env.evt.plugins,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith CAPTURE_CONFIG = { capture: true },
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith NOOP = function(){},
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // Opera implents capture phase events per spec rather than
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // the more useful way it is implemented in other browsers:
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // The event doesn't fire on a target unless there is a
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // listener on an element in the target's ancestry. If a
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // capture phase listener is added only to the element that
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // will be the target of the event, the listener won't fire.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // To get around this, we register a NOOP listener on the
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // element's parent.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith _captureHack = function(type, o) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var el = (Y.Lang.isString(o)) ? Y.Selector.query(o, null, true) : o,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith p = el && el.parentNode;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (p) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith Y.Event._attach([type, NOOP, p], CAPTURE_CONFIG);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith };
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith/**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Adds a DOM focus listener. Uses the focusin event in IE,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * and the capture phase otherwise so that
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * the event propagates in a way that enables event delegation.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith *
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Note: if you are registering this event on the intended target
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * rather than an ancestor, the element must be in the DOM in
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * order for it to work in Opera.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith *
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @for YUI
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @event focus
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smithadapt.focus = {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith on: function(type, fn, o) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var a = Y.Array(arguments, 0, true);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (Y.UA.opera) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith _captureHack(type, o);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return Y.Event._attach(a, CAPTURE_CONFIG);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith};
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith/**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Adds a DOM blur listener. Uses the focusout event in IE,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * and the capture phase otherwise so that
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * the event propagates in a way that enables event delegation.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith *
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Note: if you are registering this event on the intended target
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * rather than an ancestor, the element must be in the DOM
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * at the time of registration in order for it to work in Opera.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith *
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @for YUI
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @event blur
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smithadapt.blur = {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith on: function(type, fn, o) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var a = Y.Array(arguments, 0, true);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (Y.UA.opera) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith _captureHack(type, o);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return Y.Event._attach(a, CAPTURE_CONFIG);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith};
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith})();
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith/**
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Add a key listener. The listener will only be notified if the
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * keystroke detected meets the supplied specification. The
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * spec consists of the key event type, followed by a colon,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * followed by zero or more comma separated key codes, followed
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * by zero or more modifiers delimited by a plus sign. Ex:
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * press:12,65+shift+ctrl
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @event key
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @for YUI
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param type {string} 'key'
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param fn {string} the function to execute
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param id {string} the element(s) to bind
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param spec {string} the keyCode and modifier specification
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param o optional context object
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @param args 0..n additional arguments that should be provided
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * to the listener.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @return {Event.Handle} the detach handle
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith */
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke SmithY.Env.evt.plugins.key = {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith on: function(type, fn, id, spec, o) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var a = Y.Array(arguments, 0, true),
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith parsed, etype, criteria, ename;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (!spec || spec.indexOf(':') == -1) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith a[0] = 'keypress';
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return Y.on.apply(Y, a);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith parsed = spec.split(':');
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // key event type: 'down', 'up', or 'press'
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith etype = parsed[0];
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // list of key codes optionally followed by modifiers
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith criteria = (parsed[1]) ? parsed[1].split(/,|\+/) : null;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // the name of the custom event that will be created for the spec
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith ename = (Y.Lang.isString(id) ? id : Y.stamp(id)) + spec;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith ename = ename.replace(/,/g, '_');
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (!Y.getEvent(ename)) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // subscribe spec validator to the DOM event
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith Y.on(type + etype, function(e) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var passed = false, failed = false, i, crit, critInt;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith for (i=0; i<criteria.length; i=i+1) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith crit = criteria[i];
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith critInt = parseInt(crit, 10);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // pass this section if any supplied keyCode
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // is found
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (Y.Lang.isNumber(critInt)) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (e.charCode === critInt) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith passed = true;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith } else {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith failed = true;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // only check modifier if no keyCode was specified
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // or the keyCode check was successful. pass only
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // if every modifier passes
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith } else if (passed || !failed) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith passed = (e[crit + 'Key']);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith failed = !passed;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // fire spec custom event if spec if met
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (passed) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith Y.fire(ename, e);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }, id);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // subscribe supplied listener to custom event for spec validator
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // remove element and spec.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith a.splice(2, 2);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith a[0] = ename;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return Y.on.apply(Y, a);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith};
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith(function() {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smithvar Lang = Y.Lang,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith delegates = {},
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith resolveTextNode = function(n) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith try {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (n && 3 == n.nodeType) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return n.parentNode;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith } catch(e) { }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return n;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith },
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith _worker = function(delegateKey, e, el) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var target = resolveTextNode((e.target || e.srcElement)),
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith tests = delegates[delegateKey],
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith spec,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith ename,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith elements,
nElements,
matched,
ev,
i;
for (spec in tests) {
if (tests.hasOwnProperty(spec)) {
ename = tests[spec];
elements = Y.Selector.query(spec, el);
nElements = elements.length;
if (nElements > 0) {
i = elements.length - 1;
do {
matched = elements[i];
if (matched === target || Y.DOM.contains(matched, target)) {
if (!ev) {
ev = new Y.DOMEventFacade(e, el);
ev.container = ev.currentTarget;
}
ev.currentTarget = Y.Node.get(matched);
Y.fire(ename, ev);
}
}
while (i--);
}
}
}
},
attach = function (type, key, element) {
Y.Event._attach([type, function (e) {
_worker(key, (e || window.event), element);
}, element], { facade: false });
},
_sanitize = Y.cached(function(str) {
return str.replace(/[|,:]/g, '~');
});
/**
* Sets up a delegated listener container.
* @event delegate
* @param type {string} 'delegate'
* @param fn {string} the function to execute
* @param el {string|node} the element that is the delegation container
* @param delegateType {string} the event type to delegate
* @param spec {string} a selector that must match the target of the
* event.
* @return {EventHandle} the detach handle
* @for YUI
*/
Y.Env.evt.plugins.delegate = {
on: function(type, fn, el, delegateType, spec) {
if (!spec) {
return false;
}
// identifier to target the container
var guid = (Lang.isString(el) ? el : Y.stamp(el)),
// the custom event for the delegation spec
ename = 'delegate:' + guid + delegateType + _sanitize(spec),
// the key to the listener for the event type and container
delegateKey = delegateType + guid,
a = Y.Array(arguments, 0, true),
element;
if (!(delegateKey in delegates)) {
if (Lang.isString(el)) { // Selector
element = Y.Selector.query(el);
}
else { // Node instance
element = Y.Node.getDOMNode(el);
}
if (Lang.isArray(element)) {
Y.Array.each(element, function (v) {
attach(delegateType, delegateKey, v);
});
}
else {
attach(delegateType, delegateKey, element);
}
delegates[delegateKey] = {};
}
delegates[delegateKey][spec] = ename;
a[0] = ename;
// remove element, delegation spec and context object from the args
a.splice(2, 3);
// subscribe to the custom event for the delegation spec
return Y.on.apply(Y, a);
}
};
})();
(function() {
var detachHandle,
timerHandle,
CE_NAME = 'window:resize',
handler = function(e) {
if (Y.UA.gecko) {
Y.fire(CE_NAME, e);
} else {
if (timerHandle) {
timerHandle.cancel();
}
timerHandle = Y.later(Y.config.windowResizeDelay || 40, Y, function() {
Y.fire(CE_NAME, e);
});
}
};
/**
* Firefox fires the window resize event once when the resize action
* finishes, other browsers fire the event periodically during the
* resize. This code uses timeout logic to simulate the Firefox
* behavior in other browsers.
* @event windowresize
* @for YUI
*/
Y.Env.evt.plugins.windowresize = {
on: function(type, fn) {
// check for single window listener and add if needed
if (!detachHandle) {
detachHandle = Y.Event._attach(['resize', handler]);
}
var a = Y.Array(arguments, 0, true);
a[0] = CE_NAME;
return Y.on.apply(Y, a);
}
};
})();
var isString = Y.Lang.isString,
fireMouseEventForNode = function (node, relatedTarget, eventName, e, spec) {
if (!node.compareTo(relatedTarget) && !node.contains(relatedTarget)) {
e.container = e.currentTarget;
e.currentTarget = node;
Y.fire(eventName, e);
}
},
handleMouseEvent = function (e, eventName, spec) {
var relatedTarget = e.relatedTarget,
currentTarget = e.currentTarget,
target = e.target;
if (spec) {
currentTarget.queryAll(spec).some(function (v) {
var bReturnVal;
if (v.compareTo(target) || v.contains(target)) {
fireMouseEventForNode(v, relatedTarget, eventName, e, spec);
bReturnVal = true;
}
return bReturnVal;
});
}
else {
fireMouseEventForNode(currentTarget, relatedTarget, eventName, e);
}
},
sanitize = Y.cached(function (str) {
return str.replace(/[|,:]/g, "~");
}),
eventConfig = {
on: function(type, fn, el, spec) {
var sDOMEvent = (type === "mouseenter") ? "mouseover" : "mouseout",
sEventName = type + ":" + (isString(el) ? el : Y.stamp(el)) + sDOMEvent,
args = Y.Array(arguments, 0, true),
sSelector;
if (isString(spec)) {
sSelector = spec;
sEventName = sEventName + sanitize(sSelector);
}
if (!Y.getEvent(sEventName)) {
// Set up the listener on the container
Y.on(sDOMEvent, function (e) {
handleMouseEvent(e, sEventName, sSelector);
}, el);
}
args[0] = sEventName;
// Remove the element (and the spec--if defined) from the args
if (sSelector) {
args.splice(2, 2);
}
else {
args.splice(2, 1);
}
// Subscribe to the custom event for the delegation spec
return Y.on.apply(Y, args);
}
};
/**
* Sets up a "mouseenter" listener&#151;a listener that is called the first time
* the user's mouse enters the specified element(s). By passing a CSS selector
* as the fourth argument, can also be used to delegate a "mouseenter"
* event listener.
*
* @event mouseenter
* @param type {string} "mouseenter"
* @param fn {string} The method the event invokes.
* @param el {string|node} The element(s) to assign the listener to.
* @param spec {string} Optional. String representing a selector that must
* match the target of the event in order for the listener to be called.
* @return {EventHandle} the detach handle
* @for YUI
*/
Y.Env.evt.plugins.mouseenter = eventConfig;
/**
* Sets up a "mouseleave" listener&#151;a listener that is called the first time
* the user's mouse leaves the specified element(s). By passing a CSS selector
* as the fourth argument, can also be used to delegate a "mouseleave"
* event listener.
*
* @event mouseleave
* @param type {string} "mouseleave"
* @param fn {string} The method the event invokes.
* @param el {string|node} The element(s) to assign the listener to.
* @param spec {string} Optional. String representing a selector that must
* match the target of the event in order for the listener to be called.
* @return {EventHandle} the detach handle
* @for YUI
*/
Y.Env.evt.plugins.mouseleave = eventConfig;
}, '@VERSION@' ,{requires:['event-custom']});