You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

684 lines
28 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-util-MixedCollection-method-constructor'><span id='Ext-util-MixedCollection'>/**
  19. </span></span> * @class Ext.util.MixedCollection
  20. * @extends Ext.util.Observable
  21. * A Collection class that maintains both numeric indexes and keys and exposes events.
  22. * @constructor
  23. * @param {Boolean} allowFunctions Specify &lt;tt&gt;true&lt;/tt&gt; if the {@link #addAll}
  24. * function should add function references to the collection. Defaults to
  25. * &lt;tt&gt;false&lt;/tt&gt;.
  26. * @param {Function} keyFn A function that can accept an item of the type(s) stored in this MixedCollection
  27. * and return the key value for that item. This is used when available to look up the key on items that
  28. * were passed without an explicit key parameter to a MixedCollection method. Passing this parameter is
  29. * equivalent to providing an implementation for the {@link #getKey} method.
  30. */
  31. Ext.util.MixedCollection = function(allowFunctions, keyFn){
  32. this.items = [];
  33. this.map = {};
  34. this.keys = [];
  35. this.length = 0;
  36. this.addEvents(
  37. <span id='Ext-util-MixedCollection-event-clear'> /**
  38. </span> * @event clear
  39. * Fires when the collection is cleared.
  40. */
  41. 'clear',
  42. <span id='Ext-util-MixedCollection-event-add'> /**
  43. </span> * @event add
  44. * Fires when an item is added to the collection.
  45. * @param {Number} index The index at which the item was added.
  46. * @param {Object} o The item added.
  47. * @param {String} key The key associated with the added item.
  48. */
  49. 'add',
  50. <span id='Ext-util-MixedCollection-event-replace'> /**
  51. </span> * @event replace
  52. * Fires when an item is replaced in the collection.
  53. * @param {String} key he key associated with the new added.
  54. * @param {Object} old The item being replaced.
  55. * @param {Object} new The new item.
  56. */
  57. 'replace',
  58. <span id='Ext-util-MixedCollection-event-remove'> /**
  59. </span> * @event remove
  60. * Fires when an item is removed from the collection.
  61. * @param {Object} o The item being removed.
  62. * @param {String} key (optional) The key associated with the removed item.
  63. */
  64. 'remove',
  65. 'sort'
  66. );
  67. this.allowFunctions = allowFunctions === true;
  68. if(keyFn){
  69. this.getKey = keyFn;
  70. }
  71. Ext.util.MixedCollection.superclass.constructor.call(this);
  72. };
  73. Ext.extend(Ext.util.MixedCollection, Ext.util.Observable, {
  74. <span id='Ext-util-MixedCollection-cfg-allowFunctions'> /**
  75. </span> * @cfg {Boolean} allowFunctions Specify &lt;tt&gt;true&lt;/tt&gt; if the {@link #addAll}
  76. * function should add function references to the collection. Defaults to
  77. * &lt;tt&gt;false&lt;/tt&gt;.
  78. */
  79. allowFunctions : false,
  80. <span id='Ext-util-MixedCollection-method-add'> /**
  81. </span> * Adds an item to the collection. Fires the {@link #add} event when complete.
  82. * @param {String} key &lt;p&gt;The key to associate with the item, or the new item.&lt;/p&gt;
  83. * &lt;p&gt;If a {@link #getKey} implementation was specified for this MixedCollection,
  84. * or if the key of the stored items is in a property called &lt;tt&gt;&lt;b&gt;id&lt;/b&gt;&lt;/tt&gt;,
  85. * the MixedCollection will be able to &lt;i&gt;derive&lt;/i&gt; the key for the new item.
  86. * In this case just pass the new item in this parameter.&lt;/p&gt;
  87. * @param {Object} o The item to add.
  88. * @return {Object} The item added.
  89. */
  90. add : function(key, o){
  91. if(arguments.length == 1){
  92. o = arguments[0];
  93. key = this.getKey(o);
  94. }
  95. if(typeof key != 'undefined' &amp;&amp; key !== null){
  96. var old = this.map[key];
  97. if(typeof old != 'undefined'){
  98. return this.replace(key, o);
  99. }
  100. this.map[key] = o;
  101. }
  102. this.length++;
  103. this.items.push(o);
  104. this.keys.push(key);
  105. this.fireEvent('add', this.length-1, o, key);
  106. return o;
  107. },
  108. <span id='Ext-util-MixedCollection-method-getKey'> /**
  109. </span> * MixedCollection has a generic way to fetch keys if you implement getKey. The default implementation
  110. * simply returns &lt;b&gt;&lt;code&gt;item.id&lt;/code&gt;&lt;/b&gt; but you can provide your own implementation
  111. * to return a different value as in the following examples:&lt;pre&gt;&lt;code&gt;
  112. // normal way
  113. var mc = new Ext.util.MixedCollection();
  114. mc.add(someEl.dom.id, someEl);
  115. mc.add(otherEl.dom.id, otherEl);
  116. //and so on
  117. // using getKey
  118. var mc = new Ext.util.MixedCollection();
  119. mc.getKey = function(el){
  120. return el.dom.id;
  121. };
  122. mc.add(someEl);
  123. mc.add(otherEl);
  124. // or via the constructor
  125. var mc = new Ext.util.MixedCollection(false, function(el){
  126. return el.dom.id;
  127. });
  128. mc.add(someEl);
  129. mc.add(otherEl);
  130. * &lt;/code&gt;&lt;/pre&gt;
  131. * @param {Object} item The item for which to find the key.
  132. * @return {Object} The key for the passed item.
  133. */
  134. getKey : function(o){
  135. return o.id;
  136. },
  137. <span id='Ext-util-MixedCollection-method-replace'> /**
  138. </span> * Replaces an item in the collection. Fires the {@link #replace} event when complete.
  139. * @param {String} key &lt;p&gt;The key associated with the item to replace, or the replacement item.&lt;/p&gt;
  140. * &lt;p&gt;If you supplied a {@link #getKey} implementation for this MixedCollection, or if the key
  141. * of your stored items is in a property called &lt;tt&gt;&lt;b&gt;id&lt;/b&gt;&lt;/tt&gt;, then the MixedCollection
  142. * will be able to &lt;i&gt;derive&lt;/i&gt; the key of the replacement item. If you want to replace an item
  143. * with one having the same key value, then just pass the replacement item in this parameter.&lt;/p&gt;
  144. * @param o {Object} o (optional) If the first parameter passed was a key, the item to associate
  145. * with that key.
  146. * @return {Object} The new item.
  147. */
  148. replace : function(key, o){
  149. if(arguments.length == 1){
  150. o = arguments[0];
  151. key = this.getKey(o);
  152. }
  153. var old = this.map[key];
  154. if(typeof key == 'undefined' || key === null || typeof old == 'undefined'){
  155. return this.add(key, o);
  156. }
  157. var index = this.indexOfKey(key);
  158. this.items[index] = o;
  159. this.map[key] = o;
  160. this.fireEvent('replace', key, old, o);
  161. return o;
  162. },
  163. <span id='Ext-util-MixedCollection-method-addAll'> /**
  164. </span> * Adds all elements of an Array or an Object to the collection.
  165. * @param {Object/Array} objs An Object containing properties which will be added
  166. * to the collection, or an Array of values, each of which are added to the collection.
  167. * Functions references will be added to the collection if &lt;code&gt;{@link #allowFunctions}&lt;/code&gt;
  168. * has been set to &lt;tt&gt;true&lt;/tt&gt;.
  169. */
  170. addAll : function(objs){
  171. if(arguments.length &gt; 1 || Ext.isArray(objs)){
  172. var args = arguments.length &gt; 1 ? arguments : objs;
  173. for(var i = 0, len = args.length; i &lt; len; i++){
  174. this.add(args[i]);
  175. }
  176. }else{
  177. for(var key in objs){
  178. if(this.allowFunctions || typeof objs[key] != 'function'){
  179. this.add(key, objs[key]);
  180. }
  181. }
  182. }
  183. },
  184. <span id='Ext-util-MixedCollection-method-each'> /**
  185. </span> * Executes the specified function once for every item in the collection, passing the following arguments:
  186. * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  187. * &lt;li&gt;&lt;b&gt;item&lt;/b&gt; : Mixed&lt;p class=&quot;sub-desc&quot;&gt;The collection item&lt;/p&gt;&lt;/li&gt;
  188. * &lt;li&gt;&lt;b&gt;index&lt;/b&gt; : Number&lt;p class=&quot;sub-desc&quot;&gt;The item's index&lt;/p&gt;&lt;/li&gt;
  189. * &lt;li&gt;&lt;b&gt;length&lt;/b&gt; : Number&lt;p class=&quot;sub-desc&quot;&gt;The total number of items in the collection&lt;/p&gt;&lt;/li&gt;
  190. * &lt;/ul&gt;&lt;/div&gt;
  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 (&lt;code&gt;this&lt;/code&gt; reference) in which the function is executed. Defaults to the current item in the iteration.
  194. */
  195. each : function(fn, scope){
  196. var items = [].concat(this.items); // each safe for removal
  197. for(var i = 0, len = items.length; i &lt; len; i++){
  198. if(fn.call(scope || items[i], items[i], i, len) === false){
  199. break;
  200. }
  201. }
  202. },
  203. <span id='Ext-util-MixedCollection-method-eachKey'> /**
  204. </span> * Executes the specified function once for every key in the collection, passing each
  205. * key, and its associated item as the first two parameters.
  206. * @param {Function} fn The function to execute for each item.
  207. * @param {Object} scope (optional) The scope (&lt;code&gt;this&lt;/code&gt; reference) in which the function is executed. Defaults to the browser window.
  208. */
  209. eachKey : function(fn, scope){
  210. for(var i = 0, len = this.keys.length; i &lt; len; i++){
  211. fn.call(scope || window, this.keys[i], this.items[i], i, len);
  212. }
  213. },
  214. <span id='Ext-util-MixedCollection-method-find'> /**
  215. </span> * Returns the first item in the collection which elicits a true return value from the
  216. * passed selection function.
  217. * @param {Function} fn The selection function to execute for each item.
  218. * @param {Object} scope (optional) The scope (&lt;code&gt;this&lt;/code&gt; reference) in which the function is executed. Defaults to the browser window.
  219. * @return {Object} The first item in the collection which returned true from the selection function.
  220. */
  221. find : function(fn, scope){
  222. for(var i = 0, len = this.items.length; i &lt; len; i++){
  223. if(fn.call(scope || window, this.items[i], this.keys[i])){
  224. return this.items[i];
  225. }
  226. }
  227. return null;
  228. },
  229. <span id='Ext-util-MixedCollection-method-insert'> /**
  230. </span> * Inserts an item at the specified index in the collection. Fires the {@link #add} event when complete.
  231. * @param {Number} index The index to insert the item at.
  232. * @param {String} key The key to associate with the new item, or the item itself.
  233. * @param {Object} o (optional) If the second parameter was a key, the new item.
  234. * @return {Object} The item inserted.
  235. */
  236. insert : function(index, key, o){
  237. if(arguments.length == 2){
  238. o = arguments[1];
  239. key = this.getKey(o);
  240. }
  241. if(this.containsKey(key)){
  242. this.suspendEvents();
  243. this.removeKey(key);
  244. this.resumeEvents();
  245. }
  246. if(index &gt;= this.length){
  247. return this.add(key, o);
  248. }
  249. this.length++;
  250. this.items.splice(index, 0, o);
  251. if(typeof key != 'undefined' &amp;&amp; key !== null){
  252. this.map[key] = o;
  253. }
  254. this.keys.splice(index, 0, key);
  255. this.fireEvent('add', index, o, key);
  256. return o;
  257. },
  258. <span id='Ext-util-MixedCollection-method-remove'> /**
  259. </span> * Remove an item from the collection.
  260. * @param {Object} o The item to remove.
  261. * @return {Object} The item removed or false if no item was removed.
  262. */
  263. remove : function(o){
  264. return this.removeAt(this.indexOf(o));
  265. },
  266. <span id='Ext-util-MixedCollection-method-removeAt'> /**
  267. </span> * Remove an item from a specified index in the collection. Fires the {@link #remove} event when complete.
  268. * @param {Number} index The index within the collection of the item to remove.
  269. * @return {Object} The item removed or false if no item was removed.
  270. */
  271. removeAt : function(index){
  272. if(index &lt; this.length &amp;&amp; index &gt;= 0){
  273. this.length--;
  274. var o = this.items[index];
  275. this.items.splice(index, 1);
  276. var key = this.keys[index];
  277. if(typeof key != 'undefined'){
  278. delete this.map[key];
  279. }
  280. this.keys.splice(index, 1);
  281. this.fireEvent('remove', o, key);
  282. return o;
  283. }
  284. return false;
  285. },
  286. <span id='Ext-util-MixedCollection-method-removeKey'> /**
  287. </span> * Removed an item associated with the passed key fom the collection.
  288. * @param {String} key The key of the item to remove.
  289. * @return {Object} The item removed or false if no item was removed.
  290. */
  291. removeKey : function(key){
  292. return this.removeAt(this.indexOfKey(key));
  293. },
  294. <span id='Ext-util-MixedCollection-method-getCount'> /**
  295. </span> * Returns the number of items in the collection.
  296. * @return {Number} the number of items in the collection.
  297. */
  298. getCount : function(){
  299. return this.length;
  300. },
  301. <span id='Ext-util-MixedCollection-method-indexOf'> /**
  302. </span> * Returns index within the collection of the passed Object.
  303. * @param {Object} o The item to find the index of.
  304. * @return {Number} index of the item. Returns -1 if not found.
  305. */
  306. indexOf : function(o){
  307. return this.items.indexOf(o);
  308. },
  309. <span id='Ext-util-MixedCollection-method-indexOfKey'> /**
  310. </span> * Returns index within the collection of the passed key.
  311. * @param {String} key The key to find the index of.
  312. * @return {Number} index of the key.
  313. */
  314. indexOfKey : function(key){
  315. return this.keys.indexOf(key);
  316. },
  317. <span id='Ext-util-MixedCollection-method-item'> /**
  318. </span> * Returns the item associated with the passed key OR index.
  319. * Key has priority over index. This is the equivalent
  320. * of calling {@link #key} first, then if nothing matched calling {@link #itemAt}.
  321. * @param {String/Number} key The key or index of the item.
  322. * @return {Object} If the item is found, returns the item. If the item was not found, returns &lt;tt&gt;undefined&lt;/tt&gt;.
  323. * If an item was found, but is a Class, returns &lt;tt&gt;null&lt;/tt&gt;.
  324. */
  325. item : function(key){
  326. var mk = this.map[key],
  327. item = mk !== undefined ? mk : (typeof key == 'number') ? this.items[key] : undefined;
  328. return typeof item != 'function' || this.allowFunctions ? item : null; // for prototype!
  329. },
  330. <span id='Ext-util-MixedCollection-method-itemAt'> /**
  331. </span> * Returns the item at the specified index.
  332. * @param {Number} index The index of the item.
  333. * @return {Object} The item at the specified index.
  334. */
  335. itemAt : function(index){
  336. return this.items[index];
  337. },
  338. <span id='Ext-util-MixedCollection-method-key'> /**
  339. </span> * Returns the item associated with the passed key.
  340. * @param {String/Number} key The key of the item.
  341. * @return {Object} The item associated with the passed key.
  342. */
  343. key : function(key){
  344. return this.map[key];
  345. },
  346. <span id='Ext-util-MixedCollection-method-contains'> /**
  347. </span> * 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.
  350. */
  351. contains : function(o){
  352. return this.indexOf(o) != -1;
  353. },
  354. <span id='Ext-util-MixedCollection-method-containsKey'> /**
  355. </span> * Returns true if the collection contains the passed Object as a key.
  356. * @param {String} key The key to look for in the collection.
  357. * @return {Boolean} True if the collection contains the Object as a key.
  358. */
  359. containsKey : function(key){
  360. return typeof this.map[key] != 'undefined';
  361. },
  362. <span id='Ext-util-MixedCollection-method-clear'> /**
  363. </span> * Removes all items from the collection. Fires the {@link #clear} event when complete.
  364. */
  365. clear : function(){
  366. this.length = 0;
  367. this.items = [];
  368. this.keys = [];
  369. this.map = {};
  370. this.fireEvent('clear');
  371. },
  372. <span id='Ext-util-MixedCollection-method-first'> /**
  373. </span> * Returns the first item in the collection.
  374. * @return {Object} the first item in the collection..
  375. */
  376. first : function(){
  377. return this.items[0];
  378. },
  379. <span id='Ext-util-MixedCollection-method-last'> /**
  380. </span> * Returns the last item in the collection.
  381. * @return {Object} the last item in the collection..
  382. */
  383. last : function(){
  384. return this.items[this.length-1];
  385. },
  386. <span id='Ext-util-MixedCollection-method-_sort'> /**
  387. </span> * @private
  388. * Performs the actual sorting based on a direction and a sorting function. Internally,
  389. * this creates a temporary array of all items in the MixedCollection, sorts it and then writes
  390. * the sorted array data back into this.items and this.keys
  391. * @param {String} property Property to sort by ('key', 'value', or 'index')
  392. * @param {String} dir (optional) Direction to sort 'ASC' or 'DESC'. Defaults to 'ASC'.
  393. * @param {Function} fn (optional) Comparison function that defines the sort order.
  394. * Defaults to sorting by numeric value.
  395. */
  396. _sort : function(property, dir, fn){
  397. var i, len,
  398. dsc = String(dir).toUpperCase() == 'DESC' ? -1 : 1,
  399. //this is a temporary array used to apply the sorting function
  400. c = [],
  401. keys = this.keys,
  402. items = this.items;
  403. //default to a simple sorter function if one is not provided
  404. fn = fn || function(a, b) {
  405. return a - b;
  406. };
  407. //copy all the items into a temporary array, which we will sort
  408. for(i = 0, len = items.length; i &lt; len; i++){
  409. c[c.length] = {
  410. key : keys[i],
  411. value: items[i],
  412. index: i
  413. };
  414. }
  415. //sort the temporary array
  416. c.sort(function(a, b){
  417. var v = fn(a[property], b[property]) * dsc;
  418. if(v === 0){
  419. v = (a.index &lt; b.index ? -1 : 1);
  420. }
  421. return v;
  422. });
  423. //copy the temporary array back into the main this.items and this.keys objects
  424. for(i = 0, len = c.length; i &lt; len; i++){
  425. items[i] = c[i].value;
  426. keys[i] = c[i].key;
  427. }
  428. this.fireEvent('sort', this);
  429. },
  430. <span id='Ext-util-MixedCollection-method-sort'> /**
  431. </span> * Sorts this collection by &lt;b&gt;item&lt;/b&gt; value with the passed comparison function.
  432. * @param {String} direction (optional) 'ASC' or 'DESC'. Defaults to 'ASC'.
  433. * @param {Function} fn (optional) Comparison function that defines the sort order.
  434. * Defaults to sorting by numeric value.
  435. */
  436. sort : function(dir, fn){
  437. this._sort('value', dir, fn);
  438. },
  439. <span id='Ext-util-MixedCollection-method-reorder'> /**
  440. </span> * Reorders each of the items based on a mapping from old index to new index. Internally this
  441. * just translates into a sort. The 'sort' event is fired whenever reordering has occured.
  442. * @param {Object} mapping Mapping from old item index to new item index
  443. */
  444. reorder: function(mapping) {
  445. this.suspendEvents();
  446. var items = this.items,
  447. index = 0,
  448. length = items.length,
  449. order = [],
  450. remaining = [],
  451. oldIndex;
  452. //object of {oldPosition: newPosition} reversed to {newPosition: oldPosition}
  453. for (oldIndex in mapping) {
  454. order[mapping[oldIndex]] = items[oldIndex];
  455. }
  456. for (index = 0; index &lt; length; index++) {
  457. if (mapping[index] == undefined) {
  458. remaining.push(items[index]);
  459. }
  460. }
  461. for (index = 0; index &lt; length; index++) {
  462. if (order[index] == undefined) {
  463. order[index] = remaining.shift();
  464. }
  465. }
  466. this.clear();
  467. this.addAll(order);
  468. this.resumeEvents();
  469. this.fireEvent('sort', this);
  470. },
  471. <span id='Ext-util-MixedCollection-method-keySort'> /**
  472. </span> * Sorts this collection by &lt;b&gt;key&lt;/b&gt;s.
  473. * @param {String} direction (optional) 'ASC' or 'DESC'. Defaults to 'ASC'.
  474. * @param {Function} fn (optional) Comparison function that defines the sort order.
  475. * Defaults to sorting by case insensitive string.
  476. */
  477. keySort : function(dir, fn){
  478. this._sort('key', dir, fn || function(a, b){
  479. var v1 = String(a).toUpperCase(), v2 = String(b).toUpperCase();
  480. return v1 &gt; v2 ? 1 : (v1 &lt; v2 ? -1 : 0);
  481. });
  482. },
  483. <span id='Ext-util-MixedCollection-method-getRange'> /**
  484. </span> * Returns a range of items in this collection
  485. * @param {Number} startIndex (optional) The starting index. Defaults to 0.
  486. * @param {Number} endIndex (optional) The ending index. Defaults to the last item.
  487. * @return {Array} An array of items
  488. */
  489. getRange : function(start, end){
  490. var items = this.items;
  491. if(items.length &lt; 1){
  492. return [];
  493. }
  494. start = start || 0;
  495. end = Math.min(typeof end == 'undefined' ? this.length-1 : end, this.length-1);
  496. var i, r = [];
  497. if(start &lt;= end){
  498. for(i = start; i &lt;= end; i++) {
  499. r[r.length] = items[i];
  500. }
  501. }else{
  502. for(i = start; i &gt;= end; i--) {
  503. r[r.length] = items[i];
  504. }
  505. }
  506. return r;
  507. },
  508. <span id='Ext-util-MixedCollection-method-filter'> /**
  509. </span> * Filter the &lt;i&gt;objects&lt;/i&gt; in this collection by a specific property.
  510. * Returns a new collection that has been filtered.
  511. * @param {String} property A property on your objects
  512. * @param {String/RegExp} value Either string that the property values
  513. * should start with or a RegExp to test against the property
  514. * @param {Boolean} anyMatch (optional) True to match any part of the string, not just the beginning
  515. * @param {Boolean} caseSensitive (optional) True for case sensitive comparison (defaults to False).
  516. * @return {MixedCollection} The new filtered collection
  517. */
  518. filter : function(property, value, anyMatch, caseSensitive){
  519. if(Ext.isEmpty(value, false)){
  520. return this.clone();
  521. }
  522. value = this.createValueMatcher(value, anyMatch, caseSensitive);
  523. return this.filterBy(function(o){
  524. return o &amp;&amp; value.test(o[property]);
  525. });
  526. },
  527. <span id='Ext-util-MixedCollection-method-filterBy'> /**
  528. </span> * Filter by a function. Returns a &lt;i&gt;new&lt;/i&gt; collection that has been filtered.
  529. * The passed function will be called with each object in the collection.
  530. * If the function returns true, the value is included otherwise it is filtered.
  531. * @param {Function} fn The function to be called, it will receive the args o (the object), k (the key)
  532. * @param {Object} scope (optional) The scope (&lt;code&gt;this&lt;/code&gt; reference) in which the function is executed. Defaults to this MixedCollection.
  533. * @return {MixedCollection} The new filtered collection
  534. */
  535. filterBy : function(fn, scope){
  536. var r = new Ext.util.MixedCollection();
  537. r.getKey = this.getKey;
  538. var k = this.keys, it = this.items;
  539. for(var i = 0, len = it.length; i &lt; len; i++){
  540. if(fn.call(scope||this, it[i], k[i])){
  541. r.add(k[i], it[i]);
  542. }
  543. }
  544. return r;
  545. },
  546. <span id='Ext-util-MixedCollection-method-findIndex'> /**
  547. </span> * Finds the index of the first matching object in this collection by a specific property/value.
  548. * @param {String} property The name of a property on your objects.
  549. * @param {String/RegExp} value A string that the property values
  550. * should start with or a RegExp to test against the property.
  551. * @param {Number} start (optional) The index to start searching at (defaults to 0).
  552. * @param {Boolean} anyMatch (optional) True to match any part of the string, not just the beginning.
  553. * @param {Boolean} caseSensitive (optional) True for case sensitive comparison.
  554. * @return {Number} The matched index or -1
  555. */
  556. findIndex : function(property, value, start, anyMatch, caseSensitive){
  557. if(Ext.isEmpty(value, false)){
  558. return -1;
  559. }
  560. value = this.createValueMatcher(value, anyMatch, caseSensitive);
  561. return this.findIndexBy(function(o){
  562. return o &amp;&amp; value.test(o[property]);
  563. }, null, start);
  564. },
  565. <span id='Ext-util-MixedCollection-method-findIndexBy'> /**
  566. </span> * Find the index of the first matching object in this collection by a function.
  567. * If the function returns &lt;i&gt;true&lt;/i&gt; it is considered a match.
  568. * @param {Function} fn The function to be called, it will receive the args o (the object), k (the key).
  569. * @param {Object} scope (optional) The scope (&lt;code&gt;this&lt;/code&gt; reference) in which the function is executed. Defaults to this MixedCollection.
  570. * @param {Number} start (optional) The index to start searching at (defaults to 0).
  571. * @return {Number} The matched index or -1
  572. */
  573. findIndexBy : function(fn, scope, start){
  574. var k = this.keys, it = this.items;
  575. for(var i = (start||0), len = it.length; i &lt; len; i++){
  576. if(fn.call(scope||this, it[i], k[i])){
  577. return i;
  578. }
  579. }
  580. return -1;
  581. },
  582. <span id='Ext-util-MixedCollection-method-createValueMatcher'> /**
  583. </span> * Returns a regular expression based on the given value and matching options. This is used internally for finding and filtering,
  584. * and by Ext.data.Store#filter
  585. * @private
  586. * @param {String} value The value to create the regex for. This is escaped using Ext.escapeRe
  587. * @param {Boolean} anyMatch True to allow any match - no regex start/end line anchors will be added. Defaults to false
  588. * @param {Boolean} caseSensitive True to make the regex case sensitive (adds 'i' switch to regex). Defaults to false.
  589. * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false. Ignored if anyMatch is true.
  590. */
  591. createValueMatcher : function(value, anyMatch, caseSensitive, exactMatch) {
  592. if (!value.exec) { // not a regex
  593. var er = Ext.escapeRe;
  594. value = String(value);
  595. if (anyMatch === true) {
  596. value = er(value);
  597. } else {
  598. value = '^' + er(value);
  599. if (exactMatch === true) {
  600. value += '$';
  601. }
  602. }
  603. value = new RegExp(value, caseSensitive ? '' : 'i');
  604. }
  605. return value;
  606. },
  607. <span id='Ext-util-MixedCollection-method-clone'> /**
  608. </span> * Creates a shallow copy of this collection
  609. * @return {MixedCollection}
  610. */
  611. clone : function(){
  612. var r = new Ext.util.MixedCollection();
  613. var k = this.keys, it = this.items;
  614. for(var i = 0, len = it.length; i &lt; len; i++){
  615. r.add(k[i], it[i]);
  616. }
  617. r.getKey = this.getKey;
  618. return r;
  619. }
  620. });
  621. <span id='Ext-util-MixedCollection-method-get'>/**
  622. </span> * This method calls {@link #item item()}.
  623. * Returns the item associated with the passed key OR index. Key has priority
  624. * over index. This is the equivalent of calling {@link #key} first, then if
  625. * nothing matched calling {@link #itemAt}.
  626. * @param {String/Number} key The key or index of the item.
  627. * @return {Object} If the item is found, returns the item. If the item was
  628. * not found, returns &lt;tt&gt;undefined&lt;/tt&gt;. If an item was found, but is a Class,
  629. * returns &lt;tt&gt;null&lt;/tt&gt;.
  630. */
  631. Ext.util.MixedCollection.prototype.get = Ext.util.MixedCollection.prototype.item;
  632. </pre>
  633. </body>
  634. </html>