event.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.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 event system</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"</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"> * is complete.</span>
<span class="c"> * @method ready</span>
<span class="c"> * @param f {Function} the function to execute</span>
<span class="c"> * @param c Optional execution context</span>
<span class="c"> * to the signature provided when the event fires.</span>
<span class="c"> * @return {YUI} the YUI instance</span>
<span class="c"> */</span>
<span class="c"></span> <span class="c">// var a = arguments, m = (a.length > 1) ? Y.bind.apply(Y, a) : f;</span>
<span class="c"></span> <span class="c">// return this;</span>
<span class="c"></span> <span class="c">// };</span>
<span class="c"></span>
<span class="c">/**</span>
<span class="c"> * Attach an event listener, either to a DOM object</span>
<span class="c"> * @param type {string} the event type</span>
<span class="c"> * @param f {Function} the function to execute</span>
<span class="c"> * @param context Optional execution context</span>
<span class="c"> * to the signature provided when the event fires.</span>
<span class="c"> * @method on</span>
<span class="c"> * @for YUI</span>
<span class="c"> * unsubscribing to this event.</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">on</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">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</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="nx">indexOf</span><span class="o">(</span><span class="s1">':'</span><span class="o">)</span> <span class="o">></span> <span class="o">-</span><span class="m">1</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">cat</span> <span class="o">=</span> <span class="nx">type</span><span class="o">.</span><span class="nx">split</span><span class="o">(</span><span class="s1">':'</span><span class="o">);</span>
<span class="nx">switch</span> <span class="o">(</span><span class="nx">cat</span><span class="o">[</span><span class="m">0</span><span class="o">])</span> <span class="o">{</span>
<span class="nx">default</span><span class="o">:</span>
<span class="k">return</span> <span class="nx">Y</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">Y</span><span class="o">,</span> <span class="nx">arguments</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">return</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Event</span><span class="o">.</span><span class="nx">attach</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">arguments</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * Detach an event listener (either a custom event or a</span>
<span class="c"> * DOM event</span>
<span class="c"> * @method detach</span>
<span class="c"> * in, the other parameters are not used.</span>
<span class="c"> * @param f {Function} the subscribed function</span>
<span class="c"> * @param o the object or element the listener is subscribed</span>
<span class="c"> * to.</span>
<span class="c"> * @method detach</span>
<span class="c"> * @return {YUI} the YUI instance</span>
<span class="c"> */</span>
<span class="nx">Y</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">f</span><span class="o">,</span> <span class="nx">o</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">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">else</span> <span class="k">if</span> <span class="o">(</span><span class="nx">type</span><span class="o">.</span><span class="nx">indexOf</span><span class="o">(</span><span class="s1">':'</span><span class="o">)</span> <span class="o">></span> <span class="o">-</span><span class="m">1</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">cat</span> <span class="o">=</span> <span class="nx">type</span><span class="o">.</span><span class="nx">split</span><span class="o">(</span><span class="s1">':'</span><span class="o">);</span>
<span class="nx">switch</span> <span class="o">(</span><span class="nx">cat</span><span class="o">[</span><span class="m">0</span><span class="o">])</span> <span class="o">{</span>
<span class="nx">default</span><span class="o">:</span>
<span class="k">return</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">unsubscribe</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">arguments</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">return</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Event</span><span class="o">.</span><span class="nx">detach</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">arguments</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * Executes the callback before a DOM event, custom event</span>
<span class="c"> * or method. If the first argument is a function, it</span>
<span class="c"> * is assumed the target is a method.</span>
<span class="c"> *</span>
<span class="c"> * For DOM and custom events:</span>
<span class="c"> * type, callback, context, 1-n arguments</span>
<span class="c"> * </span>
<span class="c"> * For methods:</span>
<span class="c"> * callback, object (method host), methodName, context, 1-n arguments</span>
<span class="c"> *</span>
<span class="c"> * @method before</span>
<span class="c"> * @return unsubscribe handle</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">before</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">f</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
<span class="c">// method override</span>
<span class="c"></span> <span class="c">// callback, object, sMethod</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">isFunction</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="nx">Y</span><span class="o">.</span><span class="nx">Do</span><span class="o">.</span><span class="nx">before</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">Do</span><span class="o">,</span> <span class="nx">arguments</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">Y</span><span class="o">;</span>
<span class="o">};</span>
<span class="c">/**</span>
<span class="c"> * Executes the callback after a DOM event, custom event</span>
<span class="c"> * or method. If the first argument is a function, it</span>
<span class="c"> * is assumed the target is a method.</span>
<span class="c"> *</span>
<span class="c"> * @TODO add event</span>
<span class="c"> *</span>
<span class="c"> * For DOM and custom events:</span>
<span class="c"> * type, callback, context, 1-n arguments</span>
<span class="c"> * </span>
<span class="c"> * For methods:</span>
<span class="c"> * callback, object (method host), methodName, context, 1-n arguments</span>
<span class="c"> *</span>
<span class="c"> * @method after</span>
<span class="c"> * @return unsubscribe handle</span>
<span class="c"> */</span>
<span class="nx">Y</span><span class="o">.</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">f</span><span class="o">,</span> <span class="nx">o</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">Lang</span><span class="o">.</span><span class="nx">isFunction</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="nx">Y</span><span class="o">.</span><span class="nx">Do</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">Y</span><span class="o">.</span><span class="nx">Do</span><span class="o">,</span> <span class="nx">arguments</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">Y</span><span class="o">;</span>
<span class="o">};</span>
<span class="o">},</span> <span class="s2">"3.0.0"</span><span class="o">,</span> <span class="o">{</span>
<span class="nx">use</span><span class="o">:</span> <span class="o">[</span>
<span class="s2">"aop"</span><span class="o">,</span>
<span class="s2">"event-custom"</span><span class="o">,</span>
<span class="s2">"event-target"</span><span class="o">,</span>
<span class="s2">"event-ready"</span><span class="o">,</span>
<span class="s2">"event-dom"</span><span class="o">,</span>
<span class="s2">"event-facade"</span>
<span class="o">]</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>