event.js.html revision 4fcbec6145d16637205990699912fb90f6a3807c
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>API: event event.js (YUI Library)</title>
<link rel="stylesheet" type="text/css" href="assets/api.css">
<script type="text/javascript" src="assets/api-js"></script>
<script type="text/javascript" src="assets/ac-js"></script>
</head>
<body id="yahoo-com">
<div id="doc3" class="yui-t2">
<div id="hd">
<a href="http://developer.yahoo.com/yui/"><h1>Yahoo! UI Library</h1></a>
<h3>event&nbsp; <span class="subtitle">3.00PR1</span></h3>
<p>
<a href="/index.html">Yahoo! UI Library</a>
&gt; <a href="/module_event.html">event</a>
&gt; 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">&quot;event&quot;</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"> * Subscribes to the yui:load event, which fires when a Y.use operation</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"> * @param args* 0..n Additional arguments to append </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">// Y.ready = function(f, c) {</span>
<span class="c"></span> <span class="c">// var a = arguments, m = (a.length &gt; 1) ? Y.bind.apply(Y, a) : f;</span>
<span class="c"></span> <span class="c">// Y.on(&quot;yui:load&quot;, m);</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"> * or to an Event.Target.</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 o the Event.Target or element to attach to</span>
<span class="c"> * @param context Optional execution context</span>
<span class="c"> * @param args* 0..n additional arguments to append</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"> * @return {Event.Handle} a handle object for </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">&#39;:&#39;</span><span class="o">)</span> <span class="o">&gt;</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">&#39;:&#39;</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"> * @param type the type of event, or a Event.Handle to</span>
<span class="c"> * for the subscription. If the Event.Handle is passed</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">&amp;&amp;</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">&#39;:&#39;</span><span class="o">)</span> <span class="o">&gt;</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">&#39;:&#39;</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">&quot;3.0.0&quot;</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">&quot;aop&quot;</span><span class="o">,</span>
<span class="s2">&quot;event-custom&quot;</span><span class="o">,</span>
<span class="s2">&quot;event-target&quot;</span><span class="o">,</span>
<span class="s2">&quot;event-ready&quot;</span><span class="o">,</span>
<span class="s2">&quot;event-dom&quot;</span><span class="o">,</span>
<span class="s2">&quot;event-facade&quot;</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">
<li class=""><a href="module_anim.html">anim</a></li>
<li class=""><a href="module_attribute.html">attribute</a></li>
<li class=""><a href="module_base.html">base</a></li>
<li class=""><a href="module_dd.html">dd</a></li>
<li class=""><a href="module_dd-plugin.html">dd-plugin</a></li>
<li class=""><a href="module_dump.html">dump</a></li>
<li class="selected"><a href="module_event.html">event</a></li>
<li class=""><a href="module_io.html">io</a></li>
<li class=""><a href="module_node.html">node</a></li>
<li class=""><a href="module_oop.html">oop</a></li>
<li class=""><a href="module_queue.html">queue</a></li>
<li class=""><a href="module_substitute.html">substitute</a></li>
<li class=""><a href="module_yui.html">yui</a></li>
</ul>
</div>
<div class="module">
<h4>Classes</h4>
<ul class="content">
<li class=""><a href="Do.html">Do</a></li>
<li class=""><a href="Do.AlterArgs.html">Do.AlterArgs</a></li>
<li class=""><a href="Do.AlterReturn.html">Do.AlterReturn</a></li>
<li class=""><a href="Do.Error.html">Do.Error</a></li>
<li class=""><a href="Do.Method.html">Do.Method</a></li>
<li class=""><a href="Event.html">Event</a></li>
<li class=""><a href="Event.Custom.html">Event.Custom</a></li>
<li class=""><a href="Event.Facade.html">Event.Facade</a></li>
<li class=""><a href="Event.Handle.html">Event.Handle</a></li>
<li class=""><a href="Event.Subscriber.html">Event.Subscriber</a></li>
<li class=""><a href="Event.Target.html">Event.Target</a></li>
</ul>
</div>
<div class="module">
<h4>Files</h4>
<ul class="content">
<li class=""><a href="event-custom.js.html">event-custom.js</a></li>
<li class=""><a href="event-do.js.html">event-do.js</a></li>
<li class=""><a href="event-dom.js.html">event-dom.js</a></li>
<li class=""><a href="event-facade.js.html">event-facade.js</a></li>
<li class=""><a href="event-ready.js.html">event-ready.js</a></li>
<li class=""><a href="event-target.js.html">event-target.js</a></li>
<li class="selected"><a href="event.js.html">event.js</a></li>
</ul>
</div>
</div>
</div>
</div>
<div id="ft">
<hr />
Copyright &copy; 2008 Yahoo! Inc. All rights reserved.
</div>
</div>
</body>
</html>