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