X-Git-Url: http://git.ithinksw.org/extjs.git/blobdiff_plain/c930e9176a5a85509c5b0230e2bff5c22a591432..6e39d509471fe9b4e2660e0d1631b350d0c66f40:/docs/source/CompositeElementLite.html diff --git a/docs/source/CompositeElementLite.html b/docs/source/CompositeElementLite.html index 1f85cd1f..e84961e6 100644 --- a/docs/source/CompositeElementLite.html +++ b/docs/source/CompositeElementLite.html @@ -1,63 +1,123 @@ + The source code -
/** +
/**
  * @class Ext.CompositeElementLite
- * Flyweight composite class. Reuses the same Ext.Element for element operations.
- 

- var els = Ext.select("#some-el div.some-class");
- // or select directly from an existing element
- var el = Ext.get('some-el');
- el.select('div.some-class');
+ * 

This class encapsulates a collection of DOM elements, providing methods to filter + * members, or to perform collective actions upon the whole set.

+ *

Although they are not listed, this class supports all of the methods of {@link Ext.Element} and + * {@link Ext.Fx}. The methods from these classes will be performed on all the elements in this collection.

+ * Example:

+var els = Ext.select("#some-el div.some-class");
+// or select directly from an existing element
+var el = Ext.get('some-el');
+el.select('div.some-class');
 
- els.setWidth(100); // all elements become 100 width
- els.hide(true); // all elements fade out and hide
- // or
- els.setWidth(100).hide(true);
- 


- * NOTE: Although they are not listed, this class supports all of the set/update methods of Ext.Element. All Ext.Element - * actions will be performed on all the elements in this collection. +els.setWidth(100); // all elements become 100 width +els.hide(true); // all elements fade out and hide +// or +els.setWidth(100).hide(true); +
*/ Ext.CompositeElementLite = function(els, root){ +
/** + *

The Array of DOM elements which this CompositeElement encapsulates. Read-only.

+ *

This will not usually be accessed in developers' code, but developers wishing + * to augment the capabilities of the CompositeElementLite class may use it when adding + * methods to the class.

+ *

For example to add the nextAll method to the class to add all + * following siblings of selected elements, the code would be

+Ext.override(Ext.CompositeElementLite, {
+    nextAll: function() {
+        var els = this.elements, i, l = els.length, n, r = [], ri = -1;
+
+//      Loop through all elements in this Composite, accumulating
+//      an Array of all siblings.
+        for (i = 0; i < l; i++) {
+            for (n = els[i].nextSibling; n; n = n.nextSibling) {
+                r[++ri] = n;
+            }
+        }
+
+//      Add all found siblings to this Composite
+        return this.add(r);
+    }
+});
+ * @type Array + * @property elements + */ this.elements = []; this.add(els, root); this.el = new Ext.Element.Flyweight(); }; Ext.CompositeElementLite.prototype = { - isComposite: true, -
/** - * Returns the number of elements in this composite + isComposite: true, + + // private + getElement : function(el){ + // Set the shared flyweight dom property to the current element + var e = this.el; + e.dom = el; + e.id = el.id; + return e; + }, + + // private + transformElement : function(el){ + return Ext.getDom(el); + }, + +
/** + * Returns the number of elements in this Composite. * @return Number */ getCount : function(){ return this.elements.length; }, - add : function(els){ - if(els){ - if (Ext.isArray(els)) { - this.elements = this.elements.concat(els); - } else { - var yels = this.elements; - Ext.each(els, function(e) { - yels.push(e); - }); - } +
/** + * Adds elements to this Composite object. + * @param {Mixed} els Either an Array of DOM elements to add, or another Composite object who's elements should be added. + * @return {CompositeElement} This Composite object. + */ + add : function(els, root){ + var me = this, + elements = me.elements; + if(!els){ + return this; } - return this; + if(Ext.isString(els)){ + els = Ext.Element.selectorFunction(els, root); + }else if(els.isComposite){ + els = els.elements; + }else if(!Ext.isIterable(els)){ + els = [els]; + } + + for(var i = 0, len = els.length; i < len; ++i){ + elements.push(me.transformElement(els[i])); + } + return me; }, + invoke : function(fn, args){ - var els = this.elements, - el = this.el; - Ext.each(els, function(e) { - el.dom = e; - Ext.Element.prototype[fn].apply(el, args); - }); - return this; + var me = this, + els = me.elements, + len = els.length, + e; + + for(i = 0; i/** * Returns a flyweight Element of the dom element object at the specified index @@ -65,37 +125,97 @@ Ext.CompositeElementLite.prototype = { * @return {Ext.Element} */ item : function(index){ - var me = this; - if(!me.elements[index]){ - return null; + var me = this, + el = me.elements[index], + out = null; + + if(el){ + out = me.getElement(el); } - me.el.dom = me.elements[index]; - return me.el; + return out; }, // fixes scope with flyweight addListener : function(eventName, handler, scope, opt){ - Ext.each(this.elements, function(e) { - Ext.EventManager.on(e, eventName, handler, scope || e, opt); - }); + var els = this.elements, + len = els.length, + i, e; + + for(i = 0; i/** - * Calls the passed function passing (el, this, index) for each element in this composite. The element - * passed is the flyweight (shared) Ext.Element instance, so if you require a - * a reference to the dom node, use el.dom. - * @param {Function} fn The function to call - * @param {Object} scope (optional) The this object (defaults to the element) - * @return {CompositeElement} this - */ + *

Calls the passed function for each element in this composite.

+ * @param {Function} fn The function to call. The function is passed the following parameters:
    + *
  • el : Element
    The current Element in the iteration. + * This is the flyweight (shared) Ext.Element instance, so if you require a + * a reference to the dom node, use el.dom.
  • + *
  • c : Composite
    This Composite object.
  • + *
  • idx : Number
    The zero-based index in the iteration.
  • + *
+ * @param {Object} scope (optional) The scope (this reference) in which the function is executed. (defaults to the Element) + * @return {CompositeElement} this + */ each : function(fn, scope){ var me = this, - el = me.el; - - Ext.each(me.elements, function(e,i) { - el.dom = e; - return fn.call(scope || el, el, me, i); + els = me.elements, + len = els.length, + i, e; + + for(i = 0; i/** + * Clears this Composite and adds the elements passed. + * @param {Mixed} els Either an array of DOM elements, or another Composite from which to fill this Composite. + * @return {CompositeElement} this + */ + fill : function(els){ + var me = this; + me.elements = []; + me.add(els); + return me; + }, + +
/** + * Filters this composite to only elements that match the passed selector. + * @param {String/Function} selector A string CSS selector or a comparison function. + * The comparison function will be called with the following arguments:
    + *
  • el : Ext.Element
    The current DOM element.
  • + *
  • index : Number
    The current index within the collection.
  • + *
+ * @return {CompositeElement} this + */ + filter : function(selector){ + var els = [], + me = this, + elements = me.elements, + fn = Ext.isFunction(selector) ? selector + : function(el){ + return el.is(selector); + }; + + + me.each(function(el, self, i){ + if(fn(el, i) !== false){ + els[els.length] = me.transformElement(el); + } }); + me.elements = els; return me; }, @@ -105,7 +225,7 @@ Ext.CompositeElementLite.prototype = { * @return Number The index of the passed Ext.Element in the composite collection, or -1 if not found. */ indexOf : function(el){ - return this.elements.indexOf(Ext.getDom(el)); + return this.elements.indexOf(this.transformElement(el)); },
/** @@ -118,7 +238,7 @@ Ext.CompositeElementLite.prototype = { */ replaceElement : function(el, replacement, domReplace){ var index = !isNaN(el) ? el : this.indexOf(el), - d; + d; if(index > -1){ replacement = Ext.getDom(replacement); if(domReplace){ @@ -143,16 +263,16 @@ Ext.CompositeElementLite.prototype.on = Ext.CompositeElementLite.prototype.addLi (function(){ var fnName, - ElProto = Ext.Element.prototype, - CelProto = Ext.CompositeElementLite.prototype; - + ElProto = Ext.Element.prototype, + CelProto = Ext.CompositeElementLite.prototype; + for(fnName in ElProto){ if(Ext.isFunction(ElProto[fnName])){ - (function(fnName){ - CelProto[fnName] = CelProto[fnName] || function(){ - return this.invoke(fnName, arguments); - }; - }).call(CelProto, fnName); + (function(fnName){ + CelProto[fnName] = CelProto[fnName] || function(){ + return this.invoke(fnName, arguments); + }; + }).call(CelProto, fnName); } } @@ -167,13 +287,12 @@ if(Ext.DomQuery){ * to be applied to many related elements in one statement through the returned {@link Ext.CompositeElement CompositeElement} or * {@link Ext.CompositeElementLite CompositeElementLite} object. * @param {String/Array} selector The CSS selector or an array of elements - * @param {Boolean} unique (optional) true to create a unique Ext.Element for each element (defaults to a shared flyweight object) Not supported in core * @param {HTMLElement/String} root (optional) The root element of the query or id of the root * @return {CompositeElementLite/CompositeElement} * @member Ext.Element * @method select */ -Ext.Element.select = function(selector, unique, root){ +Ext.Element.select = function(selector, root){ var els; if(typeof selector == "string"){ els = Ext.Element.selectorFunction(selector, root); @@ -189,7 +308,6 @@ Ext.Element.select = function(selector, unique, root){ * to be applied to many related elements in one statement through the returned {@link Ext.CompositeElement CompositeElement} or * {@link Ext.CompositeElementLite CompositeElementLite} object. * @param {String/Array} selector The CSS selector or an array of elements - * @param {Boolean} unique (optional) true to create a unique Ext.Element for each element (defaults to a shared flyweight object) * @param {HTMLElement/String} root (optional) The root element of the query or id of the root * @return {CompositeElementLite/CompositeElement} * @member Ext