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){
84 if(typeof key == "undefined" || key === null){
89 var old = this.map[key];
91 return this.replace(key, o);
98 this.fireEvent("add", this.length-1, o, key);
103 * MixedCollection has a generic way to fetch keys if you implement getKey.
106 var mc = new Roo.util.MixedCollection();
107 mc.add(someEl.dom.id, someEl);
108 mc.add(otherEl.dom.id, otherEl);
112 var mc = new Roo.util.MixedCollection();
113 mc.getKey = function(el){
119 // or via the constructor
120 var mc = new Roo.util.MixedCollection(false, function(el){
126 * @param o {Object} The item for which to find the key.
127 * @return {Object} The key for the passed item.
129 getKey : function(o){
134 * Replaces an item in the collection.
135 * @param {String} key The key associated with the item to replace, or the item to replace.
136 * @param o {Object} o (optional) If the first parameter passed was a key, the item to associate with that key.
137 * @return {Object} The new item.
139 replace : function(key, o){
140 if(arguments.length == 1){
142 key = this.getKey(o);
144 var old = this.item(key);
145 if(typeof key == "undefined" || key === null || typeof old == "undefined"){
146 return this.add(key, o);
148 var index = this.indexOfKey(key);
149 this.items[index] = o;
151 this.fireEvent("replace", key, old, o);
156 * Adds all elements of an Array or an Object to the collection.
157 * @param {Object/Array} objs An Object containing properties which will be added to the collection, or
158 * an Array of values, each of which are added to the collection.
160 addAll : function(objs){
161 if(arguments.length > 1 || objs instanceof Array){
162 var args = arguments.length > 1 ? arguments : objs;
163 for(var i = 0, len = args.length; i < len; i++){
167 for(var key in objs){
168 if(this.allowFunctions || typeof objs[key] != "function"){
169 this.add(key, objs[key]);
176 * Executes the specified function once for every item in the collection, passing each
177 * item as the first and only parameter. returning false from the function will stop the iteration.
178 * @param {Function} fn The function to execute for each item.
179 * @param {Object} scope (optional) The scope in which to execute the function.
181 each : function(fn, scope){
182 var items = [].concat(this.items); // each safe for removal
183 for(var i = 0, len = items.length; i < len; i++){
184 if(fn.call(scope || items[i], items[i], i, len) === false){
191 * Executes the specified function once for every key in the collection, passing each
192 * key, and its associated item as the first two parameters.
193 * @param {Function} fn The function to execute for each item.
194 * @param {Object} scope (optional) The scope in which to execute the function.
196 eachKey : function(fn, scope){
197 for(var i = 0, len = this.keys.length; i < len; i++){
198 fn.call(scope || window, this.keys[i], this.items[i], i, len);
203 * Returns the first item in the collection which elicits a true return value from the
204 * passed selection function.
205 * @param {Function} fn The selection function to execute for each item.
206 * @param {Object} scope (optional) The scope in which to execute the function.
207 * @return {Object} The first item in the collection which returned true from the selection function.
209 find : function(fn, scope){
210 for(var i = 0, len = this.items.length; i < len; i++){
211 if(fn.call(scope || window, this.items[i], this.keys[i])){
212 return this.items[i];
219 * Inserts an item at the specified index in the collection.
220 * @param {Number} index The index to insert the item at.
221 * @param {String} key The key to associate with the new item, or the item itself.
222 * @param {Object} o (optional) If the second parameter was a key, the new item.
223 * @return {Object} The item inserted.
225 insert : function(index, key, o){
226 if(arguments.length == 2){
228 key = this.getKey(o);
230 if(index >= this.length){
231 return this.add(key, o);
234 this.items.splice(index, 0, o);
235 if(typeof key != "undefined" && key != null){
238 this.keys.splice(index, 0, key);
239 this.fireEvent("add", index, o, key);
244 * Removed an item from the collection.
245 * @param {Object} o The item to remove.
246 * @return {Object} The item removed.
248 remove : function(o){
249 return this.removeAt(this.indexOf(o));
253 * Remove an item from a specified index in the collection.
254 * @param {Number} index The index within the collection of the item to remove.
256 removeAt : function(index){
257 if(index < this.length && index >= 0){
259 var o = this.items[index];
260 this.items.splice(index, 1);
261 var key = this.keys[index];
262 if(typeof key != "undefined"){
263 delete this.map[key];
265 this.keys.splice(index, 1);
266 this.fireEvent("remove", o, key);
271 * Removed an item associated with the passed key fom the collection.
272 * @param {String} key The key of the item to remove.
274 removeKey : function(key){
275 return this.removeAt(this.indexOfKey(key));
279 * Returns the number of items in the collection.
280 * @return {Number} the number of items in the collection.
282 getCount : function(){
287 * Returns index within the collection of the passed Object.
288 * @param {Object} o The item to find the index of.
289 * @return {Number} index of the item.
291 indexOf : function(o){
292 if(!this.items.indexOf){
293 for(var i = 0, len = this.items.length; i < len; i++){
294 if(this.items[i] == o) return i;
298 return this.items.indexOf(o);
303 * Returns index within the collection of the passed key.
304 * @param {String} key The key to find the index of.
305 * @return {Number} index of the key.
307 indexOfKey : function(key){
308 if(!this.keys.indexOf){
309 for(var i = 0, len = this.keys.length; i < len; i++){
310 if(this.keys[i] == key) return i;
314 return this.keys.indexOf(key);
319 * Returns the item associated with the passed key OR index. Key has priority over index.
320 * @param {String/Number} key The key or index of the item.
321 * @return {Object} The item associated with the passed key.
323 item : function(key){
324 var item = typeof this.map[key] != "undefined" ? this.map[key] : this.items[key];
325 return typeof item != 'function' || this.allowFunctions ? item : null; // for prototype!
329 * Returns the item at the specified index.
330 * @param {Number} index The index of the item.
333 itemAt : function(index){
334 return this.items[index];
338 * Returns the item associated with the passed key.
339 * @param {String/Number} key The key of the item.
340 * @return {Object} The item associated with the passed key.
343 return this.map[key];
347 * Returns true if the collection contains the passed Object as an item.
348 * @param {Object} o The Object to look for in the collection.
349 * @return {Boolean} True if the collection contains the Object as an item.
351 contains : function(o){
352 return this.indexOf(o) != -1;
356 * Returns true if the collection contains the passed Object as a key.
357 * @param {String} key The key to look for in the collection.
358 * @return {Boolean} True if the collection contains the Object as a key.
360 containsKey : function(key){
361 return typeof this.map[key] != "undefined";
365 * Removes all items from the collection.
372 this.fireEvent("clear");
376 * Returns the first item in the collection.
377 * @return {Object} the first item in the collection..
380 return this.items[0];
384 * Returns the last item in the collection.
385 * @return {Object} the last item in the collection..
388 return this.items[this.length-1];
391 _sort : function(property, dir, fn){
392 var dsc = String(dir).toUpperCase() == "DESC" ? -1 : 1;
393 fn = fn || function(a, b){
396 var c = [], k = this.keys, items = this.items;
397 for(var i = 0, len = items.length; i < len; i++){
398 c[c.length] = {key: k[i], value: items[i], index: i};
400 c.sort(function(a, b){
401 var v = fn(a[property], b[property]) * dsc;
403 v = (a.index < b.index ? -1 : 1);
407 for(var i = 0, len = c.length; i < len; i++){
408 items[i] = c[i].value;
411 this.fireEvent("sort", this);
415 * Sorts this collection with the passed comparison function
416 * @param {String} direction (optional) "ASC" or "DESC"
417 * @param {Function} fn (optional) comparison function
419 sort : function(dir, fn){
420 this._sort("value", dir, fn);
424 * Sorts this collection by keys
425 * @param {String} direction (optional) "ASC" or "DESC"
426 * @param {Function} fn (optional) a comparison function (defaults to case insensitive string)
428 keySort : function(dir, fn){
429 this._sort("key", dir, fn || function(a, b){
430 return String(a).toUpperCase()-String(b).toUpperCase();
435 * Returns a range of items in this collection
436 * @param {Number} startIndex (optional) defaults to 0
437 * @param {Number} endIndex (optional) default to the last item
438 * @return {Array} An array of items
440 getRange : function(start, end){
441 var items = this.items;
442 if(items.length < 1){
446 end = Math.min(typeof end == "undefined" ? this.length-1 : end, this.length-1);
449 for(var i = start; i <= end; i++) {
450 r[r.length] = items[i];
453 for(var i = start; i >= end; i--) {
454 r[r.length] = items[i];
461 * Filter the <i>objects</i> in this collection by a specific property.
462 * Returns a new collection that has been filtered.
463 * @param {String} property A property on your objects
464 * @param {String/RegExp} value Either string that the property values
465 * should start with or a RegExp to test against the property
466 * @return {MixedCollection} The new filtered collection
468 filter : function(property, value){
469 if(!value.exec){ // not a regex
470 value = String(value);
471 if(value.length == 0){
474 value = new RegExp("^" + Roo.escapeRe(value), "i");
476 return this.filterBy(function(o){
477 return o && value.test(o[property]);
482 * Filter by a function. * Returns a new collection that has been filtered.
483 * The passed function will be called with each
484 * object in the collection. If the function returns true, the value is included
485 * otherwise it is filtered.
486 * @param {Function} fn The function to be called, it will receive the args o (the object), k (the key)
487 * @param {Object} scope (optional) The scope of the function (defaults to this)
488 * @return {MixedCollection} The new filtered collection
490 filterBy : function(fn, scope){
491 var r = new Roo.util.MixedCollection();
492 r.getKey = this.getKey;
493 var k = this.keys, it = this.items;
494 for(var i = 0, len = it.length; i < len; i++){
495 if(fn.call(scope||this, it[i], k[i])){
503 * Creates a duplicate of this collection
504 * @return {MixedCollection}
507 var r = new Roo.util.MixedCollection();
508 var k = this.keys, it = this.items;
509 for(var i = 0, len = it.length; i < len; i++){
512 r.getKey = this.getKey;
517 * Returns the item associated with the passed key or index.
519 * @param {String/Number} key The key or index of the item.
520 * @return {Object} The item associated with the passed key.
522 Roo.util.MixedCollection.prototype.get = Roo.util.MixedCollection.prototype.item;