4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
14 * @class Roo.util.MixedCollection
15 * @extends Roo.util.Observable
16 * A Collection class that maintains both numeric indexes and keys and exposes events.
18 * @param {Boolean} allowFunctions True if the addAll function should add function references to the
19 * collection (defaults to false)
20 * @param {Function} keyFn A function that can accept an item of the type(s) stored in this MixedCollection
21 * and return the key value for that item. This is used when available to look up the key on items that
22 * were passed without an explicit key parameter to a MixedCollection method. Passing this parameter is
23 * equivalent to providing an implementation for the {@link #getKey} method.
25 Roo.util.MixedCollection = function(allowFunctions, keyFn){
33 * Fires when the collection is cleared.
38 * Fires when an item is added to the collection.
39 * @param {Number} index The index at which the item was added.
40 * @param {Object} o The item added.
41 * @param {String} key The key associated with the added item.
46 * Fires when an item is replaced in the collection.
47 * @param {String} key he key associated with the new added.
48 * @param {Object} old The item being replaced.
49 * @param {Object} new The new item.
54 * Fires when an item is removed from the collection.
55 * @param {Object} o The item being removed.
56 * @param {String} key (optional) The key associated with the removed item.
61 this.allowFunctions = allowFunctions === true;
65 Roo.util.MixedCollection.superclass.constructor.call(this);
68 Roo.extend(Roo.util.MixedCollection, Roo.util.Observable, {
69 allowFunctions : false,
72 * Adds an item to the collection.
73 * @param {String} key The key to associate with the item
74 * @param {Object} o The item to add.
75 * @return {Object} The item added.
77 add : function(key, o){
78 if(arguments.length == 1){
82 if(typeof key == "undefined" || key === null){
87 var old = this.map[key];
89 return this.replace(key, o);
96 this.fireEvent("add", this.length-1, o, key);
101 * Adds an item to the collection.
102 * @param {String} key The key to associate with the item
103 * @param {Object} o The item to add.
104 * @return {Object} The item added.
106 unshift : function(key, o){
107 if(arguments.length == 1){
109 key = this.getKey(o);
111 if(typeof key == "undefined" || key === null){
113 this.items.unshift(o);
114 this.keys.unshift(null);
116 var old = this.map[key];
118 return this.replace(key, o);
121 this.items.unshift(o);
123 this.keys.unshift(key);
125 this.fireEvent("add", this.length-1, o, key);
129 * MixedCollection has a generic way to fetch keys if you implement getKey.
132 var mc = new Roo.util.MixedCollection();
133 mc.add(someEl.dom.id, someEl);
134 mc.add(otherEl.dom.id, otherEl);
138 var mc = new Roo.util.MixedCollection();
139 mc.getKey = function(el){
145 // or via the constructor
146 var mc = new Roo.util.MixedCollection(false, function(el){
152 * @param o {Object} The item for which to find the key.
153 * @return {Object} The key for the passed item.
155 getKey : function(o){
160 * Replaces an item in the collection.
161 * @param {String} key The key associated with the item to replace, or the item to replace.
162 * @param o {Object} o (optional) If the first parameter passed was a key, the item to associate with that key.
163 * @return {Object} The new item.
165 replace : function(key, o){
166 if(arguments.length == 1){
168 key = this.getKey(o);
170 var old = this.item(key);
171 if(typeof key == "undefined" || key === null || typeof old == "undefined"){
172 return this.add(key, o);
174 var index = this.indexOfKey(key);
175 this.items[index] = o;
177 this.fireEvent("replace", key, old, o);
182 * Adds all elements of an Array or an Object to the collection.
183 * @param {Object/Array} objs An Object containing properties which will be added to the collection, or
184 * an Array of values, each of which are added to the collection.
186 addAll : function(objs){
187 if(arguments.length > 1 || objs instanceof Array){
188 var args = arguments.length > 1 ? arguments : objs;
189 for(var i = 0, len = args.length; i < len; i++){
193 for(var key in objs){
194 if(this.allowFunctions || typeof objs[key] != "function"){
195 this.add(key, objs[key]);
202 * Executes the specified function once for every item in the collection, passing each
203 * item as the first and only parameter. returning false from the function will stop the iteration.
204 * @param {Function} fn The function to execute for each item.
205 * @param {Object} scope (optional) The scope in which to execute the function.
207 each : function(fn, scope){
208 var items = [].concat(this.items); // each safe for removal
209 for(var i = 0, len = items.length; i < len; i++){
210 if(fn.call(scope || items[i], items[i], i, len) === false){
217 * Executes the specified function once for every key in the collection, passing each
218 * key, and its associated item as the first two parameters.
219 * @param {Function} fn The function to execute for each item.
220 * @param {Object} scope (optional) The scope in which to execute the function.
222 eachKey : function(fn, scope){
223 for(var i = 0, len = this.keys.length; i < len; i++){
224 fn.call(scope || window, this.keys[i], this.items[i], i, len);
229 * Returns the first item in the collection which elicits a true return value from the
230 * passed selection function.
231 * @param {Function} fn The selection function to execute for each item.
232 * @param {Object} scope (optional) The scope in which to execute the function.
233 * @return {Object} The first item in the collection which returned true from the selection function.
235 find : function(fn, scope){
236 for(var i = 0, len = this.items.length; i < len; i++){
237 if(fn.call(scope || window, this.items[i], this.keys[i])){
238 return this.items[i];
245 * Inserts an item at the specified index in the collection.
246 * @param {Number} index The index to insert the item at.
247 * @param {String} key The key to associate with the new item, or the item itself.
248 * @param {Object} o (optional) If the second parameter was a key, the new item.
249 * @return {Object} The item inserted.
251 insert : function(index, key, o){
252 if(arguments.length == 2){
254 key = this.getKey(o);
256 if(index >= this.length){
257 return this.add(key, o);
260 this.items.splice(index, 0, o);
261 if(typeof key != "undefined" && key != null){
264 this.keys.splice(index, 0, key);
265 this.fireEvent("add", index, o, key);
270 * Removed an item from the collection.
271 * @param {Object} o The item to remove.
272 * @return {Object} The item removed.
274 remove : function(o){
275 return this.removeAt(this.indexOf(o));
279 * Remove an item from a specified index in the collection.
280 * @param {Number} index The index within the collection of the item to remove.
282 removeAt : function(index){
283 if(index < this.length && index >= 0){
285 var o = this.items[index];
286 this.items.splice(index, 1);
287 var key = this.keys[index];
288 if(typeof key != "undefined"){
289 delete this.map[key];
291 this.keys.splice(index, 1);
292 this.fireEvent("remove", o, key);
297 * Removed an item associated with the passed key fom the collection.
298 * @param {String} key The key of the item to remove.
300 removeKey : function(key){
301 return this.removeAt(this.indexOfKey(key));
305 * Returns the number of items in the collection.
306 * @return {Number} the number of items in the collection.
308 getCount : function(){
313 * Returns index within the collection of the passed Object.
314 * @param {Object} o The item to find the index of.
315 * @return {Number} index of the item.
317 indexOf : function(o){
318 if(!this.items.indexOf){
319 for(var i = 0, len = this.items.length; i < len; i++){
320 if(this.items[i] == o) return i;
324 return this.items.indexOf(o);
329 * Returns index within the collection of the passed key.
330 * @param {String} key The key to find the index of.
331 * @return {Number} index of the key.
333 indexOfKey : function(key){
334 if(!this.keys.indexOf){
335 for(var i = 0, len = this.keys.length; i < len; i++){
336 if(this.keys[i] == key) return i;
340 return this.keys.indexOf(key);
345 * Returns the item associated with the passed key OR index. Key has priority over index.
346 * @param {String/Number} key The key or index of the item.
347 * @return {Object} The item associated with the passed key.
349 item : function(key){
350 var item = typeof this.map[key] != "undefined" ? this.map[key] : this.items[key];
351 return typeof item != 'function' || this.allowFunctions ? item : null; // for prototype!
355 * Returns the item at the specified index.
356 * @param {Number} index The index of the item.
359 itemAt : function(index){
360 return this.items[index];
364 * Returns the item associated with the passed key.
365 * @param {String/Number} key The key of the item.
366 * @return {Object} The item associated with the passed key.
369 return this.map[key];
373 * Returns true if the collection contains the passed Object as an item.
374 * @param {Object} o The Object to look for in the collection.
375 * @return {Boolean} True if the collection contains the Object as an item.
377 contains : function(o){
378 return this.indexOf(o) != -1;
382 * Returns true if the collection contains the passed Object as a key.
383 * @param {String} key The key to look for in the collection.
384 * @return {Boolean} True if the collection contains the Object as a key.
386 containsKey : function(key){
387 return typeof this.map[key] != "undefined";
391 * Removes all items from the collection.
398 this.fireEvent("clear");
402 * Returns the first item in the collection.
403 * @return {Object} the first item in the collection..
406 return this.items[0];
410 * Returns the last item in the collection.
411 * @return {Object} the last item in the collection..
414 return this.items[this.length-1];
417 _sort : function(property, dir, fn){
418 var dsc = String(dir).toUpperCase() == "DESC" ? -1 : 1;
419 fn = fn || function(a, b){
422 var c = [], k = this.keys, items = this.items;
423 for(var i = 0, len = items.length; i < len; i++){
424 c[c.length] = {key: k[i], value: items[i], index: i};
426 c.sort(function(a, b){
427 var v = fn(a[property], b[property]) * dsc;
429 v = (a.index < b.index ? -1 : 1);
433 for(var i = 0, len = c.length; i < len; i++){
434 items[i] = c[i].value;
437 this.fireEvent("sort", this);
441 * Sorts this collection with the passed comparison function
442 * @param {String} direction (optional) "ASC" or "DESC"
443 * @param {Function} fn (optional) comparison function
445 sort : function(dir, fn){
446 this._sort("value", dir, fn);
450 * Sorts this collection by keys
451 * @param {String} direction (optional) "ASC" or "DESC"
452 * @param {Function} fn (optional) a comparison function (defaults to case insensitive string)
454 keySort : function(dir, fn){
455 this._sort("key", dir, fn || function(a, b){
456 return String(a).toUpperCase()-String(b).toUpperCase();
461 * Returns a range of items in this collection
462 * @param {Number} startIndex (optional) defaults to 0
463 * @param {Number} endIndex (optional) default to the last item
464 * @return {Array} An array of items
466 getRange : function(start, end){
467 var items = this.items;
468 if(items.length < 1){
472 end = Math.min(typeof end == "undefined" ? this.length-1 : end, this.length-1);
475 for(var i = start; i <= end; i++) {
476 r[r.length] = items[i];
479 for(var i = start; i >= end; i--) {
480 r[r.length] = items[i];
487 * Filter the <i>objects</i> in this collection by a specific property.
488 * Returns a new collection that has been filtered.
489 * @param {String} property A property on your objects
490 * @param {String/RegExp} value Either string that the property values
491 * should start with or a RegExp to test against the property
492 * @return {MixedCollection} The new filtered collection
494 filter : function(property, value){
495 if(!value.exec){ // not a regex
496 value = String(value);
497 if(value.length == 0){
500 value = new RegExp("^" + Roo.escapeRe(value), "i");
502 return this.filterBy(function(o){
503 return o && value.test(o[property]);
508 * Filter by a function. * Returns a new collection that has been filtered.
509 * The passed function will be called with each
510 * object in the collection. If the function returns true, the value is included
511 * otherwise it is filtered.
512 * @param {Function} fn The function to be called, it will receive the args o (the object), k (the key)
513 * @param {Object} scope (optional) The scope of the function (defaults to this)
514 * @return {MixedCollection} The new filtered collection
516 filterBy : function(fn, scope){
517 var r = new Roo.util.MixedCollection();
518 r.getKey = this.getKey;
519 var k = this.keys, it = this.items;
520 for(var i = 0, len = it.length; i < len; i++){
521 if(fn.call(scope||this, it[i], k[i])){
529 * Creates a duplicate of this collection
530 * @return {MixedCollection}
533 var r = new Roo.util.MixedCollection();
534 var k = this.keys, it = this.items;
535 for(var i = 0, len = it.length; i < len; i++){
538 r.getKey = this.getKey;
543 * Returns the item associated with the passed key or index.
545 * @param {String/Number} key The key or index of the item.
546 * @return {Object} The item associated with the passed key.
548 Roo.util.MixedCollection.prototype.get = Roo.util.MixedCollection.prototype.item;