Diffstat (limited to 'frontend/beta/js/YUI-extensions/CompositeElement.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/YUI-extensions/CompositeElement.js | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/frontend/beta/js/YUI-extensions/CompositeElement.js b/frontend/beta/js/YUI-extensions/CompositeElement.js new file mode 100644 index 0000000..7b9c875 --- a/dev/null +++ b/frontend/beta/js/YUI-extensions/CompositeElement.js | |||
@@ -0,0 +1,140 @@ | |||
1 | /** | ||
2 | * @class YAHOO.ext.CompositeElement | ||
3 | * Standard composite class. Creates a YAHOO.ext.Element for every element in the collection. | ||
4 | * <br><br> | ||
5 | * <b>NOTE: Although they are not listed, this class supports all of the set/update methods of YAHOO.ext.Element. All YAHOO.ext.Element | ||
6 | * actions will be performed on all the elements in this collection.</b> | ||
7 | * <br><br> | ||
8 | * All methods return <i>this</i> and can be chained. | ||
9 | <pre><code> | ||
10 | var els = getEls('#some-el div.some-class'); | ||
11 | // or | ||
12 | var els = YAHOO.ext.Element.select('#some-el div.some-class'); | ||
13 | els.setWidth(100); // all elements become 100 width | ||
14 | els.hide(true); // all elements fade out and hide | ||
15 | // or | ||
16 | els.setWidth(100).hide(true); | ||
17 | </code></pre> | ||
18 | */ | ||
19 | YAHOO.ext.CompositeElement = function(els){ | ||
20 | this.elements = []; | ||
21 | this.addElements(els); | ||
22 | }; | ||
23 | YAHOO.ext.CompositeElement.prototype = { | ||
24 | isComposite: true, | ||
25 | addElements : function(els){ | ||
26 | if(!els) return this; | ||
27 | var yels = this.elements; | ||
28 | var index = yels.length-1; | ||
29 | for(var i = 0, len = els.length; i < len; i++) { | ||
30 | yels[++index] = getEl(els[i], true); | ||
31 | } | ||
32 | return this; | ||
33 | }, | ||
34 | invoke : function(fn, args){ | ||
35 | var els = this.elements; | ||
36 | for(var i = 0, len = els.length; i < len; i++) { | ||
37 | YAHOO.ext.Element.prototype[fn].apply(els[i], args); | ||
38 | } | ||
39 | return this; | ||
40 | }, | ||
41 | /** | ||
42 | * Adds elements to this composite. | ||
43 | * @param {String/Array} els A string CSS selector, an array of elements or an element | ||
44 | * @return {CompositeElement} this | ||
45 | */ | ||
46 | add : function(els){ | ||
47 | if(typeof els == 'string'){ | ||
48 | this.addElements(YAHOO.ext.Element.selectorFunction(string)); | ||
49 | }else if(els instanceof Array){ | ||
50 | this.addElements(els); | ||
51 | }else{ | ||
52 | this.addElements([els]); | ||
53 | } | ||
54 | return this; | ||
55 | }, | ||
56 | /** | ||
57 | * Calls the passed function passing (el, this, index) for each element in this composite. | ||
58 | * @param {Function} fn The function to call | ||
59 | * @param {Object} scope (optional) The <i>this</i> object (defaults to the element) | ||
60 | * @return {CompositeElement} this | ||
61 | */ | ||
62 | each : function(fn, scope){ | ||
63 | var els = this.elements; | ||
64 | for(var i = 0, len = els.length; i < len; i++){ | ||
65 | fn.call(scope || els[i], els[i], this, i); | ||
66 | } | ||
67 | return this; | ||
68 | } | ||
69 | }; | ||
70 | /** | ||
71 | * @class YAHOO.ext.CompositeElementLite | ||
72 | * @extends YAHOO.ext.CompositeElement | ||
73 | * Flyweight composite class. Reuses the same YAHOO.ext.Element for element operations. | ||
74 | * <br><br> | ||
75 | * <b>NOTE: Although they are not listed, this class supports all of the set/update methods of YAHOO.ext.Element. All YAHOO.ext.Element | ||
76 | * actions will be performed on all the elements in this collection.</b> | ||
77 | */ | ||
78 | YAHOO.ext.CompositeElementLite = function(els){ | ||
79 | YAHOO.ext.CompositeElementLite.superclass.constructor.call(this, els); | ||
80 | this.el = YAHOO.ext.Element.get(this.elements[0], true); | ||
81 | }; | ||
82 | YAHOO.extendX(YAHOO.ext.CompositeElementLite, YAHOO.ext.CompositeElement, { | ||
83 | addElements : function(els){ | ||
84 | if(els){ | ||
85 | this.elements = this.elements.concat(els); | ||
86 | } | ||
87 | return this; | ||
88 | }, | ||
89 | invoke : function(fn, args){ | ||
90 | var els = this.elements; | ||
91 | var el = this.el; | ||
92 | for(var i = 0, len = els.length; i < len; i++) { | ||
93 | el.dom = els[i]; | ||
94 | YAHOO.ext.Element.prototype[fn].apply(el, args); | ||
95 | } | ||
96 | return this; | ||
97 | } | ||
98 | }); | ||
99 | YAHOO.ext.CompositeElement.createCall = function(proto, fnName){ | ||
100 | if(!proto[fnName]){ | ||
101 | proto[fnName] = function(){ | ||
102 | return this.invoke(fnName, arguments); | ||
103 | }; | ||
104 | } | ||
105 | }; | ||
106 | for(var fnName in YAHOO.ext.Element.prototype){ | ||
107 | if(typeof YAHOO.ext.Element.prototype[fnName] == 'function'){ | ||
108 | YAHOO.ext.CompositeElement.createCall(YAHOO.ext.CompositeElement.prototype, fnName); | ||
109 | } | ||
110 | } | ||
111 | if(typeof cssQuery == 'function'){// Dean Edwards cssQuery | ||
112 | YAHOO.ext.Element.selectorFunction = cssQuery; | ||
113 | }else if(typeof document.getElementsBySelector == 'function'){ // Simon Willison's getElementsBySelector | ||
114 | YAHOO.ext.Element.selectorFunction = document.getElementsBySelector.createDelegate(document); | ||
115 | } | ||
116 | /** | ||
117 | * @member YAHOO.ext.Element | ||
118 | * Selects elements based on the passed CSS selector to enable working on them as 1. | ||
119 | * @param {String/Array} selector The CSS selector or an array of elements | ||
120 | * @param {Boolean} unique (optional) true to create a unique YAHOO.ext.Element for each element (defaults to a shared flyweight object) | ||
121 | * @return {CompositeElementLite/CompositeElement} | ||
122 | * @method @static | ||
123 | */ | ||
124 | YAHOO.ext.Element.select = function(selector, unique){ | ||
125 | var els; | ||
126 | if(typeof selector == 'string'){ | ||
127 | els = YAHOO.ext.Element.selectorFunction(selector); | ||
128 | }else if(selector instanceof Array){ | ||
129 | els = selector; | ||
130 | }else{ | ||
131 | throw 'Invalid selector'; | ||
132 | } | ||
133 | if(unique === true){ | ||
134 | return new YAHOO.ext.CompositeElement(els); | ||
135 | }else{ | ||
136 | return new YAHOO.ext.CompositeElementLite(els); | ||
137 | } | ||
138 | }; | ||
139 | |||
140 | var getEls = YAHOO.ext.Element.select; | ||