source: cows/branches/cows-vis/cows/pylons/project_templates/cows_server/+package+/public/js/yui/api/oop.js.html @ 5259

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/branches/cows-vis/cows/pylons/project_templates/cows_server/+package+/public/js/yui/api/oop.js.html@5259
Revision 5259, 38.0 KB checked in by domlowe, 11 years ago (diff)

adding yui javascript

Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2<html>
3<head>
4        <title>API: oop   oop.js  (YUI Library)</title>
5        <link rel="stylesheet" type="text/css" href="assets/api.css">
6    <script type="text/javascript" src="assets/api-js"></script>
7    <script type="text/javascript" src="assets/ac-js"></script>
8</head>
9
10<body id="yahoo-com">
11
12<div id="doc3" class="yui-t2">
13
14        <div id="hd">
15        <a href="http://developer.yahoo.com/yui/"><h1>Yahoo! UI Library</h1></a>
16        <h3>oop&nbsp; <span class="subtitle">3.0.0pr1</span></h3>
17        <p>
18        <a href="./index.html">Yahoo! UI Library</a> 
19            &gt; <a href="./module_oop.html">oop</a>
20               
21                 &gt; oop.js (source view)
22            </p>
23
24
25        </div>
26
27        <div id="bd">
28                <div id="yui-main">
29                        <div class="yui-b">
30            <form name="yui-classopts-form">
31    <span id="classopts"><input type="checkbox" name="showprivate" id="showprivate" /> <label for="showprivate">Show Private</label></span>
32    <span id="classopts"><input type="checkbox" name="showprotected" id="showprotected" /> <label for="showprotected">Show Protected</label></span>
33    <span id="classopts"><input type="checkbox" name="showdeprecated" id="showdeprecated" /> <label for="showdeprecated">Show Deprecated</label></span>
34            </form>
35
36                    <div id="srcout">
37                        <style>
38                            #doc3 #classopts { display:none; }
39                        </style>
40<div class="highlight" ><pre><span class="c">/**</span>
41<span class="c"> * OOP utils.  If included, the OOP methods are added to the YUI instance</span>
42<span class="c"> * @module oop</span>
43<span class="c"> */</span>
44<span class="nx">YUI</span><span class="o">.</span><span class="nx">add</span><span class="o">(</span><span class="s2">&quot;oop&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>
45
46    <span class="k">var</span> <span class="nx">L</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Lang</span><span class="o">,</span> 
47    <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>
48    <span class="nx">OP</span> <span class="o">=</span> <span class="nb">Object</span><span class="o">.</span><span class="nx">prototype</span><span class="o">,</span>
49    <span class="nx">O</span><span class="o">;</span>
50
51    <span class="c">/**</span>
52<span class="c">     * The following methods are added to the YUI instance</span>
53<span class="c">     * @class YUI~oop</span>
54<span class="c">     */</span>
55
56    <span class="c">/**</span>
57<span class="c">     * Applies prototype properties from the supplier to the receiver.</span>
58<span class="c">     * The receiver can be a constructor or an instance.</span>
59<span class="c">     * @method augment</span>
60<span class="c">     * @param {Function} r  the object to receive the augmentation</span>
61<span class="c">     * @param {Function} s  the object that supplies the properties to augment</span>
62<span class="c">     * @param ov {boolean} if true, properties already on the receiver</span>
63<span class="c">     * will be overwritten if found on the supplier.</span>
64<span class="c">     * @param wl {string[]} a whitelist.  If supplied, only properties in </span>
65<span class="c">     * this list will be applied to the receiver.</span>
66<span class="c">     * @param args {Array | Any} arg or arguments to apply to the supplier</span>
67<span class="c">     * constructor when initializing.</span>
68<span class="c">     * @return {object} the augmented object</span>
69<span class="c">     *</span>
70<span class="c">     * @todo constructor optional?</span>
71<span class="c">     * @todo understanding what an instance is augmented with</span>
72<span class="c">     * @TODO evaluate if we can do this in a way that doesn&#39;t interfere</span>
73<span class="c">     * with normal inheritance</span>
74<span class="c">     */</span>
75    <span class="nx">Y</span><span class="o">.</span><span class="nx">augment</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">r</span><span class="o">,</span> <span class="nx">s</span><span class="o">,</span> <span class="nx">ov</span><span class="o">,</span> <span class="nx">wl</span><span class="o">,</span> <span class="nx">args</span><span class="o">)</span> <span class="o">{</span>
76
77        <span class="k">var</span> <span class="nx">sProto</span> <span class="o">=</span> <span class="nx">s</span><span class="o">.</span><span class="nx">prototype</span><span class="o">,</span> <span class="nx">newProto</span> <span class="o">=</span> <span class="kc">null</span><span class="o">,</span> <span class="nx">construct</span> <span class="o">=</span> <span class="nx">s</span><span class="o">,</span> 
78            <span class="nx">a</span> <span class="o">=</span> <span class="o">(</span><span class="nx">args</span><span class="o">)</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">args</span><span class="o">)</span> <span class="o">:</span> <span class="o">[],</span> <span class="nx">rProto</span> <span class="o">=</span> <span class="nx">r</span><span class="o">.</span><span class="nx">prototype</span><span class="o">,</span> 
79            <span class="nx">target</span> <span class="o">=</span>  <span class="nx">rProto</span> <span class="o">||</span> <span class="nx">r</span><span class="o">,</span> <span class="nx">applyConstructor</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>
80
81        <span class="c">// working on a class, so apply constructor infrastructure</span>
82<span class="c"></span>        <span class="k">if</span> <span class="o">(</span><span class="nx">rProto</span> <span class="o">&amp;&amp;</span> <span class="nx">construct</span><span class="o">)</span> <span class="o">{</span>
83
84            <span class="c">// Y.log(&#39;augment will call constructor:&#39;);</span>
85<span class="c"></span>
86            <span class="c">// Y.Do.before(r, construct);</span>
87<span class="c"></span>
88            <span class="k">var</span> <span class="nx">sequestered</span> <span class="o">=</span> <span class="o">{},</span> <span class="nx">replacements</span> <span class="o">=</span> <span class="o">{};</span>
89            <span class="nx">newProto</span> <span class="o">=</span> <span class="o">{};</span>
90
91            <span class="c">// sequester all of the functions in the supplier and replace with</span>
92<span class="c"></span>            <span class="c">// one that will restore all of them.</span>
93<span class="c"></span>            <span class="nx">Y</span><span class="o">.</span><span class="nx">each</span><span class="o">(</span><span class="nx">sProto</span><span class="o">,</span> <span class="k">function</span><span class="o">(</span><span class="nx">v</span><span class="o">,</span> <span class="nx">k</span><span class="o">)</span> <span class="o">{</span>
94
95
96                <span class="c">// var initialized = false;</span>
97<span class="c"></span>
98                <span class="nx">replacements</span><span class="o">[</span><span class="nx">k</span><span class="o">]</span> <span class="o">=</span> <span class="k">function</span><span class="o">()</span> <span class="o">{</span>
99
100                    <span class="k">var</span> <span class="nx">me</span> <span class="o">=</span> <span class="k">this</span><span class="o">;</span>
101
102<span class="c">// Y.log(&#39;sequestered function &quot;&#39; + k + &#39;&quot; executed.  Initializing Event.Target&#39;);</span>
103<span class="c"></span>
104                    <span class="c">// overwrite the prototype with all of the sequestered functions,</span>
105<span class="c"></span>                    <span class="c">// but only if it hasn&#39;t been overridden</span>
106<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="k">in</span> <span class="nx">sequestered</span><span class="o">)</span> <span class="o">{</span>
107                        <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">sequestered</span><span class="o">,</span> <span class="nx">i</span><span class="o">)</span> <span class="o">&amp;&amp;</span> <span class="o">(</span><span class="nx">me</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">===</span> <span class="nx">replacements</span><span class="o">[</span><span class="nx">i</span><span class="o">]))</span> <span class="o">{</span>
108                            <span class="c">// Y.log(&#39;... restoring &#39; + k);</span>
109<span class="c"></span>                            <span class="nx">me</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">=</span> <span class="nx">sequestered</span><span class="o">[</span><span class="nx">i</span><span class="o">];</span>
110                        <span class="o">}</span>
111                    <span class="o">}</span>
112
113                    <span class="c">// apply the constructor</span>
114<span class="c"></span>                    <span class="nx">construct</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">me</span><span class="o">,</span> <span class="nx">a</span><span class="o">);</span>
115
116                    <span class="c">// apply the original sequestered function</span>
117<span class="c"></span>                    <span class="k">return</span> <span class="nx">sequestered</span><span class="o">[</span><span class="nx">k</span><span class="o">].</span><span class="nx">apply</span><span class="o">(</span><span class="nx">me</span><span class="o">,</span> <span class="nx">arguments</span><span class="o">);</span>
118
119                <span class="o">};</span>
120
121                <span class="k">if</span> <span class="o">((!</span><span class="nx">wl</span> <span class="o">||</span> <span class="o">(</span><span class="nx">k</span> <span class="k">in</span> <span class="nx">wl</span><span class="o">))</span> <span class="o">&amp;&amp;</span> <span class="o">(</span><span class="nx">ov</span> <span class="o">||</span> <span class="o">!(</span><span class="nx">k</span> <span class="k">in</span> <span class="k">this</span><span class="o">)))</span> <span class="o">{</span>
122
123                    <span class="c">// Y.log(&#39;augment: &#39; + k);</span>
124<span class="c"></span>
125                    <span class="k">if</span> <span class="o">(</span><span class="nx">L</span><span class="o">.</span><span class="nx">isFunction</span><span class="o">(</span><span class="nx">v</span><span class="o">))</span> <span class="o">{</span>
126
127                        <span class="c">// sequester the function</span>
128<span class="c"></span>                        <span class="nx">sequestered</span><span class="o">[</span><span class="nx">k</span><span class="o">]</span> <span class="o">=</span> <span class="nx">v</span><span class="o">;</span>
129
130                        <span class="c">// replace the sequestered function with a function that will</span>
131<span class="c"></span>                        <span class="c">// restore all sequestered functions and exectue the constructor.</span>
132<span class="c"></span>                        <span class="k">this</span><span class="o">[</span><span class="nx">k</span><span class="o">]</span> <span class="o">=</span> <span class="nx">replacements</span><span class="o">[</span><span class="nx">k</span><span class="o">];</span>
133
134                    <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
135
136                        <span class="c">// Y.log(&#39;augment() applying non-function: &#39; + k);</span>
137<span class="c"></span>
138                        <span class="k">this</span><span class="o">[</span><span class="nx">k</span><span class="o">]</span> <span class="o">=</span> <span class="nx">v</span><span class="o">;</span>
139                    <span class="o">}</span>
140
141                <span class="o">}</span>
142
143            <span class="o">},</span> <span class="nx">newProto</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
144
145        <span class="c">// augmenting an instance, so apply the constructor immediately</span>
146<span class="c"></span>        <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
147            <span class="nx">applyConstructor</span> <span class="o">=</span> <span class="kc">true</span><span class="o">;</span>
148        <span class="o">}</span>
149
150        <span class="nx">Y</span><span class="o">.</span><span class="nx">mix</span><span class="o">(</span><span class="nx">target</span><span class="o">,</span> <span class="nx">newProto</span> <span class="o">||</span> <span class="nx">sProto</span><span class="o">,</span> <span class="nx">ov</span><span class="o">,</span> <span class="nx">wl</span><span class="o">);</span>
151
152        <span class="k">if</span> <span class="o">(</span><span class="nx">applyConstructor</span><span class="o">)</span> <span class="o">{</span>
153            <span class="nx">s</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">target</span><span class="o">,</span> <span class="nx">a</span><span class="o">);</span>
154        <span class="o">}</span>
155
156        <span class="k">return</span> <span class="nx">r</span><span class="o">;</span>
157    <span class="o">};</span>
158
159    <span class="c">/**</span>
160<span class="c">     * Applies object properties from the supplier to the receiver.  If</span>
161<span class="c">     * the target has the property, and the property is an object, the target</span>
162<span class="c">     * object will be augmented with the supplier&#39;s value.  If the property</span>
163<span class="c">     * is an array, the suppliers value will be appended to the target.</span>
164<span class="c">     * @method aggregate</span>
165<span class="c">     * @param {Function} r  the object to receive the augmentation</span>
166<span class="c">     * @param {Function} s  the object that supplies the properties to augment</span>
167<span class="c">     * @param ov {boolean} if true, properties already on the receiver</span>
168<span class="c">     * will be overwritten if found on the supplier.</span>
169<span class="c">     * @param wl {string[]} a whitelist.  If supplied, only properties in </span>
170<span class="c">     * this list will be applied to the receiver.</span>
171<span class="c">     * @return {object} the extended object</span>
172<span class="c">     */</span>
173    <span class="nx">Y</span><span class="o">.</span><span class="nx">aggregate</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">r</span><span class="o">,</span> <span class="nx">s</span><span class="o">,</span> <span class="nx">ov</span><span class="o">,</span> <span class="nx">wl</span><span class="o">)</span> <span class="o">{</span>
174        <span class="k">return</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">mix</span><span class="o">(</span><span class="nx">r</span><span class="o">,</span> <span class="nx">s</span><span class="o">,</span> <span class="nx">ov</span><span class="o">,</span> <span class="nx">wl</span><span class="o">,</span> <span class="m">0</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
175    <span class="o">};</span>
176
177    <span class="c">/**</span>
178<span class="c">     * Utility to set up the prototype, constructor and superclass properties to</span>
179<span class="c">     * support an inheritance strategy that can chain constructors and methods.</span>
180<span class="c">     * Static members will not be inherited.</span>
181<span class="c">     *</span>
182<span class="c">     * @method extend</span>
183<span class="c">     * @param {Function} r   the object to modify</span>
184<span class="c">     * @param {Function} s the object to inherit</span>
185<span class="c">     * @param {Object} px prototype properties to add/override</span>
186<span class="c">     * @param {Object} sx static properties to add/override</span>
187<span class="c">     * @return {YUI} the YUI instance</span>
188<span class="c">     */</span>
189    <span class="nx">Y</span><span class="o">.</span><span class="nx">extend</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">r</span><span class="o">,</span> <span class="nx">s</span><span class="o">,</span> <span class="nx">px</span><span class="o">,</span> <span class="nx">sx</span><span class="o">)</span> <span class="o">{</span>
190        <span class="k">if</span> <span class="o">(!</span><span class="nx">s</span><span class="o">||!</span><span class="nx">r</span><span class="o">)</span> <span class="o">{</span>
191            <span class="nx">Y</span><span class="o">.</span><span class="nx">fail</span><span class="o">(</span><span class="s2">&quot;extend failed, verify dependencies&quot;</span><span class="o">);</span>
192        <span class="o">}</span>
193
194        <span class="k">var</span> <span class="nx">sp</span> <span class="o">=</span> <span class="nx">s</span><span class="o">.</span><span class="nx">prototype</span><span class="o">,</span> <span class="nx">rp</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">sp</span><span class="o">),</span> <span class="nx">i</span><span class="o">;</span>
195        <span class="nx">r</span><span class="o">.</span><span class="nx">prototype</span><span class="o">=</span><span class="nx">rp</span><span class="o">;</span>
196
197        <span class="nx">rp</span><span class="o">.</span><span class="nx">constructor</span><span class="o">=</span><span class="nx">r</span><span class="o">;</span>
198        <span class="nx">r</span><span class="o">.</span><span class="nx">superclass</span><span class="o">=</span><span class="nx">sp</span><span class="o">;</span>
199
200        <span class="c">// If the superclass doesn&#39;t have a standard constructor,</span>
201<span class="c"></span>        <span class="c">// define one so that Super() works</span>
202<span class="c"></span>        <span class="k">if</span> <span class="o">(</span><span class="nx">s</span> <span class="o">!=</span> <span class="nb">Object</span> <span class="o">&amp;&amp;</span> <span class="nx">sp</span><span class="o">.</span><span class="nx">constructor</span> <span class="o">==</span> <span class="nx">OP</span><span class="o">.</span><span class="nx">constructor</span><span class="o">)</span> <span class="o">{</span>
203            <span class="nx">sp</span><span class="o">.</span><span class="nx">constructor</span><span class="o">=</span><span class="nx">s</span><span class="o">;</span>
204        <span class="o">}</span>
205   
206        <span class="c">// Add object properties too</span>
207<span class="c"></span>        <span class="c">// @TODO removed for now because it isn&#39;t that useful and</span>
208<span class="c"></span>        <span class="c">// has caused a few issues overwriting things that should</span>
209<span class="c"></span>        <span class="c">// not be.  You can do this manually if needed.  Revisit</span>
210<span class="c"></span>        <span class="c">// if this is something that really is needed for some</span>
211<span class="c"></span>        <span class="c">// reason.</span>
212<span class="c"></span>        <span class="c">// Y.mix(r, s);</span>
213<span class="c"></span>
214        <span class="c">// Add superclass convienience functions </span>
215<span class="c"></span>        <span class="c">// @TODO revisit when we have something that works</span>
216<span class="c"></span>        <span class="c">// Y.augment(r, Ext);</span>
217<span class="c"></span>
218        <span class="c">// Add prototype overrides</span>
219<span class="c"></span>        <span class="k">if</span> <span class="o">(</span><span class="nx">px</span><span class="o">)</span> <span class="o">{</span>
220            <span class="nx">Y</span><span class="o">.</span><span class="nx">mix</span><span class="o">(</span><span class="nx">rp</span><span class="o">,</span> <span class="nx">px</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
221        <span class="o">}</span>
222
223        <span class="c">// Add object overrides</span>
224<span class="c"></span>        <span class="k">if</span> <span class="o">(</span><span class="nx">sx</span><span class="o">)</span> <span class="o">{</span>
225            <span class="nx">Y</span><span class="o">.</span><span class="nx">mix</span><span class="o">(</span><span class="nx">r</span><span class="o">,</span> <span class="nx">sx</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
226        <span class="o">}</span>
227
228        <span class="k">return</span> <span class="nx">r</span><span class="o">;</span>
229    <span class="o">};</span>
230
231    <span class="c">/**</span>
232<span class="c">     * Executes the supplied function for each item in</span>
233<span class="c">     * a collection.  Supports arrays, objects, and</span>
234<span class="c">     * Y.NodeLists</span>
235<span class="c">     * @method each</span>
236<span class="c">     * @param o the object to iterate</span>
237<span class="c">     * @param f the function to execute.  This function</span>
238<span class="c">     * receives the value, key, and object as parameters</span>
239<span class="c">     * @param proto if true, prototype properties are</span>
240<span class="c">     * iterated on objects</span>
241<span class="c">     * @return {YUI} the YUI instance</span>
242<span class="c">     */</span>
243    <span class="nx">Y</span><span class="o">.</span><span class="nx">each</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="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">,</span> <span class="nx">proto</span><span class="o">)</span> <span class="o">{</span>
244
245        <span class="k">if</span> <span class="o">(</span><span class="nx">o</span><span class="o">.</span><span class="nx">each</span> <span class="o">&amp;&amp;</span> <span class="nx">o</span><span class="o">.</span><span class="nx">item</span><span class="o">)</span> <span class="o">{</span>
246            <span class="k">return</span> <span class="nx">o</span><span class="o">.</span><span class="nx">each</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">);</span>
247        <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
248            <span class="nx">switch</span> <span class="o">(</span><span class="nx">A</span><span class="o">.</span><span class="nx">test</span><span class="o">(</span><span class="nx">o</span><span class="o">))</span> <span class="o">{</span>
249                <span class="nx">case</span> <span class="m">1</span><span class="o">:</span>
250                    <span class="k">return</span> <span class="nx">A</span><span class="o">.</span><span class="nx">each</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">);</span>
251                <span class="nx">case</span> <span class="m">2</span><span class="o">:</span>
252                    <span class="k">return</span> <span class="nx">A</span><span class="o">.</span><span class="nx">each</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">o</span><span class="o">,</span> <span class="m">0</span><span class="o">,</span> <span class="kc">true</span><span class="o">),</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">);</span>
253                <span class="nx">default</span><span class="o">:</span>
254                    <span class="k">return</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Object</span><span class="o">.</span><span class="nx">each</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">,</span> <span class="nx">proto</span><span class="o">);</span>
255            <span class="o">}</span>
256        <span class="o">}</span>
257
258        <span class="c">// return Y.Object.each(o, f, c);</span>
259<span class="c"></span>    <span class="o">};</span>
260
261    <span class="c">/**</span>
262<span class="c">     * Deep obj/array copy.  Functions will are cloned with Y.bind.</span>
263<span class="c">     * Array-like objects are treated as arrays.</span>
264<span class="c">     * primitives are returned untouched.  Optionally a</span>
265<span class="c">     * function can be provided to handle other data types,</span>
266<span class="c">     * filter keys, validate values, etc.</span>
267<span class="c">     *</span>
268<span class="c">     * @method clone</span>
269<span class="c">     * @param o what to clone</span>
270<span class="c">     * @param safe {boolean} if true, objects will not have prototype</span>
271<span class="c">     * items from the source.  If false, it does.  In this case, the</span>
272<span class="c">     * original is initally protected, but the clone is not completely immune</span>
273<span class="c">     * from changes to the source object prototype.  Also, cloned prototype</span>
274<span class="c">     * items that are deleted from the clone will result in the value</span>
275<span class="c">     * of the source prototype to be exposed.  If operating on a non-safe</span>
276<span class="c">     * clone, items should be nulled out rather than deleted.</span>
277<span class="c">     * @TODO review</span>
278<span class="c">     * @param f optional function to apply to each item in a collection</span>
279<span class="c">     *          it will be executed prior to applying the value to</span>
280<span class="c">     *          the new object.  Return false to prevent the copy.</span>
281<span class="c">     * @param c optional execution context for f</span>
282<span class="c">     * @param owner Owner object passed when clone is iterating an</span>
283<span class="c">     * object.  Used to set up context for cloned functions.</span>
284<span class="c">     * @return {Array|Object} the cloned object</span>
285<span class="c">     */</span>
286    <span class="nx">Y</span><span class="o">.</span><span class="nx">clone</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="nx">safe</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">,</span> <span class="nx">owner</span><span class="o">)</span> <span class="o">{</span>
287
288        <span class="k">if</span> <span class="o">(!</span><span class="nx">L</span><span class="o">.</span><span class="nx">isObject</span><span class="o">(</span><span class="nx">o</span><span class="o">))</span> <span class="o">{</span>
289            <span class="k">return</span> <span class="nx">o</span><span class="o">;</span>
290        <span class="o">}</span>
291       
292        <span class="k">if</span> <span class="o">(</span><span class="nx">L</span><span class="o">.</span><span class="nx">isDate</span><span class="o">(</span><span class="nx">o</span><span class="o">))</span> <span class="o">{</span>
293            <span class="k">return</span> <span class="k">new</span> <span class="nb">Date</span><span class="o">(</span><span class="nx">o</span><span class="o">);</span>
294        <span class="o">}</span>
295
296        <span class="k">var</span> <span class="nx">func</span> <span class="o">=</span> <span class="nx">L</span><span class="o">.</span><span class="nx">isFunction</span><span class="o">(</span><span class="nx">o</span><span class="o">),</span> <span class="nx">o2</span><span class="o">;</span>
297
298        <span class="k">if</span> <span class="o">(</span><span class="nx">func</span><span class="o">)</span> <span class="o">{</span>
299            <span class="k">if</span> <span class="o">(</span><span class="nx">o</span> <span class="k">instanceof</span> <span class="nb">RegExp</span><span class="o">)</span> <span class="o">{</span>
300                <span class="k">return</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="o">(</span><span class="nx">o</span><span class="o">.</span><span class="nx">source</span><span class="o">);</span>
301            <span class="o">}</span>
302            <span class="nx">o2</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">bind</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="nx">owner</span><span class="o">);</span>
303        <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
304            <span class="nx">o2</span> <span class="o">=</span> <span class="o">(</span><span class="nx">safe</span><span class="o">)</span> <span class="o">?</span> <span class="o">{}</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">o</span><span class="o">);</span>
305        <span class="o">}</span>
306
307        <span class="nx">Y</span><span class="o">.</span><span class="nx">each</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span> <span class="k">function</span><span class="o">(</span><span class="nx">v</span><span class="o">,</span> <span class="nx">k</span><span class="o">)</span> <span class="o">{</span>
308            <span class="k">if</span> <span class="o">(!</span><span class="nx">f</span> <span class="o">||</span> <span class="o">(</span><span class="nx">f</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">c</span> <span class="o">||</span> <span class="k">this</span><span class="o">,</span> <span class="nx">v</span><span class="o">,</span> <span class="nx">k</span><span class="o">,</span> <span class="k">this</span><span class="o">,</span> <span class="nx">o</span><span class="o">)</span> <span class="o">!==</span> <span class="kc">false</span><span class="o">))</span> <span class="o">{</span>
309                <span class="k">this</span><span class="o">[</span><span class="nx">k</span><span class="o">]</span> <span class="o">=</span>  <span class="nx">Y</span><span class="o">.</span><span class="nx">clone</span><span class="o">(</span><span class="nx">v</span><span class="o">,</span> <span class="nx">safe</span><span class="o">,</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">,</span> <span class="k">this</span><span class="o">);</span>
310            <span class="o">}</span>
311        <span class="o">},</span> <span class="nx">o2</span><span class="o">);</span>
312
313        <span class="k">return</span> <span class="nx">o2</span><span class="o">;</span>
314    <span class="o">};</span>
315   
316    <span class="c">/**</span>
317<span class="c">     * Returns a function that will execute the supplied function in the</span>
318<span class="c">     * supplied object&#39;s context, optionally adding any additional</span>
319<span class="c">     * supplied parameters to the end of the arguments the function</span>
320<span class="c">     * is executed with.</span>
321<span class="c">     *</span>
322<span class="c">     * In some cases it is preferable to have the additional arguments</span>
323<span class="c">     * applied to the beginning of the function signature.  For instance,</span>
324<span class="c">     * FireFox setTimeout/setInterval supplies a parameter that other</span>
325<span class="c">     * browsers do not.  </span>
326<span class="c">     * Note: YUI provides a later() function which wraps setTimeout/setInterval,</span>
327<span class="c">     * providing context adjustment and parameter addition.  This can be </span>
328<span class="c">     * used instead of setTimeout/setInterval, avoiding the arguments</span>
329<span class="c">     * collection issue when using bind() in FireFox.</span>
330<span class="c">     *</span>
331<span class="c">     * @TODO review param order for PR2</span>
332<span class="c">     * @method bind</span>
333<span class="c">     * @param f {Function} the function to bind</span>
334<span class="c">     * @param c the execution context</span>
335<span class="c">     * @param args* 0..n arguments to append to the end of arguments collection</span>
336<span class="c">     * supplied to the function</span>
337<span class="c">     * @return {function} the wrapped function</span>
338<span class="c">     */</span>
339    <span class="nx">Y</span><span class="o">.</span><span class="nx">bind</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">f</span><span class="o">,</span> <span class="nx">c</span><span class="o">)</span> <span class="o">{</span>
340        <span class="k">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">arguments</span><span class="o">,</span> <span class="m">2</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
341        <span class="k">return</span> <span class="k">function</span> <span class="o">()</span> <span class="o">{</span>
342            <span class="k">return</span> <span class="nx">f</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">c</span> <span class="o">||</span> <span class="nx">f</span><span class="o">,</span> <span class="nx">Y</span><span class="o">.</span><span class="nb">Array</span><span class="o">(</span><span class="nx">arguments</span><span class="o">,</span> <span class="m">0</span><span class="o">,</span> <span class="kc">true</span><span class="o">).</span><span class="nx">concat</span><span class="o">(</span><span class="nx">a</span><span class="o">));</span>
343        <span class="o">};</span>
344    <span class="o">};</span>
345
346    <span class="c">/*</span>
347<span class="c">     * Returns a function that will execute the supplied function in the</span>
348<span class="c">     * supplied object&#39;s context, optionally adding any additional</span>
349<span class="c">     * supplied parameters to the beginning of the arguments collection the </span>
350<span class="c">     * supplied to the function.</span>
351<span class="c">     *</span>
352<span class="c">     * In some cases it is preferable to have the additional arguments</span>
353<span class="c">     * applied to the end of the function signature.  @see bind for a version </span>
354<span class="c">     * that does this.</span>
355<span class="c">     *</span>
356<span class="c">     * @TODO consider adding this in PR2</span>
357<span class="c">     *</span>
358<span class="c">     * @method rbind</span>
359<span class="c">     * @param f {Function} the function to bind</span>
360<span class="c">     * @param c the execution context</span>
361<span class="c">     * @param args* 0..n arguments to append to the arguments collection for the function</span>
362<span class="c">     * @return {function} the wrapped function</span>
363<span class="c">     */</span>
364    <span class="c">// Y.rbind = function(f, c) {</span>
365<span class="c"></span>    <span class="c">//     var a = Y.Array(arguments, 2, true);</span>
366<span class="c"></span>    <span class="c">//     return function () {</span>
367<span class="c"></span>    <span class="c">//         return f.apply(c || f, a.concat(Y.Array(arguments, 0, true)));</span>
368<span class="c"></span>    <span class="c">//     };</span>
369<span class="c"></span>    <span class="c">// };</span>
370<span class="c"></span>
371
372<span class="o">},</span> <span class="s2">&quot;@VERSION@&quot;</span><span class="o">);</span>
373</pre></div>
374                    </div>
375                        </div>
376                </div>
377                <div class="yui-b">
378            <div class="nav">
379
380                    <div class="module">
381                        <h4>Modules</h4>
382                        <ul class="content">
383
384                                <li class=""><a href="module_anim.html">anim</a></li>
385
386                                <li class=""><a href="module_attribute.html">attribute</a></li>
387
388                                <li class=""><a href="module_base.html">base</a></li>
389
390                                <li class=""><a href="module_cookie.html">cookie</a></li>
391
392                                <li class=""><a href="module_dd.html">dd</a></li>
393
394                                <li class=""><a href="module_dd-plugin.html">dd-plugin</a></li>
395
396                                <li class=""><a href="module_dom.html">dom</a></li>
397
398                                <li class=""><a href="module_dump.html">dump</a></li>
399
400                                <li class=""><a href="module_event.html">event</a></li>
401
402                                <li class=""><a href="module_io.html">io</a></li>
403
404                                <li class=""><a href="module_json.html">json</a></li>
405
406                                <li class=""><a href="module_node.html">node</a></li>
407
408                                <li class="selected"><a href="module_oop.html">oop</a></li>
409
410                                <li class=""><a href="module_queue.html">queue</a></li>
411
412                                <li class=""><a href="module_substitute.html">substitute</a></li>
413
414                                <li class=""><a href="module_yui.html">yui</a></li>
415                        </ul>
416                    </div>
417
418                    <div class="module">
419                        <h4>Classes</h4>
420                        <ul class="content">
421                                <li class=""><a href="YUI~oop.html">YUI~oop</a></li>
422                        </ul>
423                    </div>
424
425                    <div class="module">
426                        <h4>Files</h4>
427                        <ul class="content">       
428                                <li class="selected"><a href="oop.js.html">oop.js</a></li>
429                        </ul>
430                    </div>
431
432
433
434
435
436            </div>
437                </div>
438        </div>
439        <div id="ft">
440        <hr />
441        Copyright &copy; 2008 Yahoo! Inc. All rights reserved.
442        </div>
443</div>
444</body>
445</html>
Note: See TracBrowser for help on using the repository browser.