event-dom.js.html revision 4fcbec6145d16637205990699912fb90f6a3807c
<html>
<head>
</head>
<body id="yahoo-com">
<div id="doc3" class="yui-t2">
<div id="hd">
<h3>event <span class="subtitle">3.00PR1</span></h3>
<p>
> event-dom.js (source view)
</p>
</div>
<div id="bd">
<div id="yui-main">
<div class="yui-b">
<form name="yui-classopts-form">
<span id="classopts"><input type="checkbox" name="showprivate" id="showprivate" /> <label for="showprivate">Show Private</label></span>
<span id="classopts"><input type="checkbox" name="showprotected" id="showprotected" /> <label for="showprotected">Show Protected</label></span>
<span id="classopts"><input type="checkbox" name="showdeprecated" id="showdeprecated" /> <label for="showdeprecated">Show Deprecated</label></span>
</form>
<div id="srcout">
<style>
#doc3 #classopts { display:none; }
</style>
<div class="highlight" ><pre><span class="c">/*</span>
<span class="c"> * The YUI DOM event system</span>
<span class="c"> * @submodule event-dom</span>
<span class="c"> * @module event</span>
<span class="c"> */</span>
<span class="nx">YUI</span><span class="o">.</span><span class="nx">add</span><span class="o">(</span><span class="s2">"event-dom"</span><span class="o">,</span> <span class="k">function</span><span class="o">(</span><span class="nx">Y</span><span class="o">)</span> <span class="o">{</span>
<span class="c">/*</span>
<span class="c"> * The Event Utility provides utilities for managing DOM Events and tools</span>
<span class="c"> * for building event systems</span>
<span class="c"> *</span>
<span class="c"> * @module event</span>
<span class="c"> * @title Event Utility</span>
<span class="c"> */</span>
<span class="c">/**</span>
<span class="c"> * The event utility provides functions to add and remove event listeners,</span>
<span class="c"> * event cleansing. It also tries to automatically remove listeners it</span>
<span class="c"> * registers during the unload event.</span>
<span class="c"> *</span>
<span class="c"> * @class Event</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">Event</span> <span class="o">=</span> <span class="k">function</span><span class="o">()</span> <span class="o">{</span>
<span class="c">/**</span>
<span class="c"> * True after the onload event has fired</span>
<span class="c"> * @property loadComplete</span>
<span class="c"> * @type boolean</span>
<span class="c"> * @static</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="k">var</span> <span class="nx">loadComplete</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>
<span class="c">/**</span>
<span class="c"> * increased if additional late-bound handlers are requested after</span>
<span class="c"> * the page load.</span>
<span class="c"> * @property _retryCount</span>
<span class="c"> * @static</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="k">var</span> <span class="nx">_retryCount</span> <span class="o">=</span> <span class="m">0</span><span class="o">;</span>
<span class="c">/**</span>
<span class="c"> * onAvailable listeners</span>
<span class="c"> * @property _avail</span>
<span class="c"> * @static</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="k">var</span> <span class="nx">_avail</span> <span class="o">=</span> <span class="o">[];</span>
<span class="c">/**</span>
<span class="c"> * Custom event wrappers for DOM events. Key is </span>
<span class="c"> * 'event:' + Element uid stamp + event type</span>
<span class="c"> * @property _wrappers</span>
<span class="c"> * @static</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="k">var</span> <span class="nx">_wrappers</span> <span class="o">=</span> <span class="o">{};</span>
<span class="c">/**</span>
<span class="c"> * Custom event wrapper map DOM events. Key is </span>
<span class="c"> * Element uid stamp. Each item is a hash of custom event</span>
<span class="c"> * wrappers as provided in the _wrappers collection. This</span>
<span class="c"> * provides the infrastructure for getListeners.</span>
<span class="c"> * @property _el_events</span>
<span class="c"> * @static</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="k">var</span> <span class="nx">_el_events</span> <span class="o">=</span> <span class="o">{};</span>
<span class="k">return</span> <span class="o">{</span>
<span class="c">/**</span>
<span class="c"> * The number of times we should look for elements that are not</span>
<span class="c"> * in the DOM at the time the event is requested after the document</span>
<span class="c"> * has been loaded. The default is 2000@amp;20 ms, so it will poll</span>
<span class="c"> * for 40 seconds or until all outstanding handlers are bound</span>
<span class="c"> * (whichever comes first).</span>
<span class="c"> * @property POLL_RETRYS</span>
<span class="c"> * @type int</span>
<span class="c"> * @static</span>
<span class="c"> * @final</span>
<span class="c"> */</span>
<span class="nx">POLL_RETRYS</span><span class="o">:</span> <span class="m">2000</span><span class="o">,</span>
<span class="c">/**</span>
<span class="c"> * The poll interval in milliseconds</span>
<span class="c"> * @property POLL_INTERVAL</span>
<span class="c"> * @type int</span>
<span class="c"> * @static</span>
<span class="c"> * @final</span>
<span class="c"> */</span>
<span class="nx">POLL_INTERVAL</span><span class="o">:</span> <span class="m">20</span><span class="o">,</span>
<span class="c">/**</span>
<span class="c"> * These errors are suppressed, the method returns false, and this property</span>
<span class="c"> * is set</span>
<span class="c"> * @property lastError</span>
<span class="c"> * @static</span>
<span class="c"> * @type Error</span>
<span class="c"> */</span>
<span class="nx">lastError</span><span class="o">:</span> <span class="kc">null</span><span class="o">,</span>
<span class="c">/**</span>
<span class="c"> * poll handle</span>
<span class="c"> * @property _interval</span>
<span class="c"> * @static</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="nx">_interval</span><span class="o">:</span> <span class="kc">null</span><span class="o">,</span>
<span class="c">/**</span>
<span class="c"> * document readystate poll handle</span>
<span class="c"> * @property _dri</span>
<span class="c"> * @static</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="nx">_dri</span><span class="o">:</span> <span class="kc">null</span><span class="o">,</span>
<span class="c">/**</span>
<span class="c"> * True when the document is initially usable</span>
<span class="c"> * @property DOMReady</span>
<span class="c"> * @type boolean</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">DOMReady</span><span class="o">:</span> <span class="kc">false</span><span class="o">,</span>
<span class="c">/**</span>
<span class="c"> * @method startInterval</span>
<span class="c"> * @static</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="nx">startInterval</span><span class="o">:</span> <span class="k">function</span><span class="o">()</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(!</span><span class="k">this</span><span class="o">.</span><span class="nx">_interval</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="o">;</span>
<span class="k">var</span> <span class="nx">callback</span> <span class="o">=</span> <span class="k">function</span><span class="o">()</span> <span class="o">{</span> <span class="nx">self</span><span class="o">.</span><span class="nx">_tryPreloadAttach</span><span class="o">();</span> <span class="o">};</span>
<span class="k">this</span><span class="o">.</span><span class="nx">_interval</span> <span class="o">=</span> <span class="nx">setInterval</span><span class="o">(</span><span class="nx">callback</span><span class="o">,</span> <span class="k">this</span><span class="o">.</span><span class="nx">POLL_INTERVAL</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Executes the supplied callback when the item with the supplied</span>
<span class="c"> * id is found. This is meant to be used to execute behavior as</span>
<span class="c"> * soon as possible as the page loads. If you use this after the</span>
<span class="c"> * initial page load it will poll for a fixed time for the element.</span>
<span class="c"> * The number of times it will poll and the frequency are</span>
<span class="c"> * configurable. By default it will poll for 10 seconds.</span>
<span class="c"> *</span>
<span class="c"> * <p>The callback is executed with a single parameter:</span>
<span class="c"> * the custom object parameter, if provided.</p></span>
<span class="c"> *</span>
<span class="c"> * @method onAvailable</span>
<span class="c"> *</span>
<span class="c"> * @param {string||string[]} id the id of the element, or an array</span>
<span class="c"> * of ids to look for.</span>
<span class="c"> * @param {function} fn what to execute when the element is found.</span>
<span class="c"> * @param {object} p_obj an optional object to be passed back as</span>
<span class="c"> * a parameter to fn.</span>
<span class="c"> * @param {boolean|object} p_override If set to true, fn will execute</span>
<span class="c"> * in the context of p_obj, if set to an object it</span>
<span class="c"> * will execute in the context of that object</span>
<span class="c"> * @param checkContent {boolean} check child node readiness (onContentReady)</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="c">// @TODO fix arguments</span>
<span class="c"></span> <span class="nx">onAvailable</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">id</span><span class="o">,</span> <span class="nx">fn</span><span class="o">,</span> <span class="nx">p_obj</span><span class="o">,</span> <span class="nx">p_override</span><span class="o">,</span> <span class="nx">checkContent</span><span class="o">)</span> <span class="o">{</span>
<span class="c"></span> <span class="k">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">id</span><span class="o">);</span>
<span class="k">for</span> <span class="o">(</span><span class="k">var</span> <span class="nx">i</span><span class="o">=</span><span class="m">0</span><span class="o">;</span> <span class="nx">i</span><span class="o"><</span><span class="nx">a</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span><span class="o">=</span><span class="nx">i</span><span class="o">+</span><span class="m">1</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">_avail</span><span class="o">.</span><span class="nx">push</span><span class="o">({</span> <span class="nx">id</span><span class="o">:</span> <span class="nx">a</span><span class="o">[</span><span class="nx">i</span><span class="o">],</span>
<span class="nx">fn</span><span class="o">:</span> <span class="nx">fn</span><span class="o">,</span>
<span class="nx">obj</span><span class="o">:</span> <span class="nx">p_obj</span><span class="o">,</span>
<span class="nx">override</span><span class="o">:</span> <span class="nx">p_override</span><span class="o">,</span>
<span class="nx">checkReady</span><span class="o">:</span> <span class="nx">checkContent</span> <span class="o">});</span>
<span class="o">}</span>
<span class="nx">_retryCount</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">POLL_RETRYS</span><span class="o">;</span>
<span class="k">this</span><span class="o">.</span><span class="nx">startInterval</span><span class="o">();</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Works the same way as onAvailable, but additionally checks the</span>
<span class="c"> * state of sibling elements to determine if the content of the</span>
<span class="c"> * available element is safe to modify.</span>
<span class="c"> *</span>
<span class="c"> * <p>The callback is executed with a single parameter:</span>
<span class="c"> * the custom object parameter, if provided.</p></span>
<span class="c"> *</span>
<span class="c"> * @method onContentReady</span>
<span class="c"> *</span>
<span class="c"> * @param {string} id the id of the element to look for.</span>
<span class="c"> * @param {function} fn what to execute when the element is ready.</span>
<span class="c"> * @param {object} p_obj an optional object to be passed back as</span>
<span class="c"> * a parameter to fn.</span>
<span class="c"> * @param {boolean|object} p_override If set to true, fn will execute</span>
<span class="c"> * in the context of p_obj. If an object, fn will</span>
<span class="c"> * exectute in the context of that object</span>
<span class="c"> *</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="c">// @TODO fix arguments</span>
<span class="c"></span> <span class="nx">onContentReady</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">id</span><span class="o">,</span> <span class="nx">fn</span><span class="o">,</span> <span class="nx">p_obj</span><span class="o">,</span> <span class="nx">p_override</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="nx">onAvailable</span><span class="o">(</span><span class="nx">id</span><span class="o">,</span> <span class="nx">fn</span><span class="o">,</span> <span class="nx">p_obj</span><span class="o">,</span> <span class="nx">p_override</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Executes the supplied callback when the DOM is first usable. This</span>
<span class="c"> * will execute immediately if called after the DOMReady event has</span>
<span class="c"> * fired. @todo the DOMContentReady event does not fire when the</span>
<span class="c"> * script is dynamically injected into the page. This means the</span>
<span class="c"> * DOMReady custom event will never fire in FireFox or Opera when the</span>
<span class="c"> * library is injected. It _will_ fire in Safari, and the IE </span>
<span class="c"> * implementation would allow for us to fire it if the defered script</span>
<span class="c"> * is not available. We want this to behave the same in all browsers.</span>
<span class="c"> * Is there a way to identify when the script has been injected </span>
<span class="c"> * instead of included inline? Is there a way to know whether the </span>
<span class="c"> * window onload event has fired without having had a listener attached </span>
<span class="c"> * to it when it did so?</span>
<span class="c"> *</span>
<span class="c"> * <p>type &lt;string&gt;, args &lt;array&gt;, customobject &lt;object&gt;</p></span>
<span class="c"> * <p>For DOMReady events, there are no fire argments, so the</span>
<span class="c"> * signature is:</p></span>
<span class="c"> * <p>"DOMReady", [], obj</p></span>
<span class="c"> *</span>
<span class="c"> *</span>
<span class="c"> * @method onDOMReady</span>
<span class="c"> *</span>
<span class="c"> * @param {function} fn what to execute when the element is found.</span>
<span class="c"> * @optional context execution context</span>
<span class="c"> *</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">onDOMReady</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">fn</span><span class="o">)</span> <span class="o">{</span>
<span class="c"></span> <span class="k">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">arguments</span><span class="o">,</span> <span class="m">0</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
<span class="nx">a</span><span class="o">.</span><span class="nx">unshift</span><span class="o">(</span><span class="s1">'event:ready'</span><span class="o">);</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">on</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">Y</span><span class="o">,</span> <span class="nx">a</span><span class="o">);</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Appends an event handler</span>
<span class="c"> *</span>
<span class="c"> * @method addListener</span>
<span class="c"> *</span>
<span class="c"> * @param {String|HTMLElement|Array|NodeList} el An id, an element </span>
<span class="c"> * listener to.</span>
<span class="c"> * @param {String} type The type of event to append</span>
<span class="c"> * @param {Function} fn The method the event invokes</span>
<span class="c"> * @param {Object} obj An arbitrary object that will be </span>
<span class="c"> * passed as a parameter to the handler</span>
<span class="c"> * @return {Boolean} True if the action was successful or defered,</span>
<span class="c"> * false if one or more of the elements </span>
<span class="c"> * could not have the listener attached,</span>
<span class="c"> * or if the operation throws an exception.</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">addListener</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">el</span><span class="o">,</span> <span class="nx">type</span><span class="o">,</span> <span class="nx">fn</span><span class="o">,</span> <span class="nx">obj</span><span class="o">)</span> <span class="o">{</span>
<span class="c">// Y.log('addListener: ' + Y.Lang.dump(Y.Array(arguments, 0, true), 1));</span>
<span class="c"></span>
<span class="k">var</span> <span class="nx">a</span><span class="o">=</span><span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">arguments</span><span class="o">,</span> <span class="m">1</span><span class="o">,</span> <span class="kc">true</span><span class="o">),</span> <span class="nx">override</span> <span class="o">=</span> <span class="nx">a</span><span class="o">[</span><span class="m">3</span><span class="o">],</span> <span class="nx">E</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Event</span><span class="o">,</span>
<span class="nx">aa</span><span class="o">=</span><span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">arguments</span><span class="o">,</span> <span class="m">0</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
<span class="k">if</span> <span class="o">(!</span><span class="nx">fn</span> <span class="o">||</span> <span class="o">!</span><span class="nx">fn</span><span class="o">.</span><span class="nx">call</span><span class="o">)</span> <span class="o">{</span>
<span class="c">// throw new TypeError(type + " addListener call failed, callback undefined");</span>
<span class="c"></span> <span class="nx">Y</span><span class="o">.</span><span class="nx">log</span><span class="o">(</span><span class="nx">type</span> <span class="o">+</span> <span class="s2">" addListener call failed, invalid callback"</span><span class="o">,</span> <span class="s2">"error"</span><span class="o">,</span> <span class="s2">"Event"</span><span class="o">);</span>
<span class="k">return</span> <span class="kc">false</span><span class="o">;</span>
<span class="o">}</span>
<span class="c">// The el argument can be an array of elements or element ids.</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">_isValidCollection</span><span class="o">(</span><span class="nx">el</span><span class="o">))</span> <span class="o">{</span>
<span class="c"></span> <span class="c">// Y.log('collection: ' + el.item(0) + ', ' + el.item(1));</span>
<span class="c"></span>
<span class="k">var</span> <span class="nx">handles</span><span class="o">=[],</span> <span class="nx">h</span><span class="o">,</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">l</span><span class="o">,</span> <span class="nx">proc</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">v</span><span class="o">,</span> <span class="nx">k</span><span class="o">)</span> <span class="o">{</span>
<span class="c"></span>
<span class="k">var</span> <span class="nx">b</span> <span class="o">=</span> <span class="nx">a</span><span class="o">.</span><span class="nx">slice</span><span class="o">();</span>
<span class="nx">b</span><span class="o">.</span><span class="nx">unshift</span><span class="o">(</span><span class="nx">v</span><span class="o">);</span>
<span class="nx">h</span> <span class="o">=</span> <span class="nx">E</span><span class="o">.</span><span class="nx">addListener</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">E</span><span class="o">,</span> <span class="nx">b</span><span class="o">);</span>
<span class="nx">handles</span><span class="o">.</span><span class="nx">push</span><span class="o">(</span><span class="nx">h</span><span class="o">);</span>
<span class="o">};</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">each</span><span class="o">(</span><span class="nx">el</span><span class="o">,</span> <span class="nx">proc</span><span class="o">,</span> <span class="nx">E</span><span class="o">);</span>
<span class="k">return</span> <span class="nx">handles</span><span class="o">;</span>
<span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="nx">Y</span><span class="o">.</span><span class="nx">Lang</span><span class="o">.</span><span class="nx">isString</span><span class="o">(</span><span class="nx">el</span><span class="o">))</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">oEl</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">all</span><span class="o">(</span><span class="nx">el</span><span class="o">);</span>
<span class="c">// If the el argument is a string, we assume it is </span>
<span class="c"></span> <span class="c">// actually the id of the element. If the page is loaded</span>
<span class="c"></span> <span class="c">// we convert el to the actual element, otherwise we </span>
<span class="c"></span> <span class="c">// defer attaching the event until onload event fires</span>
<span class="c"></span>
<span class="c">// check to see if we need to delay hooking up the event </span>
<span class="c"></span> <span class="c">// until after the page loads.</span>
<span class="c"></span> <span class="k">var</span> <span class="nx">size</span> <span class="o">=</span> <span class="nx">oEl</span><span class="o">.</span><span class="nx">size</span><span class="o">();</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">size</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">size</span> <span class="o">></span> <span class="m">1</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">aa</span><span class="o">[</span><span class="m">0</span><span class="o">]</span> <span class="o">=</span> <span class="nx">oEl</span><span class="o">;</span>
<span class="k">return</span> <span class="nx">E</span><span class="o">.</span><span class="nx">addListener</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">E</span><span class="o">,</span> <span class="nx">aa</span><span class="o">);</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="nx">el</span> <span class="o">=</span> <span class="nx">oEl</span><span class="o">.</span><span class="nx">item</span><span class="o">(</span><span class="m">0</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="c">//</span>
<span class="c"></span> <span class="c">// defer adding the event until the element is available</span>
<span class="c"></span> <span class="k">this</span><span class="o">.</span><span class="nx">onAvailable</span><span class="o">(</span><span class="nx">el</span><span class="o">,</span> <span class="k">function</span><span class="o">()</span> <span class="o">{</span>
<span class="c"></span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Event</span><span class="o">.</span><span class="nx">addListener</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">Y</span><span class="o">.</span><span class="nx">Event</span><span class="o">,</span> <span class="nx">aa</span><span class="o">);</span>
<span class="o">});</span>
<span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c">// Element should be an html element or an array if we get </span>
<span class="c"></span> <span class="c">// here.</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(!</span><span class="nx">el</span><span class="o">)</span> <span class="o">{</span>
<span class="c"></span> <span class="k">return</span> <span class="kc">false</span><span class="o">;</span>
<span class="o">}</span>
<span class="c">// the custom event key is the uid for the element + type</span>
<span class="c"></span>
<span class="k">var</span> <span class="nx">ek</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">stamp</span><span class="o">(</span><span class="nx">el</span><span class="o">),</span> <span class="nx">key</span> <span class="o">=</span> <span class="s1">'event:'</span> <span class="o">+</span> <span class="nx">ek</span> <span class="o">+</span> <span class="nx">type</span><span class="o">,</span>
<span class="nx">cewrapper</span> <span class="o">=</span> <span class="nx">_wrappers</span><span class="o">[</span><span class="nx">key</span><span class="o">];</span>
<span class="k">if</span> <span class="o">(!</span><span class="nx">cewrapper</span><span class="o">)</span> <span class="o">{</span>
<span class="c">// create CE wrapper</span>
<span class="c"></span> <span class="nx">cewrapper</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">publish</span><span class="o">(</span><span class="nx">key</span><span class="o">,</span> <span class="o">{</span>
<span class="nx">silent</span><span class="o">:</span> <span class="kc">true</span><span class="o">,</span>
<span class="c">// host: this,</span>
<span class="c"></span> <span class="nx">bubbles</span><span class="o">:</span> <span class="kc">false</span>
<span class="o">});</span>
<span class="c">// cache the dom event details in the custom event</span>
<span class="c"></span> <span class="c">// for later removeListener calls</span>
<span class="c"></span> <span class="nx">cewrapper</span><span class="o">.</span><span class="nx">el</span> <span class="o">=</span> <span class="nx">el</span><span class="o">;</span>
<span class="nx">cewrapper</span><span class="o">.</span><span class="nx">type</span> <span class="o">=</span> <span class="nx">type</span><span class="o">;</span>
<span class="nx">cewrapper</span><span class="o">.</span><span class="nx">fn</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">e</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">cewrapper</span><span class="o">.</span><span class="nx">fire</span><span class="o">(</span><span class="nx">Y</span><span class="o">.</span><span class="nx">Event</span><span class="o">.</span><span class="nx">getEvent</span><span class="o">(</span><span class="nx">e</span><span class="o">,</span> <span class="nx">el</span><span class="o">));</span>
<span class="o">};</span>
<span class="nx">_wrappers</span><span class="o">[</span><span class="nx">key</span><span class="o">]</span> <span class="o">=</span> <span class="nx">cewrapper</span><span class="o">;</span>
<span class="nx">_el_events</span><span class="o">[</span><span class="nx">ek</span><span class="o">]</span> <span class="o">=</span> <span class="nx">_el_events</span><span class="o">[</span><span class="nx">ek</span><span class="o">]</span> <span class="o">||</span> <span class="o">{};</span>
<span class="nx">_el_events</span><span class="o">[</span><span class="nx">ek</span><span class="o">][</span><span class="nx">key</span><span class="o">]</span> <span class="o">=</span> <span class="nx">cewrapper</span><span class="o">;</span>
<span class="c"></span> <span class="c">// attach a listener that fires the custom event</span>
<span class="c"></span> <span class="k">this</span><span class="o">.</span><span class="nx">nativeAdd</span><span class="o">(</span><span class="nx">el</span><span class="o">,</span> <span class="nx">type</span><span class="o">,</span> <span class="nx">cewrapper</span><span class="o">.</span><span class="nx">fn</span><span class="o">,</span> <span class="kc">false</span><span class="o">);</span>
<span class="o">}</span>
<span class="c">// from type, fn, etc to fn, obj, override</span>
<span class="c"></span> <span class="nx">a</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">arguments</span><span class="o">,</span> <span class="m">2</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
<span class="c"></span>
<span class="k">var</span> <span class="nx">context</span> <span class="o">=</span> <span class="nx">obj</span> <span class="o">||</span> <span class="nx">el</span><span class="o">;</span>
<span class="c">// if (override) {</span>
<span class="c"></span> <span class="c">// if (override === true) {</span>
<span class="c"></span> <span class="c">// context = obj;</span>
<span class="c"></span> <span class="c">// } else {</span>
<span class="c"></span> <span class="c">// context = override;</span>
<span class="c"></span> <span class="c">// }</span>
<span class="c"></span> <span class="c">// }</span>
<span class="c"></span>
<span class="nx">a</span><span class="o">[</span><span class="m">1</span><span class="o">]</span> <span class="o">=</span> <span class="nx">context</span><span class="o">;</span>
<span class="c">// set context to element if not specified</span>
<span class="c"></span> <span class="k">return</span> <span class="nx">cewrapper</span><span class="o">.</span><span class="nx">subscribe</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">cewrapper</span><span class="o">,</span> <span class="nx">a</span><span class="o">);</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Removes an event listener</span>
<span class="c"> *</span>
<span class="c"> * @method removeListener</span>
<span class="c"> *</span>
<span class="c"> * @param {String|HTMLElement|Array|NodeList} el An id, an element </span>
<span class="c"> * the listener from.</span>
<span class="c"> * @param {String} type the type of event to remove.</span>
<span class="c"> * @param {Function} fn the method the event invokes. If fn is</span>
<span class="c"> * undefined, then all event handlers for the type of event are * removed.</span>
<span class="c"> * @return {boolean} true if the unbind was successful, false * otherwise.</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">removeListener</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">el</span><span class="o">,</span> <span class="nx">type</span><span class="o">,</span> <span class="nx">fn</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">el</span> <span class="o">&&</span> <span class="nx">el</span><span class="o">.</span><span class="nx">detach</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="nx">el</span><span class="o">.</span><span class="nx">detach</span><span class="o">();</span>
<span class="o">}</span>
<span class="k">var</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">len</span><span class="o">,</span> <span class="nx">li</span><span class="o">;</span>
<span class="c">// The el argument can be a string</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="k">typeof</span> <span class="nx">el</span> <span class="o">==</span> <span class="s2">"string"</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">el</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">get</span><span class="o">(</span><span class="nx">el</span><span class="o">);</span>
<span class="c">// The el argument can be an array of elements or element ids.</span>
<span class="c"></span> <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span> <span class="k">this</span><span class="o">.</span><span class="nx">_isValidCollection</span><span class="o">(</span><span class="nx">el</span><span class="o">))</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">ok</span> <span class="o">=</span> <span class="kc">true</span><span class="o">;</span>
<span class="k">for</span> <span class="o">(</span><span class="nx">i</span><span class="o">=</span><span class="m">0</span><span class="o">,</span><span class="nx">len</span><span class="o">=</span><span class="nx">el</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span><span class="o"><</span><span class="nx">len</span><span class="o">;</span> <span class="o">++</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">ok</span> <span class="o">=</span> <span class="o">(</span> <span class="k">this</span><span class="o">.</span><span class="nx">removeListener</span><span class="o">(</span><span class="nx">el</span><span class="o">[</span><span class="nx">i</span><span class="o">],</span> <span class="nx">type</span><span class="o">,</span> <span class="nx">fn</span><span class="o">)</span> <span class="o">&&</span> <span class="nx">ok</span> <span class="o">);</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">ok</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">if</span> <span class="o">(!</span><span class="nx">fn</span> <span class="o">||</span> <span class="o">!</span><span class="nx">fn</span><span class="o">.</span><span class="nx">call</span><span class="o">)</span> <span class="o">{</span>
<span class="c"></span> <span class="c">//return false;</span>
<span class="c"></span> <span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="nx">purgeElement</span><span class="o">(</span><span class="nx">el</span><span class="o">,</span> <span class="kc">false</span><span class="o">,</span> <span class="nx">type</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">var</span> <span class="nx">id</span> <span class="o">=</span> <span class="s1">'event:'</span> <span class="o">+</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">stamp</span><span class="o">(</span><span class="nx">el</span><span class="o">)</span> <span class="o">+</span> <span class="nx">type</span><span class="o">,</span>
<span class="nx">ce</span> <span class="o">=</span> <span class="nx">_wrappers</span><span class="o">[</span><span class="nx">id</span><span class="o">];</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">ce</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="nx">ce</span><span class="o">.</span><span class="nx">unsubscribe</span><span class="o">(</span><span class="nx">fn</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Finds the event in the window object, the caller's arguments, or</span>
<span class="c"> * in the arguments of another method in the callstack. This is</span>
<span class="c"> * executed automatically for events registered through the event</span>
<span class="c"> * manager, so the implementer should not normally need to execute</span>
<span class="c"> * this function at all.</span>
<span class="c"> * @method getEvent</span>
<span class="c"> * @param {Event} e the event parameter from the handler</span>
<span class="c"> * @param {HTMLElement} boundEl the element the listener is attached to</span>
<span class="c"> * @return {Event} the event </span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">getEvent</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">e</span><span class="o">,</span> <span class="nx">boundEl</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">ev</span> <span class="o">=</span> <span class="nx">e</span> <span class="o">||</span> <span class="nb">window</span><span class="o">.</span><span class="nx">event</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(!</span><span class="nx">ev</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">c</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">getEvent</span><span class="o">.</span><span class="nx">caller</span><span class="o">;</span>
<span class="k">while</span> <span class="o">(</span><span class="nx">c</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">ev</span> <span class="o">=</span> <span class="nx">c</span><span class="o">.</span><span class="nx">arguments</span><span class="o">[</span><span class="m">0</span><span class="o">];</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">ev</span> <span class="o">&&</span> <span class="nx">Event</span> <span class="o">==</span> <span class="nx">ev</span><span class="o">.</span><span class="nx">constructor</span><span class="o">)</span> <span class="o">{</span>
<span class="k">break</span><span class="o">;</span>
<span class="o">}</span>
<span class="nx">c</span> <span class="o">=</span> <span class="nx">c</span><span class="o">.</span><span class="nx">caller</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c">// Y.log('wrapper for facade: ' + 'event:' + Y.stamp(boundEl) + e.type);</span>
<span class="c"></span>
<span class="k">return</span> <span class="k">new</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Event</span><span class="o">.</span><span class="nx">Facade</span><span class="o">(</span><span class="nx">ev</span><span class="o">,</span> <span class="nx">boundEl</span><span class="o">,</span> <span class="nx">_wrappers</span><span class="o">[</span><span class="s1">'event:'</span> <span class="o">+</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">stamp</span><span class="o">(</span><span class="nx">boundEl</span><span class="o">)</span> <span class="o">+</span> <span class="nx">e</span><span class="o">.</span><span class="nx">type</span><span class="o">]);</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Generates an unique ID for the element if it does not already </span>
<span class="c"> * have one.</span>
<span class="c"> * @method generateId</span>
<span class="c"> * @param el the element to create the id for</span>
<span class="c"> * @return {string} the resulting id of the element</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">generateId</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">el</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">id</span> <span class="o">=</span> <span class="nx">el</span><span class="o">.</span><span class="nx">id</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(!</span><span class="nx">id</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">id</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">stamp</span><span class="o">(</span><span class="nx">el</span><span class="o">);</span>
<span class="nx">el</span><span class="o">.</span><span class="nx">id</span> <span class="o">=</span> <span class="nx">id</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">id</span><span class="o">;</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * We want to be able to use getElementsByTagName as a collection</span>
<span class="c"> * to attach a group of events to. Unfortunately, different </span>
<span class="c"> * browsers return different types of collections. This function</span>
<span class="c"> * tests to determine if the object is array-like. It will also </span>
<span class="c"> * fail if the object is an array, but is empty.</span>
<span class="c"> * @method _isValidCollection</span>
<span class="c"> * @param o the object to test</span>
<span class="c"> * @return {boolean} true if the object is array-like and populated</span>
<span class="c"> * @static</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="nx">_isValidCollection</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
<span class="k">try</span> <span class="o">{</span>
<span class="k">return</span> <span class="o">(</span> <span class="nx">o</span> <span class="o">&&</span> <span class="c">// o is something</span>
<span class="c"></span> <span class="k">typeof</span> <span class="nx">o</span> <span class="o">!==</span> <span class="s2">"string"</span> <span class="o">&&</span> <span class="c">// o is not a string</span>
<span class="c"></span> <span class="o">(</span><span class="nx">o</span><span class="o">.</span><span class="nx">each</span> <span class="o">||</span> <span class="nx">o</span><span class="o">.</span><span class="nx">length</span><span class="o">)</span> <span class="o">&&</span> <span class="c">// o is indexed</span>
<span class="c"></span> <span class="o">!</span><span class="nx">o</span><span class="o">.</span><span class="nx">tagName</span> <span class="o">&&</span> <span class="c">// o is not an HTML element</span>
<span class="c"></span> <span class="o">!</span><span class="nx">o</span><span class="o">.</span><span class="nx">alert</span> <span class="o">&&</span> <span class="c">// o is not a window</span>
<span class="c"></span> <span class="o">(</span><span class="nx">o</span><span class="o">.</span><span class="nx">item</span> <span class="o">||</span> <span class="k">typeof</span> <span class="nx">o</span><span class="o">[</span><span class="m">0</span><span class="o">]</span> <span class="o">!==</span> <span class="s2">"undefined"</span><span class="o">)</span> <span class="o">);</span>
<span class="o">}</span> <span class="k">catch</span><span class="o">(</span><span class="nx">ex</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">log</span><span class="o">(</span><span class="s2">"collection check failure"</span><span class="o">,</span> <span class="s2">"warn"</span><span class="o">);</span>
<span class="k">return</span> <span class="kc">false</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">},</span>
<span class="c">/*</span>
<span class="c"> * Custom event the fires when the dom is initially usable</span>
<span class="c"> * @event DOMReadyEvent</span>
<span class="c"> */</span>
<span class="c"></span> <span class="c">// DOMReadyEvent: Y.publish("event:ready", this, {</span>
<span class="c"></span> <span class="c">// fireOnce: true</span>
<span class="c"></span> <span class="c">// }),</span>
<span class="c"></span>
<span class="c">/**</span>
<span class="c"> * hook up any deferred listeners</span>
<span class="c"> * @method _load</span>
<span class="c"> * @static</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="nx">_load</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">e</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(!</span><span class="nx">loadComplete</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">loadComplete</span> <span class="o">=</span> <span class="kc">true</span><span class="o">;</span>
<span class="k">var</span> <span class="nx">E</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Event</span><span class="o">;</span>
<span class="c">// Just in case DOMReady did not go off for some reason</span>
<span class="c"></span> <span class="c">// E._ready();</span>
<span class="c"></span> <span class="nx">Y</span><span class="o">.</span><span class="nx">fire</span> <span class="o">&&</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">fire</span><span class="o">(</span><span class="s1">'event:ready'</span><span class="o">);</span>
<span class="c">// Available elements may not have been detected before the</span>
<span class="c"></span> <span class="c">// window load event fires. Try to find them now so that the</span>
<span class="c"></span> <span class="c">// the user is more likely to get the onAvailable notifications</span>
<span class="c"></span> <span class="c">// before the window load notification</span>
<span class="c"></span> <span class="nx">E</span><span class="o">.</span><span class="nx">_tryPreloadAttach</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Polling function that runs before the onload event fires, </span>
<span class="c"> * attempting to attach to DOM Nodes as soon as they are </span>
<span class="c"> * available</span>
<span class="c"> * @method _tryPreloadAttach</span>
<span class="c"> * @static</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="nx">_tryPreloadAttach</span><span class="o">:</span> <span class="k">function</span><span class="o">()</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">locked</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">Y</span><span class="o">.</span><span class="nx">UA</span><span class="o">.</span><span class="nx">ie</span><span class="o">)</span> <span class="o">{</span>
<span class="c">// Hold off if DOMReady has not fired and check current</span>
<span class="c"></span> <span class="c">// readyState to protect against the IE operation aborted</span>
<span class="c"></span> <span class="c">// issue.</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(!</span><span class="k">this</span><span class="o">.</span><span class="nx">DOMReady</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="nx">startInterval</span><span class="o">();</span>
<span class="k">return</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">this</span><span class="o">.</span><span class="nx">locked</span> <span class="o">=</span> <span class="kc">true</span><span class="o">;</span>
<span class="c"></span>
<span class="c">// keep trying until after the page is loaded. We need to </span>
<span class="c"></span> <span class="c">// check the page load state prior to trying to bind the </span>
<span class="c"></span> <span class="c">// elements so that we can be certain all elements have been </span>
<span class="c"></span> <span class="c">// tested appropriately</span>
<span class="c"></span> <span class="k">var</span> <span class="nx">tryAgain</span> <span class="o">=</span> <span class="o">!</span><span class="nx">loadComplete</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(!</span><span class="nx">tryAgain</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">tryAgain</span> <span class="o">=</span> <span class="o">(</span><span class="nx">_retryCount</span> <span class="o">></span> <span class="m">0</span><span class="o">);</span>
<span class="o">}</span>
<span class="c">// onAvailable</span>
<span class="c"></span> <span class="k">var</span> <span class="nx">notAvail</span> <span class="o">=</span> <span class="o">[];</span>
<span class="k">var</span> <span class="nx">executeItem</span> <span class="o">=</span> <span class="k">function</span> <span class="o">(</span><span class="nx">el</span><span class="o">,</span> <span class="nx">item</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">context</span> <span class="o">=</span> <span class="nx">el</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">item</span><span class="o">.</span><span class="nx">override</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">item</span><span class="o">.</span><span class="nx">override</span> <span class="o">===</span> <span class="kc">true</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">context</span> <span class="o">=</span> <span class="nx">item</span><span class="o">.</span><span class="nx">obj</span><span class="o">;</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="nx">context</span> <span class="o">=</span> <span class="nx">item</span><span class="o">.</span><span class="nx">override</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="nx">item</span><span class="o">.</span><span class="nx">fn</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">context</span><span class="o">,</span> <span class="nx">item</span><span class="o">.</span><span class="nx">obj</span><span class="o">);</span>
<span class="o">};</span>
<span class="k">var</span> <span class="nx">i</span><span class="o">,</span><span class="nx">len</span><span class="o">,</span><span class="nx">item</span><span class="o">,</span><span class="nx">el</span><span class="o">;</span>
<span class="c">// onAvailable</span>
<span class="c"></span> <span class="k">for</span> <span class="o">(</span><span class="nx">i</span><span class="o">=</span><span class="m">0</span><span class="o">,</span><span class="nx">len</span><span class="o">=</span><span class="nx">_avail</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span><span class="o"><</span><span class="nx">len</span><span class="o">;</span> <span class="o">++</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">item</span> <span class="o">=</span> <span class="nx">_avail</span><span class="o">[</span><span class="nx">i</span><span class="o">];</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">item</span> <span class="o">&&</span> <span class="o">!</span><span class="nx">item</span><span class="o">.</span><span class="nx">checkReady</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">el</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">get</span><span class="o">(</span><span class="nx">item</span><span class="o">.</span><span class="nx">id</span><span class="o">);</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">el</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">executeItem</span><span class="o">(</span><span class="nx">el</span><span class="o">,</span> <span class="nx">item</span><span class="o">);</span>
<span class="nx">_avail</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="nx">notAvail</span><span class="o">.</span><span class="nx">push</span><span class="o">(</span><span class="nx">item</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c">// onContentReady</span>
<span class="c"></span> <span class="k">for</span> <span class="o">(</span><span class="nx">i</span><span class="o">=</span><span class="m">0</span><span class="o">,</span><span class="nx">len</span><span class="o">=</span><span class="nx">_avail</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span><span class="o"><</span><span class="nx">len</span><span class="o">;</span> <span class="o">++</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">item</span> <span class="o">=</span> <span class="nx">_avail</span><span class="o">[</span><span class="nx">i</span><span class="o">];</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">item</span> <span class="o">&&</span> <span class="nx">item</span><span class="o">.</span><span class="nx">checkReady</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">el</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">get</span><span class="o">(</span><span class="nx">item</span><span class="o">.</span><span class="nx">id</span><span class="o">);</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">el</span><span class="o">)</span> <span class="o">{</span>
<span class="c">// The element is available, but not necessarily ready</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">loadComplete</span> <span class="o">||</span> <span class="nx">el</span><span class="o">.</span><span class="nx">nextSibling</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">executeItem</span><span class="o">(</span><span class="nx">el</span><span class="o">,</span> <span class="nx">item</span><span class="o">);</span>
<span class="nx">_avail</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="nx">notAvail</span><span class="o">.</span><span class="nx">push</span><span class="o">(</span><span class="nx">item</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="nx">_retryCount</span> <span class="o">=</span> <span class="o">(</span><span class="nx">notAvail</span><span class="o">.</span><span class="nx">length</span> <span class="o">===</span> <span class="m">0</span><span class="o">)</span> <span class="o">?</span> <span class="m">0</span> <span class="o">:</span> <span class="nx">_retryCount</span> <span class="o">-</span> <span class="m">1</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">tryAgain</span><span class="o">)</span> <span class="o">{</span>
<span class="c">// we may need to strip the nulled out items here</span>
<span class="c"></span> <span class="k">this</span><span class="o">.</span><span class="nx">startInterval</span><span class="o">();</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="nx">clearInterval</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">_interval</span><span class="o">);</span>
<span class="k">this</span><span class="o">.</span><span class="nx">_interval</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">this</span><span class="o">.</span><span class="nx">locked</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>
<span class="k">return</span><span class="o">;</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Removes all listeners attached to the given element via addListener.</span>
<span class="c"> * Optionally, the node's children can also be purged.</span>
<span class="c"> * Optionally, you can specify a specific type of event to remove.</span>
<span class="c"> * @method purgeElement</span>
<span class="c"> * @param {HTMLElement} el the element to purge</span>
<span class="c"> * @param {boolean} recurse recursively purge this element's children</span>
<span class="c"> * as well. Use with caution.</span>
<span class="c"> * @param {string} type optional type of listener to purge. If</span>
<span class="c"> * left out, all listeners will be removed</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">purgeElement</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">el</span><span class="o">,</span> <span class="nx">recurse</span><span class="o">,</span> <span class="nx">type</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">oEl</span> <span class="o">=</span> <span class="o">(</span><span class="nx">Y</span><span class="o">.</span><span class="nx">Lang</span><span class="o">.</span><span class="nx">isString</span><span class="o">(</span><span class="nx">el</span><span class="o">))</span> <span class="o">?</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">get</span><span class="o">(</span><span class="nx">el</span><span class="o">)</span> <span class="o">:</span> <span class="nx">el</span><span class="o">,</span>
<span class="nx">id</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">stamp</span><span class="o">(</span><span class="nx">oEl</span><span class="o">);</span>
<span class="k">var</span> <span class="nx">lis</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">getListeners</span><span class="o">(</span><span class="nx">oEl</span><span class="o">,</span> <span class="nx">type</span><span class="o">),</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">len</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">lis</span><span class="o">)</span> <span class="o">{</span>
<span class="k">for</span> <span class="o">(</span><span class="nx">i</span><span class="o">=</span><span class="m">0</span><span class="o">,</span><span class="nx">len</span><span class="o">=</span><span class="nx">lis</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span><span class="o"><</span><span class="nx">len</span> <span class="o">;</span> <span class="o">++</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">lis</span><span class="o">[</span><span class="nx">i</span><span class="o">].</span><span class="nx">unsubscribeAll</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">recurse</span> <span class="o">&&</span> <span class="nx">oEl</span> <span class="o">&&</span> <span class="nx">oEl</span><span class="o">.</span><span class="nx">childNodes</span><span class="o">)</span> <span class="o">{</span>
<span class="k">for</span> <span class="o">(</span><span class="nx">i</span><span class="o">=</span><span class="m">0</span><span class="o">,</span><span class="nx">len</span><span class="o">=</span><span class="nx">oEl</span><span class="o">.</span><span class="nx">childNodes</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span><span class="o"><</span><span class="nx">len</span> <span class="o">;</span> <span class="o">++</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="nx">purgeElement</span><span class="o">(</span><span class="nx">oEl</span><span class="o">.</span><span class="nx">childNodes</span><span class="o">[</span><span class="nx">i</span><span class="o">],</span> <span class="nx">recurse</span><span class="o">,</span> <span class="nx">type</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Returns all listeners attached to the given element via addListener.</span>
<span class="c"> * Optionally, you can specify a specific type of event to return.</span>
<span class="c"> * @method getListeners</span>
<span class="c"> * @param el {HTMLElement|string} the element or element id to inspect </span>
<span class="c"> * @param type {string} optional type of listener to return. If</span>
<span class="c"> * left out, all listeners will be returned</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">getListeners</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">el</span><span class="o">,</span> <span class="nx">type</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">results</span><span class="o">=[],</span> <span class="nx">ek</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">stamp</span><span class="o">(</span><span class="nx">el</span><span class="o">),</span> <span class="nx">key</span> <span class="o">=</span> <span class="o">(</span><span class="nx">type</span><span class="o">)</span> <span class="o">?</span> <span class="s1">'event:'</span> <span class="o">+</span> <span class="nx">type</span> <span class="o">:</span> <span class="kc">null</span><span class="o">,</span>
<span class="nx">evts</span> <span class="o">=</span> <span class="nx">_el_events</span><span class="o">[</span><span class="nx">ek</span><span class="o">];</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">key</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">evts</span><span class="o">[</span><span class="nx">key</span><span class="o">])</span> <span class="o">{</span>
<span class="nx">results</span><span class="o">.</span><span class="nx">push</span><span class="o">(</span><span class="nx">evts</span><span class="o">[</span><span class="nx">key</span><span class="o">]);</span>
<span class="o">}</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="k">for</span> <span class="o">(</span><span class="k">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">evts</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">results</span><span class="o">.</span><span class="nx">push</span><span class="o">(</span><span class="nx">evts</span><span class="o">[</span><span class="nx">i</span><span class="o">]);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">return</span> <span class="o">(</span><span class="nx">results</span><span class="o">.</span><span class="nx">length</span><span class="o">)</span> <span class="o">?</span> <span class="nx">results</span> <span class="o">:</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * automatically during the unload event.</span>
<span class="c"> * @method _unload</span>
<span class="c"> * @static</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="nx">_unload</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">e</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">E</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Event</span><span class="o">,</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">w</span><span class="o">;</span>
<span class="k">for</span> <span class="o">(</span><span class="nx">i</span> <span class="k">in</span> <span class="nx">_wrappers</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">w</span> <span class="o">=</span> <span class="nx">_wrappers</span><span class="o">[</span><span class="nx">i</span><span class="o">];</span>
<span class="nx">w</span><span class="o">.</span><span class="nx">unsubscribeAll</span><span class="o">();</span>
<span class="nx">E</span><span class="o">.</span><span class="nx">nativeRemove</span><span class="o">(</span><span class="nx">w</span><span class="o">.</span><span class="nx">el</span><span class="o">,</span> <span class="nx">w</span><span class="o">.</span><span class="nx">type</span><span class="o">,</span> <span class="nx">w</span><span class="o">.</span><span class="nx">fn</span><span class="o">);</span>
<span class="nx">delete</span> <span class="nx">_wrappers</span><span class="o">[</span><span class="nx">i</span><span class="o">];</span>
<span class="o">}</span>
<span class="nx">E</span><span class="o">.</span><span class="nx">nativeRemove</span><span class="o">(</span><span class="nb">window</span><span class="o">,</span> <span class="s2">"unload"</span><span class="o">,</span> <span class="nx">E</span><span class="o">.</span><span class="nx">_unload</span><span class="o">);</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Adds a DOM event directly without the caching, cleanup, context adj, etc</span>
<span class="c"> *</span>
<span class="c"> * @method nativeAdd</span>
<span class="c"> * @param {HTMLElement} el the element to bind the handler to</span>
<span class="c"> * @param {string} type the type of event handler</span>
<span class="c"> * @param {function} fn the callback to invoke</span>
<span class="c"> * @param {boolen} capture capture or bubble phase</span>
<span class="c"> * @static</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="nx">nativeAdd</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">el</span><span class="o">,</span> <span class="nx">type</span><span class="o">,</span> <span class="nx">fn</span><span class="o">,</span> <span class="nx">capture</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">el</span><span class="o">.</span><span class="nx">addEventListener</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">el</span><span class="o">.</span><span class="nx">addEventListener</span><span class="o">(</span><span class="nx">type</span><span class="o">,</span> <span class="nx">fn</span><span class="o">,</span> <span class="o">!!</span><span class="nx">capture</span><span class="o">);</span>
<span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="nx">el</span><span class="o">.</span><span class="nx">attachEvent</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">el</span><span class="o">.</span><span class="nx">attachEvent</span><span class="o">(</span><span class="s2">"on"</span> <span class="o">+</span> <span class="nx">type</span><span class="o">,</span> <span class="nx">fn</span><span class="o">);</span>
<span class="o">}</span>
<span class="c">// else {</span>
<span class="c"></span> <span class="c">// }</span>
<span class="c"></span> <span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Basic remove listener</span>
<span class="c"> *</span>
<span class="c"> * @method nativeRemove</span>
<span class="c"> * @param {HTMLElement} el the element to bind the handler to</span>
<span class="c"> * @param {string} type the type of event handler</span>
<span class="c"> * @param {function} fn the callback to invoke</span>
<span class="c"> * @param {boolen} capture capture or bubble phase</span>
<span class="c"> * @static</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="nx">nativeRemove</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">el</span><span class="o">,</span> <span class="nx">type</span><span class="o">,</span> <span class="nx">fn</span><span class="o">,</span> <span class="nx">capture</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">el</span><span class="o">.</span><span class="nx">removeEventListener</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">el</span><span class="o">.</span><span class="nx">removeEventListener</span><span class="o">(</span><span class="nx">type</span><span class="o">,</span> <span class="nx">fn</span><span class="o">,</span> <span class="o">!!</span><span class="nx">capture</span><span class="o">);</span>
<span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="nx">el</span><span class="o">.</span><span class="nx">detachEvent</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">el</span><span class="o">.</span><span class="nx">detachEvent</span><span class="o">(</span><span class="s2">"on"</span> <span class="o">+</span> <span class="nx">type</span><span class="o">,</span> <span class="nx">fn</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">};</span>
<span class="o">}();</span>
<span class="k">var</span> <span class="nx">E</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Event</span><span class="o">;</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">Y</span><span class="o">.</span><span class="nx">UA</span><span class="o">.</span><span class="nx">ie</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">subscribe</span> <span class="o">&&</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">on</span><span class="o">(</span><span class="s1">'event:ready'</span><span class="o">,</span> <span class="nx">E</span><span class="o">.</span><span class="nx">_tryPreloadAttach</span><span class="o">,</span> <span class="nx">E</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
<span class="o">}</span>
<span class="nx">E</span><span class="o">.</span><span class="nx">Custom</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">CustomEvent</span><span class="o">;</span>
<span class="nx">E</span><span class="o">.</span><span class="nx">Subscriber</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Subscriber</span><span class="o">;</span>
<span class="nx">E</span><span class="o">.</span><span class="nx">Target</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">EventTarget</span><span class="o">;</span>
<span class="c">/**</span>
<span class="c"> * @method on</span>
<span class="c"> * @see addListener</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">E</span><span class="o">.</span><span class="nx">attach</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">type</span><span class="o">,</span> <span class="nx">fn</span><span class="o">,</span> <span class="nx">el</span><span class="o">,</span> <span class="nx">data</span><span class="o">,</span> <span class="nx">context</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">arguments</span><span class="o">,</span> <span class="m">0</span><span class="o">,</span> <span class="kc">true</span><span class="o">),</span>
<span class="nx">oEl</span> <span class="o">=</span> <span class="nx">a</span><span class="o">.</span><span class="nx">splice</span><span class="o">(</span><span class="m">2</span><span class="o">,</span> <span class="m">1</span><span class="o">);</span>
<span class="nx">a</span><span class="o">.</span><span class="nx">unshift</span><span class="o">(</span><span class="nx">oEl</span><span class="o">[</span><span class="m">0</span><span class="o">]);</span>
<span class="k">return</span> <span class="nx">E</span><span class="o">.</span><span class="nx">addListener</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">E</span><span class="o">,</span> <span class="nx">a</span><span class="o">);</span>
<span class="o">};</span>
<span class="nx">E</span><span class="o">.</span><span class="nx">detach</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">type</span><span class="o">,</span> <span class="nx">fn</span><span class="o">,</span> <span class="nx">el</span><span class="o">,</span> <span class="nx">data</span><span class="o">,</span> <span class="nx">context</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="nx">E</span><span class="o">.</span><span class="nx">removeListener</span><span class="o">(</span><span class="nx">el</span><span class="o">,</span> <span class="nx">type</span><span class="o">,</span> <span class="nx">fn</span><span class="o">,</span> <span class="nx">data</span><span class="o">,</span> <span class="nx">context</span><span class="o">);</span>
<span class="o">};</span>
<span class="c"></span> <span class="nx">E</span><span class="o">.</span><span class="nx">nativeAdd</span><span class="o">(</span><span class="nb">window</span><span class="o">,</span> <span class="s2">"load"</span><span class="o">,</span> <span class="nx">E</span><span class="o">.</span><span class="nx">_load</span><span class="o">);</span>
<span class="nx">E</span><span class="o">.</span><span class="nx">nativeAdd</span><span class="o">(</span><span class="nb">window</span><span class="o">,</span> <span class="s2">"unload"</span><span class="o">,</span> <span class="nx">E</span><span class="o">.</span><span class="nx">_unload</span><span class="o">);</span>
<span class="nx">E</span><span class="o">.</span><span class="nx">_tryPreloadAttach</span><span class="o">();</span>
<span class="o">},</span> <span class="s2">"3.0.0"</span><span class="o">);</span>
</pre></div>
</div>
</div>
</div>
<div class="yui-b">
<div class="nav">
<div class="module">
<h4>Modules</h4>
<ul class="content">
</ul>
</div>
<div class="module">
<h4>Classes</h4>
<ul class="content">
</ul>
</div>
<div class="module">
<h4>Files</h4>
<ul class="content">
</ul>
</div>
</div>
</div>
</div>
<div id="ft">
<hr />
Copyright © 2008 Yahoo! Inc. All rights reserved.
</div>
</div>
</body>
</html>