yui.js.html revision f6d14b3bf6e08e09949f6a1e7d5431b7829f3550
<html>
<head>
</head>
<body id="yahoo-com">
<div id="doc3" class="yui-t2">
<div id="hd">
<h3>yui <span class="subtitle">3.00PR1</span></h3>
<p>
> yui.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="o">(</span><span class="k">function</span><span class="o">()</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">_instances</span> <span class="o">=</span> <span class="o">{},</span>
<span class="c">// @TODO: this needs to be created at build time from module metadata</span>
<span class="c"></span>
<span class="nx">_APPLY_TO_WHITE_LIST</span> <span class="o">=</span> <span class="o">{</span>
<span class="s1">'io.xdrReady'</span><span class="o">:</span> <span class="m">1</span><span class="o">,</span>
<span class="s1">'io.start'</span><span class="o">:</span> <span class="m">1</span><span class="o">,</span>
<span class="s1">'io.success'</span><span class="o">:</span> <span class="m">1</span><span class="o">,</span>
<span class="s1">'io.failure'</span><span class="o">:</span> <span class="m">1</span><span class="o">,</span>
<span class="o">};</span>
<span class="k">if</span> <span class="o">(</span><span class="k">typeof</span> <span class="nx">YUI</span> <span class="o">===</span> <span class="s1">'undefined'</span> <span class="o">||</span> <span class="o">!</span><span class="nx">YUI</span><span class="o">)</span> <span class="o">{</span>
<span class="c">/**</span>
<span class="c"> * The YUI global namespace object. If YUI is already defined, the</span>
<span class="c"> * existing YUI object will not be overwritten so that defined</span>
<span class="c"> * namespaces are preserved. </span>
<span class="c"> * @class YUI</span>
<span class="c"> * @constructor</span>
<span class="c"> * @global</span>
<span class="c"> * @param o configuration object</span>
<span class="c"> */</span>
<span class="c">/*global YUI*/</span>
<span class="nx">YUI</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">var</span> <span class="nx">Y</span> <span class="o">=</span> <span class="k">this</span><span class="o">;</span>
<span class="c">// Allow var yui = YUI() instead of var yui = new YUI()</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="nb">window</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="k">new</span> <span class="nx">YUI</span><span class="o">(</span><span class="nx">o</span><span class="o">);</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="c">// set up the core environment</span>
<span class="c"></span> <span class="nx">Y</span><span class="o">.</span><span class="nx">_init</span><span class="o">(</span><span class="nx">o</span><span class="o">);</span>
<span class="c">// bind the specified additional modules for this instance</span>
<span class="c"></span> <span class="nx">Y</span><span class="o">.</span><span class="nx">_setup</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">};</span>
<span class="o">}</span>
<span class="c">// The prototype contains the functions that are required to allow the external</span>
<span class="c">// modules to be registered and for the instance to be initialized.</span>
<span class="c"></span><span class="nx">YUI</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"> * Initialize this YUI instance</span>
<span class="c"> * @param o config options</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="nx">_init</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="c">// @todo</span>
<span class="c"></span> <span class="c">// loadcfg {</span>
<span class="c"></span> <span class="c">// base</span>
<span class="c"></span> <span class="c">// securebase</span>
<span class="c"></span> <span class="c">// filter</span>
<span class="c"></span> <span class="c">// win</span>
<span class="c"></span> <span class="c">// doc</span>
<span class="c"></span> <span class="c">// debug</span>
<span class="c"></span> <span class="c">// useConsole</span>
<span class="c"></span> <span class="c">// logInclude</span>
<span class="c"></span> <span class="c">// logExclude</span>
<span class="c"></span> <span class="c">// throwFail</span>
<span class="c"></span> <span class="c">// pollInterval</span>
<span class="c"></span> <span class="c">// core</span>
<span class="c"></span> <span class="c">// }</span>
<span class="c"></span>
<span class="nx">o</span> <span class="o">=</span> <span class="nx">o</span> <span class="o">||</span> <span class="o">{};</span>
<span class="c">// find targeted window and @TODO create facades</span>
<span class="c"></span> <span class="k">var</span> <span class="nx">w</span> <span class="o">=</span> <span class="o">(</span><span class="nx">o</span><span class="o">.</span><span class="nx">win</span><span class="o">)</span> <span class="o">?</span> <span class="o">(</span><span class="nx">o</span><span class="o">.</span><span class="nx">win</span><span class="o">.</span><span class="nx">contentWindow</span><span class="o">)</span> <span class="o">:</span> <span class="nx">o</span><span class="o">.</span><span class="nx">win</span> <span class="o">||</span> <span class="nb">window</span><span class="o">;</span>
<span class="nx">o</span><span class="o">.</span><span class="nx">win</span> <span class="o">=</span> <span class="nx">w</span><span class="o">;</span>
<span class="nx">o</span><span class="o">.</span><span class="nx">doc</span> <span class="o">=</span> <span class="nx">w</span><span class="o">.</span><span class="nb">document</span><span class="o">;</span>
<span class="nx">o</span><span class="o">.</span><span class="nx">debug</span> <span class="o">=</span> <span class="o">(</span><span class="s1">'debug'</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">debug</span> <span class="o">:</span> <span class="kc">true</span><span class="o">;</span>
<span class="nx">o</span><span class="o">.</span><span class="nx">useConsole</span> <span class="o">=</span> <span class="o">(</span><span class="s1">'useConsole'</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">debug</span> <span class="o">:</span> <span class="kc">true</span><span class="o">;</span>
<span class="c">// add a reference to o for anything that needs it</span>
<span class="c"></span> <span class="c">// before _setup is called.</span>
<span class="c"></span> <span class="k">this</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="k">this</span><span class="o">.</span><span class="nx">Env</span> <span class="o">=</span> <span class="o">{</span>
<span class="c">// @todo expand the new module metadata</span>
<span class="c"></span> <span class="nx">mods</span><span class="o">:</span> <span class="o">{},</span>
<span class="nx">_idx</span><span class="o">:</span> <span class="m">0</span><span class="o">,</span>
<span class="nx">_pre</span><span class="o">:</span> <span class="s1">'yuid'</span><span class="o">,</span>
<span class="nx">_used</span><span class="o">:</span> <span class="o">{},</span>
<span class="nx">_yidx</span><span class="o">:</span> <span class="m">0</span><span class="o">,</span>
<span class="nx">_uidx</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">YUI</span><span class="o">.</span><span class="nx">Env</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="nx">Env</span><span class="o">.</span><span class="nx">_yidx</span> <span class="o">=</span> <span class="o">++</span><span class="nx">YUI</span><span class="o">.</span><span class="nx">Env</span><span class="o">.</span><span class="nx">_idx</span><span class="o">;</span>
<span class="k">this</span><span class="o">.</span><span class="nx">id</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">stamp</span><span class="o">(</span><span class="k">this</span><span class="o">);</span>
<span class="nx">_instances</span><span class="o">[</span><span class="k">this</span><span class="o">.</span><span class="nx">id</span><span class="o">]</span> <span class="o">=</span> <span class="k">this</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">this</span><span class="o">.</span><span class="nx">constructor</span> <span class="o">=</span> <span class="nx">YUI</span><span class="o">;</span>
<span class="k">this</span><span class="o">.</span><span class="nx">log</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">id</span> <span class="o">+</span> <span class="s1">') init '</span><span class="o">);</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Finishes the instance setup. Attaches whatever modules were defined</span>
<span class="c"> * when the yui modules was registered.</span>
<span class="c"> * @method _setup</span>
<span class="c"> * @private</span>
<span class="c"> */</span>
<span class="nx">_setup</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">use</span><span class="o">(</span><span class="s2">"yui"</span><span class="o">);</span>
<span class="c">// make a shallow copy of the config. This won't fix nested configs</span>
<span class="c"></span> <span class="c">// so we need to determine if we only allow one level (probably) or</span>
<span class="c"></span> <span class="c">// if we make clone create references for functions and elements.</span>
<span class="c"></span> <span class="k">this</span><span class="o">.</span><span class="nx">config</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">merge</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">config</span><span class="o">);</span>
<span class="c"></span> <span class="c">// debug: true,</span>
<span class="c"></span> <span class="c">// useConsole: true</span>
<span class="c"></span> <span class="c">// // , throwFail: false</span>
<span class="c"></span> <span class="c">// });</span>
<span class="c"></span>
<span class="c"></span> <span class="c">// silent: true</span>
<span class="c"></span> <span class="c">// });</span>
<span class="c"></span> <span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Executes a method on a YUI instance with</span>
<span class="c"> * the specified id.</span>
<span class="c"> * @method applyTo</span>
<span class="c"> * @param id {string} the YUI instance id</span>
<span class="c"> * @param method {string} the name of the method to exectute.</span>
<span class="c"> * @param args {Array} the arguments to apply to the method</span>
<span class="c"> * @return the return value from the applied method or null</span>
<span class="c"> */</span>
<span class="nx">applyTo</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">method</span><span class="o">,</span> <span class="nx">args</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(!(</span><span class="nx">method</span> <span class="k">in</span> <span class="nx">_APPLY_TO_WHITE_LIST</span><span class="o">))</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="nx">fail</span><span class="o">(</span><span class="nx">method</span> <span class="o">+</span> <span class="s1">': applyTo not allowed'</span><span class="o">);</span>
<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">var</span> <span class="nx">instance</span> <span class="o">=</span> <span class="nx">_instances</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">instance</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">nest</span> <span class="o">=</span> <span class="nx">method</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">m</span> <span class="o">=</span> <span class="nx">instance</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">nest</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">m</span> <span class="o">=</span> <span class="nx">m</span><span class="o">[</span><span class="nx">nest</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">m</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="nx">fail</span><span class="o">(</span><span class="s1">'applyTo failure: '</span> <span class="o">+</span> <span class="nx">method</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">m</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">instance</span><span class="o">,</span> <span class="nx">args</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Register a module</span>
<span class="c"> * @method add</span>
<span class="c"> * @param name {string} module name</span>
<span class="c"> * @param namespace {string} name space for the module</span>
<span class="c"> * @param fn {Function} entry point into the module that</span>
<span class="c"> * is used to bind module to the YUI instance</span>
<span class="c"> * @param version {string} version string</span>
<span class="c"> * @return {YUI} the YUI instance</span>
<span class="c"> *</span>
<span class="c"> * requires - features that should be present before loading</span>
<span class="c"> * optional - optional features that should be present if load optional defined</span>
<span class="c"> * use - features that should be attached automatically</span>
<span class="c"> * skinnable -</span>
<span class="c"> * rollup</span>
<span class="c"> * omit - features that should not be loaded if this module is present</span>
<span class="c"> */</span>
<span class="nx">add</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">name</span><span class="o">,</span> <span class="nx">fn</span><span class="o">,</span> <span class="nx">version</span><span class="o">,</span> <span class="nx">details</span><span class="o">)</span> <span class="o">{</span>
<span class="c"></span>
<span class="c">// @todo expand this to include version mapping</span>
<span class="c"></span>
<span class="c"></span>
<span class="c">// @todo may want to restore the build property</span>
<span class="c"></span>
<span class="c">// @todo fire moduleAvailable event</span>
<span class="c"></span>
<span class="k">var</span> <span class="nx">m</span> <span class="o">=</span> <span class="o">{</span>
<span class="nx">name</span><span class="o">:</span> <span class="nx">name</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">version</span><span class="o">:</span> <span class="nx">version</span><span class="o">,</span>
<span class="nx">details</span><span class="o">:</span> <span class="nx">details</span> <span class="o">||</span> <span class="o">{}</span>
<span class="o">};</span>
<span class="nx">YUI</span><span class="o">.</span><span class="nx">Env</span><span class="o">.</span><span class="nx">mods</span><span class="o">[</span><span class="nx">name</span><span class="o">]</span> <span class="o">=</span> <span class="nx">m</span><span class="o">;</span>
<span class="k">return</span> <span class="k">this</span><span class="o">;</span> <span class="c">// chain support</span>
<span class="c"></span> <span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Bind a module to a YUI instance</span>
<span class="c"> * @param modules* {string} 1-n modules to bind (uses arguments array)</span>
<span class="c"> * @param *callback {function} callback function executed when </span>
<span class="c"> * the instance has the required functionality. If included, it</span>
<span class="c"> * must be the last parameter.</span>
<span class="c"> *</span>
<span class="c"> * @TODO </span>
<span class="c"> * Implement versioning? loader can load different versions?</span>
<span class="c"> * we add syntax for specifying these?</span>
<span class="c"> *</span>
<span class="c"> * YUI().use('dragdrop')</span>
<span class="c"> * YUI().use('dragdrop:2.4.0'); // specific version</span>
<span class="c"> * YUI().use('dragdrop:2.4.0-'); // at least this version</span>
<span class="c"> * YUI().use('dragdrop:2.4.0-2.9999.9999'); // version range</span>
<span class="c"> * YUI().use('*'); // use all available modules</span>
<span class="c"> * YUI().use('lang+dump+substitute'); // use lang and some plugins</span>
<span class="c"> * YUI().use('lang+*'); // use lang and all known plugins</span>
<span class="c"> *</span>
<span class="c"> *</span>
<span class="c"> * @return {YUI} the YUI instance</span>
<span class="c"> */</span>
<span class="nx">use</span><span class="o">:</span> <span class="k">function</span><span class="o">()</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">Y</span> <span class="o">=</span> <span class="k">this</span><span class="o">,</span>
<span class="nx">a</span><span class="o">=</span><span class="nb">Array</span><span class="o">.</span><span class="nx">prototype</span><span class="o">.</span><span class="nx">slice</span><span class="o">.</span><span class="nx">call</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="nx">mods</span> <span class="o">=</span> <span class="nx">YUI</span><span class="o">.</span><span class="nx">Env</span><span class="o">.</span><span class="nx">mods</span><span class="o">,</span>
<span class="nx">used</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Env</span><span class="o">.</span><span class="nx">_used</span><span class="o">,</span>
<span class="nx">loader</span><span class="o">,</span>
<span class="nx">firstArg</span> <span class="o">=</span> <span class="nx">a</span><span class="o">[</span><span class="m">0</span><span class="o">],</span>
<span class="nx">dynamic</span> <span class="o">=</span> <span class="kc">false</span><span class="o">,</span>
<span class="nx">sorted</span> <span class="o">=</span> <span class="kc">false</span><span class="o">,</span>
<span class="nx">callback</span> <span class="o">=</span> <span class="nx">a</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="m">1</span><span class="o">];</span>
<span class="c">// The last argument supplied to use can be a load complete callback</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="k">typeof</span> <span class="nx">callback</span> <span class="o">===</span> <span class="s1">'function'</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">a</span><span class="o">.</span><span class="nx">pop</span><span class="o">();</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="nx">callback</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">}</span>
<span class="c">// YUI().use('*'); // bind everything available</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">firstArg</span> <span class="o">===</span> <span class="s2">"*"</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">a</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">k</span> <span class="k">in</span> <span class="nx">mods</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">mods</span><span class="o">.</span><span class="nx">hasOwnProperty</span><span class="o">(</span><span class="nx">k</span><span class="o">))</span> <span class="o">{</span>
<span class="nx">a</span><span class="o">.</span><span class="nx">push</span><span class="o">(</span><span class="nx">k</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">callback</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">a</span><span class="o">.</span><span class="nx">push</span><span class="o">(</span><span class="nx">callback</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">use</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="c">// Accept a loader instance with a pre-sorted list of dependencies</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">typeof</span> <span class="nx">firstArg</span> <span class="o">===</span> <span class="s1">'object'</span> <span class="o">&&</span> <span class="nx">firstArg</span><span class="o">.</span><span class="nx">sorted</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">a</span> <span class="o">=</span> <span class="nx">firstArg</span><span class="o">.</span><span class="nx">sorted</span><span class="o">;</span>
<span class="nx">sorted</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">// use loader to optimize and sort the requirements if it</span>
<span class="c"></span> <span class="c">// is available.</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(!</span><span class="nx">sorted</span> <span class="o">&&</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Loader</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">dynamic</span> <span class="o">=</span> <span class="kc">true</span><span class="o">;</span>
<span class="nx">loader</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Loader</span><span class="o">(</span><span class="nx">Y</span><span class="o">.</span><span class="nx">config</span><span class="o">);</span>
<span class="nx">loader</span><span class="o">.</span><span class="nx">require</span><span class="o">(</span><span class="nx">a</span><span class="o">);</span>
<span class="nx">loader</span><span class="o">.</span><span class="nx">ignoreRegistered</span> <span class="o">=</span> <span class="kc">true</span><span class="o">;</span>
<span class="nx">loader</span><span class="o">.</span><span class="nx">calculate</span><span class="o">();</span>
<span class="nx">a</span> <span class="o">=</span> <span class="nx">loader</span><span class="o">.</span><span class="nx">sorted</span><span class="o">;</span>
<span class="o">}</span>
<span class="c"></span>
<span class="k">var</span> <span class="nx">missing</span> <span class="o">=</span> <span class="o">[],</span> <span class="nx">r</span> <span class="o">=</span> <span class="o">[],</span> <span class="nx">f</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">name</span><span class="o">)</span> <span class="o">{</span>
<span class="c">// only attach a module once</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">used</span><span class="o">[</span><span class="nx">name</span><span class="o">])</span> <span class="o">{</span>
<span class="c"></span> <span class="k">return</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">var</span> <span class="nx">m</span> <span class="o">=</span> <span class="nx">mods</span><span class="o">[</span><span class="nx">name</span><span class="o">],</span> <span class="nx">j</span><span class="o">,</span> <span class="nx">req</span><span class="o">,</span> <span class="nx">use</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">m</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">used</span><span class="o">[</span><span class="nx">name</span><span class="o">]</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">dynamic</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">log</span><span class="o">(</span><span class="s1">'attaching '</span> <span class="o">+</span> <span class="nx">name</span><span class="o">);</span>
<span class="nx">m</span><span class="o">.</span><span class="nx">fn</span><span class="o">(</span><span class="nx">Y</span><span class="o">);</span>
<span class="o">}</span>
<span class="c"></span> <span class="nx">req</span> <span class="o">=</span> <span class="nx">m</span><span class="o">.</span><span class="nx">details</span><span class="o">.</span><span class="nx">requires</span><span class="o">;</span>
<span class="nx">use</span> <span class="o">=</span> <span class="nx">m</span><span class="o">.</span><span class="nx">details</span><span class="o">.</span><span class="nx">use</span><span class="o">;</span>
<span class="o">}</span> <span class="k">else</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="s1">'module not found: '</span> <span class="o">+</span> <span class="nx">name</span><span class="o">,</span> <span class="s1">'info'</span><span class="o">,</span> <span class="s1">'YUI'</span><span class="o">);</span>
<span class="nx">missing</span><span class="o">.</span><span class="nx">push</span><span class="o">(</span><span class="nx">name</span><span class="o">);</span>
<span class="o">}</span>
<span class="c">// make sure requirements are attached</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">req</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">isString</span><span class="o">(</span><span class="nx">req</span><span class="o">))</span> <span class="o">{</span>
<span class="nx">f</span><span class="o">(</span><span class="nx">req</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="nx">j</span> <span class="o">=</span> <span class="m">0</span><span class="o">;</span> <span class="nx">j</span> <span class="o"><</span> <span class="nx">req</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">j</span> <span class="o">=</span> <span class="nx">j</span> <span class="o">+</span> <span class="m">1</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">f</span><span class="o">(</span><span class="nx">req</span><span class="o">[</span><span class="nx">j</span><span class="o">]);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c">// add this module to full list of things to attach</span>
<span class="c"></span> <span class="nx">r</span><span class="o">.</span><span class="nx">push</span><span class="o">(</span><span class="nx">name</span><span class="o">);</span>
<span class="c">// auto-attach sub-modules</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">use</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">isString</span><span class="o">(</span><span class="nx">use</span><span class="o">))</span> <span class="o">{</span>
<span class="nx">f</span><span class="o">(</span><span class="nx">req</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="nx">j</span> <span class="o">=</span> <span class="m">0</span><span class="o">;</span> <span class="nx">j</span> <span class="o"><</span> <span class="nx">use</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">j</span> <span class="o">=</span> <span class="nx">j</span> <span class="o">+</span> <span class="m">1</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">f</span><span class="o">(</span><span class="nx">use</span><span class="o">[</span><span class="nx">j</span><span class="o">]);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">};</span>
<span class="c">// process each requirement and any additional requirements </span>
<span class="c"></span> <span class="c">// the module metadata specifies</span>
<span class="c"></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">l</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">l</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">f</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="o">}</span>
<span class="c"></span>
<span class="k">var</span> <span class="nx">attach</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">fromLoader</span><span class="o">)</span> <span class="o">{</span>
<span class="c"></span>
<span class="k">if</span> <span class="o">(!</span><span class="nx">fromLoader</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">l</span><span class="o">=</span><span class="nx">r</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">l</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="k">var</span> <span class="nx">m</span> <span class="o">=</span> <span class="nx">mods</span><span class="o">[</span><span class="nx">r</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">m</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="s1">'attaching '</span> <span class="o">+</span> <span class="nx">r</span><span class="o">[</span><span class="nx">i</span><span class="o">],</span> <span class="s1">'info'</span><span class="o">,</span> <span class="s1">'YUI'</span><span class="o">);</span>
<span class="nx">m</span><span class="o">.</span><span class="nx">fn</span><span class="o">(</span><span class="nx">Y</span><span class="o">);</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">callback</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">callback</span><span class="o">(</span><span class="nx">Y</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">fire</span><span class="o">)</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">'yui:load'</span><span class="o">,</span> <span class="nx">Y</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">sorted</span> <span class="o">&&</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Loader</span> <span class="o">&&</span> <span class="nx">missing</span><span class="o">.</span><span class="nx">length</span><span class="o">)</span> <span class="o">{</span>
<span class="c">// dynamic load</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="s1">'trying to get the missing modules '</span> <span class="o">+</span> <span class="nx">missing</span><span class="o">);</span>
<span class="nx">loader</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Loader</span><span class="o">(</span><span class="nx">Y</span><span class="o">.</span><span class="nx">config</span><span class="o">);</span>
<span class="nx">loader</span><span class="o">.</span><span class="nx">require</span><span class="o">(</span><span class="nx">missing</span><span class="o">);</span>
<span class="nx">loader</span><span class="o">.</span><span class="nx">insert</span><span class="o">();</span>
<span class="c">// loader calls use to automatically attach when finished</span>
<span class="c"></span> <span class="c">// but we still need to execute the callback.</span>
<span class="c"></span> <span class="nx">loader</span><span class="o">.</span><span class="nx">subscribe</span><span class="o">(</span><span class="s1">'success'</span><span class="o">,</span> <span class="nx">attach</span><span class="o">,</span> <span class="nx">loader</span><span class="o">,</span> <span class="s1">'loader'</span><span class="o">);</span>
<span class="c"></span> <span class="c">// });</span>
<span class="c"></span> <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="nx">attach</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="c">// chain support var yui = YUI().use('dragdrop');</span>
<span class="c"></span> <span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Returns the namespace specified and creates it if it doesn't exist</span>
<span class="c"> * <pre></span>
<span class="c"> * </pre></span>
<span class="c"> *</span>
<span class="c"> * Be careful when naming packages. Reserved words may work in some browsers</span>
<span class="c"> * and not others. For instance, the following will fail in Safari:</span>
<span class="c"> * <pre></span>
<span class="c"> * </pre></span>
<span class="c"> * This fails because "long" is a future reserved word in ECMAScript</span>
<span class="c"> *</span>
<span class="c"> * @method namespace</span>
<span class="c"> * @static</span>
<span class="c"> * @param {String*} arguments 1-n namespaces to create </span>
<span class="c"> * @return {Object} A reference to the last namespace object created</span>
<span class="c"> */</span>
<span class="nx">namespace</span><span class="o">:</span> <span class="k">function</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">arguments</span><span class="o">,</span> <span class="nx">o</span><span class="o">=</span><span class="kc">null</span><span class="o">,</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">j</span><span class="o">,</span> <span class="nx">d</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">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">d</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">split</span><span class="o">(</span><span class="s2">"."</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="k">for</span> <span class="o">(</span><span class="nx">j</span><span class="o">=(</span><span class="nx">d</span><span class="o">[</span><span class="m">0</span><span class="o">]</span> <span class="o">==</span> <span class="s2">"YUI"</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="nx">j</span><span class="o"><</span><span class="nx">d</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">j</span><span class="o">=</span><span class="nx">j</span><span class="o">+</span><span class="m">1</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">o</span><span class="o">[</span><span class="nx">d</span><span class="o">[</span><span class="nx">j</span><span class="o">]]</span> <span class="o">=</span> <span class="nx">o</span><span class="o">[</span><span class="nx">d</span><span class="o">[</span><span class="nx">j</span><span class="o">]]</span> <span class="o">||</span> <span class="o">{};</span>
<span class="nx">o</span> <span class="o">=</span> <span class="nx">o</span><span class="o">[</span><span class="nx">d</span><span class="o">[</span><span class="nx">j</span><span class="o">]];</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">o</span><span class="o">;</span>
<span class="o">},</span>
<span class="c">// this is replaced if the log module is included</span>
<span class="c"></span> <span class="nx">log</span><span class="o">:</span> <span class="k">function</span><span class="o">()</span> <span class="o">{</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Report an error. The reporting mechanism is controled by</span>
<span class="c"> * the 'throwFail' configuration attribute. If throwFail is</span>
<span class="c"> * not specified, the message is written to the logger, otherwise</span>
<span class="c"> * a JS error is thrown</span>
<span class="c"> * @method fail</span>
<span class="c"> * @param msg {string} the failure message</span>
<span class="c"> * @param e {Error} Optional JS error that was caught. If supplied</span>
<span class="c"> * and throwFail is specified, this error will be re-thrown.</span>
<span class="c"> * @return {YUI} this YUI instance</span>
<span class="c"> */</span>
<span class="nx">fail</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">msg</span><span class="o">,</span> <span class="nx">e</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="nx">log</span><span class="o">(</span><span class="nx">msg</span><span class="o">,</span> <span class="s2">"error"</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">config</span><span class="o">.</span><span class="nx">throwFail</span><span class="o">)</span> <span class="o">{</span>
<span class="k">throw</span> <span class="nx">e</span> <span class="o">||</span> <span class="k">new</span> <span class="nb">Error</span><span class="o">(</span><span class="nx">msg</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="o">},</span>
<span class="c">/**</span>
<span class="c"> * Generate an id that is unique among all YUI instances</span>
<span class="c"> * @method guid</span>
<span class="c"> * @param pre {string} optional guid prefix</span>
<span class="c"> * @return {string} the guid</span>
<span class="c"> */</span>
<span class="nx">guid</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">pre</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">Env</span><span class="o">,</span> <span class="nx">p</span> <span class="o">=</span> <span class="o">(</span><span class="nx">pre</span><span class="o">)</span> <span class="o">||</span> <span class="nx">e</span><span class="o">.</span><span class="nx">_pre</span><span class="o">;</span>
<span class="k">return</span> <span class="nx">p</span> <span class="o">+</span><span class="s1">'-'</span> <span class="o">+</span> <span class="nx">e</span><span class="o">.</span><span class="nx">_yidx</span> <span class="o">+</span> <span class="s1">'-'</span> <span class="o">+</span> <span class="nx">e</span><span class="o">.</span><span class="nx">_uidx</span><span class="o">++;</span>
<span class="o">},</span>
<span class="c">/**</span>
<span class="c"> * Stamps an object with a guid. If the object already</span>
<span class="c"> * has one, a new one is not created</span>
<span class="c"> * @method stamp</span>
<span class="c"> * @param o The object to stamp</span>
<span class="c"> * @return {string} The object's guid</span>
<span class="c"> */</span>
<span class="nx">stamp</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">if</span> <span class="o">(!</span><span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="nx">o</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">var</span> <span class="nx">uid</span> <span class="o">=</span> <span class="o">(</span><span class="k">typeof</span> <span class="nx">o</span> <span class="o">===</span> <span class="s1">'string'</span><span class="o">)</span> <span class="o">?</span> <span class="nx">o</span> <span class="o">:</span> <span class="nx">o</span><span class="o">.</span><span class="nx">_yuid</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(!</span><span class="nx">uid</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">uid</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">guid</span><span class="o">();</span>
<span class="nx">o</span><span class="o">.</span><span class="nx">_yuid</span> <span class="o">=</span> <span class="nx">uid</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">uid</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">};</span>
<span class="c">// Give the YUI global the same properties as an instance.</span>
<span class="c">// This makes it so that the YUI global can be used like the YAHOO</span>
<span class="c">// provides global metadata, so env needs to be configured.</span>
<span class="c">// @TODO review</span>
<span class="c"></span>
<span class="k">var</span> <span class="nx">Y</span> <span class="o">=</span> <span class="nx">YUI</span><span class="o">,</span> <span class="nx">p</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">prototype</span><span class="o">,</span> <span class="nx">i</span><span class="o">;</span>
<span class="c">// inheritance utilities are not available yet</span>
<span class="c"></span> <span class="k">for</span> <span class="o">(</span><span class="nx">i</span> <span class="k">in</span> <span class="nx">p</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="kc">true</span><span class="o">)</span> <span class="o">{</span> <span class="c">// hasOwnProperty not available yet and not needed</span>
<span class="c"></span> <span class="nx">Y</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">=</span> <span class="nx">p</span><span class="o">[</span><span class="nx">i</span><span class="o">];</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c">// set up the environment</span>
<span class="c"></span> <span class="nx">Y</span><span class="o">.</span><span class="nx">_init</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>