4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
14 * @class Roo.CompositeElement
15 * Standard composite class. Creates a Roo.Element for every element in the collection.
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>
20 * All methods return <i>this</i> and can be chained.
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);
27 els.setWidth(100); // all elements become 100 width
28 els.hide(true); // all elements fade out and hide
30 els.setWidth(100).hide(true);
33 Roo.CompositeElement = function(els){
35 this.addElements(els);
37 Roo.CompositeElement.prototype = {
39 addElements : function(els){
43 if(typeof els == "string"){
44 els = Roo.Element.selectorFunction(els);
46 var yels = this.elements;
47 var index = yels.length-1;
48 for(var i = 0, len = els.length; i < len; i++) {
49 yels[++index] = Roo.get(els[i]);
55 * Clears this composite and adds the elements returned by the passed selector.
56 * @param {String/Array} els A string CSS selector, an array of elements or an element
57 * @return {CompositeElement} this
66 * Filters this composite to only elements that match the passed selector.
67 * @param {String} selector A string CSS selector
68 * @param {Boolean} inverse return inverse filter (not matches)
69 * @return {CompositeElement} this
71 filter : function(selector, inverse){
73 inverse = inverse || false;
74 this.each(function(el){
75 var match = inverse ? !el.is(selector) : el.is(selector);
77 els[els.length] = el.dom;
84 invoke : function(fn, args){
85 var els = this.elements;
86 for(var i = 0, len = els.length; i < len; i++) {
87 Roo.Element.prototype[fn].apply(els[i], args);
92 * Adds elements to this composite.
93 * @param {String/Array} els A string CSS selector, an array of elements or an element
94 * @return {CompositeElement} this
97 if(typeof els == "string"){
98 this.addElements(Roo.Element.selectorFunction(els));
99 }else if(els.length !== undefined){
100 this.addElements(els);
102 this.addElements([els]);
107 * Calls the passed function passing (el, this, index) for each element in this composite.
108 * @param {Function} fn The function to call
109 * @param {Object} scope (optional) The <i>this</i> object (defaults to the element)
110 * @return {CompositeElement} this
112 each : function(fn, scope){
113 var els = this.elements;
114 for(var i = 0, len = els.length; i < len; i++){
115 if(fn.call(scope || els[i], els[i], this, i) === false) {
123 * Returns the Element object at the specified index
124 * @param {Number} index
125 * @return {Roo.Element}
127 item : function(index){
128 return this.elements[index] || null;
132 * Returns the first Element
133 * @return {Roo.Element}
140 * Returns the last Element
141 * @return {Roo.Element}
144 return this.item(this.elements.length-1);
148 * Returns the number of elements in this composite
151 getCount : function(){
152 return this.elements.length;
156 * Returns true if this composite contains the passed element
159 contains : function(el){
160 return this.indexOf(el) !== -1;
164 * Returns true if this composite contains the passed element
167 indexOf : function(el){
168 return this.elements.indexOf(Roo.get(el));
173 * Removes the specified element(s).
174 * @param {Mixed} el The id of an element, the Element itself, the index of the element in this composite
175 * or an array of any of those.
176 * @param {Boolean} removeDom (optional) True to also remove the element from the document
177 * @return {CompositeElement} this
179 removeElement : function(el, removeDom){
180 if(el instanceof Array){
181 for(var i = 0, len = el.length; i < len; i++){
182 this.removeElement(el[i]);
186 var index = typeof el == 'number' ? el : this.indexOf(el);
189 var d = this.elements[index];
193 d.parentNode.removeChild(d);
196 this.elements.splice(index, 1);
202 * Replaces the specified element with the passed element.
203 * @param {String/HTMLElement/Element/Number} el The id of an element, the Element itself, the index of the element in this composite
205 * @param {String/HTMLElement/Element} replacement The id of an element or the Element itself.
206 * @param {Boolean} domReplace (Optional) True to remove and replace the element in the document too.
207 * @return {CompositeElement} this
209 replaceElement : function(el, replacement, domReplace){
210 var index = typeof el == 'number' ? el : this.indexOf(el);
213 this.elements[index].replaceWith(replacement);
215 this.elements.splice(index, 1, Roo.get(replacement))
222 * Removes all elements.
229 Roo.CompositeElement.createCall = function(proto, fnName){
231 proto[fnName] = function(){
232 return this.invoke(fnName, arguments);
236 for(var fnName in Roo.Element.prototype){
237 if(typeof Roo.Element.prototype[fnName] == "function"){
238 Roo.CompositeElement.createCall(Roo.CompositeElement.prototype, fnName);