3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
15 * @class Ext.CompositeElementLite
\r
16 * <p>This class encapsulates a <i>collection</i> of DOM elements, providing methods to filter
\r
17 * members, or to perform collective actions upon the whole set.</p>
\r
18 * <p>Although they are not listed, this class supports all of the methods of {@link Ext.Element} and
\r
19 * {@link Ext.Fx}. The methods from these classes will be performed on all the elements in this collection.</p>
\r
20 * Example:<pre><code>
\r
21 var els = Ext.select("#some-el div.some-class");
\r
22 // or select directly from an existing element
\r
23 var el = Ext.get('some-el');
\r
24 el.select('div.some-class');
\r
26 els.setWidth(100); // all elements become 100 width
\r
27 els.hide(true); // all elements fade out and hide
\r
29 els.setWidth(100).hide(true);
\r
32 Ext.CompositeElementLite = function(els, root){
\r
33 <div id="prop-Ext.CompositeElementLite-elements"></div>/**
\r
34 * <p>The Array of DOM elements which this CompositeElement encapsulates. Read-only.</p>
\r
35 * <p>This will not <i>usually</i> be accessed in developers' code, but developers wishing
\r
36 * to augment the capabilities of the CompositeElementLite class may use it when adding
\r
37 * methods to the class.</p>
\r
38 * <p>For example to add the <code>nextAll</code> method to the class to <b>add</b> all
\r
39 * following siblings of selected elements, the code would be</p><code><pre>
\r
40 Ext.override(Ext.CompositeElementLite, {
\r
41 nextAll: function() {
\r
42 var els = this.elements, i, l = els.length, n, r = [], ri = -1;
\r
44 // Loop through all elements in this Composite, accumulating
\r
45 // an Array of all siblings.
\r
46 for (i = 0; i < l; i++) {
\r
47 for (n = els[i].nextSibling; n; n = n.nextSibling) {
\r
52 // Add all found siblings to this Composite
\r
57 * @property elements
\r
60 this.add(els, root);
\r
61 this.el = new Ext.Element.Flyweight();
\r
64 Ext.CompositeElementLite.prototype = {
\r
66 <div id="method-Ext.CompositeElementLite-getCount"></div>/**
\r
67 * Returns the number of elements in this Composite.
\r
70 getCount : function(){
\r
71 return this.elements.length;
\r
73 <div id="method-Ext.CompositeElementLite-add"></div>/**
\r
74 * Adds elements to this Composite object.
\r
75 * @param {Mixed} els Either an Array of DOM elements to add, or another Composite object who's elements should be added.
\r
76 * @return {CompositeElement} This Composite object.
\r
78 add : function(els){
\r
80 if (Ext.isArray(els)) {
\r
81 this.elements = this.elements.concat(els);
\r
83 var yels = this.elements;
\r
84 Ext.each(els, function(e) {
\r
91 invoke : function(fn, args){
\r
92 var els = this.elements,
\r
94 Ext.each(els, function(e) {
\r
96 Ext.Element.prototype[fn].apply(el, args);
\r
100 <div id="method-Ext.CompositeElementLite-item"></div>/**
\r
101 * Returns a flyweight Element of the dom element object at the specified index
\r
102 * @param {Number} index
\r
103 * @return {Ext.Element}
\r
105 item : function(index){
\r
107 if(!me.elements[index]){
\r
110 me.el.dom = me.elements[index];
\r
114 // fixes scope with flyweight
\r
115 addListener : function(eventName, handler, scope, opt){
\r
116 Ext.each(this.elements, function(e) {
\r
117 Ext.EventManager.on(e, eventName, handler, scope || e, opt);
\r
121 <div id="method-Ext.CompositeElementLite-each"></div>/**
\r
122 * <p>Calls the passed function for each element in this composite.</p>
\r
123 * @param {Function} fn The function to call. The function is passed the following parameters:<ul>
\r
124 * <li><b>el</b> : Element<div class="sub-desc">The current Element in the iteration.
\r
125 * <b>This is the flyweight (shared) Ext.Element instance, so if you require a
\r
126 * a reference to the dom node, use el.dom.</b></div></li>
\r
127 * <li><b>c</b> : Composite<div class="sub-desc">This Composite object.</div></li>
\r
128 * <li><b>idx</b> : Number<div class="sub-desc">The zero-based index in the iteration.</div></li>
\r
130 * @param {Object} scope (optional) The scope (<i>this</i> reference) in which the function is executed. (defaults to the Element)
\r
131 * @return {CompositeElement} this
\r
133 each : function(fn, scope){
\r
137 Ext.each(me.elements, function(e,i) {
\r
139 return fn.call(scope || el, el, me, i);
\r
144 <div id="method-Ext.CompositeElementLite-fill"></div>/**
\r
145 * Clears this Composite and adds the elements passed.
\r
146 * @param {Mixed} els Either an array of DOM elements, or another Composite from which to fill this Composite.
\r
147 * @return {CompositeElement} this
\r
149 fill : function(els){
\r
156 <div id="method-Ext.CompositeElementLite-filter"></div>/**
\r
157 * Filters this composite to only elements that match the passed selector.
\r
158 * @param {String/Function} selector A string CSS selector or a comparison function.
\r
159 * The comparison function will be called with the following arguments:<ul>
\r
160 * <li><code>el</code> : Ext.Element<div class="sub-desc">The current DOM element.</div></li>
\r
161 * <li><code>index</code> : Number<div class="sub-desc">The current index within the collection.</div></li>
\r
163 * @return {CompositeElement} this
\r
165 filter : function(selector){
\r
168 fn = Ext.isFunction(selector) ? selector
\r
170 return el.is(selector);
\r
172 me.each(function(el, self, i){
\r
173 if(fn(el, i) !== false){
\r
174 els[els.length] = el.dom;
\r
181 <div id="method-Ext.CompositeElementLite-indexOf"></div>/**
\r
182 * Find the index of the passed element within the composite collection.
\r
183 * @param el {Mixed} The id of an element, or an Ext.Element, or an HtmlElement to find within the composite collection.
\r
184 * @return Number The index of the passed Ext.Element in the composite collection, or -1 if not found.
\r
186 indexOf : function(el){
\r
187 return this.elements.indexOf(Ext.getDom(el));
\r
190 <div id="method-Ext.CompositeElementLite-replaceElement"></div>/**
\r
191 * Replaces the specified element with the passed element.
\r
192 * @param {Mixed} el The id of an element, the Element itself, the index of the element in this composite
\r
194 * @param {Mixed} replacement The id of an element or the Element itself.
\r
195 * @param {Boolean} domReplace (Optional) True to remove and replace the element in the document too.
\r
196 * @return {CompositeElement} this
\r
198 replaceElement : function(el, replacement, domReplace){
\r
199 var index = !isNaN(el) ? el : this.indexOf(el),
\r
202 replacement = Ext.getDom(replacement);
\r
204 d = this.elements[index];
\r
205 d.parentNode.insertBefore(replacement, d);
\r
208 this.elements.splice(index, 1, replacement);
\r
213 <div id="method-Ext.CompositeElementLite-clear"></div>/**
\r
214 * Removes all elements.
\r
216 clear : function(){
\r
217 this.elements = [];
\r
221 Ext.CompositeElementLite.prototype.on = Ext.CompositeElementLite.prototype.addListener;
\r
225 ElProto = Ext.Element.prototype,
\r
226 CelProto = Ext.CompositeElementLite.prototype;
\r
228 for(fnName in ElProto){
\r
229 if(Ext.isFunction(ElProto[fnName])){
\r
230 (function(fnName){
\r
231 CelProto[fnName] = CelProto[fnName] || function(){
\r
232 return this.invoke(fnName, arguments);
\r
234 }).call(CelProto, fnName);
\r
241 Ext.Element.selectorFunction = Ext.DomQuery.select;
\r
244 <div id="method-Ext.Element-select"></div>/**
\r
245 * Selects elements based on the passed CSS selector to enable {@link Ext.Element Element} methods
\r
246 * to be applied to many related elements in one statement through the returned {@link Ext.CompositeElement CompositeElement} or
\r
247 * {@link Ext.CompositeElementLite CompositeElementLite} object.
\r
248 * @param {String/Array} selector The CSS selector or an array of elements
\r
249 * @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
250 * @param {HTMLElement/String} root (optional) The root element of the query or id of the root
\r
251 * @return {CompositeElementLite/CompositeElement}
\r
252 * @member Ext.Element
\r
255 Ext.Element.select = function(selector, unique, root){
\r
257 if(typeof selector == "string"){
\r
258 els = Ext.Element.selectorFunction(selector, root);
\r
259 }else if(selector.length !== undefined){
\r
262 throw "Invalid selector";
\r
264 return new Ext.CompositeElementLite(els);
\r
266 <div id="method-Ext-select"></div>/**
\r
267 * Selects elements based on the passed CSS selector to enable {@link Ext.Element Element} methods
\r
268 * to be applied to many related elements in one statement through the returned {@link Ext.CompositeElement CompositeElement} or
\r
269 * {@link Ext.CompositeElementLite CompositeElementLite} object.
\r
270 * @param {String/Array} selector The CSS selector or an array of elements
\r
271 * @param {Boolean} unique (optional) true to create a unique Ext.Element for each element (defaults to a shared flyweight object)
\r
272 * @param {HTMLElement/String} root (optional) The root element of the query or id of the root
\r
273 * @return {CompositeElementLite/CompositeElement}
\r
277 Ext.select = Ext.Element.select;</pre>