summaryrefslogtreecommitdiff
path: root/frontend/beta/js/YUI-extensions/MixedCollection.js
Unidiff
Diffstat (limited to 'frontend/beta/js/YUI-extensions/MixedCollection.js') (more/less context) (show whitespace changes)
-rw-r--r--frontend/beta/js/YUI-extensions/MixedCollection.js344
1 files changed, 344 insertions, 0 deletions
diff --git a/frontend/beta/js/YUI-extensions/MixedCollection.js b/frontend/beta/js/YUI-extensions/MixedCollection.js
new file mode 100644
index 0000000..2e3d88a
--- a/dev/null
+++ b/frontend/beta/js/YUI-extensions/MixedCollection.js
@@ -0,0 +1,344 @@
1/**
2 * @class YAHOO.ext.util.MixedCollection
3 * A Collection class that maintains both numeric indexes and keys and exposes events.<br>
4 * @constructor
5 * @param {Boolean} allowFunctions True if the addAll function should add function references
6 * to the collection.
7 */
8YAHOO.ext.util.MixedCollection = function(allowFunctions){
9 this.items = [];
10 this.keys = [];
11 this.events = {
12 /**
13 * @event clear
14 * Fires when the collection is cleared.
15 */
16 'clear' : new YAHOO.util.CustomEvent('clear'),
17 /**
18 * @event add
19 * Fires when an item is added to the collection.
20 * @param {Number} index The index at which the item was added.
21 * @param {Object} o The item added.
22 * @param {String} key The key associated with the added item.
23 */
24 'add' : new YAHOO.util.CustomEvent('add'),
25 /**
26 * @event replace
27 * Fires when an item is replaced in the collection.
28 * @param {String} key he key associated with the new added.
29 * @param {Object} old The item being replaced.
30 * @param {Object} new The new item.
31 */
32 'replace' : new YAHOO.util.CustomEvent('replace'),
33 /**
34 * @event remove
35 * Fires when an item is removed from the collection.
36 * @param {Object} o The item being removed.
37 * @param {String} key (optional) The key associated with the removed item.
38 */
39 'remove' : new YAHOO.util.CustomEvent('remove')
40 }
41 this.allowFunctions = allowFunctions === true;
42};
43
44YAHOO.extendX(YAHOO.ext.util.MixedCollection, YAHOO.ext.util.Observable, {
45 allowFunctions : false,
46
47/**
48 * Adds an item to the collection.
49 * @param {String} key The key to associate with the item
50 * @param {Object} o The item to add.
51 * @return {Object} The item added.
52 */
53 add : function(key, o){
54 if(arguments.length == 1){
55 o = arguments[0];
56 key = this.getKey(o);
57 }
58 this.items.push(o);
59 if(typeof key != 'undefined' && key != null){
60 this.items[key] = o;
61 this.keys.push(key);
62 }
63 this.fireEvent('add', this.items.length-1, o, key);
64 return o;
65 },
66
67/**
68 * MixedCollection has a generic way to fetch keys if you implement getKey.
69 <pre><code>
70 // normal way
71 var mc = new YAHOO.ext.util.MixedCollection();
72 mc.add(someEl.dom.id, someEl);
73 mc.add(otherEl.dom.id, otherEl);
74 //and so on
75
76 // using getKey
77 var mc = new YAHOO.ext.util.MixedCollection();
78 mc.getKey = function(el){
79 return el.dom.id;
80 }
81 mc.add(someEl);
82 mc.add(otherEl);
83 // etc
84 </code>
85 * @param o {Object} The item for which to find the key.
86 * @return {Object} The key for the passed item.
87 */
88 getKey : function(o){
89 return null;
90 },
91
92/**
93 * Replaces an item in the collection.
94 * @param {String} key The key associated with the item to replace, or the item to replace.
95 * @param o {Object} o (optional) If the first parameter passed was a key, the item to associate with that key.
96 * @return {Object} The new item.
97 */
98 replace : function(key, o){
99 if(arguments.length == 1){
100 o = arguments[0];
101 key = this.getKey(o);
102 }
103 if(typeof this.items[key] == 'undefined'){
104 return this.add(key, o);
105 }
106 var old = this.items[key];
107 if(typeof key == 'number'){ // array index key
108 this.items[key] = o;
109 }else{
110 var index = this.indexOfKey(key);
111 this.items[index] = o;
112 this.items[key] = o;
113 }
114 this.fireEvent('replace', key, old, o);
115 return o;
116 },
117
118/**
119 * Adds all elements of an Array or an Object to the collection.
120 * @param {Object/Array} objs An Object containing properties which will be added to the collection, or
121 * an Array of values, each of which are added to the collection.
122 */
123 addAll : function(objs){
124 if(arguments.length > 1 || objs instanceof Array){
125 var args = arguments.length > 1 ? arguments : objs;
126 for(var i = 0, len = args.length; i < len; i++){
127 this.add(args[i]);
128 }
129 }else{
130 for(var key in objs){
131 if(this.allowFunctions || typeof objs[key] != 'function'){
132 this.add(objs[key], key);
133 }
134 }
135 }
136 },
137
138/**
139 * Executes the specified function once for every item in the collection, passing each
140 * item as the first and only parameter.
141 * @param {Function} fn The function to execute for each item.
142 * @param {Object} scope (optional) The scope in which to execute the function.
143 */
144 each : function(fn, scope){
145 for(var i = 0, len = this.items.length; i < len; i++){
146 fn.call(scope || window, this.items[i]);
147 }
148 },
149
150/**
151 * Executes the specified function once for every key in the collection, passing each
152 * key, and its associated item as the first two parameters.
153 * @param {Function} fn The function to execute for each item.
154 * @param {Object} scope (optional) The scope in which to execute the function.
155 */
156 eachKey : function(fn, scope){
157 for(var i = 0, len = this.keys.length; i < len; i++){
158 fn.call(scope || window, this.keys[i], this.items[i]);
159 }
160 },
161
162/**
163 * Returns the first item in the collection which elicits a true return value from the
164 * passed selection function.
165 * @param {Function} fn The selection function to execute for each item.
166 * @param {Object} scope (optional) The scope in which to execute the function.
167 * @return {Object} The first item in the collection which returned true from the selection function.
168 */
169 find : function(fn, scope){
170 for(var i = 0, len = this.items.length; i < len; i++){
171 if(fn.call(scope || window, this.items[i])){
172 return this.items[i];
173 }
174 }
175 return null;
176 },
177
178/**
179 * Inserts an item at the specified index in the collection.
180 * @param {Number} index The index to insert the item at.
181 * @param {String} key The key to associate with the new item, or the item itself.
182 * @param {Object} o (optional) If the second parameter was a key, the new item.
183 * @return {Object} The item inserted.
184 */
185 insert : function(index, key, o){
186 if(arguments.length == 2){
187 o = arguments[1];
188 key = this.getKey(o);
189 }
190 if(index >= this.items.length){
191 return this.add(o, key);
192 }
193 this.items.splice(index, 0, o);
194 if(typeof key != 'undefined' && key != null){
195 this.items[key] = o;
196 this.keys.splice(index, 0, key);
197 }
198 this.fireEvent('add', index, o, key);
199 return o;
200 },
201
202/**
203 * Removed an item from the collection.
204 * @param {Object} o The item to remove.
205 * @return {Object} The item removed.
206 */
207 remove : function(o){
208 var index = this.indexOf(o);
209 this.items.splice(index, 1);
210 if(typeof this.keys[index] != 'undefined'){
211 var key = this.keys[index];
212 this.keys.splice(index, 1);
213 delete this.items[key];
214 }
215 this.fireEvent('remove', o);
216 return o;
217 },
218
219/**
220 * Remove an item from a specified index in the collection.
221 * @param {Number} index The index within the collection of the item to remove.
222 */
223 removeAt : function(index){
224 this.items.splice(index, 1);
225 var key = this.keys[index];
226 if(typeof key != 'undefined'){
227 this.keys.splice(index, 1);
228 delete this.items[key];
229 }
230 this.fireEvent('remove', o, key);
231 },
232
233/**
234 * Removed an item associated with the passed key fom the collection.
235 * @param {String} key The key of the item to remove.
236 */
237 removeKey : function(key){
238 var o = this.items[key];
239 var index = this.indexOf(o);
240 this.items.splice(index, 1);
241 this.keys.splice(index, 1);
242 delete this.items[key];
243 this.fireEvent('remove', o, key);
244 },
245
246/**
247 * Returns the number of items in the collection.
248 * @return {Number} the number of items in the collection.
249 */
250 getCount : function(){
251 return this.items.length;
252 },
253
254/**
255 * Returns index within the collection of the passed Object.
256 * @param {Object} o The item to find the index of.
257 * @return {Number} index of the item.
258 */
259 indexOf : function(o){
260 if(!this.items.indexOf){
261 for(var i = 0, len = this.items.length; i < len; i++){
262 if(this.items[i] == o) return i;
263 }
264 return -1;
265 }else{
266 return this.items.indexOf(o);
267 }
268 },
269
270/**
271 * Returns index within the collection of the passed key.
272 * @param {String} key The key to find the index of.
273 * @return {Number} index of the key.
274 */
275 indexOfKey : function(key){
276 if(!this.keys.indexOf){
277 for(var i = 0, len = this.keys.length; i < len; i++){
278 if(this.keys[i] == key) return i;
279 }
280 return -1;
281 }else{
282 return this.keys.indexOf(key);
283 }
284 },
285
286/**
287 * Returns the item associated with the passed key.
288 * @param {String/Number} key The key or index of the item.
289 * @return {Object} The item associated with the passed key.
290 */
291 item : function(key){
292 return this.items[key];
293 },
294
295/**
296 * Returns true if the collection contains the passed Object as an item.
297 * @param {Object} o The Object to look for in the collection.
298 * @return {Boolean} True if the collection contains the Object as an item.
299 */
300 contains : function(o){
301 return this.indexOf(o) != -1;
302 },
303
304/**
305 * Returns true if the collection contains the passed Object as a key.
306 * @param {String} key The key to look for in the collection.
307 * @return {Boolean} True if the collection contains the Object as a key.
308 */
309 containsKey : function(key){
310 return typeof this.items[key] != 'undefined';
311 },
312
313/**
314 * Removes all items from the collection.
315 */
316 clear : function(o){
317 this.items = [];
318 this.keys = [];
319 this.fireEvent('clear');
320 },
321
322/**
323 * Returns the first item in the collection.
324 * @return {Object} the first item in the collection..
325 */
326 first : function(){
327 return this.items[0];
328 },
329
330/**
331 * Returns the last item in the collection.
332 * @return {Object} the last item in the collection..
333 */
334 last : function(){
335 return this.items[this.items.length];
336 }
337});
338/**
339 * Returns the item associated with the passed key or index.
340 * @method
341 * @param {String/Number} key The key or index of the item.
342 * @return {Object} The item associated with the passed key.
343 */
344YAHOO.ext.util.MixedCollection.prototype.get = YAHOO.ext.util.MixedCollection.prototype.item;