CompositeElement.js
/**
* @class Ext.CompositeElement
* Standard composite class. Creates a Ext.Element for every element in the collection.
* <br><br>
* <b>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.</b>
* <br><br>
* All methods return <i>this</i> and can be chained.
<pre><code>
var els = getEls('#some-el div.some-class');
// or
var els = Ext.Element.select('#some-el 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);
</code></pre>
*/
Ext.CompositeElement = function(els){
this.elements = [];
this.addElements(els);
};
Ext.CompositeElement.prototype = {
isComposite: true,
addElements : function(els){
if(!els) return this;
if(typeof els == 'string'){
els = Ext.Element.selectorFunction(els);
}
var yels = this.elements;
var index = yels.length-1;
for(var i = 0, len = els.length; i < len; i++) {
yels[++index] = Ext.get(els[i], true);
}
return this;
},
invoke : function(fn, args){
var els = this.elements;
for(var i = 0, len = els.length; i < len; i++) {
Ext.Element.prototype[fn].apply(els[i], args);
}
return this;
},
/**
* Adds elements to this composite.
* @param {String/Array} els A string CSS selector, an array of elements or an element
* @return {CompositeElement} this
*/
add : function(els){
if(typeof els == 'string'){
this.addElements(Ext.Element.selectorFunction(string));
}else if(els.length !== undefined){
this.addElements(els);
}else{
this.addElements([els]);
}
return this;
},
/**
* Calls the passed function passing (el, this, index) for each element in this composite.
* @param {Function} fn The function to call
* @param {Object} scope (optional) The <i>this</i> object (defaults to the element)
* @return {CompositeElement} this
*/
each : function(fn, scope){
var els = this.elements;
for(var i = 0, len = els.length; i < len; i++){
if(fn.call(scope || els[i], els[i], this, i) === false) {
break;
}
}
return this;
},
/**
* Returns the Element object at the specified index
* @param {Number} index
* @return {Ext.Element}
*/
item : function(index){
return this.elements[index];
}
};
Ext.CompositeElement.createCall = function(proto, fnName){
if(!proto[fnName]){
proto[fnName] = function(){
return this.invoke(fnName, arguments);
};
}
};
for(var fnName in Ext.Element.prototype){
if(typeof Ext.Element.prototype[fnName] == 'function'){
Ext.CompositeElement.createCall(Ext.CompositeElement.prototype, fnName);
}
};
/**
* @class Ext.CompositeElementLite
* @extends Ext.CompositeElement
* Flyweight composite class. Reuses the same Ext.Element for element operations.
* <br><br>
* <b>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.</b>
*/
Ext.CompositeElementLite = function(els){
Ext.CompositeElementLite.superclass.constructor.call(this, els);
this.el = new Ext.Element(document.body, true);
};
Ext.extend(Ext.CompositeElementLite, Ext.CompositeElement, {
addElements : function(els){
if(els){
if(els instanceof Array){
this.elements = this.elements.concat(els);
}else{
var yels = this.elements;
var index = yels.length-1;
for(var i = 0, len = els.length; i < len; i++) {
yels[++index] = els[i];
}
}
}
return this;
},
invoke : function(fn, args){
var els = this.elements;
var el = this.el;
for(var i = 0, len = els.length; i < len; i++) {
el.dom = els[i];
Ext.Element.prototype[fn].apply(el, args);
}
return this;
},
/**
* Returns a flyweight Element of the dom element object at the specified index
* @param {Number} index
* @return {Ext.Element}
*/
item : function(index){
this.el.dom = this.elements[index];
return this.el;
},
// fixes scope with flyweight
mon : function(eventName, handler, scope){
var els = this.elements;
for(var i = 0, len = els.length; i < len; i++) {
Ext.EventManager.on(els[i], eventName, handler, scope || els[i], true);
}
return this;
},
// fixes scope with flyweight
on : function(eventName, handler, scope){
var els = this.elements;
for(var i = 0, len = els.length; i < len; i++) {
YAHOO.util.Event.on(els[i], eventName, handler, scope || els[i], true);
}
return this;
},
each : function(fn, scope){
var els = this.elements;
var el = this.el;
for(var i = 0, len = els.length; i < len; i++){
el.dom = els[i];
if(fn.call(scope || el, el, this, i) === false){
break;
}
}
return this;
}
});
if(Ext.DomQuery){
Ext.Element.selectorFunction = Ext.DomQuery.select;
}
/**
* @member Ext.Element
* Selects elements based on the passed CSS selector to enable working on them as 1.
* @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)
* @return {CompositeElementLite/CompositeElement}
* @method @static
*/
Ext.Element.select = function(selector, unique){
var els;
if(typeof selector == 'string'){
els = Ext.Element.selectorFunction(selector);
}else if(selector.length !== undefined){
els = selector;
}else{
throw 'Invalid selector';
}
if(unique === true){
return new Ext.CompositeElement(els);
}else{
return new Ext.CompositeElementLite(els);
}
};
/**
* Selects elements based on the passed CSS selector to enable working on them as 1. Shorthand of {@link Ext.Element#select}
* Selects elements based on the passed CSS selector to enable working on them as 1.
* @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)
* @return {CompositeElementLite/CompositeElement}
* @member Ext
* @method select
*/
Ext.select = Ext.Element.select;
yui-ext - Copyright © 2006 Jack Slocum. |
Yahoo! UI - Copyright © 2006 Yahoo! Inc.
All rights reserved.