3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.util.MixedCollection"></div>/**
\r
15 * @class Ext.util.MixedCollection
\r
16 * @extends Ext.util.Observable
\r
17 * A Collection class that maintains both numeric indexes and keys and exposes events.
\r
19 * @param {Boolean} allowFunctions Specify <tt>true</tt> if the {@link #addAll}
\r
20 * function should add function references to the collection. Defaults to
\r
22 * @param {Function} keyFn A function that can accept an item of the type(s) stored in this MixedCollection
\r
23 * and return the key value for that item. This is used when available to look up the key on items that
\r
24 * were passed without an explicit key parameter to a MixedCollection method. Passing this parameter is
\r
25 * equivalent to providing an implementation for the {@link #getKey} method.
\r
27 Ext.util.MixedCollection = function(allowFunctions, keyFn){
\r
33 <div id="event-Ext.util.MixedCollection-clear"></div>/**
\r
35 * Fires when the collection is cleared.
\r
38 <div id="event-Ext.util.MixedCollection-add"></div>/**
\r
40 * Fires when an item is added to the collection.
\r
41 * @param {Number} index The index at which the item was added.
\r
42 * @param {Object} o The item added.
\r
43 * @param {String} key The key associated with the added item.
\r
46 <div id="event-Ext.util.MixedCollection-replace"></div>/**
\r
48 * Fires when an item is replaced in the collection.
\r
49 * @param {String} key he key associated with the new added.
\r
50 * @param {Object} old The item being replaced.
\r
51 * @param {Object} new The new item.
\r
54 <div id="event-Ext.util.MixedCollection-remove"></div>/**
\r
56 * Fires when an item is removed from the collection.
\r
57 * @param {Object} o The item being removed.
\r
58 * @param {String} key (optional) The key associated with the removed item.
\r
63 this.allowFunctions = allowFunctions === true;
\r
65 this.getKey = keyFn;
\r
67 Ext.util.MixedCollection.superclass.constructor.call(this);
\r
70 Ext.extend(Ext.util.MixedCollection, Ext.util.Observable, {
\r
72 <div id="cfg-Ext.util.MixedCollection-allowFunctions"></div>/**
\r
73 * @cfg {Boolean} allowFunctions Specify <tt>true</tt> if the {@link #addAll}
\r
74 * function should add function references to the collection. Defaults to
\r
77 allowFunctions : false,
\r
79 <div id="method-Ext.util.MixedCollection-add"></div>/**
\r
80 * Adds an item to the collection. Fires the {@link #add} event when complete.
\r
81 * @param {String} key <p>The key to associate with the item, or the new item.</p>
\r
82 * <p>If a {@link #getKey} implementation was specified for this MixedCollection,
\r
83 * or if the key of the stored items is in a property called <tt><b>id</b></tt>,
\r
84 * the MixedCollection will be able to <i>derive</i> the key for the new item.
\r
85 * In this case just pass the new item in this parameter.</p>
\r
86 * @param {Object} o The item to add.
\r
87 * @return {Object} The item added.
\r
89 add : function(key, o){
\r
90 if(arguments.length == 1){
\r
92 key = this.getKey(o);
\r
94 if(typeof key != 'undefined' && key !== null){
\r
95 var old = this.map[key];
\r
96 if(typeof old != 'undefined'){
\r
97 return this.replace(key, o);
\r
102 this.items.push(o);
\r
103 this.keys.push(key);
\r
104 this.fireEvent('add', this.length-1, o, key);
\r
108 <div id="method-Ext.util.MixedCollection-getKey"></div>/**
\r
109 * MixedCollection has a generic way to fetch keys if you implement getKey. The default implementation
\r
110 * simply returns <b><code>item.id</code></b> but you can provide your own implementation
\r
111 * to return a different value as in the following examples:<pre><code>
\r
113 var mc = new Ext.util.MixedCollection();
\r
114 mc.add(someEl.dom.id, someEl);
\r
115 mc.add(otherEl.dom.id, otherEl);
\r
119 var mc = new Ext.util.MixedCollection();
\r
120 mc.getKey = function(el){
\r
126 // or via the constructor
\r
127 var mc = new Ext.util.MixedCollection(false, function(el){
\r
133 * @param {Object} item The item for which to find the key.
\r
134 * @return {Object} The key for the passed item.
\r
136 getKey : function(o){
\r
140 <div id="method-Ext.util.MixedCollection-replace"></div>/**
\r
141 * Replaces an item in the collection. Fires the {@link #replace} event when complete.
\r
142 * @param {String} key <p>The key associated with the item to replace, or the replacement item.</p>
\r
143 * <p>If you supplied a {@link #getKey} implementation for this MixedCollection, or if the key
\r
144 * of your stored items is in a property called <tt><b>id</b></tt>, then the MixedCollection
\r
145 * will be able to <i>derive</i> the key of the replacement item. If you want to replace an item
\r
146 * with one having the same key value, then just pass the replacement item in this parameter.</p>
\r
147 * @param o {Object} o (optional) If the first parameter passed was a key, the item to associate
\r
149 * @return {Object} The new item.
\r
151 replace : function(key, o){
\r
152 if(arguments.length == 1){
\r
154 key = this.getKey(o);
\r
156 var old = this.map[key];
\r
157 if(typeof key == 'undefined' || key === null || typeof old == 'undefined'){
\r
158 return this.add(key, o);
\r
160 var index = this.indexOfKey(key);
\r
161 this.items[index] = o;
\r
163 this.fireEvent('replace', key, old, o);
\r
167 <div id="method-Ext.util.MixedCollection-addAll"></div>/**
\r
168 * Adds all elements of an Array or an Object to the collection.
\r
169 * @param {Object/Array} objs An Object containing properties which will be added
\r
170 * to the collection, or an Array of values, each of which are added to the collection.
\r
171 * Functions references will be added to the collection if <code>{@link #allowFunctions}</code>
\r
172 * has been set to <tt>true</tt>.
\r
174 addAll : function(objs){
\r
175 if(arguments.length > 1 || Ext.isArray(objs)){
\r
176 var args = arguments.length > 1 ? arguments : objs;
\r
177 for(var i = 0, len = args.length; i < len; i++){
\r
181 for(var key in objs){
\r
182 if(this.allowFunctions || typeof objs[key] != 'function'){
\r
183 this.add(key, objs[key]);
\r
189 <div id="method-Ext.util.MixedCollection-each"></div>/**
\r
190 * Executes the specified function once for every item in the collection, passing the following arguments:
\r
191 * <div class="mdetail-params"><ul>
\r
192 * <li><b>item</b> : Mixed<p class="sub-desc">The collection item</p></li>
\r
193 * <li><b>index</b> : Number<p class="sub-desc">The item's index</p></li>
\r
194 * <li><b>length</b> : Number<p class="sub-desc">The total number of items in the collection</p></li>
\r
196 * The function should return a boolean value. Returning false from the function will stop the iteration.
\r
197 * @param {Function} fn The function to execute for each item.
\r
198 * @param {Object} scope (optional) The scope in which to execute the function.
\r
200 each : function(fn, scope){
\r
201 var items = [].concat(this.items); // each safe for removal
\r
202 for(var i = 0, len = items.length; i < len; i++){
\r
203 if(fn.call(scope || items[i], items[i], i, len) === false){
\r
209 <div id="method-Ext.util.MixedCollection-eachKey"></div>/**
\r
210 * Executes the specified function once for every key in the collection, passing each
\r
211 * key, and its associated item as the first two parameters.
\r
212 * @param {Function} fn The function to execute for each item.
\r
213 * @param {Object} scope (optional) The scope in which to execute the function.
\r
215 eachKey : function(fn, scope){
\r
216 for(var i = 0, len = this.keys.length; i < len; i++){
\r
217 fn.call(scope || window, this.keys[i], this.items[i], i, len);
\r
221 <div id="method-Ext.util.MixedCollection-find"></div>/**
\r
222 * Returns the first item in the collection which elicits a true return value from the
\r
223 * passed selection function.
\r
224 * @param {Function} fn The selection function to execute for each item.
\r
225 * @param {Object} scope (optional) The scope in which to execute the function.
\r
226 * @return {Object} The first item in the collection which returned true from the selection function.
\r
228 find : function(fn, scope){
\r
229 for(var i = 0, len = this.items.length; i < len; i++){
\r
230 if(fn.call(scope || window, this.items[i], this.keys[i])){
\r
231 return this.items[i];
\r
237 <div id="method-Ext.util.MixedCollection-insert"></div>/**
\r
238 * Inserts an item at the specified index in the collection. Fires the {@link #add} event when complete.
\r
239 * @param {Number} index The index to insert the item at.
\r
240 * @param {String} key The key to associate with the new item, or the item itself.
\r
241 * @param {Object} o (optional) If the second parameter was a key, the new item.
\r
242 * @return {Object} The item inserted.
\r
244 insert : function(index, key, o){
\r
245 if(arguments.length == 2){
\r
247 key = this.getKey(o);
\r
249 if(this.containsKey(key)){
\r
250 this.suspendEvents();
\r
251 this.removeKey(key);
\r
252 this.resumeEvents();
\r
254 if(index >= this.length){
\r
255 return this.add(key, o);
\r
258 this.items.splice(index, 0, o);
\r
259 if(typeof key != 'undefined' && key !== null){
\r
262 this.keys.splice(index, 0, key);
\r
263 this.fireEvent('add', index, o, key);
\r
267 <div id="method-Ext.util.MixedCollection-remove"></div>/**
\r
268 * Remove an item from the collection.
\r
269 * @param {Object} o The item to remove.
\r
270 * @return {Object} The item removed or false if no item was removed.
\r
272 remove : function(o){
\r
273 return this.removeAt(this.indexOf(o));
\r
276 <div id="method-Ext.util.MixedCollection-removeAt"></div>/**
\r
277 * Remove an item from a specified index in the collection. Fires the {@link #remove} event when complete.
\r
278 * @param {Number} index The index within the collection of the item to remove.
\r
279 * @return {Object} The item removed or false if no item was removed.
\r
281 removeAt : function(index){
\r
282 if(index < this.length && index >= 0){
\r
284 var o = this.items[index];
\r
285 this.items.splice(index, 1);
\r
286 var key = this.keys[index];
\r
287 if(typeof key != 'undefined'){
\r
288 delete this.map[key];
\r
290 this.keys.splice(index, 1);
\r
291 this.fireEvent('remove', o, key);
\r
297 <div id="method-Ext.util.MixedCollection-removeKey"></div>/**
\r
298 * Removed an item associated with the passed key fom the collection.
\r
299 * @param {String} key The key of the item to remove.
\r
300 * @return {Object} The item removed or false if no item was removed.
\r
302 removeKey : function(key){
\r
303 return this.removeAt(this.indexOfKey(key));
\r
306 <div id="method-Ext.util.MixedCollection-getCount"></div>/**
\r
307 * Returns the number of items in the collection.
\r
308 * @return {Number} the number of items in the collection.
\r
310 getCount : function(){
\r
311 return this.length;
\r
314 <div id="method-Ext.util.MixedCollection-indexOf"></div>/**
\r
315 * Returns index within the collection of the passed Object.
\r
316 * @param {Object} o The item to find the index of.
\r
317 * @return {Number} index of the item. Returns -1 if not found.
\r
319 indexOf : function(o){
\r
320 return this.items.indexOf(o);
\r
323 <div id="method-Ext.util.MixedCollection-indexOfKey"></div>/**
\r
324 * Returns index within the collection of the passed key.
\r
325 * @param {String} key The key to find the index of.
\r
326 * @return {Number} index of the key.
\r
328 indexOfKey : function(key){
\r
329 return this.keys.indexOf(key);
\r
332 <div id="method-Ext.util.MixedCollection-item"></div>/**
\r
333 * Returns the item associated with the passed key OR index.
\r
334 * Key has priority over index. This is the equivalent
\r
335 * of calling {@link #key} first, then if nothing matched calling {@link #itemAt}.
\r
336 * @param {String/Number} key The key or index of the item.
\r
337 * @return {Object} If the item is found, returns the item. If the item was not found, returns <tt>undefined</tt>.
\r
338 * If an item was found, but is a Class, returns <tt>null</tt>.
\r
340 item : function(key){
\r
341 var mk = this.map[key],
\r
342 item = mk !== undefined ? mk : (typeof key == 'number') ? this.items[key] : undefined;
\r
343 return !Ext.isFunction(item) || this.allowFunctions ? item : null; // for prototype!
\r
346 <div id="method-Ext.util.MixedCollection-itemAt"></div>/**
\r
347 * Returns the item at the specified index.
\r
348 * @param {Number} index The index of the item.
\r
349 * @return {Object} The item at the specified index.
\r
351 itemAt : function(index){
\r
352 return this.items[index];
\r
355 <div id="method-Ext.util.MixedCollection-key"></div>/**
\r
356 * Returns the item associated with the passed key.
\r
357 * @param {String/Number} key The key of the item.
\r
358 * @return {Object} The item associated with the passed key.
\r
360 key : function(key){
\r
361 return this.map[key];
\r
364 <div id="method-Ext.util.MixedCollection-contains"></div>/**
\r
365 * Returns true if the collection contains the passed Object as an item.
\r
366 * @param {Object} o The Object to look for in the collection.
\r
367 * @return {Boolean} True if the collection contains the Object as an item.
\r
369 contains : function(o){
\r
370 return this.indexOf(o) != -1;
\r
373 <div id="method-Ext.util.MixedCollection-containsKey"></div>/**
\r
374 * Returns true if the collection contains the passed Object as a key.
\r
375 * @param {String} key The key to look for in the collection.
\r
376 * @return {Boolean} True if the collection contains the Object as a key.
\r
378 containsKey : function(key){
\r
379 return typeof this.map[key] != 'undefined';
\r
382 <div id="method-Ext.util.MixedCollection-clear"></div>/**
\r
383 * Removes all items from the collection. Fires the {@link #clear} event when complete.
\r
385 clear : function(){
\r
390 this.fireEvent('clear');
\r
393 <div id="method-Ext.util.MixedCollection-first"></div>/**
\r
394 * Returns the first item in the collection.
\r
395 * @return {Object} the first item in the collection..
\r
397 first : function(){
\r
398 return this.items[0];
\r
401 <div id="method-Ext.util.MixedCollection-last"></div>/**
\r
402 * Returns the last item in the collection.
\r
403 * @return {Object} the last item in the collection..
\r
406 return this.items[this.length-1];
\r
411 * @param {String} property Property to sort by ('key', 'value', or 'index')
\r
412 * @param {String} dir (optional) Direction to sort 'ASC' or 'DESC'. Defaults to 'ASC'.
\r
413 * @param {Function} fn (optional) Comparison function that defines the sort order.
\r
414 * Defaults to sorting by numeric value.
\r
416 _sort : function(property, dir, fn){
\r
419 dsc = String(dir).toUpperCase() == 'DESC' ? -1 : 1,
\r
420 c = [], k = this.keys, items = this.items;
\r
422 fn = fn || function(a, b){
\r
425 for(i = 0, len = items.length; i < len; i++){
\r
426 c[c.length] = {key: k[i], value: items[i], index: i};
\r
428 c.sort(function(a, b){
\r
429 var v = fn(a[property], b[property]) * dsc;
\r
431 v = (a.index < b.index ? -1 : 1);
\r
435 for(i = 0, len = c.length; i < len; i++){
\r
436 items[i] = c[i].value;
\r
439 this.fireEvent('sort', this);
\r
442 <div id="method-Ext.util.MixedCollection-sort"></div>/**
\r
443 * Sorts this collection by <b>item</b> value with the passed comparison function.
\r
444 * @param {String} direction (optional) 'ASC' or 'DESC'. Defaults to 'ASC'.
\r
445 * @param {Function} fn (optional) Comparison function that defines the sort order.
\r
446 * Defaults to sorting by numeric value.
\r
448 sort : function(dir, fn){
\r
449 this._sort('value', dir, fn);
\r
452 <div id="method-Ext.util.MixedCollection-keySort"></div>/**
\r
453 * Sorts this collection by <b>key</b>s.
\r
454 * @param {String} direction (optional) 'ASC' or 'DESC'. Defaults to 'ASC'.
\r
455 * @param {Function} fn (optional) Comparison function that defines the sort order.
\r
456 * Defaults to sorting by case insensitive string.
\r
458 keySort : function(dir, fn){
\r
459 this._sort('key', dir, fn || function(a, b){
\r
460 var v1 = String(a).toUpperCase(), v2 = String(b).toUpperCase();
\r
461 return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
\r
465 <div id="method-Ext.util.MixedCollection-getRange"></div>/**
\r
466 * Returns a range of items in this collection
\r
467 * @param {Number} startIndex (optional) The starting index. Defaults to 0.
\r
468 * @param {Number} endIndex (optional) The ending index. Defaults to the last item.
\r
469 * @return {Array} An array of items
\r
471 getRange : function(start, end){
\r
472 var items = this.items;
\r
473 if(items.length < 1){
\r
476 start = start || 0;
\r
477 end = Math.min(typeof end == 'undefined' ? this.length-1 : end, this.length-1);
\r
480 for(i = start; i <= end; i++) {
\r
481 r[r.length] = items[i];
\r
484 for(i = start; i >= end; i--) {
\r
485 r[r.length] = items[i];
\r
491 <div id="method-Ext.util.MixedCollection-filter"></div>/**
\r
492 * Filter the <i>objects</i> in this collection by a specific property.
\r
493 * Returns a new collection that has been filtered.
\r
494 * @param {String} property A property on your objects
\r
495 * @param {String/RegExp} value Either string that the property values
\r
496 * should start with or a RegExp to test against the property
\r
497 * @param {Boolean} anyMatch (optional) True to match any part of the string, not just the beginning
\r
498 * @param {Boolean} caseSensitive (optional) True for case sensitive comparison (defaults to False).
\r
499 * @return {MixedCollection} The new filtered collection
\r
501 filter : function(property, value, anyMatch, caseSensitive){
\r
502 if(Ext.isEmpty(value, false)){
\r
503 return this.clone();
\r
505 value = this.createValueMatcher(value, anyMatch, caseSensitive);
\r
506 return this.filterBy(function(o){
\r
507 return o && value.test(o[property]);
\r
511 <div id="method-Ext.util.MixedCollection-filterBy"></div>/**
\r
512 * Filter by a function. Returns a <i>new</i> collection that has been filtered.
\r
513 * The passed function will be called with each object in the collection.
\r
514 * If the function returns true, the value is included otherwise it is filtered.
\r
515 * @param {Function} fn The function to be called, it will receive the args o (the object), k (the key)
\r
516 * @param {Object} scope (optional) The scope of the function (defaults to this)
\r
517 * @return {MixedCollection} The new filtered collection
\r
519 filterBy : function(fn, scope){
\r
520 var r = new Ext.util.MixedCollection();
\r
521 r.getKey = this.getKey;
\r
522 var k = this.keys, it = this.items;
\r
523 for(var i = 0, len = it.length; i < len; i++){
\r
524 if(fn.call(scope||this, it[i], k[i])){
\r
525 r.add(k[i], it[i]);
\r
531 <div id="method-Ext.util.MixedCollection-findIndex"></div>/**
\r
532 * Finds the index of the first matching object in this collection by a specific property/value.
\r
533 * @param {String} property The name of a property on your objects.
\r
534 * @param {String/RegExp} value A string that the property values
\r
535 * should start with or a RegExp to test against the property.
\r
536 * @param {Number} start (optional) The index to start searching at (defaults to 0).
\r
537 * @param {Boolean} anyMatch (optional) True to match any part of the string, not just the beginning.
\r
538 * @param {Boolean} caseSensitive (optional) True for case sensitive comparison.
\r
539 * @return {Number} The matched index or -1
\r
541 findIndex : function(property, value, start, anyMatch, caseSensitive){
\r
542 if(Ext.isEmpty(value, false)){
\r
545 value = this.createValueMatcher(value, anyMatch, caseSensitive);
\r
546 return this.findIndexBy(function(o){
\r
547 return o && value.test(o[property]);
\r
551 <div id="method-Ext.util.MixedCollection-findIndexBy"></div>/**
\r
552 * Find the index of the first matching object in this collection by a function.
\r
553 * If the function returns <i>true</i> it is considered a match.
\r
554 * @param {Function} fn The function to be called, it will receive the args o (the object), k (the key).
\r
555 * @param {Object} scope (optional) The scope of the function (defaults to this).
\r
556 * @param {Number} start (optional) The index to start searching at (defaults to 0).
\r
557 * @return {Number} The matched index or -1
\r
559 findIndexBy : function(fn, scope, start){
\r
560 var k = this.keys, it = this.items;
\r
561 for(var i = (start||0), len = it.length; i < len; i++){
\r
562 if(fn.call(scope||this, it[i], k[i])){
\r
570 createValueMatcher : function(value, anyMatch, caseSensitive){
\r
571 if(!value.exec){ // not a regex
\r
572 value = String(value);
\r
573 value = new RegExp((anyMatch === true ? '' : '^') + Ext.escapeRe(value), caseSensitive ? '' : 'i');
\r
578 <div id="method-Ext.util.MixedCollection-clone"></div>/**
\r
579 * Creates a shallow copy of this collection
\r
580 * @return {MixedCollection}
\r
582 clone : function(){
\r
583 var r = new Ext.util.MixedCollection();
\r
584 var k = this.keys, it = this.items;
\r
585 for(var i = 0, len = it.length; i < len; i++){
\r
586 r.add(k[i], it[i]);
\r
588 r.getKey = this.getKey;
\r
592 <div id="method-Ext.util.MixedCollection-get"></div>/**
\r
593 * This method calls {@link #item item()}.
\r
594 * Returns the item associated with the passed key OR index. Key has priority
\r
595 * over index. This is the equivalent of calling {@link #key} first, then if
\r
596 * nothing matched calling {@link #itemAt}.
\r
597 * @param {String/Number} key The key or index of the item.
\r
598 * @return {Object} If the item is found, returns the item. If the item was
\r
599 * not found, returns <tt>undefined</tt>. If an item was found, but is a Class,
\r
600 * returns <tt>null</tt>.
\r
602 Ext.util.MixedCollection.prototype.get = Ext.util.MixedCollection.prototype.item;</pre>