Upgrade to ExtJS 3.2.1 - Released 04/27/2010
[extjs.git] / src / ext-core / src / core / CompositeElementLite.js
1 /*!
2  * Ext JS Library 3.2.1
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.CompositeElementLite
9  * <p>This class encapsulates a <i>collection</i> of DOM elements, providing methods to filter
10  * members, or to perform collective actions upon the whole set.</p>
11  * <p>Although they are not listed, this class supports all of the methods of {@link Ext.Element} and
12  * {@link Ext.Fx}. The methods from these classes will be performed on all the elements in this collection.</p>
13  * Example:<pre><code>
14 var els = Ext.select("#some-el div.some-class");
15 // or select directly from an existing element
16 var el = Ext.get('some-el');
17 el.select('div.some-class');
18
19 els.setWidth(100); // all elements become 100 width
20 els.hide(true); // all elements fade out and hide
21 // or
22 els.setWidth(100).hide(true);
23 </code>
24  */
25 Ext.CompositeElementLite = function(els, root){
26     /**
27      * <p>The Array of DOM elements which this CompositeElement encapsulates. Read-only.</p>
28      * <p>This will not <i>usually</i> be accessed in developers' code, but developers wishing
29      * to augment the capabilities of the CompositeElementLite class may use it when adding
30      * methods to the class.</p>
31      * <p>For example to add the <code>nextAll</code> method to the class to <b>add</b> all
32      * following siblings of selected elements, the code would be</p><code><pre>
33 Ext.override(Ext.CompositeElementLite, {
34     nextAll: function() {
35         var els = this.elements, i, l = els.length, n, r = [], ri = -1;
36
37 //      Loop through all elements in this Composite, accumulating
38 //      an Array of all siblings.
39         for (i = 0; i < l; i++) {
40             for (n = els[i].nextSibling; n; n = n.nextSibling) {
41                 r[++ri] = n;
42             }
43         }
44
45 //      Add all found siblings to this Composite
46         return this.add(r);
47     }
48 });</pre></code>
49      * @type Array
50      * @property elements
51      */
52     this.elements = [];
53     this.add(els, root);
54     this.el = new Ext.Element.Flyweight();
55 };
56
57 Ext.CompositeElementLite.prototype = {
58     isComposite: true,
59
60     // private
61     getElement : function(el){
62         // Set the shared flyweight dom property to the current element
63         var e = this.el;
64         e.dom = el;
65         e.id = el.id;
66         return e;
67     },
68
69     // private
70     transformElement : function(el){
71         return Ext.getDom(el);
72     },
73
74     /**
75      * Returns the number of elements in this Composite.
76      * @return Number
77      */
78     getCount : function(){
79         return this.elements.length;
80     },
81     /**
82      * Adds elements to this Composite object.
83      * @param {Mixed} els Either an Array of DOM elements to add, or another Composite object who's elements should be added.
84      * @return {CompositeElement} This Composite object.
85      */
86     add : function(els, root){
87         var me = this,
88             elements = me.elements;
89         if(!els){
90             return this;
91         }
92         if(typeof els == "string"){
93             els = Ext.Element.selectorFunction(els, root);
94         }else if(els.isComposite){
95             els = els.elements;
96         }else if(!Ext.isIterable(els)){
97             els = [els];
98         }
99
100         for(var i = 0, len = els.length; i < len; ++i){
101             elements.push(me.transformElement(els[i]));
102         }
103         return me;
104     },
105
106     invoke : function(fn, args){
107         var me = this,
108             els = me.elements,
109             len = els.length,
110             e,
111             i;
112
113         for(i = 0; i < len; i++) {
114             e = els[i];
115             if(e){
116                 Ext.Element.prototype[fn].apply(me.getElement(e), args);
117             }
118         }
119         return me;
120     },
121     /**
122      * Returns a flyweight Element of the dom element object at the specified index
123      * @param {Number} index
124      * @return {Ext.Element}
125      */
126     item : function(index){
127         var me = this,
128             el = me.elements[index],
129             out = null;
130
131         if(el){
132             out = me.getElement(el);
133         }
134         return out;
135     },
136
137     // fixes scope with flyweight
138     addListener : function(eventName, handler, scope, opt){
139         var els = this.elements,
140             len = els.length,
141             i, e;
142
143         for(i = 0; i<len; i++) {
144             e = els[i];
145             if(e) {
146                 Ext.EventManager.on(e, eventName, handler, scope || e, opt);
147             }
148         }
149         return this;
150     },
151     /**
152      * <p>Calls the passed function for each element in this composite.</p>
153      * @param {Function} fn The function to call. The function is passed the following parameters:<ul>
154      * <li><b>el</b> : Element<div class="sub-desc">The current Element in the iteration.
155      * <b>This is the flyweight (shared) Ext.Element instance, so if you require a
156      * a reference to the dom node, use el.dom.</b></div></li>
157      * <li><b>c</b> : Composite<div class="sub-desc">This Composite object.</div></li>
158      * <li><b>idx</b> : Number<div class="sub-desc">The zero-based index in the iteration.</div></li>
159      * </ul>
160      * @param {Object} scope (optional) The scope (<i>this</i> reference) in which the function is executed. (defaults to the Element)
161      * @return {CompositeElement} this
162      */
163     each : function(fn, scope){
164         var me = this,
165             els = me.elements,
166             len = els.length,
167             i, e;
168
169         for(i = 0; i<len; i++) {
170             e = els[i];
171             if(e){
172                 e = this.getElement(e);
173                 if(fn.call(scope || e, e, me, i) === false){
174                     break;
175                 }
176             }
177         }
178         return me;
179     },
180
181     /**
182     * Clears this Composite and adds the elements passed.
183     * @param {Mixed} els Either an array of DOM elements, or another Composite from which to fill this Composite.
184     * @return {CompositeElement} this
185     */
186     fill : function(els){
187         var me = this;
188         me.elements = [];
189         me.add(els);
190         return me;
191     },
192
193     /**
194      * Filters this composite to only elements that match the passed selector.
195      * @param {String/Function} selector A string CSS selector or a comparison function.
196      * The comparison function will be called with the following arguments:<ul>
197      * <li><code>el</code> : Ext.Element<div class="sub-desc">The current DOM element.</div></li>
198      * <li><code>index</code> : Number<div class="sub-desc">The current index within the collection.</div></li>
199      * </ul>
200      * @return {CompositeElement} this
201      */
202     filter : function(selector){
203         var els = [],
204             me = this,
205             elements = me.elements,
206             fn = Ext.isFunction(selector) ? selector
207                 : function(el){
208                     return el.is(selector);
209                 };
210
211
212         me.each(function(el, self, i){
213             if(fn(el, i) !== false){
214                 els[els.length] = me.transformElement(el);
215             }
216         });
217         me.elements = els;
218         return me;
219     },
220
221     /**
222      * Find the index of the passed element within the composite collection.
223      * @param el {Mixed} The id of an element, or an Ext.Element, or an HtmlElement to find within the composite collection.
224      * @return Number The index of the passed Ext.Element in the composite collection, or -1 if not found.
225      */
226     indexOf : function(el){
227         return this.elements.indexOf(this.transformElement(el));
228     },
229
230     /**
231     * Replaces the specified element with the passed element.
232     * @param {Mixed} el The id of an element, the Element itself, the index of the element in this composite
233     * to replace.
234     * @param {Mixed} replacement The id of an element or the Element itself.
235     * @param {Boolean} domReplace (Optional) True to remove and replace the element in the document too.
236     * @return {CompositeElement} this
237     */
238     replaceElement : function(el, replacement, domReplace){
239         var index = !isNaN(el) ? el : this.indexOf(el),
240             d;
241         if(index > -1){
242             replacement = Ext.getDom(replacement);
243             if(domReplace){
244                 d = this.elements[index];
245                 d.parentNode.insertBefore(replacement, d);
246                 Ext.removeNode(d);
247             }
248             this.elements.splice(index, 1, replacement);
249         }
250         return this;
251     },
252
253     /**
254      * Removes all elements.
255      */
256     clear : function(){
257         this.elements = [];
258     }
259 };
260
261 Ext.CompositeElementLite.prototype.on = Ext.CompositeElementLite.prototype.addListener;
262
263 (function(){
264 var fnName,
265     ElProto = Ext.Element.prototype,
266     CelProto = Ext.CompositeElementLite.prototype;
267
268 for(fnName in ElProto){
269     if(Ext.isFunction(ElProto[fnName])){
270         (function(fnName){
271             CelProto[fnName] = CelProto[fnName] || function(){
272                 return this.invoke(fnName, arguments);
273             };
274         }).call(CelProto, fnName);
275
276     }
277 }
278 })();
279
280 if(Ext.DomQuery){
281     Ext.Element.selectorFunction = Ext.DomQuery.select;
282 }
283
284 /**
285  * Selects elements based on the passed CSS selector to enable {@link Ext.Element Element} methods
286  * to be applied to many related elements in one statement through the returned {@link Ext.CompositeElement CompositeElement} or
287  * {@link Ext.CompositeElementLite CompositeElementLite} object.
288  * @param {String/Array} selector The CSS selector or an array of elements
289  * @param {HTMLElement/String} root (optional) The root element of the query or id of the root
290  * @return {CompositeElementLite/CompositeElement}
291  * @member Ext.Element
292  * @method select
293  */
294 Ext.Element.select = function(selector, root){
295     var els;
296     if(typeof selector == "string"){
297         els = Ext.Element.selectorFunction(selector, root);
298     }else if(selector.length !== undefined){
299         els = selector;
300     }else{
301         throw "Invalid selector";
302     }
303     return new Ext.CompositeElementLite(els);
304 };
305 /**
306  * Selects elements based on the passed CSS selector to enable {@link Ext.Element Element} methods
307  * to be applied to many related elements in one statement through the returned {@link Ext.CompositeElement CompositeElement} or
308  * {@link Ext.CompositeElementLite CompositeElementLite} object.
309  * @param {String/Array} selector The CSS selector or an array of elements
310  * @param {HTMLElement/String} root (optional) The root element of the query or id of the root
311  * @return {CompositeElementLite/CompositeElement}
312  * @member Ext
313  * @method select
314  */
315 Ext.select = Ext.Element.select;