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){
86 if(typeof key == "undefined" || key === null){
92 var old = this.map[key * 1];
94 Roo.log('old.........................');
97 return this.replace(key, o);
104 this.fireEvent("add", this.length-1, o, key);
109 * MixedCollection has a generic way to fetch keys if you implement getKey.
112 var mc = new Roo.util.MixedCollection();
113 mc.add(someEl.dom.id, someEl);
114 mc.add(otherEl.dom.id, otherEl);
118 var mc = new Roo.util.MixedCollection();
119 mc.getKey = function(el){
125 // or via the constructor
126 var mc = new Roo.util.MixedCollection(false, function(el){
132 * @param o {Object} The item for which to find the key.
133 * @return {Object} The key for the passed item.
135 getKey : function(o){
140 * Replaces an item in the collection.
141 * @param {String} key The key associated with the item to replace, or the item to replace.
142 * @param o {Object} o (optional) If the first parameter passed was a key, the item to associate with that key.
143 * @return {Object} The new item.
145 replace : function(key, o){
146 if(arguments.length == 1){
148 key = this.getKey(o);
150 Roo.log('replace.........................');
152 var old = this.item(key);
153 if(typeof key == "undefined" || key === null || typeof old == "undefined"){
154 return this.add(key, o);
156 var index = this.indexOfKey(key);
159 this.items[index] = o;
161 this.fireEvent("replace", key, old, o);
166 * Adds all elements of an Array or an Object to the collection.
167 * @param {Object/Array} objs An Object containing properties which will be added to the collection, or
168 * an Array of values, each of which are added to the collection.
170 addAll : function(objs){
171 if(arguments.length > 1 || objs instanceof Array){
172 var args = arguments.length > 1 ? arguments : objs;
173 for(var i = 0, len = args.length; i < len; i++){
177 for(var key in objs){
178 if(this.allowFunctions || typeof objs[key] != "function"){
179 this.add(key, objs[key]);
186 * Executes the specified function once for every item in the collection, passing each
187 * item as the first and only parameter. returning false from the function will stop the iteration.
188 * @param {Function} fn The function to execute for each item.
189 * @param {Object} scope (optional) The scope in which to execute the function.
191 each : function(fn, scope){
192 var items = [].concat(this.items); // each safe for removal
193 for(var i = 0, len = items.length; i < len; i++){
194 if(fn.call(scope || items[i], items[i], i, len) === false){
201 * Executes the specified function once for every key in the collection, passing each
202 * key, and its associated item as the first two parameters.
203 * @param {Function} fn The function to execute for each item.
204 * @param {Object} scope (optional) The scope in which to execute the function.
206 eachKey : function(fn, scope){
207 for(var i = 0, len = this.keys.length; i < len; i++){
208 fn.call(scope || window, this.keys[i], this.items[i], i, len);
213 * Returns the first item in the collection which elicits a true return value from the
214 * passed selection function.
215 * @param {Function} fn The selection function to execute for each item.
216 * @param {Object} scope (optional) The scope in which to execute the function.
217 * @return {Object} The first item in the collection which returned true from the selection function.
219 find : function(fn, scope){
220 for(var i = 0, len = this.items.length; i < len; i++){
221 if(fn.call(scope || window, this.items[i], this.keys[i])){
222 return this.items[i];
229 * Inserts an item at the specified index in the collection.
230 * @param {Number} index The index to insert the item at.
231 * @param {String} key The key to associate with the new item, or the item itself.
232 * @param {Object} o (optional) If the second parameter was a key, the new item.
233 * @return {Object} The item inserted.
235 insert : function(index, key, o){
236 if(arguments.length == 2){
238 key = this.getKey(o);
240 if(index >= this.length){
241 return this.add(key, o);
244 this.items.splice(index, 0, o);
245 if(typeof key != "undefined" && key != null){
248 this.keys.splice(index, 0, key);
249 this.fireEvent("add", index, o, key);
254 * Removed an item from the collection.
255 * @param {Object} o The item to remove.
256 * @return {Object} The item removed.
258 remove : function(o){
259 return this.removeAt(this.indexOf(o));
263 * Remove an item from a specified index in the collection.
264 * @param {Number} index The index within the collection of the item to remove.
266 removeAt : function(index){
267 if(index < this.length && index >= 0){
269 var o = this.items[index];
270 this.items.splice(index, 1);
271 var key = this.keys[index];
272 if(typeof key != "undefined"){
273 delete this.map[key];
275 this.keys.splice(index, 1);
276 this.fireEvent("remove", o, key);
281 * Removed an item associated with the passed key fom the collection.
282 * @param {String} key The key of the item to remove.
284 removeKey : function(key){
285 return this.removeAt(this.indexOfKey(key));
289 * Returns the number of items in the collection.
290 * @return {Number} the number of items in the collection.
292 getCount : function(){
297 * Returns index within the collection of the passed Object.
298 * @param {Object} o The item to find the index of.
299 * @return {Number} index of the item.
301 indexOf : function(o){
302 if(!this.items.indexOf){
303 for(var i = 0, len = this.items.length; i < len; i++){
304 if(this.items[i] == o) return i;
308 return this.items.indexOf(o);
313 * Returns index within the collection of the passed key.
314 * @param {String} key The key to find the index of.
315 * @return {Number} index of the key.
317 indexOfKey : function(key){
318 if(!this.keys.indexOf){
319 for(var i = 0, len = this.keys.length; i < len; i++){
320 if(this.keys[i] == key) return i;
324 return this.keys.indexOf(key);
329 * Returns the item associated with the passed key OR index. Key has priority over index.
330 * @param {String/Number} key The key or index of the item.
331 * @return {Object} The item associated with the passed key.
333 item : function(key){
334 var item = typeof this.map[key] != "undefined" ? this.map[key] : this.items[key];
335 return typeof item != 'function' || this.allowFunctions ? item : null; // for prototype!
339 * Returns the item at the specified index.
340 * @param {Number} index The index of the item.
343 itemAt : function(index){
344 return this.items[index];
348 * Returns the item associated with the passed key.
349 * @param {String/Number} key The key of the item.
350 * @return {Object} The item associated with the passed key.
353 return this.map[key];
357 * Returns true if the collection contains the passed Object as an item.
358 * @param {Object} o The Object to look for in the collection.
359 * @return {Boolean} True if the collection contains the Object as an item.
361 contains : function(o){
362 return this.indexOf(o) != -1;
366 * Returns true if the collection contains the passed Object as a key.
367 * @param {String} key The key to look for in the collection.
368 * @return {Boolean} True if the collection contains the Object as a key.
370 containsKey : function(key){
371 return typeof this.map[key] != "undefined";
375 * Removes all items from the collection.
382 this.fireEvent("clear");
386 * Returns the first item in the collection.
387 * @return {Object} the first item in the collection..
390 return this.items[0];
394 * Returns the last item in the collection.
395 * @return {Object} the last item in the collection..
398 return this.items[this.length-1];
401 _sort : function(property, dir, fn){
402 var dsc = String(dir).toUpperCase() == "DESC" ? -1 : 1;
403 fn = fn || function(a, b){
406 var c = [], k = this.keys, items = this.items;
407 for(var i = 0, len = items.length; i < len; i++){
408 c[c.length] = {key: k[i], value: items[i], index: i};
410 c.sort(function(a, b){
411 var v = fn(a[property], b[property]) * dsc;
413 v = (a.index < b.index ? -1 : 1);
417 for(var i = 0, len = c.length; i < len; i++){
418 items[i] = c[i].value;
421 this.fireEvent("sort", this);
425 * Sorts this collection with the passed comparison function
426 * @param {String} direction (optional) "ASC" or "DESC"
427 * @param {Function} fn (optional) comparison function
429 sort : function(dir, fn){
430 this._sort("value", dir, fn);
434 * Sorts this collection by keys
435 * @param {String} direction (optional) "ASC" or "DESC"
436 * @param {Function} fn (optional) a comparison function (defaults to case insensitive string)
438 keySort : function(dir, fn){
439 this._sort("key", dir, fn || function(a, b){
440 return String(a).toUpperCase()-String(b).toUpperCase();
445 * Returns a range of items in this collection
446 * @param {Number} startIndex (optional) defaults to 0
447 * @param {Number} endIndex (optional) default to the last item
448 * @return {Array} An array of items
450 getRange : function(start, end){
451 var items = this.items;
452 if(items.length < 1){
456 end = Math.min(typeof end == "undefined" ? this.length-1 : end, this.length-1);
459 for(var i = start; i <= end; i++) {
460 r[r.length] = items[i];
463 for(var i = start; i >= end; i--) {
464 r[r.length] = items[i];
471 * Filter the <i>objects</i> in this collection by a specific property.
472 * Returns a new collection that has been filtered.
473 * @param {String} property A property on your objects
474 * @param {String/RegExp} value Either string that the property values
475 * should start with or a RegExp to test against the property
476 * @return {MixedCollection} The new filtered collection
478 filter : function(property, value){
479 if(!value.exec){ // not a regex
480 value = String(value);
481 if(value.length == 0){
484 value = new RegExp("^" + Roo.escapeRe(value), "i");
486 return this.filterBy(function(o){
487 return o && value.test(o[property]);
492 * Filter by a function. * Returns a new collection that has been filtered.
493 * The passed function will be called with each
494 * object in the collection. If the function returns true, the value is included
495 * otherwise it is filtered.
496 * @param {Function} fn The function to be called, it will receive the args o (the object), k (the key)
497 * @param {Object} scope (optional) The scope of the function (defaults to this)
498 * @return {MixedCollection} The new filtered collection
500 filterBy : function(fn, scope){
501 var r = new Roo.util.MixedCollection();
502 r.getKey = this.getKey;
503 var k = this.keys, it = this.items;
504 for(var i = 0, len = it.length; i < len; i++){
505 if(fn.call(scope||this, it[i], k[i])){
513 * Creates a duplicate of this collection
514 * @return {MixedCollection}
517 var r = new Roo.util.MixedCollection();
518 var k = this.keys, it = this.items;
519 for(var i = 0, len = it.length; i < len; i++){
522 r.getKey = this.getKey;
527 * Returns the item associated with the passed key or index.
529 * @param {String/Number} key The key or index of the item.
530 * @return {Object} The item associated with the passed key.
532 Roo.util.MixedCollection.prototype.get = Roo.util.MixedCollection.prototype.item;