1
0
mirror of http://git.whoc.org.uk/git/password-manager.git synced 2024-11-01 09:27:15 +01:00
password-manager-mirror/frontend/beta/js/YUI-extensions/CompositeElement.js

141 lines
5.0 KiB
JavaScript
Raw Normal View History

/**
* @class YAHOO.ext.CompositeElement
* Standard composite class. Creates a YAHOO.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 YAHOO.ext.Element. All YAHOO.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 = YAHOO.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>
*/
YAHOO.ext.CompositeElement = function(els){
this.elements = [];
this.addElements(els);
};
YAHOO.ext.CompositeElement.prototype = {
isComposite: true,
addElements : function(els){
if(!els) return this;
var yels = this.elements;
var index = yels.length-1;
for(var i = 0, len = els.length; i < len; i++) {
yels[++index] = getEl(els[i], true);
}
return this;
},
invoke : function(fn, args){
var els = this.elements;
for(var i = 0, len = els.length; i < len; i++) {
YAHOO.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(YAHOO.ext.Element.selectorFunction(string));
}else if(els instanceof Array){
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++){
fn.call(scope || els[i], els[i], this, i);
}
return this;
}
};
/**
* @class YAHOO.ext.CompositeElementLite
* @extends YAHOO.ext.CompositeElement
* Flyweight composite class. Reuses the same YAHOO.ext.Element for element operations.
* <br><br>
* <b>NOTE: Although they are not listed, this class supports all of the set/update methods of YAHOO.ext.Element. All YAHOO.ext.Element
* actions will be performed on all the elements in this collection.</b>
*/
YAHOO.ext.CompositeElementLite = function(els){
YAHOO.ext.CompositeElementLite.superclass.constructor.call(this, els);
this.el = YAHOO.ext.Element.get(this.elements[0], true);
};
YAHOO.extendX(YAHOO.ext.CompositeElementLite, YAHOO.ext.CompositeElement, {
addElements : function(els){
if(els){
this.elements = this.elements.concat(els);
}
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];
YAHOO.ext.Element.prototype[fn].apply(el, args);
}
return this;
}
});
YAHOO.ext.CompositeElement.createCall = function(proto, fnName){
if(!proto[fnName]){
proto[fnName] = function(){
return this.invoke(fnName, arguments);
};
}
};
for(var fnName in YAHOO.ext.Element.prototype){
if(typeof YAHOO.ext.Element.prototype[fnName] == 'function'){
YAHOO.ext.CompositeElement.createCall(YAHOO.ext.CompositeElement.prototype, fnName);
}
}
if(typeof cssQuery == 'function'){// Dean Edwards cssQuery
YAHOO.ext.Element.selectorFunction = cssQuery;
}else if(typeof document.getElementsBySelector == 'function'){ // Simon Willison's getElementsBySelector
YAHOO.ext.Element.selectorFunction = document.getElementsBySelector.createDelegate(document);
}
/**
* @member YAHOO.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 YAHOO.ext.Element for each element (defaults to a shared flyweight object)
* @return {CompositeElementLite/CompositeElement}
* @method @static
*/
YAHOO.ext.Element.select = function(selector, unique){
var els;
if(typeof selector == 'string'){
els = YAHOO.ext.Element.selectorFunction(selector);
}else if(selector instanceof Array){
els = selector;
}else{
throw 'Invalid selector';
}
if(unique === true){
return new YAHOO.ext.CompositeElement(els);
}else{
return new YAHOO.ext.CompositeElementLite(els);
}
};
var getEls = YAHOO.ext.Element.select;