3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.util.MixedCollection"></div>/**
10 * @class Ext.util.MixedCollection
11 * @extends Ext.util.Observable
12 * A Collection class that maintains both numeric indexes and keys and exposes events.
14 * @param {Boolean} allowFunctions Specify <tt>true</tt> if the {@link #addAll}
15 * function should add function references to the collection. Defaults to
17 * @param {Function} keyFn A function that can accept an item of the type(s) stored in this MixedCollection
18 * and return the key value for that item. This is used when available to look up the key on items that
19 * were passed without an explicit key parameter to a MixedCollection method. Passing this parameter is
20 * equivalent to providing an implementation for the {@link #getKey} method.
22 Ext.util.MixedCollection = function(allowFunctions, keyFn){
28 <div id="event-Ext.util.MixedCollection-clear"></div>/**
30 * Fires when the collection is cleared.
33 <div id="event-Ext.util.MixedCollection-add"></div>/**
35 * Fires when an item is added to the collection.
36 * @param {Number} index The index at which the item was added.
37 * @param {Object} o The item added.
38 * @param {String} key The key associated with the added item.
41 <div id="event-Ext.util.MixedCollection-replace"></div>/**
43 * Fires when an item is replaced in the collection.
44 * @param {String} key he key associated with the new added.
45 * @param {Object} old The item being replaced.
46 * @param {Object} new The new item.
49 <div id="event-Ext.util.MixedCollection-remove"></div>/**
51 * Fires when an item is removed from the collection.
52 * @param {Object} o The item being removed.
53 * @param {String} key (optional) The key associated with the removed item.
58 this.allowFunctions = allowFunctions === true;
62 Ext.util.MixedCollection.superclass.constructor.call(this);
65 Ext.extend(Ext.util.MixedCollection, Ext.util.Observable, {
67 <div id="cfg-Ext.util.MixedCollection-allowFunctions"></div>/**
68 * @cfg {Boolean} allowFunctions Specify <tt>true</tt> if the {@link #addAll}
69 * function should add function references to the collection. Defaults to
72 allowFunctions : false,
74 <div id="method-Ext.util.MixedCollection-add"></div>/**
75 * Adds an item to the collection. Fires the {@link #add} event when complete.
76 * @param {String} key <p>The key to associate with the item, or the new item.</p>
77 * <p>If a {@link #getKey} implementation was specified for this MixedCollection,
78 * or if the key of the stored items is in a property called <tt><b>id</b></tt>,
79 * the MixedCollection will be able to <i>derive</i> the key for the new item.
80 * In this case just pass the new item in this parameter.</p>
81 * @param {Object} o The item to add.
82 * @return {Object} The item added.
84 add : function(key, o){
85 if(arguments.length == 1){
89 if(typeof key != 'undefined' && key !== null){
90 var old = this.map[key];
91 if(typeof old != 'undefined'){
92 return this.replace(key, o);
99 this.fireEvent('add', this.length-1, o, key);
103 <div id="method-Ext.util.MixedCollection-getKey"></div>/**
104 * MixedCollection has a generic way to fetch keys if you implement getKey. The default implementation
105 * simply returns <b><code>item.id</code></b> but you can provide your own implementation
106 * to return a different value as in the following examples:<pre><code>
108 var mc = new Ext.util.MixedCollection();
109 mc.add(someEl.dom.id, someEl);
110 mc.add(otherEl.dom.id, otherEl);
114 var mc = new Ext.util.MixedCollection();
115 mc.getKey = function(el){
121 // or via the constructor
122 var mc = new Ext.util.MixedCollection(false, function(el){
128 * @param {Object} item The item for which to find the key.
129 * @return {Object} The key for the passed item.
131 getKey : function(o){
135 <div id="method-Ext.util.MixedCollection-replace"></div>/**
136 * Replaces an item in the collection. Fires the {@link #replace} event when complete.
137 * @param {String} key <p>The key associated with the item to replace, or the replacement item.</p>
138 * <p>If you supplied a {@link #getKey} implementation for this MixedCollection, or if the key
139 * of your stored items is in a property called <tt><b>id</b></tt>, then the MixedCollection
140 * will be able to <i>derive</i> the key of the replacement item. If you want to replace an item
141 * with one having the same key value, then just pass the replacement item in this parameter.</p>
142 * @param o {Object} o (optional) If the first parameter passed was a key, the item to associate
144 * @return {Object} The new item.
146 replace : function(key, o){
147 if(arguments.length == 1){
149 key = this.getKey(o);
151 var old = this.map[key];
152 if(typeof key == 'undefined' || key === null || typeof old == 'undefined'){
153 return this.add(key, o);
155 var index = this.indexOfKey(key);
156 this.items[index] = o;
158 this.fireEvent('replace', key, old, o);
162 <div id="method-Ext.util.MixedCollection-addAll"></div>/**
163 * Adds all elements of an Array or an Object to the collection.
164 * @param {Object/Array} objs An Object containing properties which will be added
165 * to the collection, or an Array of values, each of which are added to the collection.
166 * Functions references will be added to the collection if <code>{@link #allowFunctions}</code>
167 * has been set to <tt>true</tt>.
169 addAll : function(objs){
170 if(arguments.length > 1 || Ext.isArray(objs)){
171 var args = arguments.length > 1 ? arguments : objs;
172 for(var i = 0, len = args.length; i < len; i++){
176 for(var key in objs){
177 if(this.allowFunctions || typeof objs[key] != 'function'){
178 this.add(key, objs[key]);
184 <div id="method-Ext.util.MixedCollection-each"></div>/**
185 * Executes the specified function once for every item in the collection, passing the following arguments:
186 * <div class="mdetail-params"><ul>
187 * <li><b>item</b> : Mixed<p class="sub-desc">The collection item</p></li>
188 * <li><b>index</b> : Number<p class="sub-desc">The item's index</p></li>
189 * <li><b>length</b> : Number<p class="sub-desc">The total number of items in the collection</p></li>
191 * The function should return a boolean value. Returning false from the function will stop the iteration.
192 * @param {Function} fn The function to execute for each item.
193 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the current item in the iteration.
195 each : function(fn, scope){
196 var items = [].concat(this.items); // each safe for removal
197 for(var i = 0, len = items.length; i < len; i++){
198 if(fn.call(scope || items[i], items[i], i, len) === false){
204 <div id="method-Ext.util.MixedCollection-eachKey"></div>/**
205 * Executes the specified function once for every key in the collection, passing each
206 * key, and its associated item as the first two parameters.
207 * @param {Function} fn The function to execute for each item.
208 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.
210 eachKey : function(fn, scope){
211 for(var i = 0, len = this.keys.length; i < len; i++){
212 fn.call(scope || window, this.keys[i], this.items[i], i, len);
216 <div id="method-Ext.util.MixedCollection-find"></div>/**
217 * Returns the first item in the collection which elicits a true return value from the
218 * passed selection function.
219 * @param {Function} fn The selection function to execute for each item.
220 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the browser window.
221 * @return {Object} The first item in the collection which returned true from the selection function.
223 find : function(fn, scope){
224 for(var i = 0, len = this.items.length; i < len; i++){
225 if(fn.call(scope || window, this.items[i], this.keys[i])){
226 return this.items[i];
232 <div id="method-Ext.util.MixedCollection-insert"></div>/**
233 * Inserts an item at the specified index in the collection. Fires the {@link #add} event when complete.
234 * @param {Number} index The index to insert the item at.
235 * @param {String} key The key to associate with the new item, or the item itself.
236 * @param {Object} o (optional) If the second parameter was a key, the new item.
237 * @return {Object} The item inserted.
239 insert : function(index, key, o){
240 if(arguments.length == 2){
242 key = this.getKey(o);
244 if(this.containsKey(key)){
245 this.suspendEvents();
249 if(index >= this.length){
250 return this.add(key, o);
253 this.items.splice(index, 0, o);
254 if(typeof key != 'undefined' && key !== null){
257 this.keys.splice(index, 0, key);
258 this.fireEvent('add', index, o, key);
262 <div id="method-Ext.util.MixedCollection-remove"></div>/**
263 * Remove an item from the collection.
264 * @param {Object} o The item to remove.
265 * @return {Object} The item removed or false if no item was removed.
267 remove : function(o){
268 return this.removeAt(this.indexOf(o));
271 <div id="method-Ext.util.MixedCollection-removeAt"></div>/**
272 * Remove an item from a specified index in the collection. Fires the {@link #remove} event when complete.
273 * @param {Number} index The index within the collection of the item to remove.
274 * @return {Object} The item removed or false if no item was removed.
276 removeAt : function(index){
277 if(index < this.length && index >= 0){
279 var o = this.items[index];
280 this.items.splice(index, 1);
281 var key = this.keys[index];
282 if(typeof key != 'undefined'){
283 delete this.map[key];
285 this.keys.splice(index, 1);
286 this.fireEvent('remove', o, key);
292 <div id="method-Ext.util.MixedCollection-removeKey"></div>/**
293 * Removed an item associated with the passed key fom the collection.
294 * @param {String} key The key of the item to remove.
295 * @return {Object} The item removed or false if no item was removed.
297 removeKey : function(key){
298 return this.removeAt(this.indexOfKey(key));
301 <div id="method-Ext.util.MixedCollection-getCount"></div>/**
302 * Returns the number of items in the collection.
303 * @return {Number} the number of items in the collection.
305 getCount : function(){
309 <div id="method-Ext.util.MixedCollection-indexOf"></div>/**
310 * Returns index within the collection of the passed Object.
311 * @param {Object} o The item to find the index of.
312 * @return {Number} index of the item. Returns -1 if not found.
314 indexOf : function(o){
315 return this.items.indexOf(o);
318 <div id="method-Ext.util.MixedCollection-indexOfKey"></div>/**
319 * Returns index within the collection of the passed key.
320 * @param {String} key The key to find the index of.
321 * @return {Number} index of the key.
323 indexOfKey : function(key){
324 return this.keys.indexOf(key);
327 <div id="method-Ext.util.MixedCollection-item"></div>/**
328 * Returns the item associated with the passed key OR index.
329 * Key has priority over index. This is the equivalent
330 * of calling {@link #key} first, then if nothing matched calling {@link #itemAt}.
331 * @param {String/Number} key The key or index of the item.
332 * @return {Object} If the item is found, returns the item. If the item was not found, returns <tt>undefined</tt>.
333 * If an item was found, but is a Class, returns <tt>null</tt>.
335 item : function(key){
336 var mk = this.map[key],
337 item = mk !== undefined ? mk : (typeof key == 'number') ? this.items[key] : undefined;
338 return !Ext.isFunction(item) || this.allowFunctions ? item : null; // for prototype!
341 <div id="method-Ext.util.MixedCollection-itemAt"></div>/**
342 * Returns the item at the specified index.
343 * @param {Number} index The index of the item.
344 * @return {Object} The item at the specified index.
346 itemAt : function(index){
347 return this.items[index];
350 <div id="method-Ext.util.MixedCollection-key"></div>/**
351 * Returns the item associated with the passed key.
352 * @param {String/Number} key The key of the item.
353 * @return {Object} The item associated with the passed key.
356 return this.map[key];
359 <div id="method-Ext.util.MixedCollection-contains"></div>/**
360 * Returns true if the collection contains the passed Object as an item.
361 * @param {Object} o The Object to look for in the collection.
362 * @return {Boolean} True if the collection contains the Object as an item.
364 contains : function(o){
365 return this.indexOf(o) != -1;
368 <div id="method-Ext.util.MixedCollection-containsKey"></div>/**
369 * Returns true if the collection contains the passed Object as a key.
370 * @param {String} key The key to look for in the collection.
371 * @return {Boolean} True if the collection contains the Object as a key.
373 containsKey : function(key){
374 return typeof this.map[key] != 'undefined';
377 <div id="method-Ext.util.MixedCollection-clear"></div>/**
378 * Removes all items from the collection. Fires the {@link #clear} event when complete.
385 this.fireEvent('clear');
388 <div id="method-Ext.util.MixedCollection-first"></div>/**
389 * Returns the first item in the collection.
390 * @return {Object} the first item in the collection..
393 return this.items[0];
396 <div id="method-Ext.util.MixedCollection-last"></div>/**
397 * Returns the last item in the collection.
398 * @return {Object} the last item in the collection..
401 return this.items[this.length-1];
406 * @param {String} property Property to sort by ('key', 'value', or 'index')
407 * @param {String} dir (optional) Direction to sort 'ASC' or 'DESC'. Defaults to 'ASC'.
408 * @param {Function} fn (optional) Comparison function that defines the sort order.
409 * Defaults to sorting by numeric value.
411 _sort : function(property, dir, fn){
414 dsc = String(dir).toUpperCase() == 'DESC' ? -1 : 1,
415 c = [], k = this.keys, items = this.items;
417 fn = fn || function(a, b){
420 for(i = 0, len = items.length; i < len; i++){
421 c[c.length] = {key: k[i], value: items[i], index: i};
423 c.sort(function(a, b){
424 var v = fn(a[property], b[property]) * dsc;
426 v = (a.index < b.index ? -1 : 1);
430 for(i = 0, len = c.length; i < len; i++){
431 items[i] = c[i].value;
434 this.fireEvent('sort', this);
437 <div id="method-Ext.util.MixedCollection-sort"></div>/**
438 * Sorts this collection by <b>item</b> value with the passed comparison function.
439 * @param {String} direction (optional) 'ASC' or 'DESC'. Defaults to 'ASC'.
440 * @param {Function} fn (optional) Comparison function that defines the sort order.
441 * Defaults to sorting by numeric value.
443 sort : function(dir, fn){
444 this._sort('value', dir, fn);
447 <div id="method-Ext.util.MixedCollection-keySort"></div>/**
448 * Sorts this collection by <b>key</b>s.
449 * @param {String} direction (optional) 'ASC' or 'DESC'. Defaults to 'ASC'.
450 * @param {Function} fn (optional) Comparison function that defines the sort order.
451 * Defaults to sorting by case insensitive string.
453 keySort : function(dir, fn){
454 this._sort('key', dir, fn || function(a, b){
455 var v1 = String(a).toUpperCase(), v2 = String(b).toUpperCase();
456 return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
460 <div id="method-Ext.util.MixedCollection-getRange"></div>/**
461 * Returns a range of items in this collection
462 * @param {Number} startIndex (optional) The starting index. Defaults to 0.
463 * @param {Number} endIndex (optional) The ending index. Defaults to the last item.
464 * @return {Array} An array of items
466 getRange : function(start, end){
467 var items = this.items;
468 if(items.length < 1){
472 end = Math.min(typeof end == 'undefined' ? this.length-1 : end, this.length-1);
475 for(i = start; i <= end; i++) {
476 r[r.length] = items[i];
479 for(i = start; i >= end; i--) {
480 r[r.length] = items[i];
486 <div id="method-Ext.util.MixedCollection-filter"></div>/**
487 * Filter the <i>objects</i> in this collection by a specific property.
488 * Returns a new collection that has been filtered.
489 * @param {String} property A property on your objects
490 * @param {String/RegExp} value Either string that the property values
491 * should start with or a RegExp to test against the property
492 * @param {Boolean} anyMatch (optional) True to match any part of the string, not just the beginning
493 * @param {Boolean} caseSensitive (optional) True for case sensitive comparison (defaults to False).
494 * @return {MixedCollection} The new filtered collection
496 filter : function(property, value, anyMatch, caseSensitive){
497 if(Ext.isEmpty(value, false)){
500 value = this.createValueMatcher(value, anyMatch, caseSensitive);
501 return this.filterBy(function(o){
502 return o && value.test(o[property]);
506 <div id="method-Ext.util.MixedCollection-filterBy"></div>/**
507 * Filter by a function. Returns a <i>new</i> collection that has been filtered.
508 * The passed function will be called with each object in the collection.
509 * If the function returns true, the value is included otherwise it is filtered.
510 * @param {Function} fn The function to be called, it will receive the args o (the object), k (the key)
511 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this MixedCollection.
512 * @return {MixedCollection} The new filtered collection
514 filterBy : function(fn, scope){
515 var r = new Ext.util.MixedCollection();
516 r.getKey = this.getKey;
517 var k = this.keys, it = this.items;
518 for(var i = 0, len = it.length; i < len; i++){
519 if(fn.call(scope||this, it[i], k[i])){
526 <div id="method-Ext.util.MixedCollection-findIndex"></div>/**
527 * Finds the index of the first matching object in this collection by a specific property/value.
528 * @param {String} property The name of a property on your objects.
529 * @param {String/RegExp} value A string that the property values
530 * should start with or a RegExp to test against the property.
531 * @param {Number} start (optional) The index to start searching at (defaults to 0).
532 * @param {Boolean} anyMatch (optional) True to match any part of the string, not just the beginning.
533 * @param {Boolean} caseSensitive (optional) True for case sensitive comparison.
534 * @return {Number} The matched index or -1
536 findIndex : function(property, value, start, anyMatch, caseSensitive){
537 if(Ext.isEmpty(value, false)){
540 value = this.createValueMatcher(value, anyMatch, caseSensitive);
541 return this.findIndexBy(function(o){
542 return o && value.test(o[property]);
546 <div id="method-Ext.util.MixedCollection-findIndexBy"></div>/**
547 * Find the index of the first matching object in this collection by a function.
548 * If the function returns <i>true</i> it is considered a match.
549 * @param {Function} fn The function to be called, it will receive the args o (the object), k (the key).
550 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this MixedCollection.
551 * @param {Number} start (optional) The index to start searching at (defaults to 0).
552 * @return {Number} The matched index or -1
554 findIndexBy : function(fn, scope, start){
555 var k = this.keys, it = this.items;
556 for(var i = (start||0), len = it.length; i < len; i++){
557 if(fn.call(scope||this, it[i], k[i])){
565 createValueMatcher : function(value, anyMatch, caseSensitive, exactMatch) {
566 if (!value.exec) { // not a regex
567 var er = Ext.escapeRe;
568 value = String(value);
569 if (anyMatch === true) {
572 value = '^' + er(value);
573 if (exactMatch === true) {
577 value = new RegExp(value, caseSensitive ? '' : 'i');
582 <div id="method-Ext.util.MixedCollection-clone"></div>/**
583 * Creates a shallow copy of this collection
584 * @return {MixedCollection}
587 var r = new Ext.util.MixedCollection();
588 var k = this.keys, it = this.items;
589 for(var i = 0, len = it.length; i < len; i++){
592 r.getKey = this.getKey;
596 <div id="method-Ext.util.MixedCollection-get"></div>/**
597 * This method calls {@link #item item()}.
598 * Returns the item associated with the passed key OR index. Key has priority
599 * over index. This is the equivalent of calling {@link #key} first, then if
600 * nothing matched calling {@link #itemAt}.
601 * @param {String/Number} key The key or index of the item.
602 * @return {Object} If the item is found, returns the item. If the item was
603 * not found, returns <tt>undefined</tt>. If an item was found, but is a Class,
604 * returns <tt>null</tt>.
606 Ext.util.MixedCollection.prototype.get = Ext.util.MixedCollection.prototype.item;</pre>
\r