3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.CompositeElementLite
\r
9 * <p>This class encapsulates a <i>collection</i> of DOM elements, providing methods to filter
\r
10 * members, or to perform collective actions upon the whole set.</p>
\r
11 * <p>Although they are not listed, this class supports all of the methods of {@link Ext.Element} and
\r
12 * {@link Ext.Fx}. The methods from these classes will be performed on all the elements in this collection.</p>
\r
13 * Example:<pre><code>
\r
14 var els = Ext.select("#some-el div.some-class");
\r
15 // or select directly from an existing element
\r
16 var el = Ext.get('some-el');
\r
17 el.select('div.some-class');
\r
19 els.setWidth(100); // all elements become 100 width
\r
20 els.hide(true); // all elements fade out and hide
\r
22 els.setWidth(100).hide(true);
\r
25 Ext.CompositeElementLite = function(els, root){
\r
27 * <p>The Array of DOM elements which this CompositeElement encapsulates. Read-only.</p>
\r
28 * <p>This will not <i>usually</i> be accessed in developers' code, but developers wishing
\r
29 * to augment the capabilities of the CompositeElementLite class may use it when adding
\r
30 * methods to the class.</p>
\r
31 * <p>For example to add the <code>nextAll</code> method to the class to <b>add</b> all
\r
32 * following siblings of selected elements, the code would be</p><code><pre>
\r
33 Ext.override(Ext.CompositeElementLite, {
\r
34 nextAll: function() {
\r
35 var els = this.elements, i, l = els.length, n, r = [], ri = -1;
\r
37 // Loop through all elements in this Composite, accumulating
\r
38 // an Array of all siblings.
\r
39 for (i = 0; i < l; i++) {
\r
40 for (n = els[i].nextSibling; n; n = n.nextSibling) {
\r
45 // Add all found siblings to this Composite
\r
50 * @property elements
\r
53 this.add(els, root);
\r
54 this.el = new Ext.Element.Flyweight();
\r
57 Ext.CompositeElementLite.prototype = {
\r
60 * Returns the number of elements in this Composite.
\r
63 getCount : function(){
\r
64 return this.elements.length;
\r
67 * Adds elements to this Composite object.
\r
68 * @param {Mixed} els Either an Array of DOM elements to add, or another Composite object who's elements should be added.
\r
69 * @return {CompositeElement} This Composite object.
\r
71 add : function(els){
\r
73 if (Ext.isArray(els)) {
\r
74 this.elements = this.elements.concat(els);
\r
76 var yels = this.elements;
\r
77 Ext.each(els, function(e) {
\r
84 invoke : function(fn, args){
\r
85 var els = this.elements,
\r
87 Ext.each(els, function(e) {
\r
89 Ext.Element.prototype[fn].apply(el, args);
\r
94 * Returns a flyweight Element of the dom element object at the specified index
\r
95 * @param {Number} index
\r
96 * @return {Ext.Element}
\r
98 item : function(index){
\r
100 if(!me.elements[index]){
\r
103 me.el.dom = me.elements[index];
\r
107 // fixes scope with flyweight
\r
108 addListener : function(eventName, handler, scope, opt){
\r
109 Ext.each(this.elements, function(e) {
\r
110 Ext.EventManager.on(e, eventName, handler, scope || e, opt);
\r
115 * <p>Calls the passed function for each element in this composite.</p>
\r
116 * @param {Function} fn The function to call. The function is passed the following parameters:<ul>
\r
117 * <li><b>el</b> : Element<div class="sub-desc">The current Element in the iteration.
\r
118 * <b>This is the flyweight (shared) Ext.Element instance, so if you require a
\r
119 * a reference to the dom node, use el.dom.</b></div></li>
\r
120 * <li><b>c</b> : Composite<div class="sub-desc">This Composite object.</div></li>
\r
121 * <li><b>idx</b> : Number<div class="sub-desc">The zero-based index in the iteration.</div></li>
\r
123 * @param {Object} scope (optional) The scope (<i>this</i> reference) in which the function is executed. (defaults to the Element)
\r
124 * @return {CompositeElement} this
\r
126 each : function(fn, scope){
\r
130 Ext.each(me.elements, function(e,i) {
\r
132 return fn.call(scope || el, el, me, i);
\r
138 * Clears this Composite and adds the elements passed.
\r
139 * @param {Mixed} els Either an array of DOM elements, or another Composite from which to fill this Composite.
\r
140 * @return {CompositeElement} this
\r
142 fill : function(els){
\r
150 * Filters this composite to only elements that match the passed selector.
\r
151 * @param {String/Function} selector A string CSS selector or a comparison function.
\r
152 * The comparison function will be called with the following arguments:<ul>
\r
153 * <li><code>el</code> : Ext.Element<div class="sub-desc">The current DOM element.</div></li>
\r
154 * <li><code>index</code> : Number<div class="sub-desc">The current index within the collection.</div></li>
\r
156 * @return {CompositeElement} this
\r
158 filter : function(selector){
\r
161 fn = Ext.isFunction(selector) ? selector
\r
163 return el.is(selector);
\r
165 me.each(function(el, self, i){
\r
166 if(fn(el, i) !== false){
\r
167 els[els.length] = el.dom;
\r
175 * Find the index of the passed element within the composite collection.
\r
176 * @param el {Mixed} The id of an element, or an Ext.Element, or an HtmlElement to find within the composite collection.
\r
177 * @return Number The index of the passed Ext.Element in the composite collection, or -1 if not found.
\r
179 indexOf : function(el){
\r
180 return this.elements.indexOf(Ext.getDom(el));
\r
184 * Replaces the specified element with the passed element.
\r
185 * @param {Mixed} el The id of an element, the Element itself, the index of the element in this composite
\r
187 * @param {Mixed} replacement The id of an element or the Element itself.
\r
188 * @param {Boolean} domReplace (Optional) True to remove and replace the element in the document too.
\r
189 * @return {CompositeElement} this
\r
191 replaceElement : function(el, replacement, domReplace){
\r
192 var index = !isNaN(el) ? el : this.indexOf(el),
\r
195 replacement = Ext.getDom(replacement);
\r
197 d = this.elements[index];
\r
198 d.parentNode.insertBefore(replacement, d);
\r
201 this.elements.splice(index, 1, replacement);
\r
207 * Removes all elements.
\r
209 clear : function(){
\r
210 this.elements = [];
\r
214 Ext.CompositeElementLite.prototype.on = Ext.CompositeElementLite.prototype.addListener;
\r
218 ElProto = Ext.Element.prototype,
\r
219 CelProto = Ext.CompositeElementLite.prototype;
\r
221 for(fnName in ElProto){
\r
222 if(Ext.isFunction(ElProto[fnName])){
\r
223 (function(fnName){
\r
224 CelProto[fnName] = CelProto[fnName] || function(){
\r
225 return this.invoke(fnName, arguments);
\r
227 }).call(CelProto, fnName);
\r
234 Ext.Element.selectorFunction = Ext.DomQuery.select;
\r
238 * Selects elements based on the passed CSS selector to enable {@link Ext.Element Element} methods
\r
239 * to be applied to many related elements in one statement through the returned {@link Ext.CompositeElement CompositeElement} or
\r
240 * {@link Ext.CompositeElementLite CompositeElementLite} object.
\r
241 * @param {String/Array} selector The CSS selector or an array of elements
\r
242 * @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>
\r
243 * @param {HTMLElement/String} root (optional) The root element of the query or id of the root
\r
244 * @return {CompositeElementLite/CompositeElement}
\r
245 * @member Ext.Element
\r
248 Ext.Element.select = function(selector, unique, root){
\r
250 if(typeof selector == "string"){
\r
251 els = Ext.Element.selectorFunction(selector, root);
\r
252 }else if(selector.length !== undefined){
\r
255 throw "Invalid selector";
\r
257 return new Ext.CompositeElementLite(els);
\r
260 * Selects elements based on the passed CSS selector to enable {@link Ext.Element Element} methods
\r
261 * to be applied to many related elements in one statement through the returned {@link Ext.CompositeElement CompositeElement} or
\r
262 * {@link Ext.CompositeElementLite CompositeElementLite} object.
\r
263 * @param {String/Array} selector The CSS selector or an array of elements
\r
264 * @param {Boolean} unique (optional) true to create a unique Ext.Element for each element (defaults to a shared flyweight object)
\r
265 * @param {HTMLElement/String} root (optional) The root element of the query or id of the root
\r
266 * @return {CompositeElementLite/CompositeElement}
\r
270 Ext.select = Ext.Element.select;