Roo/CompositeElement.js
[roojs1] / Roo / CompositeElement.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11
12
13 /**
14  * @class Roo.CompositeElement
15  * Standard composite class. Creates a Roo.Element for every element in the collection.
16  * <br><br>
17  * <b>NOTE: Although they are not listed, this class supports all of the set/update methods of Roo.Element. All Roo.Element
18  * actions will be performed on all the elements in this collection.</b>
19  * <br><br>
20  * All methods return <i>this</i> and can be chained.
21  <pre><code>
22  var els = Roo.select("#some-el div.some-class", true);
23  // or select directly from an existing element
24  var el = Roo.get('some-el');
25  el.select('div.some-class', true);
26
27  els.setWidth(100); // all elements become 100 width
28  els.hide(true); // all elements fade out and hide
29  // or
30  els.setWidth(100).hide(true);
31  </code></pre>
32  */
33 Roo.CompositeElement = function(els){
34     this.elements = [];
35     this.addElements(els);
36 };
37 Roo.CompositeElement.prototype = {
38     isComposite: true,
39     addElements : function(els){
40         if(!els) return this;
41         if(typeof els == "string"){
42             els = Roo.Element.selectorFunction(els);
43         }
44         var yels = this.elements;
45         var index = yels.length-1;
46         for(var i = 0, len = els.length; i < len; i++) {
47                 yels[++index] = Roo.get(els[i]);
48         }
49         return this;
50     },
51
52     /**
53     * Clears this composite and adds the elements returned by the passed selector.
54     * @param {String/Array} els A string CSS selector, an array of elements or an element
55     * @return {CompositeElement} this
56     */
57     fill : function(els){
58         this.elements = [];
59         this.add(els);
60         return this;
61     },
62
63     /**
64     * Filters this composite to only elements that match the passed selector.
65     * @param {String} selector A string CSS selector
66     * @param {Boolean} inverse return inverse filter (not matches)
67     * @return {CompositeElement} this
68     */
69     filter : function(selector, inverse){
70         var els = [];
71         this.each(function(el){
72             if(el.is(selector)){
73                 els[els.length] = el.dom;
74             }
75         });
76         this.fill(els);
77         return this;
78     },
79
80     invoke : function(fn, args){
81         var els = this.elements;
82         for(var i = 0, len = els.length; i < len; i++) {
83                 Roo.Element.prototype[fn].apply(els[i], args);
84         }
85         return this;
86     },
87     /**
88     * Adds elements to this composite.
89     * @param {String/Array} els A string CSS selector, an array of elements or an element
90     * @return {CompositeElement} this
91     */
92     add : function(els){
93         if(typeof els == "string"){
94             this.addElements(Roo.Element.selectorFunction(els));
95         }else if(els.length !== undefined){
96             this.addElements(els);
97         }else{
98             this.addElements([els]);
99         }
100         return this;
101     },
102     /**
103     * Calls the passed function passing (el, this, index) for each element in this composite.
104     * @param {Function} fn The function to call
105     * @param {Object} scope (optional) The <i>this</i> object (defaults to the element)
106     * @return {CompositeElement} this
107     */
108     each : function(fn, scope){
109         var els = this.elements;
110         for(var i = 0, len = els.length; i < len; i++){
111             if(fn.call(scope || els[i], els[i], this, i) === false) {
112                 break;
113             }
114         }
115         return this;
116     },
117
118     /**
119      * Returns the Element object at the specified index
120      * @param {Number} index
121      * @return {Roo.Element}
122      */
123     item : function(index){
124         return this.elements[index] || null;
125     },
126
127     /**
128      * Returns the first Element
129      * @return {Roo.Element}
130      */
131     first : function(){
132         return this.item(0);
133     },
134
135     /**
136      * Returns the last Element
137      * @return {Roo.Element}
138      */
139     last : function(){
140         return this.item(this.elements.length-1);
141     },
142
143     /**
144      * Returns the number of elements in this composite
145      * @return Number
146      */
147     getCount : function(){
148         return this.elements.length;
149     },
150
151     /**
152      * Returns true if this composite contains the passed element
153      * @return Boolean
154      */
155     contains : function(el){
156         return this.indexOf(el) !== -1;
157     },
158
159     /**
160      * Returns true if this composite contains the passed element
161      * @return Boolean
162      */
163     indexOf : function(el){
164         return this.elements.indexOf(Roo.get(el));
165     },
166
167
168     /**
169     * Removes the specified element(s).
170     * @param {Mixed} el The id of an element, the Element itself, the index of the element in this composite
171     * or an array of any of those.
172     * @param {Boolean} removeDom (optional) True to also remove the element from the document
173     * @return {CompositeElement} this
174     */
175     removeElement : function(el, removeDom){
176         if(el instanceof Array){
177             for(var i = 0, len = el.length; i < len; i++){
178                 this.removeElement(el[i]);
179             }
180             return this;
181         }
182         var index = typeof el == 'number' ? el : this.indexOf(el);
183         if(index !== -1){
184             if(removeDom){
185                 var d = this.elements[index];
186                 if(d.dom){
187                     d.remove();
188                 }else{
189                     d.parentNode.removeChild(d);
190                 }
191             }
192             this.elements.splice(index, 1);
193         }
194         return this;
195     },
196
197     /**
198     * Replaces the specified element with the passed element.
199     * @param {String/HTMLElement/Element/Number} el The id of an element, the Element itself, the index of the element in this composite
200     * to replace.
201     * @param {String/HTMLElement/Element} replacement The id of an element or the Element itself.
202     * @param {Boolean} domReplace (Optional) True to remove and replace the element in the document too.
203     * @return {CompositeElement} this
204     */
205     replaceElement : function(el, replacement, domReplace){
206         var index = typeof el == 'number' ? el : this.indexOf(el);
207         if(index !== -1){
208             if(domReplace){
209                 this.elements[index].replaceWith(replacement);
210             }else{
211                 this.elements.splice(index, 1, Roo.get(replacement))
212             }
213         }
214         return this;
215     },
216
217     /**
218      * Removes all elements.
219      */
220     clear : function(){
221         this.elements = [];
222     }
223 };
224 (function(){
225     Roo.CompositeElement.createCall = function(proto, fnName){
226         if(!proto[fnName]){
227             proto[fnName] = function(){
228                 return this.invoke(fnName, arguments);
229             };
230         }
231     };
232     for(var fnName in Roo.Element.prototype){
233         if(typeof Roo.Element.prototype[fnName] == "function"){
234             Roo.CompositeElement.createCall(Roo.CompositeElement.prototype, fnName);
235         }
236     };
237 })();