3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.3.1
11 * Copyright(c) 2006-2010 Sencha Inc.
12 * licensing@sencha.com
13 * http://www.sencha.com/license
15 <div id="cls-Ext.util.MixedCollection"></div>/**
16 * @class Ext.util.MixedCollection
17 * @extends Ext.util.Observable
18 * A Collection class that maintains both numeric indexes and keys and exposes events.
20 * @param {Boolean} allowFunctions Specify <tt>true</tt> if the {@link #addAll}
21 * function should add function references to the collection. Defaults to
23 * @param {Function} keyFn A function that can accept an item of the type(s) stored in this MixedCollection
24 * and return the key value for that item. This is used when available to look up the key on items that
25 * were passed without an explicit key parameter to a MixedCollection method. Passing this parameter is
26 * equivalent to providing an implementation for the {@link #getKey} method.
28 Ext.util.MixedCollection = function(allowFunctions, keyFn){
34 <div id="event-Ext.util.MixedCollection-clear"></div>/**
36 * Fires when the collection is cleared.
39 <div id="event-Ext.util.MixedCollection-add"></div>/**
41 * Fires when an item is added to the collection.
42 * @param {Number} index The index at which the item was added.
43 * @param {Object} o The item added.
44 * @param {String} key The key associated with the added item.
47 <div id="event-Ext.util.MixedCollection-replace"></div>/**
49 * Fires when an item is replaced in the collection.
50 * @param {String} key he key associated with the new added.
51 * @param {Object} old The item being replaced.
52 * @param {Object} new The new item.
55 <div id="event-Ext.util.MixedCollection-remove"></div>/**
57 * Fires when an item is removed from the collection.
58 * @param {Object} o The item being removed.
59 * @param {String} key (optional) The key associated with the removed item.
64 this.allowFunctions = allowFunctions === true;
68 Ext.util.MixedCollection.superclass.constructor.call(this);
71 Ext.extend(Ext.util.MixedCollection, Ext.util.Observable, {
73 <div id="cfg-Ext.util.MixedCollection-allowFunctions"></div>/**
74 * @cfg {Boolean} allowFunctions Specify <tt>true</tt> if the {@link #addAll}
75 * function should add function references to the collection. Defaults to
78 allowFunctions : false,
80 <div id="method-Ext.util.MixedCollection-add"></div>/**
81 * Adds an item to the collection. Fires the {@link #add} event when complete.
82 * @param {String} key <p>The key to associate with the item, or the new item.</p>
83 * <p>If a {@link #getKey} implementation was specified for this MixedCollection,
84 * or if the key of the stored items is in a property called <tt><b>id</b></tt>,
85 * the MixedCollection will be able to <i>derive</i> the key for the new item.
86 * In this case just pass the new item in this parameter.</p>
87 * @param {Object} o The item to add.
88 * @return {Object} The item added.
90 add : function(key, o){
91 if(arguments.length == 1){
95 if(typeof key != 'undefined' && key !== null){
96 var old = this.map[key];
97 if(typeof old != 'undefined'){
98 return this.replace(key, o);
105 this.fireEvent('add', this.length-1, o, key);
109 <div id="method-Ext.util.MixedCollection-getKey"></div>/**
110 * MixedCollection has a generic way to fetch keys if you implement getKey. The default implementation
111 * simply returns <b><code>item.id</code></b> but you can provide your own implementation
112 * to return a different value as in the following examples:<pre><code>
114 var mc = new Ext.util.MixedCollection();
115 mc.add(someEl.dom.id, someEl);
116 mc.add(otherEl.dom.id, otherEl);
120 var mc = new Ext.util.MixedCollection();
121 mc.getKey = function(el){
127 // or via the constructor
128 var mc = new Ext.util.MixedCollection(false, function(el){
134 * @param {Object} item The item for which to find the key.
135 * @return {Object} The key for the passed item.
137 getKey : function(o){
141 <div id="method-Ext.util.MixedCollection-replace"></div>/**
142 * Replaces an item in the collection. Fires the {@link #replace} event when complete.
143 * @param {String} key <p>The key associated with the item to replace, or the replacement item.</p>
144 * <p>If you supplied a {@link #getKey} implementation for this MixedCollection, or if the key
145 * of your stored items is in a property called <tt><b>id</b></tt>, then the MixedCollection
146 * will be able to <i>derive</i> the key of the replacement item. If you want to replace an item
147 * with one having the same key value, then just pass the replacement item in this parameter.</p>
148 * @param o {Object} o (optional) If the first parameter passed was a key, the item to associate
150 * @return {Object} The new item.
152 replace : function(key, o){
153 if(arguments.length == 1){
155 key = this.getKey(o);
157 var old = this.map[key];
158 if(typeof key == 'undefined' || key === null || typeof old == 'undefined'){
159 return this.add(key, o);
161 var index = this.indexOfKey(key);
162 this.items[index] = o;
164 this.fireEvent('replace', key, old, o);
168 <div id="method-Ext.util.MixedCollection-addAll"></div>/**
169 * Adds all elements of an Array or an Object to the collection.
170 * @param {Object/Array} objs An Object containing properties which will be added
171 * to the collection, or an Array of values, each of which are added to the collection.
172 * Functions references will be added to the collection if <code>{@link #allowFunctions}</code>
173 * has been set to <tt>true</tt>.
175 addAll : function(objs){
176 if(arguments.length > 1 || Ext.isArray(objs)){
177 var args = arguments.length > 1 ? arguments : objs;
178 for(var i = 0, len = args.length; i < len; i++){
182 for(var key in objs){
183 if(this.allowFunctions || typeof objs[key] != 'function'){
184 this.add(key, objs[key]);
190 <div id="method-Ext.util.MixedCollection-each"></div>/**
191 * Executes the specified function once for every item in the collection, passing the following arguments:
192 * <div class="mdetail-params"><ul>
193 * <li><b>item</b> : Mixed<p class="sub-desc">The collection item</p></li>
194 * <li><b>index</b> : Number<p class="sub-desc">The item's index</p></li>
195 * <li><b>length</b> : Number<p class="sub-desc">The total number of items in the collection</p></li>
197 * The function should return a boolean value. Returning false from the function will stop the iteration.
198 * @param {Function} fn The function to execute for each item.
199 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the current item in the iteration.
201 each : function(fn, scope){
202 var items = [].concat(this.items); // each safe for removal
203 for(var i = 0, len = items.length; i < len; i++){
204 if(fn.call(scope || items[i], items[i], i, len) === false){
210 <div id="method-Ext.util.MixedCollection-eachKey"></div>/**
211 * Executes the specified function once for every key in the collection, passing each
212 * key, and its associated item as the first two parameters.
213 * @param {Function} fn The function to execute for each item.
214 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.
216 eachKey : function(fn, scope){
217 for(var i = 0, len = this.keys.length; i < len; i++){
218 fn.call(scope || window, this.keys[i], this.items[i], i, len);
222 <div id="method-Ext.util.MixedCollection-find"></div>/**
223 * Returns the first item in the collection which elicits a true return value from the
224 * passed selection function.
225 * @param {Function} fn The selection function to execute for each item.
226 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.
227 * @return {Object} The first item in the collection which returned true from the selection function.
229 find : function(fn, scope){
230 for(var i = 0, len = this.items.length; i < len; i++){
231 if(fn.call(scope || window, this.items[i], this.keys[i])){
232 return this.items[i];
238 <div id="method-Ext.util.MixedCollection-insert"></div>/**
239 * Inserts an item at the specified index in the collection. Fires the {@link #add} event when complete.
240 * @param {Number} index The index to insert the item at.
241 * @param {String} key The key to associate with the new item, or the item itself.
242 * @param {Object} o (optional) If the second parameter was a key, the new item.
243 * @return {Object} The item inserted.
245 insert : function(index, key, o){
246 if(arguments.length == 2){
248 key = this.getKey(o);
250 if(this.containsKey(key)){
251 this.suspendEvents();
255 if(index >= this.length){
256 return this.add(key, o);
259 this.items.splice(index, 0, o);
260 if(typeof key != 'undefined' && key !== null){
263 this.keys.splice(index, 0, key);
264 this.fireEvent('add', index, o, key);
268 <div id="method-Ext.util.MixedCollection-remove"></div>/**
269 * Remove an item from the collection.
270 * @param {Object} o The item to remove.
271 * @return {Object} The item removed or false if no item was removed.
273 remove : function(o){
274 return this.removeAt(this.indexOf(o));
277 <div id="method-Ext.util.MixedCollection-removeAt"></div>/**
278 * Remove an item from a specified index in the collection. Fires the {@link #remove} event when complete.
279 * @param {Number} index The index within the collection of the item to remove.
280 * @return {Object} The item removed or false if no item was removed.
282 removeAt : function(index){
283 if(index < this.length && index >= 0){
285 var o = this.items[index];
286 this.items.splice(index, 1);
287 var key = this.keys[index];
288 if(typeof key != 'undefined'){
289 delete this.map[key];
291 this.keys.splice(index, 1);
292 this.fireEvent('remove', o, key);
298 <div id="method-Ext.util.MixedCollection-removeKey"></div>/**
299 * Removed an item associated with the passed key fom the collection.
300 * @param {String} key The key of the item to remove.
301 * @return {Object} The item removed or false if no item was removed.
303 removeKey : function(key){
304 return this.removeAt(this.indexOfKey(key));
307 <div id="method-Ext.util.MixedCollection-getCount"></div>/**
308 * Returns the number of items in the collection.
309 * @return {Number} the number of items in the collection.
311 getCount : function(){
315 <div id="method-Ext.util.MixedCollection-indexOf"></div>/**
316 * Returns index within the collection of the passed Object.
317 * @param {Object} o The item to find the index of.
318 * @return {Number} index of the item. Returns -1 if not found.
320 indexOf : function(o){
321 return this.items.indexOf(o);
324 <div id="method-Ext.util.MixedCollection-indexOfKey"></div>/**
325 * Returns index within the collection of the passed key.
326 * @param {String} key The key to find the index of.
327 * @return {Number} index of the key.
329 indexOfKey : function(key){
330 return this.keys.indexOf(key);
333 <div id="method-Ext.util.MixedCollection-item"></div>/**
334 * Returns the item associated with the passed key OR index.
335 * Key has priority over index. This is the equivalent
336 * of calling {@link #key} first, then if nothing matched calling {@link #itemAt}.
337 * @param {String/Number} key The key or index of the item.
338 * @return {Object} If the item is found, returns the item. If the item was not found, returns <tt>undefined</tt>.
339 * If an item was found, but is a Class, returns <tt>null</tt>.
341 item : function(key){
342 var mk = this.map[key],
343 item = mk !== undefined ? mk : (typeof key == 'number') ? this.items[key] : undefined;
344 return typeof item != 'function' || this.allowFunctions ? item : null; // for prototype!
347 <div id="method-Ext.util.MixedCollection-itemAt"></div>/**
348 * Returns the item at the specified index.
349 * @param {Number} index The index of the item.
350 * @return {Object} The item at the specified index.
352 itemAt : function(index){
353 return this.items[index];
356 <div id="method-Ext.util.MixedCollection-key"></div>/**
357 * Returns the item associated with the passed key.
358 * @param {String/Number} key The key of the item.
359 * @return {Object} The item associated with the passed key.
362 return this.map[key];
365 <div id="method-Ext.util.MixedCollection-contains"></div>/**
366 * Returns true if the collection contains the passed Object as an item.
367 * @param {Object} o The Object to look for in the collection.
368 * @return {Boolean} True if the collection contains the Object as an item.
370 contains : function(o){
371 return this.indexOf(o) != -1;
374 <div id="method-Ext.util.MixedCollection-containsKey"></div>/**
375 * Returns true if the collection contains the passed Object as a key.
376 * @param {String} key The key to look for in the collection.
377 * @return {Boolean} True if the collection contains the Object as a key.
379 containsKey : function(key){
380 return typeof this.map[key] != 'undefined';
383 <div id="method-Ext.util.MixedCollection-clear"></div>/**
384 * Removes all items from the collection. Fires the {@link #clear} event when complete.
391 this.fireEvent('clear');
394 <div id="method-Ext.util.MixedCollection-first"></div>/**
395 * Returns the first item in the collection.
396 * @return {Object} the first item in the collection..
399 return this.items[0];
402 <div id="method-Ext.util.MixedCollection-last"></div>/**
403 * Returns the last item in the collection.
404 * @return {Object} the last item in the collection..
407 return this.items[this.length-1];
412 * Performs the actual sorting based on a direction and a sorting function. Internally,
413 * this creates a temporary array of all items in the MixedCollection, sorts it and then writes
414 * the sorted array data back into this.items and this.keys
415 * @param {String} property Property to sort by ('key', 'value', or 'index')
416 * @param {String} dir (optional) Direction to sort 'ASC' or 'DESC'. Defaults to 'ASC'.
417 * @param {Function} fn (optional) Comparison function that defines the sort order.
418 * Defaults to sorting by numeric value.
420 _sort : function(property, dir, fn){
422 dsc = String(dir).toUpperCase() == 'DESC' ? -1 : 1,
424 //this is a temporary array used to apply the sorting function
429 //default to a simple sorter function if one is not provided
430 fn = fn || function(a, b) {
434 //copy all the items into a temporary array, which we will sort
435 for(i = 0, len = items.length; i < len; i++){
443 //sort the temporary array
444 c.sort(function(a, b){
445 var v = fn(a[property], b[property]) * dsc;
447 v = (a.index < b.index ? -1 : 1);
452 //copy the temporary array back into the main this.items and this.keys objects
453 for(i = 0, len = c.length; i < len; i++){
454 items[i] = c[i].value;
458 this.fireEvent('sort', this);
461 <div id="method-Ext.util.MixedCollection-sort"></div>/**
462 * Sorts this collection by <b>item</b> value with the passed comparison function.
463 * @param {String} direction (optional) 'ASC' or 'DESC'. Defaults to 'ASC'.
464 * @param {Function} fn (optional) Comparison function that defines the sort order.
465 * Defaults to sorting by numeric value.
467 sort : function(dir, fn){
468 this._sort('value', dir, fn);
471 <div id="method-Ext.util.MixedCollection-reorder"></div>/**
472 * Reorders each of the items based on a mapping from old index to new index. Internally this
473 * just translates into a sort. The 'sort' event is fired whenever reordering has occured.
474 * @param {Object} mapping Mapping from old item index to new item index
476 reorder: function(mapping) {
477 this.suspendEvents();
479 var items = this.items,
481 length = items.length,
486 //object of {oldPosition: newPosition} reversed to {newPosition: oldPosition}
487 for (oldIndex in mapping) {
488 order[mapping[oldIndex]] = items[oldIndex];
491 for (index = 0; index < length; index++) {
492 if (mapping[index] == undefined) {
493 remaining.push(items[index]);
497 for (index = 0; index < length; index++) {
498 if (order[index] == undefined) {
499 order[index] = remaining.shift();
507 this.fireEvent('sort', this);
510 <div id="method-Ext.util.MixedCollection-keySort"></div>/**
511 * Sorts this collection by <b>key</b>s.
512 * @param {String} direction (optional) 'ASC' or 'DESC'. Defaults to 'ASC'.
513 * @param {Function} fn (optional) Comparison function that defines the sort order.
514 * Defaults to sorting by case insensitive string.
516 keySort : function(dir, fn){
517 this._sort('key', dir, fn || function(a, b){
518 var v1 = String(a).toUpperCase(), v2 = String(b).toUpperCase();
519 return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
523 <div id="method-Ext.util.MixedCollection-getRange"></div>/**
524 * Returns a range of items in this collection
525 * @param {Number} startIndex (optional) The starting index. Defaults to 0.
526 * @param {Number} endIndex (optional) The ending index. Defaults to the last item.
527 * @return {Array} An array of items
529 getRange : function(start, end){
530 var items = this.items;
531 if(items.length < 1){
535 end = Math.min(typeof end == 'undefined' ? this.length-1 : end, this.length-1);
538 for(i = start; i <= end; i++) {
539 r[r.length] = items[i];
542 for(i = start; i >= end; i--) {
543 r[r.length] = items[i];
549 <div id="method-Ext.util.MixedCollection-filter"></div>/**
550 * Filter the <i>objects</i> in this collection by a specific property.
551 * Returns a new collection that has been filtered.
552 * @param {String} property A property on your objects
553 * @param {String/RegExp} value Either string that the property values
554 * should start with or a RegExp to test against the property
555 * @param {Boolean} anyMatch (optional) True to match any part of the string, not just the beginning
556 * @param {Boolean} caseSensitive (optional) True for case sensitive comparison (defaults to False).
557 * @return {MixedCollection} The new filtered collection
559 filter : function(property, value, anyMatch, caseSensitive){
560 if(Ext.isEmpty(value, false)){
563 value = this.createValueMatcher(value, anyMatch, caseSensitive);
564 return this.filterBy(function(o){
565 return o && value.test(o[property]);
569 <div id="method-Ext.util.MixedCollection-filterBy"></div>/**
570 * Filter by a function. Returns a <i>new</i> collection that has been filtered.
571 * The passed function will be called with each object in the collection.
572 * If the function returns true, the value is included otherwise it is filtered.
573 * @param {Function} fn The function to be called, it will receive the args o (the object), k (the key)
574 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this MixedCollection.
575 * @return {MixedCollection} The new filtered collection
577 filterBy : function(fn, scope){
578 var r = new Ext.util.MixedCollection();
579 r.getKey = this.getKey;
580 var k = this.keys, it = this.items;
581 for(var i = 0, len = it.length; i < len; i++){
582 if(fn.call(scope||this, it[i], k[i])){
589 <div id="method-Ext.util.MixedCollection-findIndex"></div>/**
590 * Finds the index of the first matching object in this collection by a specific property/value.
591 * @param {String} property The name of a property on your objects.
592 * @param {String/RegExp} value A string that the property values
593 * should start with or a RegExp to test against the property.
594 * @param {Number} start (optional) The index to start searching at (defaults to 0).
595 * @param {Boolean} anyMatch (optional) True to match any part of the string, not just the beginning.
596 * @param {Boolean} caseSensitive (optional) True for case sensitive comparison.
597 * @return {Number} The matched index or -1
599 findIndex : function(property, value, start, anyMatch, caseSensitive){
600 if(Ext.isEmpty(value, false)){
603 value = this.createValueMatcher(value, anyMatch, caseSensitive);
604 return this.findIndexBy(function(o){
605 return o && value.test(o[property]);
609 <div id="method-Ext.util.MixedCollection-findIndexBy"></div>/**
610 * Find the index of the first matching object in this collection by a function.
611 * If the function returns <i>true</i> it is considered a match.
612 * @param {Function} fn The function to be called, it will receive the args o (the object), k (the key).
613 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this MixedCollection.
614 * @param {Number} start (optional) The index to start searching at (defaults to 0).
615 * @return {Number} The matched index or -1
617 findIndexBy : function(fn, scope, start){
618 var k = this.keys, it = this.items;
619 for(var i = (start||0), len = it.length; i < len; i++){
620 if(fn.call(scope||this, it[i], k[i])){
628 * Returns a regular expression based on the given value and matching options. This is used internally for finding and filtering,
629 * and by Ext.data.Store#filter
631 * @param {String} value The value to create the regex for. This is escaped using Ext.escapeRe
632 * @param {Boolean} anyMatch True to allow any match - no regex start/end line anchors will be added. Defaults to false
633 * @param {Boolean} caseSensitive True to make the regex case sensitive (adds 'i' switch to regex). Defaults to false.
634 * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false. Ignored if anyMatch is true.
636 createValueMatcher : function(value, anyMatch, caseSensitive, exactMatch) {
637 if (!value.exec) { // not a regex
638 var er = Ext.escapeRe;
639 value = String(value);
641 if (anyMatch === true) {
644 value = '^' + er(value);
645 if (exactMatch === true) {
649 value = new RegExp(value, caseSensitive ? '' : 'i');
654 <div id="method-Ext.util.MixedCollection-clone"></div>/**
655 * Creates a shallow copy of this collection
656 * @return {MixedCollection}
659 var r = new Ext.util.MixedCollection();
660 var k = this.keys, it = this.items;
661 for(var i = 0, len = it.length; i < len; i++){
664 r.getKey = this.getKey;
668 <div id="method-Ext.util.MixedCollection-get"></div>/**
669 * This method calls {@link #item item()}.
670 * Returns the item associated with the passed key OR index. Key has priority
671 * over index. This is the equivalent of calling {@link #key} first, then if
672 * nothing matched calling {@link #itemAt}.
673 * @param {String/Number} key The key or index of the item.
674 * @return {Object} If the item is found, returns the item. If the item was
675 * not found, returns <tt>undefined</tt>. If an item was found, but is a Class,
676 * returns <tt>null</tt>.
678 Ext.util.MixedCollection.prototype.get = Ext.util.MixedCollection.prototype.item;