event-target.js.html revision 62e7a7d39cc0c6d25fec20964c26bc35b9fa116a
<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-target.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" /> Show Private</span>
<span id="classopts"><input type="checkbox" name="showprotected" id="showprotected" /> Show Protected</span>
<span id="classopts"><input type="checkbox" name="showdeprecated" id="showdeprecated" /> Show Deprecated</span>
</form>
<div id="srcout">
<style>
#doc3 #classopts { display:none; }
</style>
<div class="highlight" ><pre><span class="nx">YUI</span><span class="o">.</span><span class="nx">add</span><span class="o">(</span><span class="s2">"event-target"</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="k">var</span> <span class="nx">SILENT</span> <span class="o">=</span> <span class="o">{</span> <span class="s1">'yui:log'</span><span class="o">:</span> <span class="kc">true</span> <span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * and fired by name. This makes it possible for implementing code to</span>
<span class="c"> * subscribe to an event that either has not been created yet, or will</span>
<span class="c"> * not be created at all.</span>
<span class="c"> *</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">EventTarget</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">opts</span><span class="o">)</span> <span class="o">{</span>
<span class="c"></span>
<span class="k">var</span> <span class="nx">o</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">isObject</span><span class="o">(</span><span class="nx">opts</span><span class="o">))</span> <span class="o">?</span> <span class="nx">opts</span> <span class="o">:</span> <span class="o">{};</span>
<span class="k">this</span><span class="o">.</span><span class="nx">_yuievt</span> <span class="o">=</span> <span class="o">{</span>
<span class="nx">events</span><span class="o">:</span> <span class="o">{},</span>
<span class="nx">targets</span><span class="o">:</span> <span class="o">{},</span>
<span class="nx">config</span><span class="o">:</span> <span class="nx">o</span><span class="o">,</span>
<span class="nx">defaults</span><span class="o">:</span> <span class="o">{</span>
<span class="nx">context</span><span class="o">:</span> <span class="k">this</span><span class="o">,</span>
<span class="nx">host</span><span class="o">:</span> <span class="k">this</span><span class="o">,</span>
<span class="nx">emitFacade</span><span class="o">:</span> <span class="nx">o</span><span class="o">.</span><span class="nx">emitFacade</span> <span class="o">||</span> <span class="kc">false</span><span class="o">,</span>
<span class="nx">bubbles</span><span class="o">:</span> <span class="o">(</span><span class="s1">'bubbles'</span> <span class="k">in</span> <span class="nx">o</span><span class="o">)</span> <span class="o">?</span> <span class="nx">o</span><span class="o">.</span><span class="nx">bubbles</span> <span class="o">:</span> <span class="kc">true</span>
<span class="o">}</span>
<span class="o">};</span>
<span class="o">};</span>
<span class="k">var</span> <span class="nx">ET</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="nx">ET</span><span class="o">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="o">{</span>
<span class="c">/**</span>
<span class="c"> * Subscribe to a custom event hosted by this object</span>
<span class="c"> * @method subscribe</span>
<span class="c"> * @param type {string} The type of the event</span>
<span class="c"> * @param fn {Function} The callback</span>
<span class="c"> * @param context The execution context</span>
<span class="c"> */</span>
<span class="nx">subscribe</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">context</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">ce</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">_yuievt</span><span class="o">.</span><span class="nx">events</span><span class="o">[</span><span class="nx">type</span><span class="o">]</span> <span class="o">||</span> <span class="k">this</span><span class="o">.</span><span class="nx">publish</span><span class="o">(</span><span class="nx">type</span><span class="o">),</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="k">return</span> <span class="nx">ce</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">ce</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"> * Unsubscribes one or more listeners the from the specified event</span>
<span class="c"> * @method unsubscribe</span>
<span class="c"> * @param type {string|Object} Either the handle to the subscriber or the </span>
<span class="c"> * type of event. If the type</span>
<span class="c"> * is not specified, it will attempt to remove</span>
<span class="c"> * the listener from all hosted events.</span>
<span class="c"> * @param fn {Function} The subscribed function to unsubscribe, if not</span>
<span class="c"> * supplied, all subscribers will be removed.</span>
<span class="c"> * @param context {Object} The custom object passed to subscribe. This is</span>
<span class="c"> * optional, but if supplied will be used to</span>
<span class="c"> * disambiguate multiple listeners that are the same</span>
<span class="c"> * that lives on the prototype)</span>
<span class="c"> * @return {boolean} true if the subscriber was found and detached.</span>
<span class="c"> */</span>
<span class="nx">unsubscribe</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">context</span><span class="o">)</span> <span class="o">{</span>
<span class="c">// If this is an event handle, use it to detach</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">Lang</span><span class="o">.</span><span class="nx">isObject</span><span class="o">(</span><span class="nx">type</span><span class="o">)</span> <span class="o">&&</span> <span class="nx">type</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">type</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">evts</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">_yuievt</span><span class="o">.</span><span class="nx">events</span><span class="o">;</span>
<span class="k">if</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">ce</span> <span class="o">=</span> <span class="nx">evts</span><span class="o">[</span><span class="nx">type</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="nx">context</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">var</span> <span class="nx">ret</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="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="k">if</span> <span class="o">(</span><span class="nx">Y</span><span class="o">.</span><span class="nb">Object</span><span class="o">.</span><span class="nx">owns</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="nx">ret</span> <span class="o">=</span> <span class="nx">ret</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="nx">unsubscribe</span><span class="o">(</span><span class="nx">fn</span><span class="o">,</span> <span class="nx">context</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">ret</span><span class="o">;</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">/**</span>
<span class="c"> * Removes all listeners from the specified event. If the event type</span>
<span class="c"> * is not specified, all listeners from all hosted custom events will</span>
<span class="c"> * be removed.</span>
<span class="c"> * @method unsubscribeAll</span>
<span class="c"> * @param type {string} The type, or name of the event</span>
<span class="c"> */</span>
<span class="nx">unsubscribeAll</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="o">{</span>
<span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="nx">unsubscribe</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"> * Creates a new custom event of the specified type. If a custom event</span>
<span class="c"> * by that name already exists, it will not be re-created. In either</span>
<span class="c"> * case the custom event is returned. </span>
<span class="c"> *</span>
<span class="c"> * @method publish</span>
<span class="c"> *</span>
<span class="c"> * @param type {string} the type, or name of the event</span>
<span class="c"> * @param opts {object} optional config params. Valid properties are:</span>
<span class="c"> *</span>
<span class="c"> * <ul></span>
<span class="c"> * <li></span>
<span class="c"> * 'broadcast': whether or not the YUI instance and YUI global are notified when the event is fired (false)</span>
<span class="c"> * </li></span>
<span class="c"> * <li></span>
<span class="c"> * 'bubbles': whether or not this event bubbles (true)</span>
<span class="c"> * </li></span>
<span class="c"> * <li></span>
<span class="c"> * 'context': the default execution context for the listeners (this)</span>
<span class="c"> * </li></span>
<span class="c"> * <li></span>
<span class="c"> * 'defaultFn': the default function to execute when this event fires if preventDefault was not called</span>
<span class="c"> * </li></span>
<span class="c"> * <li></span>
<span class="c"> * 'emitFacade': whether or not this event emits a facade (false)</span>
<span class="c"> * </li></span>
<span class="c"> * <li></span>
<span class="c"> * 'fireOnce': if an event is configured to fire once, new subscribers after</span>
<span class="c"> * the fire will be notified immediately.</span>
<span class="c"> * </li></span>
<span class="c"> * <li></span>
<span class="c"> * 'preventable': whether or not preventDefault() has an effect (true)</span>
<span class="c"> * </li></span>
<span class="c"> * <li></span>
<span class="c"> * 'preventedFn': a function that is executed when preventDefault is called</span>
<span class="c"> * </li></span>
<span class="c"> * <li></span>
<span class="c"> * 'queuable': whether or not this event can be queued during bubbling (false)</span>
<span class="c"> * </li></span>
<span class="c"> * <li></span>
<span class="c"> * 'silent': if silent is true, debug messages are not provided for this event.</span>
<span class="c"> * </li></span>
<span class="c"> * <li></span>
<span class="c"> * 'stoppedFn': a function that is executed when stopPropagation is called</span>
<span class="c"> * </li></span>
<span class="c"> * <li></span>
<span class="c"> * 'type': the event type (valid option if not provided as the first parameter to publish)</span>
<span class="c"> * </li></span>
<span class="c"> * </ul></span>
<span class="c"> *</span>
<span class="c"> *</span>
<span class="c"> */</span>
<span class="nx">publish</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">opts</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">o</span> <span class="o">=</span> <span class="nx">opts</span> <span class="o">||</span> <span class="o">{},</span> <span class="nx">events</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">_yuievt</span><span class="o">.</span><span class="nx">events</span><span class="o">,</span> <span class="nx">ce</span> <span class="o">=</span> <span class="nx">events</span><span class="o">[</span><span class="nx">type</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="nx">Y</span><span class="o">.</span><span class="nx">log</span><span class="o">(</span><span class="s2">"publish() skipped: '"</span><span class="o">+</span><span class="nx">type</span><span class="o">+</span><span class="s2">"' exists"</span><span class="o">,</span> <span class="s1">'info'</span><span class="o">,</span> <span class="s1">'Event'</span><span class="o">);</span>
<span class="c">// update config for the event</span>
<span class="c"></span> <span class="nx">ce</span><span class="o">.</span><span class="nx">applyConfig</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="c">// apply defaults</span>
<span class="c"></span> <span class="nx">Y</span><span class="o">.</span><span class="nx">mix</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="k">this</span><span class="o">.</span><span class="nx">_yuievt</span><span class="o">.</span><span class="nx">defaults</span><span class="o">);</span>
<span class="nx">ce</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">CustomEvent</span><span class="o">(</span><span class="nx">type</span><span class="o">,</span> <span class="nx">o</span><span class="o">);</span>
<span class="nx">events</span><span class="o">[</span><span class="nx">type</span><span class="o">]</span> <span class="o">=</span> <span class="nx">ce</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">o</span><span class="o">.</span><span class="nx">onSubscribeCallback</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">ce</span><span class="o">.</span><span class="nx">subscribeEvent</span><span class="o">.</span><span class="nx">subscribe</span><span class="o">(</span><span class="nx">o</span><span class="o">.</span><span class="nx">onSubscribeCallback</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">events</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"> * is determined by the order registered. Multiple targets can</span>
<span class="c"> * be specified.</span>
<span class="c"> * @method addTarget</span>
<span class="c"> */</span>
<span class="nx">addTarget</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">this</span><span class="o">.</span><span class="nx">_yuievt</span><span class="o">.</span><span class="nx">targets</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">o</span><span class="o">)]</span> <span class="o">=</span> <span class="nx">o</span><span class="o">;</span>
<span class="k">this</span><span class="o">.</span><span class="nx">_yuievt</span><span class="o">.</span><span class="nx">hasTargets</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"> * Removes a bubble target</span>
<span class="c"> * @method removeTarget</span>
<span class="c"> */</span>
<span class="nx">removeTarget</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="nx">delete</span> <span class="k">this</span><span class="o">.</span><span class="nx">_yuievt</span><span class="o">.</span><span class="nx">targets</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">o</span><span class="o">)];</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Fire a custom event by name. The callback functions will be executed</span>
<span class="c"> * from the context specified when the event was created, and with the </span>
<span class="c"> * following parameters.</span>
<span class="c"> *</span>
<span class="c"> * If the custom event object hasn't been created, then the event hasn't </span>
<span class="c"> * been published and it has no subscribers. For performance sake, we </span>
<span class="c"> * immediate exit in this case. This means the event won't bubble, so </span>
<span class="c"> * if the intention is that a bubble target be notified, the event must </span>
<span class="c"> * be published on this object first.</span>
<span class="c"> *</span>
<span class="c"> * @method fire</span>
<span class="c"> * @param type {String|Object} The type of the event, or an object that contains</span>
<span class="c"> * a 'type' property.</span>
<span class="c"> * @param arguments {Object*} an arbitrary set of parameters to pass to </span>
<span class="c"> * the handler.</span>
<span class="c"> * </span>
<span class="c"> */</span>
<span class="nx">fire</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="o">{</span>
<span class="k">var</span> <span class="nx">typeIncluded</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">type</span><span class="o">),</span>
<span class="nx">t</span> <span class="o">=</span> <span class="o">(</span><span class="nx">typeIncluded</span><span class="o">)</span> <span class="o">?</span> <span class="nx">type</span> <span class="o">:</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">type</span><span class="o">);</span>
<span class="k">var</span> <span class="nx">ce</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">t</span><span class="o">);</span>
<span class="c">// this event has not been published or subscribed to</span>
<span class="c"></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="c">// if this object has bubble targets, we need to publish the</span>
<span class="c"></span> <span class="c">// event in order for it to bubble.</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">_yuievt</span><span class="o">.</span><span class="nx">hasTargets</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">ce</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">publish</span><span class="o">(</span><span class="nx">t</span><span class="o">);</span>
<span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="nx">bubble</span><span class="o">(</span><span class="nx">ce</span><span class="o">);</span>
<span class="o">}</span>
<span class="c">// otherwise there is nothing to be done</span>
<span class="c"></span> <span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
<span class="o">}</span>
<span class="c">// Provide this object's subscribers the object they are listening to.</span>
<span class="c"></span>
<span class="c">// This this the target unless target is current not null</span>
<span class="c"></span> <span class="c">// (set in bubble()).</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="o">(</span><span class="nx">typeIncluded</span><span class="o">)</span> <span class="o">?</span> <span class="m">1</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">var</span> <span class="nx">ret</span> <span class="o">=</span> <span class="nx">ce</span><span class="o">.</span><span class="nx">fire</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">ce</span><span class="o">,</span> <span class="nx">a</span><span class="o">);</span>
<span class="c">// clear target for next fire()</span>
<span class="c"></span> <span class="nx">ce</span><span class="o">.</span><span class="nx">target</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
<span class="k">return</span> <span class="nx">ret</span><span class="o">;</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Returns the custom event of the provided type has been created, a</span>
<span class="c"> * falsy value otherwise</span>
<span class="c"> * @method getEvent</span>
<span class="c"> * @param type {string} the type, or name of the event</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">type</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="k">this</span><span class="o">.</span><span class="nx">_yuievt</span><span class="o">.</span><span class="nx">events</span><span class="o">;</span>
<span class="k">return</span> <span class="o">(</span><span class="nx">e</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"> * Propagate an event</span>
<span class="c"> * @method bubble</span>
<span class="c"> */</span>
<span class="nx">bubble</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">evt</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">targs</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">_yuievt</span><span class="o">.</span><span class="nx">targets</span><span class="o">,</span> <span class="nx">ret</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">evt</span><span class="o">.</span><span class="nx">stopped</span> <span class="o">&&</span> <span class="nx">targs</span><span class="o">)</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">targs</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">targs</span><span class="o">.</span><span class="nx">hasOwnProperty</span><span class="o">(</span><span class="nx">i</span><span class="o">))</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">t</span> <span class="o">=</span> <span class="nx">targs</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">evt</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">t</span><span class="o">.</span><span class="nx">getEvent</span><span class="o">(</span><span class="nx">type</span><span class="o">),</span> <span class="nx">targetProp</span> <span class="o">=</span> <span class="nx">evt</span><span class="o">.</span><span class="nx">target</span> <span class="o">||</span> <span class="k">this</span><span class="o">;</span>
<span class="c">// if this event was not published on the bubble target,</span>
<span class="c"></span> <span class="c">// publish it with sensible default properties</span>
<span class="c"></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="c">// publish the event on the bubble target using this event</span>
<span class="c"></span> <span class="c">// for its configuration</span>
<span class="c"></span> <span class="nx">ce</span> <span class="o">=</span> <span class="nx">t</span><span class="o">.</span><span class="nx">publish</span><span class="o">(</span><span class="nx">type</span><span class="o">,</span> <span class="nx">evt</span><span class="o">);</span>
<span class="c">// set the host and context appropriately</span>
<span class="c"></span> <span class="nx">ce</span><span class="o">.</span><span class="nx">context</span> <span class="o">=</span> <span class="o">(</span><span class="nx">evt</span><span class="o">.</span><span class="nx">host</span> <span class="o">===</span> <span class="nx">evt</span><span class="o">.</span><span class="nx">context</span><span class="o">)</span> <span class="o">?</span> <span class="nx">t</span> <span class="o">:</span> <span class="nx">evt</span><span class="o">.</span><span class="nx">context</span><span class="o">;</span>
<span class="nx">ce</span><span class="o">.</span><span class="nx">host</span> <span class="o">=</span> <span class="nx">t</span><span class="o">;</span>
<span class="c">// clear handlers if specified on this event</span>
<span class="c"></span> <span class="nx">ce</span><span class="o">.</span><span class="nx">defaultFn</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
<span class="nx">ce</span><span class="o">.</span><span class="nx">preventedFn</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
<span class="nx">ce</span><span class="o">.</span><span class="nx">stoppedFn</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">}</span>
<span class="nx">ce</span><span class="o">.</span><span class="nx">target</span> <span class="o">=</span> <span class="nx">targetProp</span><span class="o">;</span>
<span class="nx">ce</span><span class="o">.</span><span class="nx">currentTarget</span> <span class="o">=</span> <span class="nx">t</span><span class="o">;</span>
<span class="c"></span>
<span class="nx">ret</span> <span class="o">=</span> <span class="nx">ret</span> <span class="o">&&</span> <span class="nx">ce</span><span class="o">.</span><span class="nx">fire</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">ce</span><span class="o">,</span> <span class="nx">evt</span><span class="o">.</span><span class="nx">details</span><span class="o">);</span>
<span class="c">// stopPropagation() was called</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">ce</span><span class="o">.</span><span class="nx">stopped</span><span class="o">)</span> <span class="o">{</span>
<span class="k">break</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">ret</span><span class="o">;</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Subscribe to a custom event hosted by this object. The</span>
<span class="c"> * supplied callback will execute after any listeners add</span>
<span class="c"> * via the subscribe method, and after the default function,</span>
<span class="c"> * if configured for the event, has executed.</span>
<span class="c"> * @method after</span>
<span class="c"> * @param type {string} The type of the event</span>
<span class="c"> * @param fn {Function} The callback</span>
<span class="c"> * @param context The execution context</span>
<span class="c"> */</span>
<span class="nx">after</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="o">{</span>
<span class="k">var</span> <span class="nx">ce</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">_yuievt</span><span class="o">.</span><span class="nx">events</span><span class="o">[</span><span class="nx">type</span><span class="o">]</span> <span class="o">||</span> <span class="k">this</span><span class="o">.</span><span class="nx">publish</span><span class="o">(</span><span class="nx">type</span><span class="o">),</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="k">return</span> <span class="nx">ce</span><span class="o">.</span><span class="nx">after</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">ce</span><span class="o">,</span> <span class="nx">a</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">};</span>
<span class="c">// make Y an event target</span>
<span class="c"></span> <span class="nx">Y</span><span class="o">.</span><span class="nx">mix</span><span class="o">(</span><span class="nx">Y</span><span class="o">,</span> <span class="nx">ET</span><span class="o">.</span><span class="nx">prototype</span><span class="o">,</span> <span class="kc">false</span><span class="o">,</span> <span class="kc">false</span><span class="o">,</span> <span class="o">{</span>
<span class="nx">bubbles</span><span class="o">:</span> <span class="kc">false</span>
<span class="o">});</span>
<span class="nx">ET</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">Y</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>