[76] | 1 | /*! |
---|
| 2 | * Ext JS Library 3.4.0 |
---|
| 3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
| 4 | * licensing@sencha.com |
---|
| 5 | * http://www.sencha.com/license |
---|
| 6 | */ |
---|
| 7 | /** |
---|
| 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> |
---|
| 13 | * Example:<pre><code> |
---|
| 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'); |
---|
| 18 | |
---|
| 19 | els.setWidth(100); // all elements become 100 width |
---|
| 20 | els.hide(true); // all elements fade out and hide |
---|
| 21 | // or |
---|
| 22 | els.setWidth(100).hide(true); |
---|
| 23 | </code> |
---|
| 24 | */ |
---|
| 25 | Ext.CompositeElementLite = function(els, root){ |
---|
| 26 | /** |
---|
| 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, { |
---|
| 34 | nextAll: function() { |
---|
| 35 | var els = this.elements, i, l = els.length, n, r = [], ri = -1; |
---|
| 36 | |
---|
| 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) { |
---|
| 41 | r[++ri] = n; |
---|
| 42 | } |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | // Add all found siblings to this Composite |
---|
| 46 | return this.add(r); |
---|
| 47 | } |
---|
| 48 | });</pre></code> |
---|
| 49 | * @type Array |
---|
| 50 | * @property elements |
---|
| 51 | */ |
---|
| 52 | this.elements = []; |
---|
| 53 | this.add(els, root); |
---|
| 54 | this.el = new Ext.Element.Flyweight(); |
---|
| 55 | }; |
---|
| 56 | |
---|
| 57 | Ext.CompositeElementLite.prototype = { |
---|
| 58 | isComposite: true, |
---|
| 59 | |
---|
| 60 | // private |
---|
| 61 | getElement : function(el){ |
---|
| 62 | // Set the shared flyweight dom property to the current element |
---|
| 63 | var e = this.el; |
---|
| 64 | e.dom = el; |
---|
| 65 | e.id = el.id; |
---|
| 66 | return e; |
---|
| 67 | }, |
---|
| 68 | |
---|
| 69 | // private |
---|
| 70 | transformElement : function(el){ |
---|
| 71 | return Ext.getDom(el); |
---|
| 72 | }, |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * Returns the number of elements in this Composite. |
---|
| 76 | * @return Number |
---|
| 77 | */ |
---|
| 78 | getCount : function(){ |
---|
| 79 | return this.elements.length; |
---|
| 80 | }, |
---|
| 81 | /** |
---|
| 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. |
---|
| 85 | */ |
---|
| 86 | add : function(els, root){ |
---|
| 87 | var me = this, |
---|
| 88 | elements = me.elements; |
---|
| 89 | if(!els){ |
---|
| 90 | return this; |
---|
| 91 | } |
---|
| 92 | if(typeof els == "string"){ |
---|
| 93 | els = Ext.Element.selectorFunction(els, root); |
---|
| 94 | }else if(els.isComposite){ |
---|
| 95 | els = els.elements; |
---|
| 96 | }else if(!Ext.isIterable(els)){ |
---|
| 97 | els = [els]; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | for(var i = 0, len = els.length; i < len; ++i){ |
---|
| 101 | elements.push(me.transformElement(els[i])); |
---|
| 102 | } |
---|
| 103 | return me; |
---|
| 104 | }, |
---|
| 105 | |
---|
| 106 | invoke : function(fn, args){ |
---|
| 107 | var me = this, |
---|
| 108 | els = me.elements, |
---|
| 109 | len = els.length, |
---|
| 110 | e, |
---|
| 111 | i; |
---|
| 112 | |
---|
| 113 | for(i = 0; i < len; i++) { |
---|
| 114 | e = els[i]; |
---|
| 115 | if(e){ |
---|
| 116 | Ext.Element.prototype[fn].apply(me.getElement(e), args); |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | return me; |
---|
| 120 | }, |
---|
| 121 | /** |
---|
| 122 | * Returns a flyweight Element of the dom element object at the specified index |
---|
| 123 | * @param {Number} index |
---|
| 124 | * @return {Ext.Element} |
---|
| 125 | */ |
---|
| 126 | item : function(index){ |
---|
| 127 | var me = this, |
---|
| 128 | el = me.elements[index], |
---|
| 129 | out = null; |
---|
| 130 | |
---|
| 131 | if(el){ |
---|
| 132 | out = me.getElement(el); |
---|
| 133 | } |
---|
| 134 | return out; |
---|
| 135 | }, |
---|
| 136 | |
---|
| 137 | // fixes scope with flyweight |
---|
| 138 | addListener : function(eventName, handler, scope, opt){ |
---|
| 139 | var els = this.elements, |
---|
| 140 | len = els.length, |
---|
| 141 | i, e; |
---|
| 142 | |
---|
| 143 | for(i = 0; i<len; i++) { |
---|
| 144 | e = els[i]; |
---|
| 145 | if(e) { |
---|
| 146 | Ext.EventManager.on(e, eventName, handler, scope || e, opt); |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | return this; |
---|
| 150 | }, |
---|
| 151 | /** |
---|
| 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> |
---|
| 159 | * </ul> |
---|
| 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 |
---|
| 162 | */ |
---|
| 163 | each : function(fn, scope){ |
---|
| 164 | var me = this, |
---|
| 165 | els = me.elements, |
---|
| 166 | len = els.length, |
---|
| 167 | i, e; |
---|
| 168 | |
---|
| 169 | for(i = 0; i<len; i++) { |
---|
| 170 | e = els[i]; |
---|
| 171 | if(e){ |
---|
| 172 | e = this.getElement(e); |
---|
| 173 | if(fn.call(scope || e, e, me, i) === false){ |
---|
| 174 | break; |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | return me; |
---|
| 179 | }, |
---|
| 180 | |
---|
| 181 | /** |
---|
| 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 |
---|
| 185 | */ |
---|
| 186 | fill : function(els){ |
---|
| 187 | var me = this; |
---|
| 188 | me.elements = []; |
---|
| 189 | me.add(els); |
---|
| 190 | return me; |
---|
| 191 | }, |
---|
| 192 | |
---|
| 193 | /** |
---|
| 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> |
---|
| 199 | * </ul> |
---|
| 200 | * @return {CompositeElement} this |
---|
| 201 | */ |
---|
| 202 | filter : function(selector){ |
---|
| 203 | var els = [], |
---|
| 204 | me = this, |
---|
| 205 | fn = Ext.isFunction(selector) ? selector |
---|
| 206 | : function(el){ |
---|
| 207 | return el.is(selector); |
---|
| 208 | }; |
---|
| 209 | |
---|
| 210 | me.each(function(el, self, i) { |
---|
| 211 | if (fn(el, i) !== false) { |
---|
| 212 | els[els.length] = me.transformElement(el); |
---|
| 213 | } |
---|
| 214 | }); |
---|
| 215 | |
---|
| 216 | me.elements = els; |
---|
| 217 | return me; |
---|
| 218 | }, |
---|
| 219 | |
---|
| 220 | /** |
---|
| 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. |
---|
| 224 | */ |
---|
| 225 | indexOf : function(el){ |
---|
| 226 | return this.elements.indexOf(this.transformElement(el)); |
---|
| 227 | }, |
---|
| 228 | |
---|
| 229 | /** |
---|
| 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 |
---|
| 232 | * to replace. |
---|
| 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 |
---|
| 236 | */ |
---|
| 237 | replaceElement : function(el, replacement, domReplace){ |
---|
| 238 | var index = !isNaN(el) ? el : this.indexOf(el), |
---|
| 239 | d; |
---|
| 240 | if(index > -1){ |
---|
| 241 | replacement = Ext.getDom(replacement); |
---|
| 242 | if(domReplace){ |
---|
| 243 | d = this.elements[index]; |
---|
| 244 | d.parentNode.insertBefore(replacement, d); |
---|
| 245 | Ext.removeNode(d); |
---|
| 246 | } |
---|
| 247 | this.elements.splice(index, 1, replacement); |
---|
| 248 | } |
---|
| 249 | return this; |
---|
| 250 | }, |
---|
| 251 | |
---|
| 252 | /** |
---|
| 253 | * Removes all elements. |
---|
| 254 | */ |
---|
| 255 | clear : function(){ |
---|
| 256 | this.elements = []; |
---|
| 257 | } |
---|
| 258 | }; |
---|
| 259 | |
---|
| 260 | Ext.CompositeElementLite.prototype.on = Ext.CompositeElementLite.prototype.addListener; |
---|
| 261 | |
---|
| 262 | /** |
---|
| 263 | * @private |
---|
| 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 |
---|
| 267 | */ |
---|
| 268 | Ext.CompositeElementLite.importElementMethods = function() { |
---|
| 269 | var fnName, |
---|
| 270 | ElProto = Ext.Element.prototype, |
---|
| 271 | CelProto = Ext.CompositeElementLite.prototype; |
---|
| 272 | |
---|
| 273 | for (fnName in ElProto) { |
---|
| 274 | if (typeof ElProto[fnName] == 'function'){ |
---|
| 275 | (function(fnName) { |
---|
| 276 | CelProto[fnName] = CelProto[fnName] || function() { |
---|
| 277 | return this.invoke(fnName, arguments); |
---|
| 278 | }; |
---|
| 279 | }).call(CelProto, fnName); |
---|
| 280 | |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | }; |
---|
| 284 | |
---|
| 285 | Ext.CompositeElementLite.importElementMethods(); |
---|
| 286 | |
---|
| 287 | if(Ext.DomQuery){ |
---|
| 288 | Ext.Element.selectorFunction = Ext.DomQuery.select; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | /** |
---|
| 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 |
---|
| 299 | * @method select |
---|
| 300 | */ |
---|
| 301 | Ext.Element.select = function(selector, root){ |
---|
| 302 | var els; |
---|
| 303 | if(typeof selector == "string"){ |
---|
| 304 | els = Ext.Element.selectorFunction(selector, root); |
---|
| 305 | }else if(selector.length !== undefined){ |
---|
| 306 | els = selector; |
---|
| 307 | }else{ |
---|
| 308 | throw "Invalid selector"; |
---|
| 309 | } |
---|
| 310 | return new Ext.CompositeElementLite(els); |
---|
| 311 | }; |
---|
| 312 | /** |
---|
| 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} |
---|
| 319 | * @member Ext |
---|
| 320 | * @method select |
---|
| 321 | */ |
---|
| 322 | Ext.select = Ext.Element.select; |
---|