AbstractMixedCollection.html 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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-AbstractMixedCollection'>/**
  19. </span> * @class Ext.util.AbstractMixedCollection
  20. * @private
  21. */
  22. Ext.define('Ext.util.AbstractMixedCollection', {
  23. requires: ['Ext.util.Filter'],
  24. mixins: {
  25. observable: 'Ext.util.Observable'
  26. },
  27. <span id='Ext-util-AbstractMixedCollection-property-isMixedCollection'> /**
  28. </span> * @property {Boolean} isMixedCollection
  29. * `true` in this class to identify an object as an instantiated MixedCollection, or subclass thereof.
  30. */
  31. isMixedCollection: true,
  32. <span id='Ext-util-AbstractMixedCollection-property-generation'> /**
  33. </span> * @private Mutation counter which is incremented upon add and remove.
  34. */
  35. generation: 0,
  36. constructor: function(allowFunctions, keyFn) {
  37. var me = this;
  38. me.items = [];
  39. me.map = {};
  40. me.keys = [];
  41. me.length = 0;
  42. <span id='Ext-util-AbstractMixedCollection-event-clear'> /**
  43. </span> * @event clear
  44. * Fires when the collection is cleared.
  45. */
  46. <span id='Ext-util-AbstractMixedCollection-event-add'> /**
  47. </span> * @event add
  48. * Fires when an item is added to the collection.
  49. * @param {Number} index The index at which the item was added.
  50. * @param {Object} o The item added.
  51. * @param {String} key The key associated with the added item.
  52. */
  53. <span id='Ext-util-AbstractMixedCollection-event-replace'> /**
  54. </span> * @event replace
  55. * Fires when an item is replaced in the collection.
  56. * @param {String} key he key associated with the new added.
  57. * @param {Object} old The item being replaced.
  58. * @param {Object} new The new item.
  59. */
  60. <span id='Ext-util-AbstractMixedCollection-event-remove'> /**
  61. </span> * @event remove
  62. * Fires when an item is removed from the collection.
  63. * @param {Object} o The item being removed.
  64. * @param {String} key (optional) The key associated with the removed item.
  65. */
  66. me.allowFunctions = allowFunctions === true;
  67. if (keyFn) {
  68. me.getKey = keyFn;
  69. }
  70. me.mixins.observable.constructor.call(me);
  71. },
  72. <span id='Ext-util-AbstractMixedCollection-cfg-allowFunctions'> /**
  73. </span> * @cfg {Boolean} allowFunctions Specify &lt;code&gt;true&lt;/code&gt; if the {@link #addAll}
  74. * function should add function references to the collection. Defaults to
  75. * &lt;code&gt;false&lt;/code&gt;.
  76. */
  77. allowFunctions : false,
  78. <span id='Ext-util-AbstractMixedCollection-method-add'> /**
  79. </span> * Adds an item to the collection. Fires the {@link #event-add} event when complete.
  80. *
  81. * @param {String/Object} key The key to associate with the item, or the new item.
  82. *
  83. * If a {@link #getKey} implementation was specified for this MixedCollection,
  84. * or if the key of the stored items is in a property called `id`,
  85. * the MixedCollection will be able to *derive* the key for the new item.
  86. * In this case just pass the new item in this parameter.
  87. *
  88. * @param {Object} [o] The item to add.
  89. *
  90. * @return {Object} The item added.
  91. */
  92. add : function(key, obj){
  93. var me = this,
  94. myObj = obj,
  95. myKey = key,
  96. old;
  97. if (arguments.length == 1) {
  98. myObj = myKey;
  99. myKey = me.getKey(myObj);
  100. }
  101. if (typeof myKey != 'undefined' &amp;&amp; myKey !== null) {
  102. old = me.map[myKey];
  103. if (typeof old != 'undefined') {
  104. return me.replace(myKey, myObj);
  105. }
  106. me.map[myKey] = myObj;
  107. }
  108. me.generation++;
  109. me.length++;
  110. me.items.push(myObj);
  111. me.keys.push(myKey);
  112. if (me.hasListeners.add) {
  113. me.fireEvent('add', me.length - 1, myObj, myKey);
  114. }
  115. return myObj;
  116. },
  117. <span id='Ext-util-AbstractMixedCollection-method-getKey'> /**
  118. </span> * MixedCollection has a generic way to fetch keys if you implement getKey. The default implementation
  119. * simply returns &lt;b&gt;&lt;code&gt;item.id&lt;/code&gt;&lt;/b&gt; but you can provide your own implementation
  120. * to return a different value as in the following examples:&lt;pre&gt;&lt;code&gt;
  121. // normal way
  122. var mc = new Ext.util.MixedCollection();
  123. mc.add(someEl.dom.id, someEl);
  124. mc.add(otherEl.dom.id, otherEl);
  125. //and so on
  126. // using getKey
  127. var mc = new Ext.util.MixedCollection();
  128. mc.getKey = function(el){
  129. return el.dom.id;
  130. };
  131. mc.add(someEl);
  132. mc.add(otherEl);
  133. // or via the constructor
  134. var mc = new Ext.util.MixedCollection(false, function(el){
  135. return el.dom.id;
  136. });
  137. mc.add(someEl);
  138. mc.add(otherEl);
  139. * &lt;/code&gt;&lt;/pre&gt;
  140. * @param {Object} item The item for which to find the key.
  141. * @return {Object} The key for the passed item.
  142. */
  143. getKey : function(o){
  144. return o.id;
  145. },
  146. <span id='Ext-util-AbstractMixedCollection-method-replace'> /**
  147. </span> * Replaces an item in the collection. Fires the {@link #event-replace} event when complete.
  148. * @param {String} key &lt;p&gt;The key associated with the item to replace, or the replacement item.&lt;/p&gt;
  149. * &lt;p&gt;If you supplied a {@link #getKey} implementation for this MixedCollection, or if the key
  150. * of your stored items is in a property called &lt;code&gt;&lt;b&gt;id&lt;/b&gt;&lt;/code&gt;, then the MixedCollection
  151. * will be able to &lt;i&gt;derive&lt;/i&gt; the key of the replacement item. If you want to replace an item
  152. * with one having the same key value, then just pass the replacement item in this parameter.&lt;/p&gt;
  153. * @param o {Object} o (optional) If the first parameter passed was a key, the item to associate
  154. * with that key.
  155. * @return {Object} The new item.
  156. */
  157. replace : function(key, o){
  158. var me = this,
  159. old,
  160. index;
  161. if (arguments.length == 1) {
  162. o = arguments[0];
  163. key = me.getKey(o);
  164. }
  165. old = me.map[key];
  166. if (typeof key == 'undefined' || key === null || typeof old == 'undefined') {
  167. return me.add(key, o);
  168. }
  169. me.generation++;
  170. index = me.indexOfKey(key);
  171. me.items[index] = o;
  172. me.map[key] = o;
  173. if (me.hasListeners.replace) {
  174. me.fireEvent('replace', key, old, o);
  175. }
  176. return o;
  177. },
  178. <span id='Ext-util-AbstractMixedCollection-method-addAll'> /**
  179. </span> * Adds all elements of an Array or an Object to the collection.
  180. * @param {Object/Array} objs An Object containing properties which will be added
  181. * to the collection, or an Array of values, each of which are added to the collection.
  182. * Functions references will be added to the collection if &lt;code&gt;{@link #allowFunctions}&lt;/code&gt;
  183. * has been set to &lt;code&gt;true&lt;/code&gt;.
  184. */
  185. addAll : function(objs){
  186. var me = this,
  187. i = 0,
  188. args,
  189. len,
  190. key;
  191. if (arguments.length &gt; 1 || Ext.isArray(objs)) {
  192. args = arguments.length &gt; 1 ? arguments : objs;
  193. for (len = args.length; i &lt; len; i++) {
  194. me.add(args[i]);
  195. }
  196. } else {
  197. for (key in objs) {
  198. if (objs.hasOwnProperty(key)) {
  199. if (me.allowFunctions || typeof objs[key] != 'function') {
  200. me.add(key, objs[key]);
  201. }
  202. }
  203. }
  204. }
  205. },
  206. <span id='Ext-util-AbstractMixedCollection-method-each'> /**
  207. </span> * Executes the specified function once for every item in the collection.
  208. * The function should return a boolean value.
  209. * Returning false from the function will stop the iteration.
  210. *
  211. * @param {Function} fn The function to execute for each item.
  212. * @param {Mixed} fn.item The collection item.
  213. * @param {Number} fn.index The index of item.
  214. * @param {Number} fn.len Total length of collection.
  215. * @param {Object} scope (optional) The scope (&lt;code&gt;this&lt;/code&gt; reference)
  216. * in which the function is executed. Defaults to the current item in the iteration.
  217. */
  218. each : function(fn, scope){
  219. var items = [].concat(this.items), // each safe for removal
  220. i = 0,
  221. len = items.length,
  222. item;
  223. for (; i &lt; len; i++) {
  224. item = items[i];
  225. if (fn.call(scope || item, item, i, len) === false) {
  226. break;
  227. }
  228. }
  229. },
  230. <span id='Ext-util-AbstractMixedCollection-method-eachKey'> /**
  231. </span> * Executes the specified function once for every key in the collection, passing each
  232. * key, and its associated item as the first two parameters.
  233. * @param {Function} fn The function to execute for each item.
  234. * @param {String} fn.key The key of collection item.
  235. * @param {Mixed} fn.item The collection item.
  236. * @param {Number} fn.index The index of item.
  237. * @param {Number} fn.len Total length of collection.
  238. * @param {Object} scope (optional) The scope (&lt;code&gt;this&lt;/code&gt; reference) in which the
  239. * function is executed. Defaults to the browser window.
  240. */
  241. eachKey : function(fn, scope){
  242. var keys = this.keys,
  243. items = this.items,
  244. i = 0,
  245. len = keys.length;
  246. for (; i &lt; len; i++) {
  247. fn.call(scope || window, keys[i], items[i], i, len);
  248. }
  249. },
  250. <span id='Ext-util-AbstractMixedCollection-method-findBy'> /**
  251. </span> * Returns the first item in the collection which elicits a true return value from the
  252. * passed selection function.
  253. * @param {Function} fn The selection function to execute for each item.
  254. * @param {Mixed} fn.item The collection item.
  255. * @param {String} fn.key The key of collection item.
  256. * @param {Object} scope (optional) The scope (&lt;code&gt;this&lt;/code&gt; reference) in which the
  257. * function is executed. Defaults to the browser window.
  258. * @return {Object} The first item in the collection which returned true from the selection
  259. * function, or null if none was found.
  260. */
  261. findBy : function(fn, scope) {
  262. var keys = this.keys,
  263. items = this.items,
  264. i = 0,
  265. len = items.length;
  266. for (; i &lt; len; i++) {
  267. if (fn.call(scope || window, items[i], keys[i])) {
  268. return items[i];
  269. }
  270. }
  271. return null;
  272. },
  273. //&lt;deprecated since=&quot;0.99&quot;&gt;
  274. find : function() {
  275. if (Ext.isDefined(Ext.global.console)) {
  276. Ext.global.console.warn('Ext.util.MixedCollection: find has been deprecated. Use findBy instead.');
  277. }
  278. return this.findBy.apply(this, arguments);
  279. },
  280. //&lt;/deprecated&gt;
  281. <span id='Ext-util-AbstractMixedCollection-method-insert'> /**
  282. </span> * Inserts an item at the specified index in the collection. Fires the {@link #event-add} event when complete.
  283. * @param {Number} index The index to insert the item at.
  284. * @param {String} key The key to associate with the new item, or the item itself.
  285. * @param {Object} o (optional) If the second parameter was a key, the new item.
  286. * @return {Object} The item inserted.
  287. */
  288. insert : function(index, key, obj){
  289. var me = this,
  290. myKey = key,
  291. myObj = obj;
  292. if (arguments.length == 2) {
  293. myObj = myKey;
  294. myKey = me.getKey(myObj);
  295. }
  296. if (me.containsKey(myKey)) {
  297. me.suspendEvents();
  298. me.removeAtKey(myKey);
  299. me.resumeEvents();
  300. }
  301. if (index &gt;= me.length) {
  302. return me.add(myKey, myObj);
  303. }
  304. me.generation++;
  305. me.length++;
  306. Ext.Array.splice(me.items, index, 0, myObj);
  307. if (typeof myKey != 'undefined' &amp;&amp; myKey !== null) {
  308. me.map[myKey] = myObj;
  309. }
  310. Ext.Array.splice(me.keys, index, 0, myKey);
  311. if (me.hasListeners.add) {
  312. me.fireEvent('add', index, myObj, myKey);
  313. }
  314. return myObj;
  315. },
  316. <span id='Ext-util-AbstractMixedCollection-method-remove'> /**
  317. </span> * Remove an item from the collection.
  318. * @param {Object} o The item to remove.
  319. * @return {Object} The item removed or false if no item was removed.
  320. */
  321. remove : function(o) {
  322. this.generation++;
  323. return this.removeAt(this.indexOf(o));
  324. },
  325. <span id='Ext-util-AbstractMixedCollection-method-removeAll'> /**
  326. </span> * Remove all items in the passed array from the collection.
  327. * @param {Array} items An array of items to be removed.
  328. * @return {Ext.util.MixedCollection} this object
  329. */
  330. removeAll : function(items) {
  331. items = [].concat(items);
  332. var i, iLen = items.length;
  333. for (i = 0; i &lt; iLen; i++) {
  334. this.remove(items[i]);
  335. }
  336. return this;
  337. },
  338. <span id='Ext-util-AbstractMixedCollection-method-removeAt'> /**
  339. </span> * Remove an item from a specified index in the collection. Fires the {@link #event-remove} event when complete.
  340. * @param {Number} index The index within the collection of the item to remove.
  341. * @return {Object} The item removed or false if no item was removed.
  342. */
  343. removeAt : function(index) {
  344. var me = this,
  345. o,
  346. key;
  347. if (index &lt; me.length &amp;&amp; index &gt;= 0) {
  348. me.length--;
  349. o = me.items[index];
  350. Ext.Array.erase(me.items, index, 1);
  351. key = me.keys[index];
  352. if (typeof key != 'undefined') {
  353. delete me.map[key];
  354. }
  355. Ext.Array.erase(me.keys, index, 1);
  356. if (me.hasListeners.remove) {
  357. me.fireEvent('remove', o, key);
  358. }
  359. me.generation++;
  360. return o;
  361. }
  362. return false;
  363. },
  364. <span id='Ext-util-AbstractMixedCollection-method-removeAtKey'> /**
  365. </span> * Removed an item associated with the passed key fom the collection.
  366. * @param {String} key The key of the item to remove.
  367. * @return {Object} The item removed or false if no item was removed.
  368. */
  369. removeAtKey : function(key){
  370. return this.removeAt(this.indexOfKey(key));
  371. },
  372. <span id='Ext-util-AbstractMixedCollection-method-getCount'> /**
  373. </span> * Returns the number of items in the collection.
  374. * @return {Number} the number of items in the collection.
  375. */
  376. getCount : function(){
  377. return this.length;
  378. },
  379. <span id='Ext-util-AbstractMixedCollection-method-indexOf'> /**
  380. </span> * Returns index within the collection of the passed Object.
  381. * @param {Object} o The item to find the index of.
  382. * @return {Number} index of the item. Returns -1 if not found.
  383. */
  384. indexOf : function(o){
  385. return Ext.Array.indexOf(this.items, o);
  386. },
  387. <span id='Ext-util-AbstractMixedCollection-method-indexOfKey'> /**
  388. </span> * Returns index within the collection of the passed key.
  389. * @param {String} key The key to find the index of.
  390. * @return {Number} index of the key.
  391. */
  392. indexOfKey : function(key){
  393. return Ext.Array.indexOf(this.keys, key);
  394. },
  395. <span id='Ext-util-AbstractMixedCollection-method-get'> /**
  396. </span> * Returns the item associated with the passed key OR index.
  397. * Key has priority over index. This is the equivalent
  398. * of calling {@link #getByKey} first, then if nothing matched calling {@link #getAt}.
  399. * @param {String/Number} key The key or index of the item.
  400. * @return {Object} If the item is found, returns the item. If the item was not found, returns &lt;code&gt;undefined&lt;/code&gt;.
  401. * If an item was found, but is a Class, returns &lt;code&gt;null&lt;/code&gt;.
  402. */
  403. get : function(key) {
  404. var me = this,
  405. mk = me.map[key],
  406. item = mk !== undefined ? mk : (typeof key == 'number') ? me.items[key] : undefined;
  407. return typeof item != 'function' || me.allowFunctions ? item : null; // for prototype!
  408. },
  409. <span id='Ext-util-AbstractMixedCollection-method-getAt'> /**
  410. </span> * Returns the item at the specified index.
  411. * @param {Number} index The index of the item.
  412. * @return {Object} The item at the specified index.
  413. */
  414. getAt : function(index) {
  415. return this.items[index];
  416. },
  417. <span id='Ext-util-AbstractMixedCollection-method-getByKey'> /**
  418. </span> * Returns the item associated with the passed key.
  419. * @param {String/Number} key The key of the item.
  420. * @return {Object} The item associated with the passed key.
  421. */
  422. getByKey : function(key) {
  423. return this.map[key];
  424. },
  425. <span id='Ext-util-AbstractMixedCollection-method-contains'> /**
  426. </span> * Returns true if the collection contains the passed Object as an item.
  427. * @param {Object} o The Object to look for in the collection.
  428. * @return {Boolean} True if the collection contains the Object as an item.
  429. */
  430. contains : function(o){
  431. return typeof this.map[this.getKey(o)] != 'undefined';
  432. },
  433. <span id='Ext-util-AbstractMixedCollection-method-containsKey'> /**
  434. </span> * Returns true if the collection contains the passed Object as a key.
  435. * @param {String} key The key to look for in the collection.
  436. * @return {Boolean} True if the collection contains the Object as a key.
  437. */
  438. containsKey : function(key){
  439. return typeof this.map[key] != 'undefined';
  440. },
  441. <span id='Ext-util-AbstractMixedCollection-method-clear'> /**
  442. </span> * Removes all items from the collection. Fires the {@link #event-clear} event when complete.
  443. */
  444. clear : function(){
  445. var me = this;
  446. me.length = 0;
  447. me.items = [];
  448. me.keys = [];
  449. me.map = {};
  450. me.generation++;
  451. if (me.hasListeners.clear) {
  452. me.fireEvent('clear');
  453. }
  454. },
  455. <span id='Ext-util-AbstractMixedCollection-method-first'> /**
  456. </span> * Returns the first item in the collection.
  457. * @return {Object} the first item in the collection..
  458. */
  459. first : function() {
  460. return this.items[0];
  461. },
  462. <span id='Ext-util-AbstractMixedCollection-method-last'> /**
  463. </span> * Returns the last item in the collection.
  464. * @return {Object} the last item in the collection..
  465. */
  466. last : function() {
  467. return this.items[this.length - 1];
  468. },
  469. <span id='Ext-util-AbstractMixedCollection-method-sum'> /**
  470. </span> * Collects all of the values of the given property and returns their sum
  471. * @param {String} property The property to sum by
  472. * @param {String} [root] 'root' property to extract the first argument from. This is used mainly when
  473. * summing fields in records, where the fields are all stored inside the 'data' object
  474. * @param {Number} [start=0] The record index to start at
  475. * @param {Number} [end=-1] The record index to end at
  476. * @return {Number} The total
  477. */
  478. sum: function(property, root, start, end) {
  479. var values = this.extractValues(property, root),
  480. length = values.length,
  481. sum = 0,
  482. i;
  483. start = start || 0;
  484. end = (end || end === 0) ? end : length - 1;
  485. for (i = start; i &lt;= end; i++) {
  486. sum += values[i];
  487. }
  488. return sum;
  489. },
  490. <span id='Ext-util-AbstractMixedCollection-method-collect'> /**
  491. </span> * Collects unique values of a particular property in this MixedCollection
  492. * @param {String} property The property to collect on
  493. * @param {String} root (optional) 'root' property to extract the first argument from. This is used mainly when
  494. * summing fields in records, where the fields are all stored inside the 'data' object
  495. * @param {Boolean} allowBlank (optional) Pass true to allow null, undefined or empty string values
  496. * @return {Array} The unique values
  497. */
  498. collect: function(property, root, allowNull) {
  499. var values = this.extractValues(property, root),
  500. length = values.length,
  501. hits = {},
  502. unique = [],
  503. value, strValue, i;
  504. for (i = 0; i &lt; length; i++) {
  505. value = values[i];
  506. strValue = String(value);
  507. if ((allowNull || !Ext.isEmpty(value)) &amp;&amp; !hits[strValue]) {
  508. hits[strValue] = true;
  509. unique.push(value);
  510. }
  511. }
  512. return unique;
  513. },
  514. <span id='Ext-util-AbstractMixedCollection-method-extractValues'> /**
  515. </span> * @private
  516. * Extracts all of the given property values from the items in the MC. Mainly used as a supporting method for
  517. * functions like sum and collect.
  518. * @param {String} property The property to extract
  519. * @param {String} root (optional) 'root' property to extract the first argument from. This is used mainly when
  520. * extracting field data from Model instances, where the fields are stored inside the 'data' object
  521. * @return {Array} The extracted values
  522. */
  523. extractValues: function(property, root) {
  524. var values = this.items;
  525. if (root) {
  526. values = Ext.Array.pluck(values, root);
  527. }
  528. return Ext.Array.pluck(values, property);
  529. },
  530. <span id='Ext-util-AbstractMixedCollection-method-getRange'> /**
  531. </span> * Returns a range of items in this collection
  532. * @param {Number} startIndex (optional) The starting index. Defaults to 0.
  533. * @param {Number} endIndex (optional) The ending index. Defaults to the last item.
  534. * @return {Array} An array of items
  535. */
  536. getRange : function(start, end){
  537. var me = this,
  538. items = me.items,
  539. range = [],
  540. i;
  541. if (items.length &lt; 1) {
  542. return range;
  543. }
  544. start = start || 0;
  545. end = Math.min(typeof end == 'undefined' ? me.length - 1 : end, me.length - 1);
  546. if (start &lt;= end) {
  547. for (i = start; i &lt;= end; i++) {
  548. range[range.length] = items[i];
  549. }
  550. } else {
  551. for (i = start; i &gt;= end; i--) {
  552. range[range.length] = items[i];
  553. }
  554. }
  555. return range;
  556. },
  557. <span id='Ext-util-AbstractMixedCollection-method-filter'> /**
  558. </span> * &lt;p&gt;Filters the objects in this collection by a set of {@link Ext.util.Filter Filter}s, or by a single
  559. * property/value pair with optional parameters for substring matching and case sensitivity. See
  560. * {@link Ext.util.Filter Filter} for an example of using Filter objects (preferred). Alternatively,
  561. * MixedCollection can be easily filtered by property like this:&lt;/p&gt;
  562. &lt;pre&gt;&lt;code&gt;
  563. //create a simple store with a few people defined
  564. var people = new Ext.util.MixedCollection();
  565. people.addAll([
  566. {id: 1, age: 25, name: 'Ed'},
  567. {id: 2, age: 24, name: 'Tommy'},
  568. {id: 3, age: 24, name: 'Arne'},
  569. {id: 4, age: 26, name: 'Aaron'}
  570. ]);
  571. //a new MixedCollection containing only the items where age == 24
  572. var middleAged = people.filter('age', 24);
  573. &lt;/code&gt;&lt;/pre&gt;
  574. *
  575. *
  576. * @param {Ext.util.Filter[]/String} property A property on your objects, or an array of {@link Ext.util.Filter Filter} objects
  577. * @param {String/RegExp} value Either string that the property values
  578. * should start with or a RegExp to test against the property
  579. * @param {Boolean} [anyMatch=false] True to match any part of the string, not just the beginning
  580. * @param {Boolean} [caseSensitive=false] True for case sensitive comparison.
  581. * @return {Ext.util.MixedCollection} The new filtered collection
  582. */
  583. filter : function(property, value, anyMatch, caseSensitive) {
  584. var filters = [],
  585. filterFn;
  586. //support for the simple case of filtering by property/value
  587. if (Ext.isString(property)) {
  588. filters.push(new Ext.util.Filter({
  589. property : property,
  590. value : value,
  591. anyMatch : anyMatch,
  592. caseSensitive: caseSensitive
  593. }));
  594. } else if (Ext.isArray(property) || property instanceof Ext.util.Filter) {
  595. filters = filters.concat(property);
  596. }
  597. //at this point we have an array of zero or more Ext.util.Filter objects to filter with,
  598. //so here we construct a function that combines these filters by ANDing them together
  599. filterFn = function(record) {
  600. var isMatch = true,
  601. length = filters.length,
  602. i,
  603. filter,
  604. fn,
  605. scope;
  606. for (i = 0; i &lt; length; i++) {
  607. filter = filters[i];
  608. fn = filter.filterFn;
  609. scope = filter.scope;
  610. isMatch = isMatch &amp;&amp; fn.call(scope, record);
  611. }
  612. return isMatch;
  613. };
  614. return this.filterBy(filterFn);
  615. },
  616. <span id='Ext-util-AbstractMixedCollection-method-filterBy'> /**
  617. </span> * Filter by a function. Returns a &lt;i&gt;new&lt;/i&gt; collection that has been filtered.
  618. * The passed function will be called with each object in the collection.
  619. * If the function returns true, the value is included otherwise it is filtered.
  620. * @param {Function} fn The function to be called.
  621. * @param {Mixed} fn.item The collection item.
  622. * @param {String} fn.key The key of collection item.
  623. * @param {Object} scope (optional) The scope (&lt;code&gt;this&lt;/code&gt; reference) in
  624. * which the function is executed. Defaults to this MixedCollection.
  625. * @return {Ext.util.MixedCollection} The new filtered collection
  626. */
  627. filterBy : function(fn, scope) {
  628. var me = this,
  629. newMC = new this.self(),
  630. keys = me.keys,
  631. items = me.items,
  632. length = items.length,
  633. i;
  634. newMC.getKey = me.getKey;
  635. for (i = 0; i &lt; length; i++) {
  636. if (fn.call(scope || me, items[i], keys[i])) {
  637. newMC.add(keys[i], items[i]);
  638. }
  639. }
  640. return newMC;
  641. },
  642. <span id='Ext-util-AbstractMixedCollection-method-findIndex'> /**
  643. </span> * Finds the index of the first matching object in this collection by a specific property/value.
  644. * @param {String} property The name of a property on your objects.
  645. * @param {String/RegExp} value A string that the property values
  646. * should start with or a RegExp to test against the property.
  647. * @param {Number} [start=0] The index to start searching at.
  648. * @param {Boolean} [anyMatch=false] True to match any part of the string, not just the beginning.
  649. * @param {Boolean} [caseSensitive=false] True for case sensitive comparison.
  650. * @return {Number} The matched index or -1
  651. */
  652. findIndex : function(property, value, start, anyMatch, caseSensitive){
  653. if(Ext.isEmpty(value, false)){
  654. return -1;
  655. }
  656. value = this.createValueMatcher(value, anyMatch, caseSensitive);
  657. return this.findIndexBy(function(o){
  658. return o &amp;&amp; value.test(o[property]);
  659. }, null, start);
  660. },
  661. <span id='Ext-util-AbstractMixedCollection-method-findIndexBy'> /**
  662. </span> * Find the index of the first matching object in this collection by a function.
  663. * If the function returns &lt;i&gt;true&lt;/i&gt; it is considered a match.
  664. * @param {Function} fn The function to be called.
  665. * @param {Mixed} fn.item The collection item.
  666. * @param {String} fn.key The key of collection item.
  667. * @param {Object} [scope] The scope (&lt;code&gt;this&lt;/code&gt; reference) in which the function is executed. Defaults to this MixedCollection.
  668. * @param {Number} [start=0] The index to start searching at.
  669. * @return {Number} The matched index or -1
  670. */
  671. findIndexBy : function(fn, scope, start){
  672. var me = this,
  673. keys = me.keys,
  674. items = me.items,
  675. i = start || 0,
  676. len = items.length;
  677. for (; i &lt; len; i++) {
  678. if (fn.call(scope || me, items[i], keys[i])) {
  679. return i;
  680. }
  681. }
  682. return -1;
  683. },
  684. <span id='Ext-util-AbstractMixedCollection-method-createValueMatcher'> /**
  685. </span> * Returns a regular expression based on the given value and matching options. This is used internally for finding and filtering,
  686. * and by Ext.data.Store#filter
  687. * @private
  688. * @param {String} value The value to create the regex for. This is escaped using Ext.escapeRe
  689. * @param {Boolean} anyMatch True to allow any match - no regex start/end line anchors will be added. Defaults to false
  690. * @param {Boolean} caseSensitive True to make the regex case sensitive (adds 'i' switch to regex). Defaults to false.
  691. * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false. Ignored if anyMatch is true.
  692. */
  693. createValueMatcher : function(value, anyMatch, caseSensitive, exactMatch) {
  694. if (!value.exec) { // not a regex
  695. var er = Ext.String.escapeRegex;
  696. value = String(value);
  697. if (anyMatch === true) {
  698. value = er(value);
  699. } else {
  700. value = '^' + er(value);
  701. if (exactMatch === true) {
  702. value += '$';
  703. }
  704. }
  705. value = new RegExp(value, caseSensitive ? '' : 'i');
  706. }
  707. return value;
  708. },
  709. <span id='Ext-util-AbstractMixedCollection-method-clone'> /**
  710. </span> * Creates a shallow copy of this collection
  711. * @return {Ext.util.MixedCollection}
  712. */
  713. clone : function() {
  714. var me = this,
  715. copy = new this.self(),
  716. keys = me.keys,
  717. items = me.items,
  718. i = 0,
  719. len = items.length;
  720. for(; i &lt; len; i++){
  721. copy.add(keys[i], items[i]);
  722. }
  723. copy.getKey = me.getKey;
  724. return copy;
  725. }
  726. });
  727. </pre>
  728. </body>
  729. </html>