1 | /*! |
---|
2 | * Ext JS Library 3.0.0 |
---|
3 | * Copyright(c) 2006-2009 Ext JS, LLC |
---|
4 | * licensing@extjs.com |
---|
5 | * http://www.extjs.com/license |
---|
6 | */ |
---|
7 | /** |
---|
8 | * @class Ext.CompositeElementLite |
---|
9 | * Flyweight composite class. Reuses the same Ext.Element for element operations. |
---|
10 | <pre><code> |
---|
11 | var els = Ext.select("#some-el div.some-class"); |
---|
12 | // or select directly from an existing element |
---|
13 | var el = Ext.get('some-el'); |
---|
14 | el.select('div.some-class'); |
---|
15 | |
---|
16 | els.setWidth(100); // all elements become 100 width |
---|
17 | els.hide(true); // all elements fade out and hide |
---|
18 | // or |
---|
19 | els.setWidth(100).hide(true); |
---|
20 | </code></pre><br><br> |
---|
21 | * <b>NOTE: Although they are not listed, this class supports all of the set/update methods of Ext.Element. All Ext.Element |
---|
22 | * actions will be performed on all the elements in this collection.</b> |
---|
23 | */ |
---|
24 | Ext.CompositeElementLite = function(els, root){ |
---|
25 | this.elements = []; |
---|
26 | this.add(els, root); |
---|
27 | this.el = new Ext.Element.Flyweight(); |
---|
28 | }; |
---|
29 | |
---|
30 | Ext.CompositeElementLite.prototype = { |
---|
31 | isComposite: true, |
---|
32 | /** |
---|
33 | * Returns the number of elements in this composite |
---|
34 | * @return Number |
---|
35 | */ |
---|
36 | getCount : function(){ |
---|
37 | return this.elements.length; |
---|
38 | }, |
---|
39 | add : function(els){ |
---|
40 | if(els){ |
---|
41 | if (Ext.isArray(els)) { |
---|
42 | this.elements = this.elements.concat(els); |
---|
43 | } else { |
---|
44 | var yels = this.elements; |
---|
45 | Ext.each(els, function(e) { |
---|
46 | yels.push(e); |
---|
47 | }); |
---|
48 | } |
---|
49 | } |
---|
50 | return this; |
---|
51 | }, |
---|
52 | invoke : function(fn, args){ |
---|
53 | var els = this.elements, |
---|
54 | el = this.el; |
---|
55 | Ext.each(els, function(e) { |
---|
56 | el.dom = e; |
---|
57 | Ext.Element.prototype[fn].apply(el, args); |
---|
58 | }); |
---|
59 | return this; |
---|
60 | }, |
---|
61 | /** |
---|
62 | * Returns a flyweight Element of the dom element object at the specified index |
---|
63 | * @param {Number} index |
---|
64 | * @return {Ext.Element} |
---|
65 | */ |
---|
66 | item : function(index){ |
---|
67 | var me = this; |
---|
68 | if(!me.elements[index]){ |
---|
69 | return null; |
---|
70 | } |
---|
71 | me.el.dom = me.elements[index]; |
---|
72 | return me.el; |
---|
73 | }, |
---|
74 | |
---|
75 | // fixes scope with flyweight |
---|
76 | addListener : function(eventName, handler, scope, opt){ |
---|
77 | Ext.each(this.elements, function(e) { |
---|
78 | Ext.EventManager.on(e, eventName, handler, scope || e, opt); |
---|
79 | }); |
---|
80 | return this; |
---|
81 | }, |
---|
82 | /** |
---|
83 | * Calls the passed function passing (el, this, index) for each element in this composite. <b>The element |
---|
84 | * passed is the flyweight (shared) Ext.Element instance, so if you require a |
---|
85 | * a reference to the dom node, use el.dom.</b> |
---|
86 | * @param {Function} fn The function to call |
---|
87 | * @param {Object} scope (optional) The <i>this</i> object (defaults to the element) |
---|
88 | * @return {CompositeElement} this |
---|
89 | */ |
---|
90 | each : function(fn, scope){ |
---|
91 | var me = this, |
---|
92 | el = me.el; |
---|
93 | |
---|
94 | Ext.each(me.elements, function(e,i) { |
---|
95 | el.dom = e; |
---|
96 | return fn.call(scope || el, el, me, i); |
---|
97 | }); |
---|
98 | return me; |
---|
99 | }, |
---|
100 | |
---|
101 | /** |
---|
102 | * Find the index of the passed element within the composite collection. |
---|
103 | * @param el {Mixed} The id of an element, or an Ext.Element, or an HtmlElement to find within the composite collection. |
---|
104 | * @return Number The index of the passed Ext.Element in the composite collection, or -1 if not found. |
---|
105 | */ |
---|
106 | indexOf : function(el){ |
---|
107 | return this.elements.indexOf(Ext.getDom(el)); |
---|
108 | }, |
---|
109 | |
---|
110 | /** |
---|
111 | * Replaces the specified element with the passed element. |
---|
112 | * @param {Mixed} el The id of an element, the Element itself, the index of the element in this composite |
---|
113 | * to replace. |
---|
114 | * @param {Mixed} replacement The id of an element or the Element itself. |
---|
115 | * @param {Boolean} domReplace (Optional) True to remove and replace the element in the document too. |
---|
116 | * @return {CompositeElement} this |
---|
117 | */ |
---|
118 | replaceElement : function(el, replacement, domReplace){ |
---|
119 | var index = !isNaN(el) ? el : this.indexOf(el), |
---|
120 | d; |
---|
121 | if(index > -1){ |
---|
122 | replacement = Ext.getDom(replacement); |
---|
123 | if(domReplace){ |
---|
124 | d = this.elements[index]; |
---|
125 | d.parentNode.insertBefore(replacement, d); |
---|
126 | Ext.removeNode(d); |
---|
127 | } |
---|
128 | this.elements.splice(index, 1, replacement); |
---|
129 | } |
---|
130 | return this; |
---|
131 | }, |
---|
132 | |
---|
133 | /** |
---|
134 | * Removes all elements. |
---|
135 | */ |
---|
136 | clear : function(){ |
---|
137 | this.elements = []; |
---|
138 | } |
---|
139 | }; |
---|
140 | |
---|
141 | Ext.CompositeElementLite.prototype.on = Ext.CompositeElementLite.prototype.addListener; |
---|
142 | |
---|
143 | (function(){ |
---|
144 | var fnName, |
---|
145 | ElProto = Ext.Element.prototype, |
---|
146 | CelProto = Ext.CompositeElementLite.prototype; |
---|
147 | |
---|
148 | for(fnName in ElProto){ |
---|
149 | if(Ext.isFunction(ElProto[fnName])){ |
---|
150 | (function(fnName){ |
---|
151 | CelProto[fnName] = CelProto[fnName] || function(){ |
---|
152 | return this.invoke(fnName, arguments); |
---|
153 | }; |
---|
154 | }).call(CelProto, fnName); |
---|
155 | |
---|
156 | } |
---|
157 | } |
---|
158 | })(); |
---|
159 | |
---|
160 | if(Ext.DomQuery){ |
---|
161 | Ext.Element.selectorFunction = Ext.DomQuery.select; |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * Selects elements based on the passed CSS selector to enable {@link Ext.Element Element} methods |
---|
166 | * to be applied to many related elements in one statement through the returned {@link Ext.CompositeElement CompositeElement} or |
---|
167 | * {@link Ext.CompositeElementLite CompositeElementLite} object. |
---|
168 | * @param {String/Array} selector The CSS selector or an array of elements |
---|
169 | * @param {Boolean} unique (optional) true to create a unique Ext.Element for each element (defaults to a shared flyweight object) <b>Not supported in core</b> |
---|
170 | * @param {HTMLElement/String} root (optional) The root element of the query or id of the root |
---|
171 | * @return {CompositeElementLite/CompositeElement} |
---|
172 | * @member Ext.Element |
---|
173 | * @method select |
---|
174 | */ |
---|
175 | Ext.Element.select = function(selector, unique, root){ |
---|
176 | var els; |
---|
177 | if(typeof selector == "string"){ |
---|
178 | els = Ext.Element.selectorFunction(selector, root); |
---|
179 | }else if(selector.length !== undefined){ |
---|
180 | els = selector; |
---|
181 | }else{ |
---|
182 | throw "Invalid selector"; |
---|
183 | } |
---|
184 | return new Ext.CompositeElementLite(els); |
---|
185 | }; |
---|
186 | /** |
---|
187 | * Selects elements based on the passed CSS selector to enable {@link Ext.Element Element} methods |
---|
188 | * to be applied to many related elements in one statement through the returned {@link Ext.CompositeElement CompositeElement} or |
---|
189 | * {@link Ext.CompositeElementLite CompositeElementLite} object. |
---|
190 | * @param {String/Array} selector The CSS selector or an array of elements |
---|
191 | * @param {Boolean} unique (optional) true to create a unique Ext.Element for each element (defaults to a shared flyweight object) |
---|
192 | * @param {HTMLElement/String} root (optional) The root element of the query or id of the root |
---|
193 | * @return {CompositeElementLite/CompositeElement} |
---|
194 | * @member Ext |
---|
195 | * @method select |
---|
196 | */ |
---|
197 | Ext.select = Ext.Element.select; |
---|