Upgrade to ExtJS 3.1.1 - Released 02/08/2010
[extjs.git] / src / core / CompositeElement.js
1 /*!
2  * Ext JS Library 3.1.1
3  * Copyright(c) 2006-2010 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.CompositeElement\r
9  * @extends Ext.CompositeElementLite\r
10  * <p>This class encapsulates a <i>collection</i> of DOM elements, providing methods to filter\r
11  * members, or to perform collective actions upon the whole set.</p>\r
12  * <p>Although they are not listed, this class supports all of the methods of {@link Ext.Element} and\r
13  * {@link Ext.Fx}. The methods from these classes will be performed on all the elements in this collection.</p>\r
14  * <p>All methods return <i>this</i> and can be chained.</p>\r
15  * Usage:\r
16 <pre><code>\r
17 var els = Ext.select("#some-el div.some-class", true);\r
18 // or select directly from an existing element\r
19 var el = Ext.get('some-el');\r
20 el.select('div.some-class', true);\r
21 \r
22 els.setWidth(100); // all elements become 100 width\r
23 els.hide(true); // all elements fade out and hide\r
24 // or\r
25 els.setWidth(100).hide(true);\r
26 </code></pre>\r
27  */\r
28 Ext.CompositeElement = function(els, root){\r
29     this.elements = [];\r
30     this.add(els, root);\r
31 };\r
32 \r
33 Ext.extend(Ext.CompositeElement, Ext.CompositeElementLite, {\r
34     \r
35     // private\r
36     getElement : function(el){\r
37         // In this case just return it, since we already have a reference to it\r
38         return el;\r
39     },\r
40     \r
41     // private\r
42     transformElement : function(el){\r
43         return Ext.get(el);\r
44     }\r
45 \r
46     /**\r
47     * Adds elements to this composite.\r
48     * @param {String/Array} els A string CSS selector, an array of elements or an element\r
49     * @return {CompositeElement} this\r
50     */\r
51 \r
52     /**\r
53      * Returns the Element object at the specified index\r
54      * @param {Number} index\r
55      * @return {Ext.Element}\r
56      */\r
57 \r
58     /**\r
59      * Iterates each <code>element</code> in this <code>composite</code>\r
60      * calling the supplied function using {@link Ext#each}.\r
61      * @param {Function} fn The function to be called with each\r
62      * <code>element</code>. If the supplied function returns <tt>false</tt>,\r
63      * iteration stops. This function is called with the following arguments:\r
64      * <div class="mdetail-params"><ul>\r
65      * <li><code>element</code> : <i>Ext.Element</i><div class="sub-desc">The element at the current <code>index</code>\r
66      * in the <code>composite</code></div></li>\r
67      * <li><code>composite</code> : <i>Object</i> <div class="sub-desc">This composite.</div></li>\r
68      * <li><code>index</code> : <i>Number</i> <div class="sub-desc">The current index within the <code>composite</code> </div></li>\r
69      * </ul></div>\r
70      * @param {Object} scope (optional) The scope (<code><this</code> reference) in which the specified function is executed.\r
71      * Defaults to the <code>element</code> at the current <code>index</code>\r
72      * within the composite.\r
73      * @return {CompositeElement} this\r
74      */\r
75 });\r
76 \r
77 /**\r
78  * Selects elements based on the passed CSS selector to enable {@link Ext.Element Element} methods\r
79  * to be applied to many related elements in one statement through the returned {@link Ext.CompositeElement CompositeElement} or\r
80  * {@link Ext.CompositeElementLite CompositeElementLite} object.\r
81  * @param {String/Array} selector The CSS selector or an array of elements\r
82  * @param {Boolean} unique (optional) true to create a unique Ext.Element for each element (defaults to a shared flyweight object)\r
83  * @param {HTMLElement/String} root (optional) The root element of the query or id of the root\r
84  * @return {CompositeElementLite/CompositeElement}\r
85  * @member Ext.Element\r
86  * @method select\r
87  */\r
88 Ext.Element.select = function(selector, unique, root){\r
89     var els;\r
90     if(typeof selector == "string"){\r
91         els = Ext.Element.selectorFunction(selector, root);\r
92     }else if(selector.length !== undefined){\r
93         els = selector;\r
94     }else{\r
95         throw "Invalid selector";\r
96     }\r
97 \r
98     return (unique === true) ? new Ext.CompositeElement(els) : new Ext.CompositeElementLite(els);\r
99 };\r
100 \r
101 /**\r
102  * Selects elements based on the passed CSS selector to enable {@link Ext.Element Element} methods\r
103  * to be applied to many related elements in one statement through the returned {@link Ext.CompositeElement CompositeElement} or\r
104  * {@link Ext.CompositeElementLite CompositeElementLite} object.\r
105  * @param {String/Array} selector The CSS selector or an array of elements\r
106  * @param {Boolean} unique (optional) true to create a unique Ext.Element for each element (defaults to a shared flyweight object)\r
107  * @param {HTMLElement/String} root (optional) The root element of the query or id of the root\r
108  * @return {CompositeElementLite/CompositeElement}\r
109  * @member Ext.Element\r
110  * @method select\r
111  */\r
112 Ext.select = Ext.Element.select;