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){
79 if(arguments.length == 1){
85 if(typeof key == "undefined" || key === null){
91 var old = this.map[key];
92 Roo.log('old.........................');
95 return this.replace(key, o);
102 this.fireEvent("add", this.length-1, o, key);
107 * MixedCollection has a generic way to fetch keys if you implement getKey.
110 var mc = new Roo.util.MixedCollection();
111 mc.add(someEl.dom.id, someEl);
112 mc.add(otherEl.dom.id, otherEl);
116 var mc = new Roo.util.MixedCollection();
117 mc.getKey = function(el){
123 // or via the constructor
124 var mc = new Roo.util.MixedCollection(false, function(el){
130 * @param o {Object} The item for which to find the key.
131 * @return {Object} The key for the passed item.
133 getKey : function(o){
138 * Replaces an item in the collection.
139 * @param {String} key The key associated with the item to replace, or the item to replace.
140 * @param o {Object} o (optional) If the first parameter passed was a key, the item to associate with that key.
141 * @return {Object} The new item.
143 replace : function(key, o){
144 if(arguments.length == 1){
146 key = this.getKey(o);
148 Roo.log('replace.........................');
150 var old = this.item(key);
151 if(typeof key == "undefined" || key === null || typeof old == "undefined"){
152 return this.add(key, o);
154 var index = this.indexOfKey(key);
157 this.items[index] = o;
159 this.fireEvent("replace", key, old, o);
164 * Adds all elements of an Array or an Object to the collection.
165 * @param {Object/Array} objs An Object containing properties which will be added to the collection, or
166 * an Array of values, each of which are added to the collection.
168 addAll : function(objs){
169 if(arguments.length > 1 || objs instanceof Array){
170 var args = arguments.length > 1 ? arguments : objs;
171 for(var i = 0, len = args.length; i < len; i++){
175 for(var key in objs){
176 if(this.allowFunctions || typeof objs[key] != "function"){
177 this.add(key, objs[key]);
184 * Executes the specified function once for every item in the collection, passing each
185 * item as the first and only parameter. returning false from the function will stop the iteration.
186 * @param {Function} fn The function to execute for each item.
187 * @param {Object} scope (optional) The scope in which to execute the function.
189 each : function(fn, scope){
190 var items = [].concat(this.items); // each safe for removal
191 for(var i = 0, len = items.length; i < len; i++){
192 if(fn.call(scope || items[i], items[i], i, len) === false){
199 * Executes the specified function once for every key in the collection, passing each
200 * key, and its associated item as the first two parameters.
201 * @param {Function} fn The function to execute for each item.
202 * @param {Object} scope (optional) The scope in which to execute the function.
204 eachKey : function(fn, scope){
205 for(var i = 0, len = this.keys.length; i < len; i++){
206 fn.call(scope || window, this.keys[i], this.items[i], i, len);
211 * Returns the first item in the collection which elicits a true return value from the
212 * passed selection function.
213 * @param {Function} fn The selection function to execute for each item.
214 * @param {Object} scope (optional) The scope in which to execute the function.
215 * @return {Object} The first item in the collection which returned true from the selection function.
217 find : function(fn, scope){
218 for(var i = 0, len = this.items.length; i < len; i++){
219 if(fn.call(scope || window, this.items[i], this.keys[i])){
220 return this.items[i];
227 * Inserts an item at the specified index in the collection.
228 * @param {Number} index The index to insert the item at.
229 * @param {String} key The key to associate with the new item, or the item itself.
230 * @param {Object} o (optional) If the second parameter was a key, the new item.
231 * @return {Object} The item inserted.
233 insert : function(index, key, o){
234 if(arguments.length == 2){
236 key = this.getKey(o);
238 if(index >= this.length){
239 return this.add(key, o);
242 this.items.splice(index, 0, o);
243 if(typeof key != "undefined" && key != null){
246 this.keys.splice(index, 0, key);
247 this.fireEvent("add", index, o, key);
252 * Removed an item from the collection.
253 * @param {Object} o The item to remove.
254 * @return {Object} The item removed.
256 remove : function(o){
257 return this.removeAt(this.indexOf(o));
261 * Remove an item from a specified index in the collection.
262 * @param {Number} index The index within the collection of the item to remove.
264 removeAt : function(index){
265 if(index < this.length && index >= 0){
267 var o = this.items[index];
268 this.items.splice(index, 1);
269 var key = this.keys[index];
270 if(typeof key != "undefined"){
271 delete this.map[key];
273 this.keys.splice(index, 1);
274 this.fireEvent("remove", o, key);
279 * Removed an item associated with the passed key fom the collection.
280 * @param {String} key The key of the item to remove.
282 removeKey : function(key){
283 return this.removeAt(this.indexOfKey(key));
287 * Returns the number of items in the collection.
288 * @return {Number} the number of items in the collection.
290 getCount : function(){
295 * Returns index within the collection of the passed Object.
296 * @param {Object} o The item to find the index of.
297 * @return {Number} index of the item.
299 indexOf : function(o){
300 if(!this.items.indexOf){
301 for(var i = 0, len = this.items.length; i < len; i++){
302 if(this.items[i] == o) return i;
306 return this.items.indexOf(o);
311 * Returns index within the collection of the passed key.
312 * @param {String} key The key to find the index of.
313 * @return {Number} index of the key.
315 indexOfKey : function(key){
316 if(!this.keys.indexOf){
317 for(var i = 0, len = this.keys.length; i < len; i++){
318 if(this.keys[i] == key) return i;
322 return this.keys.indexOf(key);
327 * Returns the item associated with the passed key OR index. Key has priority over index.
328 * @param {String/Number} key The key or index of the item.
329 * @return {Object} The item associated with the passed key.
331 item : function(key){
332 var item = typeof this.map[key] != "undefined" ? this.map[key] : this.items[key];
333 return typeof item != 'function' || this.allowFunctions ? item : null; // for prototype!
337 * Returns the item at the specified index.
338 * @param {Number} index The index of the item.
341 itemAt : function(index){
342 return this.items[index];
346 * Returns the item associated with the passed key.
347 * @param {String/Number} key The key of the item.
348 * @return {Object} The item associated with the passed key.
351 return this.map[key];
355 * Returns true if the collection contains the passed Object as an item.
356 * @param {Object} o The Object to look for in the collection.
357 * @return {Boolean} True if the collection contains the Object as an item.
359 contains : function(o){
360 return this.indexOf(o) != -1;
364 * Returns true if the collection contains the passed Object as a key.
365 * @param {String} key The key to look for in the collection.
366 * @return {Boolean} True if the collection contains the Object as a key.
368 containsKey : function(key){
369 return typeof this.map[key] != "undefined";
373 * Removes all items from the collection.
380 this.fireEvent("clear");
384 * Returns the first item in the collection.
385 * @return {Object} the first item in the collection..
388 return this.items[0];
392 * Returns the last item in the collection.
393 * @return {Object} the last item in the collection..
396 return this.items[this.length-1];
399 _sort : function(property, dir, fn){
400 var dsc = String(dir).toUpperCase() == "DESC" ? -1 : 1;
401 fn = fn || function(a, b){
404 var c = [], k = this.keys, items = this.items;
405 for(var i = 0, len = items.length; i < len; i++){
406 c[c.length] = {key: k[i], value: items[i], index: i};
408 c.sort(function(a, b){
409 var v = fn(a[property], b[property]) * dsc;
411 v = (a.index < b.index ? -1 : 1);
415 for(var i = 0, len = c.length; i < len; i++){
416 items[i] = c[i].value;
419 this.fireEvent("sort", this);
423 * Sorts this collection with the passed comparison function
424 * @param {String} direction (optional) "ASC" or "DESC"
425 * @param {Function} fn (optional) comparison function
427 sort : function(dir, fn){
428 this._sort("value", dir, fn);
432 * Sorts this collection by keys
433 * @param {String} direction (optional) "ASC" or "DESC"
434 * @param {Function} fn (optional) a comparison function (defaults to case insensitive string)
436 keySort : function(dir, fn){
437 this._sort("key", dir, fn || function(a, b){
438 return String(a).toUpperCase()-String(b).toUpperCase();
443 * Returns a range of items in this collection
444 * @param {Number} startIndex (optional) defaults to 0
445 * @param {Number} endIndex (optional) default to the last item
446 * @return {Array} An array of items
448 getRange : function(start, end){
449 var items = this.items;
450 if(items.length < 1){
454 end = Math.min(typeof end == "undefined" ? this.length-1 : end, this.length-1);
457 for(var i = start; i <= end; i++) {
458 r[r.length] = items[i];
461 for(var i = start; i >= end; i--) {
462 r[r.length] = items[i];
469 * Filter the <i>objects</i> in this collection by a specific property.
470 * Returns a new collection that has been filtered.
471 * @param {String} property A property on your objects
472 * @param {String/RegExp} value Either string that the property values
473 * should start with or a RegExp to test against the property
474 * @return {MixedCollection} The new filtered collection
476 filter : function(property, value){
477 if(!value.exec){ // not a regex
478 value = String(value);
479 if(value.length == 0){
482 value = new RegExp("^" + Roo.escapeRe(value), "i");
484 return this.filterBy(function(o){
485 return o && value.test(o[property]);
490 * Filter by a function. * Returns a new collection that has been filtered.
491 * The passed function will be called with each
492 * object in the collection. If the function returns true, the value is included
493 * otherwise it is filtered.
494 * @param {Function} fn The function to be called, it will receive the args o (the object), k (the key)
495 * @param {Object} scope (optional) The scope of the function (defaults to this)
496 * @return {MixedCollection} The new filtered collection
498 filterBy : function(fn, scope){
499 var r = new Roo.util.MixedCollection();
500 r.getKey = this.getKey;
501 var k = this.keys, it = this.items;
502 for(var i = 0, len = it.length; i < len; i++){
503 if(fn.call(scope||this, it[i], k[i])){
511 * Creates a duplicate of this collection
512 * @return {MixedCollection}
515 var r = new Roo.util.MixedCollection();
516 var k = this.keys, it = this.items;
517 for(var i = 0, len = it.length; i < len; i++){
520 r.getKey = this.getKey;
525 * Returns the item associated with the passed key or index.
527 * @param {String/Number} key The key or index of the item.
528 * @return {Object} The item associated with the passed key.
530 Roo.util.MixedCollection.prototype.get = Roo.util.MixedCollection.prototype.item;