3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
8 * @class Ext.CompositeElementLite
9 * <p>This class encapsulates a <i>collection</i> of DOM elements, providing methods to filter
10 * members, or to perform collective actions upon the whole set.</p>
11 * <p>Although they are not listed, this class supports all of the methods of {@link Ext.Element} and
12 * {@link Ext.Fx}. The methods from these classes will be performed on all the elements in this collection.</p>
14 var els = Ext.select("#some-el div.some-class");
15 // or select directly from an existing element
16 var el = Ext.get('some-el');
17 el.select('div.some-class');
19 els.setWidth(100); // all elements become 100 width
20 els.hide(true); // all elements fade out and hide
22 els.setWidth(100).hide(true);
25 Ext.CompositeElementLite = function(els, root){
27 * <p>The Array of DOM elements which this CompositeElement encapsulates. Read-only.</p>
28 * <p>This will not <i>usually</i> be accessed in developers' code, but developers wishing
29 * to augment the capabilities of the CompositeElementLite class may use it when adding
30 * methods to the class.</p>
31 * <p>For example to add the <code>nextAll</code> method to the class to <b>add</b> all
32 * following siblings of selected elements, the code would be</p><code><pre>
33 Ext.override(Ext.CompositeElementLite, {
35 var els = this.elements, i, l = els.length, n, r = [], ri = -1;
37 // Loop through all elements in this Composite, accumulating
38 // an Array of all siblings.
39 for (i = 0; i < l; i++) {
40 for (n = els[i].nextSibling; n; n = n.nextSibling) {
45 // Add all found siblings to this Composite
54 this.el = new Ext.Element.Flyweight();
57 Ext.CompositeElementLite.prototype = {
61 getElement : function(el){
62 // Set the shared flyweight dom property to the current element
70 transformElement : function(el){
71 return Ext.getDom(el);
75 * Returns the number of elements in this Composite.
78 getCount : function(){
79 return this.elements.length;
82 * Adds elements to this Composite object.
83 * @param {Mixed} els Either an Array of DOM elements to add, or another Composite object who's elements should be added.
84 * @return {CompositeElement} This Composite object.
86 add : function(els, root){
88 elements = me.elements;
92 if(typeof els == "string"){
93 els = Ext.Element.selectorFunction(els, root);
94 }else if(els.isComposite){
96 }else if(!Ext.isIterable(els)){
100 for(var i = 0, len = els.length; i < len; ++i){
101 elements.push(me.transformElement(els[i]));
106 invoke : function(fn, args){
113 for(i = 0; i < len; i++) {
116 Ext.Element.prototype[fn].apply(me.getElement(e), args);
122 * Returns a flyweight Element of the dom element object at the specified index
123 * @param {Number} index
124 * @return {Ext.Element}
126 item : function(index){
128 el = me.elements[index],
132 out = me.getElement(el);
137 // fixes scope with flyweight
138 addListener : function(eventName, handler, scope, opt){
139 var els = this.elements,
143 for(i = 0; i<len; i++) {
146 Ext.EventManager.on(e, eventName, handler, scope || e, opt);
152 * <p>Calls the passed function for each element in this composite.</p>
153 * @param {Function} fn The function to call. The function is passed the following parameters:<ul>
154 * <li><b>el</b> : Element<div class="sub-desc">The current Element in the iteration.
155 * <b>This is the flyweight (shared) Ext.Element instance, so if you require a
156 * a reference to the dom node, use el.dom.</b></div></li>
157 * <li><b>c</b> : Composite<div class="sub-desc">This Composite object.</div></li>
158 * <li><b>idx</b> : Number<div class="sub-desc">The zero-based index in the iteration.</div></li>
160 * @param {Object} scope (optional) The scope (<i>this</i> reference) in which the function is executed. (defaults to the Element)
161 * @return {CompositeElement} this
163 each : function(fn, scope){
169 for(i = 0; i<len; i++) {
172 e = this.getElement(e);
173 if(fn.call(scope || e, e, me, i) === false){
182 * Clears this Composite and adds the elements passed.
183 * @param {Mixed} els Either an array of DOM elements, or another Composite from which to fill this Composite.
184 * @return {CompositeElement} this
186 fill : function(els){
194 * Filters this composite to only elements that match the passed selector.
195 * @param {String/Function} selector A string CSS selector or a comparison function.
196 * The comparison function will be called with the following arguments:<ul>
197 * <li><code>el</code> : Ext.Element<div class="sub-desc">The current DOM element.</div></li>
198 * <li><code>index</code> : Number<div class="sub-desc">The current index within the collection.</div></li>
200 * @return {CompositeElement} this
202 filter : function(selector){
205 fn = Ext.isFunction(selector) ? selector
207 return el.is(selector);
210 me.each(function(el, self, i) {
211 if (fn(el, i) !== false) {
212 els[els.length] = me.transformElement(el);
221 * Find the index of the passed element within the composite collection.
222 * @param el {Mixed} The id of an element, or an Ext.Element, or an HtmlElement to find within the composite collection.
223 * @return Number The index of the passed Ext.Element in the composite collection, or -1 if not found.
225 indexOf : function(el){
226 return this.elements.indexOf(this.transformElement(el));
230 * Replaces the specified element with the passed element.
231 * @param {Mixed} el The id of an element, the Element itself, the index of the element in this composite
233 * @param {Mixed} replacement The id of an element or the Element itself.
234 * @param {Boolean} domReplace (Optional) True to remove and replace the element in the document too.
235 * @return {CompositeElement} this
237 replaceElement : function(el, replacement, domReplace){
238 var index = !isNaN(el) ? el : this.indexOf(el),
241 replacement = Ext.getDom(replacement);
243 d = this.elements[index];
244 d.parentNode.insertBefore(replacement, d);
247 this.elements.splice(index, 1, replacement);
253 * Removes all elements.
260 Ext.CompositeElementLite.prototype.on = Ext.CompositeElementLite.prototype.addListener;
264 * Copies all of the functions from Ext.Element's prototype onto CompositeElementLite's prototype.
265 * This is called twice - once immediately below, and once again after additional Ext.Element
266 * are added in Ext JS
268 Ext.CompositeElementLite.importElementMethods = function() {
270 ElProto = Ext.Element.prototype,
271 CelProto = Ext.CompositeElementLite.prototype;
273 for (fnName in ElProto) {
274 if (typeof ElProto[fnName] == 'function'){
276 CelProto[fnName] = CelProto[fnName] || function() {
277 return this.invoke(fnName, arguments);
279 }).call(CelProto, fnName);
285 Ext.CompositeElementLite.importElementMethods();
288 Ext.Element.selectorFunction = Ext.DomQuery.select;
292 * Selects elements based on the passed CSS selector to enable {@link Ext.Element Element} methods
293 * to be applied to many related elements in one statement through the returned {@link Ext.CompositeElement CompositeElement} or
294 * {@link Ext.CompositeElementLite CompositeElementLite} object.
295 * @param {String/Array} selector The CSS selector or an array of elements
296 * @param {HTMLElement/String} root (optional) The root element of the query or id of the root
297 * @return {CompositeElementLite/CompositeElement}
298 * @member Ext.Element
301 Ext.Element.select = function(selector, root){
303 if(typeof selector == "string"){
304 els = Ext.Element.selectorFunction(selector, root);
305 }else if(selector.length !== undefined){
308 throw "Invalid selector";
310 return new Ext.CompositeElementLite(els);
313 * Selects elements based on the passed CSS selector to enable {@link Ext.Element Element} methods
314 * to be applied to many related elements in one statement through the returned {@link Ext.CompositeElement CompositeElement} or
315 * {@link Ext.CompositeElementLite CompositeElementLite} object.
316 * @param {String/Array} selector The CSS selector or an array of elements
317 * @param {HTMLElement/String} root (optional) The root element of the query or id of the root
318 * @return {CompositeElementLite/CompositeElement}
322 Ext.select = Ext.Element.select;