initial import
[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     * @return {CompositeElement} this
67     */
68     filter : function(selector){
69         var els = [];
70         this.each(function(el){
71             if(el.is(selector)){
72                 els[els.length] = el.dom;
73             }
74         });
75         this.fill(els);
76         return this;
77     },
78
79     invoke : function(fn, args){
80         var els = this.elements;
81         for(var i = 0, len = els.length; i < len; i++) {
82                 Roo.Element.prototype[fn].apply(els[i], args);
83         }
84         return this;
85     },
86     /**
87     * Adds elements to this composite.
88     * @param {String/Array} els A string CSS selector, an array of elements or an element
89     * @return {CompositeElement} this
90     */
91     add : function(els){
92         if(typeof els == "string"){
93             this.addElements(Roo.Element.selectorFunction(els));
94         }else if(els.length !== undefined){
95             this.addElements(els);
96         }else{
97             this.addElements([els]);
98         }
99         return this;
100     },
101     /**
102     * Calls the passed function passing (el, this, index) for each element in this composite.
103     * @param {Function} fn The function to call
104     * @param {Object} scope (optional) The <i>this</i> object (defaults to the element)
105     * @return {CompositeElement} this
106     */
107     each : function(fn, scope){
108         var els = this.elements;
109         for(var i = 0, len = els.length; i < len; i++){
110             if(fn.call(scope || els[i], els[i], this, i) === false) {
111                 break;
112             }
113         }
114         return this;
115     },
116
117     /**
118      * Returns the Element object at the specified index
119      * @param {Number} index
120      * @return {Roo.Element}
121      */
122     item : function(index){
123         return this.elements[index] || null;
124     },
125
126     /**
127      * Returns the first Element
128      * @return {Roo.Element}
129      */
130     first : function(){
131         return this.item(0);
132     },
133
134     /**
135      * Returns the last Element
136      * @return {Roo.Element}
137      */
138     last : function(){
139         return this.item(this.elements.length-1);
140     },
141
142     /**
143      * Returns the number of elements in this composite
144      * @return Number
145      */
146     getCount : function(){
147         return this.elements.length;
148     },
149
150     /**
151      * Returns true if this composite contains the passed element
152      * @return Boolean
153      */
154     contains : function(el){
155         return this.indexOf(el) !== -1;
156     },
157
158     /**
159      * Returns true if this composite contains the passed element
160      * @return Boolean
161      */
162     indexOf : function(el){
163         return this.elements.indexOf(Roo.get(el));
164     },
165
166
167     /**
168     * Removes the specified element(s).
169     * @param {Mixed} el The id of an element, the Element itself, the index of the element in this composite
170     * or an array of any of those.
171     * @param {Boolean} removeDom (optional) True to also remove the element from the document
172     * @return {CompositeElement} this
173     */
174     removeElement : function(el, removeDom){
175         if(el instanceof Array){
176             for(var i = 0, len = el.length; i < len; i++){
177                 this.removeElement(el[i]);
178             }
179             return this;
180         }
181         var index = typeof el == 'number' ? el : this.indexOf(el);
182         if(index !== -1){
183             if(removeDom){
184                 var d = this.elements[index];
185                 if(d.dom){
186                     d.remove();
187                 }else{
188                     d.parentNode.removeChild(d);
189                 }
190             }
191             this.elements.splice(index, 1);
192         }
193         return this;
194     },
195
196     /**
197     * Replaces the specified element with the passed element.
198     * @param {String/HTMLElement/Element/Number} el The id of an element, the Element itself, the index of the element in this composite
199     * to replace.
200     * @param {String/HTMLElement/Element} replacement The id of an element or the Element itself.
201     * @param {Boolean} domReplace (Optional) True to remove and replace the element in the document too.
202     * @return {CompositeElement} this
203     */
204     replaceElement : function(el, replacement, domReplace){
205         var index = typeof el == 'number' ? el : this.indexOf(el);
206         if(index !== -1){
207             if(domReplace){
208                 this.elements[index].replaceWith(replacement);
209             }else{
210                 this.elements.splice(index, 1, Roo.get(replacement))
211             }
212         }
213         return this;
214     },
215
216     /**
217      * Removes all elements.
218      */
219     clear : function(){
220         this.elements = [];
221     }
222 };
223 (function(){
224     Roo.CompositeElement.createCall = function(proto, fnName){
225         if(!proto[fnName]){
226             proto[fnName] = function(){
227                 return this.invoke(fnName, arguments);
228             };
229         }
230     };
231     for(var fnName in Roo.Element.prototype){
232         if(typeof Roo.Element.prototype[fnName] == "function"){
233             Roo.CompositeElement.createCall(Roo.CompositeElement.prototype, fnName);
234         }
235     };
236 })();