allow string based values for comboboxarray
[roojs1] / Roo / CompositeElementLite.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  * @class Roo.CompositeElementLite
14  * @extends Roo.CompositeElement
15  * Flyweight composite class. Reuses the same Roo.Element for element operations.
16  <pre><code>
17  var els = Roo.select("#some-el div.some-class");
18  // or select directly from an existing element
19  var el = Roo.get('some-el');
20  el.select('div.some-class');
21
22  els.setWidth(100); // all elements become 100 width
23  els.hide(true); // all elements fade out and hide
24  // or
25  els.setWidth(100).hide(true);
26  </code></pre><br><br>
27  * <b>NOTE: Although they are not listed, this class supports all of the set/update methods of Roo.Element. All Roo.Element
28  * actions will be performed on all the elements in this collection.</b>
29  */
30 Roo.CompositeElementLite = function(els){
31     Roo.CompositeElementLite.superclass.constructor.call(this, els);
32     this.el = new Roo.Element.Flyweight();
33 };
34 Roo.extend(Roo.CompositeElementLite, Roo.CompositeElement, {
35     addElements : function(els){
36         if(els){
37             if(els instanceof Array){
38                 this.elements = this.elements.concat(els);
39             }else{
40                 var yels = this.elements;
41                 var index = yels.length-1;
42                 for(var i = 0, len = els.length; i < len; i++) {
43                     yels[++index] = els[i];
44                 }
45             }
46         }
47         return this;
48     },
49     invoke : function(fn, args){
50         var els = this.elements;
51         var el = this.el;
52         for(var i = 0, len = els.length; i < len; i++) {
53             el.dom = els[i];
54                 Roo.Element.prototype[fn].apply(el, args);
55         }
56         return this;
57     },
58     /**
59      * Returns a flyweight Element of the dom element object at the specified index
60      * @param {Number} index
61      * @return {Roo.Element}
62      */
63     item : function(index){
64         if(!this.elements[index]){
65             return null;
66         }
67         this.el.dom = this.elements[index];
68         return this.el;
69     },
70
71     // fixes scope with flyweight
72     addListener : function(eventName, handler, scope, opt){
73         var els = this.elements;
74         for(var i = 0, len = els.length; i < len; i++) {
75             Roo.EventManager.on(els[i], eventName, handler, scope || els[i], opt);
76         }
77         return this;
78     },
79
80     /**
81     * Calls the passed function passing (el, this, index) for each element in this composite. <b>The element
82     * passed is the flyweight (shared) Roo.Element instance, so if you require a
83     * a reference to the dom node, use el.dom.</b>
84     * @param {Function} fn The function to call
85     * @param {Object} scope (optional) The <i>this</i> object (defaults to the element)
86     * @return {CompositeElement} this
87     */
88     each : function(fn, scope){
89         var els = this.elements;
90         var el = this.el;
91         for(var i = 0, len = els.length; i < len; i++){
92             el.dom = els[i];
93                 if(fn.call(scope || el, el, this, i) === false){
94                 break;
95             }
96         }
97         return this;
98     },
99
100     indexOf : function(el){
101         return this.elements.indexOf(Roo.getDom(el));
102     },
103
104     replaceElement : function(el, replacement, domReplace){
105         var index = typeof el == 'number' ? el : this.indexOf(el);
106         if(index !== -1){
107             replacement = Roo.getDom(replacement);
108             if(domReplace){
109                 var d = this.elements[index];
110                 d.parentNode.insertBefore(replacement, d);
111                 d.parentNode.removeChild(d);
112             }
113             this.elements.splice(index, 1, replacement);
114         }
115         return this;
116     }
117 });
118 Roo.CompositeElementLite.prototype.on = Roo.CompositeElementLite.prototype.addListener;
119