event.js revision 49aeff0977f7697801747d30bbd74b749891c2d4
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith * DOM event listener abstraction layer
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith * @module event
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith(function() {
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 _ready = function(e) {
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith // Remove the DOMContentLoaded (FF/Opera/Safari)
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith D.removeEventListener("DOMContentLoaded", _ready, false);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // create custom event
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith/*! DOMReady: based on work by: Dean Edwards/John Resig/Matthias Miller/Diego Perini */
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.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // throws an error if doc is not ready
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith } catch (ex) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // FireFox, Opera, Safari 3+: These browsers provide a event for this
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith D.addEventListener("DOMContentLoaded", _ready, false);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith /////////////////////////////////////////////////////////////
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith(function() {
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith * DOM event listener abstraction layer
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith * @module event
abf34f3b3d65331ec654c9980a6d60d7f9701bfeLuke Smith yready = function() {
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?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * <p>The callback is a Event.Custom, so the signature is:</p>
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * <p>type <string>, args <array>, customobject <object></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 * @event domready
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
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * Use domready event instead. @see domready
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @event event:ready
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @deprecated use 'domready' instead
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith 'event:ready': {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith on: function() {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith detach: function() {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // console.log('DOMReady already fired', 'info', 'event');
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // console.log('setting up before listener', 'info', 'event');
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // console.log('env: ' + YUI.Env.windowLoaded, 'info', 'event');
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith(function() {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Custom event engine, DOM event listener abstraction layer, synthetic DOM
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @module event
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
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @TODO constants? LEFTBUTTON, MIDDLEBUTTON, RIGHTBUTTON, keys
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smithvar whitelist = {
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,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "view" : 1,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // "which" : 1, // we supply
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // "width" : 1, // needed?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * webkit key remapping required for Safari < 3.1
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @property webkitKeymap
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 * 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
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method resolve
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith resolve = function(n) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return null;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith } catch(ex) { }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith// provide a single event with browser abstractions resolved
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith// include all properties for both browers?
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith// include only DOM2 spec properties?
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith// provide browser-specific facade?
b32d148b0052e1f874e9bc9803bef729bf859d97Luke SmithY.DOMEventFacade = function(ev, currentTarget, wrapper) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var e = ev, ot = currentTarget, d = Y.config.doc, b = d.body,
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith //////////////////////////////////////////////////////
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (!x && 0 !== x) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith x += Math.max(d.documentElement.scrollLeft, b.scrollLeft);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith y += Math.max(d.documentElement.scrollTop, b.scrollTop);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * The X location of the event on the page (including scroll)
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @property pageX
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * The Y location of the event on the page (including scroll)
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property pageY
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith //////////////////////////////////////////////////////
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * The keyCode for key events. Uses charCode if keyCode is not available
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @property keyCode
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * The charCode for key events. Same as keyCode
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property charCode
16479f9c396537f36d0d9c5633b24df618eee1e6Luke Smith //////////////////////////////////////////////////////
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * The button that was pushed.
16479f9c396537f36d0d9c5633b24df618eee1e6Luke Smith * @property button
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * The button that was pushed. Same as button.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property which
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith //////////////////////////////////////////////////////
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Node reference for the targeted element
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @propery target
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @type Node
970d3605073835415b62245302838340763d086fLuke Smith * Node reference for the element that the listener was attached to.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @propery currentTarget
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @type Node
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * Node reference to the relatedTarget
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @propery relatedTarget
3cefe5340bfed84038e5816dbfbe7cec44f0df31Luke Smith * @type Node
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith //////////////////////////////////////////////////////
5800f4bbed853824dd8797caae32f78bcc9ef09bLuke Smith * Stops the propagation to the next bubble target
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method stopPropagation
0b911fcfd6406910a95696c8d583a698e6304e5fSatyen Desai this.stopPropagation = function() {
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 this.stopImmediatePropagation = function() {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * Prevents the event's default behavior
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method preventDefault
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith this.preventDefault = function() {
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(function() {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * DOM event listener abstraction layer
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @module event
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.
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke Smith * @class Event
8652e2d20f8ea9c9bfe21217ba427cf0d85ada9aLuke SmithonLoad = function() {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke SmithonUnload = function() {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke SmithshouldIterate = function(o) {
c36390194c43d30adc1059a74fdf9f51a235b8a9Luke Smith // if (o instanceof Y.Node) {
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith // o.tagName ="adsf";
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith return ( o && // o is something
abdfe7cf11d34f89f17b26e4779bf6079d22a910Adam Moore // o.length && // o is indexed
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith (o.length && ((!o.size) || (o.size() > 1))) && // o is indexed
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith return false;
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke SmithEvent = function() {
4db02d4b38afe802ad625f6c389e106fdc7c26faLuke Smith * True after the onload event has fired
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property _loadComplete
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @type boolean
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 * onAvailable listeners
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property _avail
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
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
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
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * The poll interval in milliseconds
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property POLL_INTERVAL
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 * @property lastError
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @type Error
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * poll handle
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @property _interval
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * document readystate poll handle
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * @property _dri
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * True when the document is initially usable
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @property DOMReady
35eeab4975a1c9e62cf466455a59e80ba5e00fb0Luke Smith * @type boolean
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @method startInterval
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke SmithE._interval = setInterval(Y.bind(E._poll, E), E.POLL_INTERVAL);
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 * <p>The callback is executed with a single parameter:
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * the custom object parameter, if provided.</p>
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @method onAvailable
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 * @deprecated Use Y.on("available")
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // @TODO fix arguments
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith onAvailable: function(id, fn, p_obj, p_override, checkContent, compat) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith var a = Y.Array(id), i;
08299549a01ea998d0fc3bfbf97c5ff4d351f543Luke Smith // We want the first test to be immediate, but async
08299549a01ea998d0fc3bfbf97c5ff4d351f543Luke Smith return new Y.EventHandle(); // @TODO by id needs a defered handle
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 * <p>The callback is executed with a single parameter:
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * the custom object parameter, if provided.</p>
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * @method onContentReady
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 * @deprecated Use Y.on("contentready")
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);
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * Appends an event handler
ed51daad3a3ab15255ca10edef69d5f703d69adbLuke Smith * @method attach
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 return Y.Event._attach(Y.Array(arguments, 0, true));
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith _createWrapper: function (el, type, capture, compat, facade) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith if (false === facade) {
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // create CE wrapper
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith //silent: true,
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith // host: this,
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith contextFn: function() {
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith cewrapper.nodeRef = cewrapper.nodeRef || Y.get(cewrapper.el);
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith // for later removeListener calls
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith cewrapper.fire(Y.Event.getEvent(e, el, (compat || (false === facade))));
6ee0f2f556dcd1d88784ad6f12771d36759017a1Luke Smith // window load happens once
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith if (trimmedArgs[trimmedArgs.length-1] === COMPAT_ARG) {
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith// throw new TypeError(type + " attach call failed, callback undefined");
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith return false;
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // The el argument can be an array of elements or element ids.
ee2325dc6d26d8f385007b70eef20ba8890138e8Satyen Desai return (handles.length === 1) ? handles[0] : handles;
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 // @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);
970d3605073835415b62245302838340763d086fLuke Smith // HTMLElement
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // Not found = defer adding the event until the element is available
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith }, E, true, false, compat);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // Element should be an html element or an array if we get here.
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith return false;
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // the custom event key is the uid for the element + type
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).
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith cewrapper = this._createWrapper(el, type, capture, compat, facade);
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // if the load is complete, fire immediately.
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // all subscribers, including the current one
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // will be notified.
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // switched from obj to trimmedArgs[2] to deal with appened compat param
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // context = trimmedArgs[2] || ((compat) ? el : Y.get(el));
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // set the context as the second arg to subscribe
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // remove the 'obj' param
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith // set context to the Node if not specified
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith ret = cewrapper.subscribe.apply(cewrapper, trimmedArgs);
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 * @method detach
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 var args=Y.Array(arguments, 0, true), compat, i, len, ok,
970d3605073835415b62245302838340763d086fLuke Smith // args.pop();
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith // The el argument can be a string
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 // The el argument can be an array of elements or element ids.
d463aba91a07cde67f9ec9382bce464db5bcc9d3Luke Smith ok = ( Y.Event.detach.apply(Y.Event, args) && ok );
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith return false;
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 new Y.DOMEventFacade(ev, el, _wrappers['event:' + Y.stamp(el) + e.type]);
b32d148b0052e1f874e9bc9803bef729bf859d97Luke Smith * Generates an unique ID for the element if it does not already
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 * 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 * hook up any deferred listeners
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method _load
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith _load: function(e) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // Just in case DOMReady did not go off for some reason
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // E._ready();
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 * Polling function that runs before the onload event fires,
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith * attempting to attach to DOM Nodes as soon as they are
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method _poll
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith _poll: function() {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // Hold off if DOMReady has not fired and check current
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // readyState to protect against the IE operation aborted
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith this.locked = true;
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 // onAvailable
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith if (ov === true) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith item.fn.apply(context, (Y.Lang.isArray(ov)) ? ov : []);
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // onAvailable
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 // onContentReady
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 // 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 _retryCount = (notAvail.length === 0) ? 0 : _retryCount - 1;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // we may need to strip the nulled out items here
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.locked = false;
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 // 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 for (i=0,len=oEl.childNodes.length; i<len ; ++i) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith this.purgeElement(oEl.childNodes[i], recurse, type);
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)
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var ek = Y.stamp(el, true), evts = _el_events[ek],
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith results=[] , key = (type) ? 'event:' + ek + type : null;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith return null;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Removes all listeners registered by pe.event. Called
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * automatically during the unload event.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @method _unload
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith _unload: function(e) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Adds a DOM event directly without the caching, cleanup, context adj, etc
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 * Basic remove listener
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// Process onAvailable/onContentReady items when when the DOM is ready in IE
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * Executes the callback as soon as the specified element
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * is detected in the DOM.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith * @event available
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 * 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 * @event contentready
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(function() {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith NOOP = function(){},
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 var el = (Y.Lang.isString(o)) ? Y.Selector.query(o, null, true) : o,
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith Y.Event._attach([type, NOOP, p], CAPTURE_CONFIG);
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 * 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 * @event focus
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 * 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 * @event blur
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 * @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 // key event type: 'down', 'up', or 'press'
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // list of key codes optionally followed by modifiers
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith criteria = (parsed[1]) ? parsed[1].split(/,|\+/) : null;
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 // subscribe spec validator to the DOM event
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var passed = false, failed = false, i, crit, critInt;
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // pass this section if any supplied keyCode
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 // fire spec custom event if spec if met
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // subscribe supplied listener to custom event for spec validator
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith // remove element and spec.
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith(function() {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith resolveTextNode = function(n) {
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith } catch(e) { }
cde38d5b8e9eb36871c8ed5d690424e27d7c0923Luke Smith var target = resolveTextNode((e.target || e.srcElement)),
ev,
if (!ev) {
if (!spec) {
var detachHandle,
handler = function(e) {
if (timerHandle) {
if (!detachHandle) {
if (spec) {
var bReturnVal;
bReturnVal = true;
return bReturnVal;
eventConfig = {
}, el);
if (sSelector) {