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 * MixedCollection has a generic way to fetch keys if you implement getKey.
104 var mc = new Roo.util.MixedCollection();
105 mc.add(someEl.dom.id, someEl);
106 mc.add(otherEl.dom.id, otherEl);
110 var mc = new Roo.util.MixedCollection();
111 mc.getKey = function(el){
117 // or via the constructor
118 var mc = new Roo.util.MixedCollection(false, function(el){
124 * @param o {Object} The item for which to find the key.
125 * @return {Object} The key for the passed item.
127 getKey : function(o){
132 * Replaces an item in the collection.
133 * @param {String} key The key associated with the item to replace, or the item to replace.
134 * @param o {Object} o (optional) If the first parameter passed was a key, the item to associate with that key.
135 * @return {Object} The new item.
137 replace : function(key, o){
138 if(arguments.length == 1){
140 key = this.getKey(o);
142 var old = this.item(key);
143 if(typeof key == "undefined" || key === null || typeof old == "undefined"){
144 return this.add(key, o);
146 var index = this.indexOfKey(key);
147 this.items[index] = o;
149 this.fireEvent("replace", key, old, o);
154 * Adds all elements of an Array or an Object to the collection.
155 * @param {Object/Array} objs An Object containing properties which will be added to the collection, or
156 * an Array of values, each of which are added to the collection.
158 addAll : function(objs){
159 if(arguments.length > 1 || objs instanceof Array){
160 var args = arguments.length > 1 ? arguments : objs;
161 for(var i = 0, len = args.length; i < len; i++){
165 for(var key in objs){
166 if(this.allowFunctions || typeof objs[key] != "function"){
167 this.add(key, objs[key]);
174 * Executes the specified function once for every item in the collection, passing each
175 * item as the first and only parameter. returning false from the function will stop the iteration.
176 * @param {Function} fn The function to execute for each item.
177 * @param {Object} scope (optional) The scope in which to execute the function.
179 each : function(fn, scope){
180 var items = [].concat(this.items); // each safe for removal
181 for(var i = 0, len = items.length; i < len; i++){
182 if(fn.call(scope || items[i], items[i], i, len) === false){
189 * Executes the specified function once for every key in the collection, passing each
190 * key, and its associated item as the first two parameters.
191 * @param {Function} fn The function to execute for each item.
192 * @param {Object} scope (optional) The scope in which to execute the function.
194 eachKey : function(fn, scope){
195 for(var i = 0, len = this.keys.length; i < len; i++){
196 fn.call(scope || window, this.keys[i], this.items[i], i, len);
201 * Returns the first item in the collection which elicits a true return value from the
202 * passed selection function.
203 * @param {Function} fn The selection function to execute for each item.
204 * @param {Object} scope (optional) The scope in which to execute the function.
205 * @return {Object} The first item in the collection which returned true from the selection function.
207 find : function(fn, scope){
208 for(var i = 0, len = this.items.length; i < len; i++){
209 if(fn.call(scope || window, this.items[i], this.keys[i])){
210 return this.items[i];
217 * Inserts an item at the specified index in the collection.
218 * @param {Number} index The index to insert the item at.
219 * @param {String} key The key to associate with the new item, or the item itself.
220 * @param {Object} o (optional) If the second parameter was a key, the new item.
221 * @return {Object} The item inserted.
223 insert : function(index, key, o){
224 if(arguments.length == 2){
226 key = this.getKey(o);
228 if(index >= this.length){
229 return this.add(key, o);
232 this.items.splice(index, 0, o);
233 if(typeof key != "undefined" && key != null){
236 this.keys.splice(index, 0, key);
237 this.fireEvent("add", index, o, key);
242 * Removed an item from the collection.
243 * @param {Object} o The item to remove.
244 * @return {Object} The item removed.
246 remove : function(o){
247 return this.removeAt(this.indexOf(o));
251 * Remove an item from a specified index in the collection.
252 * @param {Number} index The index within the collection of the item to remove.
254 removeAt : function(index){
255 if(index < this.length && index >= 0){
257 var o = this.items[index];
258 this.items.splice(index, 1);
259 var key = this.keys[index];
260 if(typeof key != "undefined"){
261 delete this.map[key];
263 this.keys.splice(index, 1);
264 this.fireEvent("remove", o, key);
269 * Removed an item associated with the passed key fom the collection.
270 * @param {String} key The key of the item to remove.
272 removeKey : function(key){
273 return this.removeAt(this.indexOfKey(key));
277 * Returns the number of items in the collection.
278 * @return {Number} the number of items in the collection.
280 getCount : function(){
285 * Returns index within the collection of the passed Object.
286 * @param {Object} o The item to find the index of.
287 * @return {Number} index of the item.
289 indexOf : function(o){
290 if(!this.items.indexOf){
291 for(var i = 0, len = this.items.length; i < len; i++){
292 if(this.items[i] == o) return i;
296 return this.items.indexOf(o);
301 * Returns index within the collection of the passed key.
302 * @param {String} key The key to find the index of.
303 * @return {Number} index of the key.
305 indexOfKey : function(key){
306 if(!this.keys.indexOf){
307 for(var i = 0, len = this.keys.length; i < len; i++){
308 if(this.keys[i] == key) return i;
312 return this.keys.indexOf(key);
317 * Returns the item associated with the passed key OR index. Key has priority over index.
318 * @param {String/Number} key The key or index of the item.
319 * @return {Object} The item associated with the passed key.
321 item : function(key){
322 var item = typeof this.map[key] != "undefined" ? this.map[key] : this.items[key];
323 return typeof item != 'function' || this.allowFunctions ? item : null; // for prototype!
327 * Returns the item at the specified index.
328 * @param {Number} index The index of the item.
331 itemAt : function(index){
332 return this.items[index];
336 * Returns the item associated with the passed key.
337 * @param {String/Number} key The key of the item.
338 * @return {Object} The item associated with the passed key.
341 return this.map[key];
345 * Returns true if the collection contains the passed Object as an item.
346 * @param {Object} o The Object to look for in the collection.
347 * @return {Boolean} True if the collection contains the Object as an item.
349 contains : function(o){
350 return this.indexOf(o) != -1;
354 * Returns true if the collection contains the passed Object as a key.
355 * @param {String} key The key to look for in the collection.
356 * @return {Boolean} True if the collection contains the Object as a key.
358 containsKey : function(key){
359 return typeof this.map[key] != "undefined";
363 * Removes all items from the collection.
370 this.fireEvent("clear");
374 * Returns the first item in the collection.
375 * @return {Object} the first item in the collection..
378 return this.items[0];
382 * Returns the last item in the collection.
383 * @return {Object} the last item in the collection..
386 return this.items[this.length-1];
389 _sort : function(property, dir, fn){
390 var dsc = String(dir).toUpperCase() == "DESC" ? -1 : 1;
391 fn = fn || function(a, b){
394 var c = [], k = this.keys, items = this.items;
395 for(var i = 0, len = items.length; i < len; i++){
396 var v = typeof(items[i][property]) == 'undefined' ? items[i] : items[i][property];
397 c[c.length] = {key: k[i], value: v, index: i};
399 c.sort(function(a, b){
400 var v = fn(a[property], b[property]) * dsc;
402 v = (a.index < b.index ? -1 : 1);
406 for(var i = 0, len = c.length; i < len; i++){
407 items[i] = c[i].value;
410 this.fireEvent("sort", this);
414 * Sorts this collection with the passed comparison function
415 * @param {String} direction (optional) "ASC" or "DESC"
416 * @param {Function} fn (optional) comparison function
418 sort : function(dir, fn){
419 this._sort("value", dir, fn);
423 * Sorts this collection by keys
424 * @param {String} direction (optional) "ASC" or "DESC"
425 * @param {Function} fn (optional) a comparison function (defaults to case insensitive string)
427 keySort : function(dir, fn){
428 this._sort("key", dir, fn || function(a, b){
429 return String(a).toUpperCase()-String(b).toUpperCase();
434 * Returns a range of items in this collection
435 * @param {Number} startIndex (optional) defaults to 0
436 * @param {Number} endIndex (optional) default to the last item
437 * @return {Array} An array of items
439 getRange : function(start, end){
440 var items = this.items;
441 if(items.length < 1){
445 end = Math.min(typeof end == "undefined" ? this.length-1 : end, this.length-1);
448 for(var i = start; i <= end; i++) {
449 r[r.length] = items[i];
452 for(var i = start; i >= end; i--) {
453 r[r.length] = items[i];
460 * Filter the <i>objects</i> in this collection by a specific property.
461 * Returns a new collection that has been filtered.
462 * @param {String} property A property on your objects
463 * @param {String/RegExp} value Either string that the property values
464 * should start with or a RegExp to test against the property
465 * @return {MixedCollection} The new filtered collection
467 filter : function(property, value){
468 if(!value.exec){ // not a regex
469 value = String(value);
470 if(value.length == 0){
473 value = new RegExp("^" + Roo.escapeRe(value), "i");
475 return this.filterBy(function(o){
476 return o && value.test(o[property]);
481 * Filter by a function. * Returns a new collection that has been filtered.
482 * The passed function will be called with each
483 * object in the collection. If the function returns true, the value is included
484 * otherwise it is filtered.
485 * @param {Function} fn The function to be called, it will receive the args o (the object), k (the key)
486 * @param {Object} scope (optional) The scope of the function (defaults to this)
487 * @return {MixedCollection} The new filtered collection
489 filterBy : function(fn, scope){
490 var r = new Roo.util.MixedCollection();
491 r.getKey = this.getKey;
492 var k = this.keys, it = this.items;
493 for(var i = 0, len = it.length; i < len; i++){
494 if(fn.call(scope||this, it[i], k[i])){
502 * Creates a duplicate of this collection
503 * @return {MixedCollection}
506 var r = new Roo.util.MixedCollection();
507 var k = this.keys, it = this.items;
508 for(var i = 0, len = it.length; i < len; i++){
511 r.getKey = this.getKey;
516 * Returns the item associated with the passed key or index.
518 * @param {String/Number} key The key or index of the item.
519 * @return {Object} The item associated with the passed key.
521 Roo.util.MixedCollection.prototype.get = Roo.util.MixedCollection.prototype.item;