MixedCollection-min.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. Ext.util.MixedCollection = function(B, A) {
  7. this.items = [];
  8. this.map = {};
  9. this.keys = [];
  10. this.length = 0;
  11. this.addEvents("clear", "add", "replace", "remove", "sort");
  12. this.allowFunctions = B === true;
  13. if (A) {
  14. this.getKey = A
  15. }
  16. Ext.util.MixedCollection.superclass.constructor.call(this)
  17. };
  18. Ext.extend(Ext.util.MixedCollection, Ext.util.Observable, {
  19. allowFunctions : false,
  20. add : function(B, C) {
  21. if (arguments.length == 1) {
  22. C = arguments[0];
  23. B = this.getKey(C)
  24. }
  25. if (typeof B == "undefined" || B === null) {
  26. this.length++;
  27. this.items.push(C);
  28. this.keys.push(null)
  29. } else {
  30. var A = this.map[B];
  31. if (A) {
  32. return this.replace(B, C)
  33. }
  34. this.length++;
  35. this.items.push(C);
  36. this.map[B] = C;
  37. this.keys.push(B)
  38. }
  39. this.fireEvent("add", this.length - 1, C, B);
  40. return C
  41. },
  42. getKey : function(A) {
  43. return A.id
  44. },
  45. replace : function(C, D) {
  46. if (arguments.length == 1) {
  47. D = arguments[0];
  48. C = this.getKey(D)
  49. }
  50. var A = this.item(C);
  51. if (typeof C == "undefined" || C === null || typeof A == "undefined") {
  52. return this.add(C, D)
  53. }
  54. var B = this.indexOfKey(C);
  55. this.items[B] = D;
  56. this.map[C] = D;
  57. this.fireEvent("replace", C, A, D);
  58. return D
  59. },
  60. addAll : function(E) {
  61. if (arguments.length > 1 || E instanceof Array) {
  62. var B = arguments.length > 1 ? arguments : E;
  63. for (var D = 0, A = B.length; D < A; D++) {
  64. this.add(B[D])
  65. }
  66. } else {
  67. for (var C in E) {
  68. if (this.allowFunctions || typeof E[C] != "function") {
  69. this.add(C, E[C])
  70. }
  71. }
  72. }
  73. },
  74. each : function(E, D) {
  75. var B = [].concat(this.items);
  76. for (var C = 0, A = B.length; C < A; C++) {
  77. if (E.call(D || B[C], B[C], C, A) === false) {
  78. break
  79. }
  80. }
  81. },
  82. eachKey : function(D, C) {
  83. for (var B = 0, A = this.keys.length; B < A; B++) {
  84. D.call(C || window, this.keys[B], this.items[B], B, A)
  85. }
  86. },
  87. find : function(D, C) {
  88. for (var B = 0, A = this.items.length; B < A; B++) {
  89. if (D.call(C || window, this.items[B], this.keys[B])) {
  90. return this.items[B]
  91. }
  92. }
  93. return null
  94. },
  95. insert : function(A, B, C) {
  96. if (arguments.length == 2) {
  97. C = arguments[1];
  98. B = this.getKey(C)
  99. }
  100. if (A >= this.length) {
  101. return this.add(B, C)
  102. }
  103. this.length++;
  104. this.items.splice(A, 0, C);
  105. if (typeof B != "undefined" && B != null) {
  106. this.map[B] = C
  107. }
  108. this.keys.splice(A, 0, B);
  109. this.fireEvent("add", A, C, B);
  110. return C
  111. },
  112. remove : function(A) {
  113. return this.removeAt(this.indexOf(A))
  114. },
  115. removeAt : function(A) {
  116. if (A < this.length && A >= 0) {
  117. this.length--;
  118. var C = this.items[A];
  119. this.items.splice(A, 1);
  120. var B = this.keys[A];
  121. if (typeof B != "undefined") {
  122. delete this.map[B]
  123. }
  124. this.keys.splice(A, 1);
  125. this.fireEvent("remove", C, B);
  126. return C
  127. }
  128. return false
  129. },
  130. removeKey : function(A) {
  131. return this.removeAt(this.indexOfKey(A))
  132. },
  133. getCount : function() {
  134. return this.length
  135. },
  136. indexOf : function(A) {
  137. return this.items.indexOf(A)
  138. },
  139. indexOfKey : function(A) {
  140. return this.keys.indexOf(A)
  141. },
  142. item : function(A) {
  143. var B = typeof this.map[A] != "undefined" ? this.map[A] : this.items[A];
  144. return typeof B != "function" || this.allowFunctions ? B : null
  145. },
  146. itemAt : function(A) {
  147. return this.items[A]
  148. },
  149. key : function(A) {
  150. return this.map[A]
  151. },
  152. contains : function(A) {
  153. return this.indexOf(A) != -1
  154. },
  155. containsKey : function(A) {
  156. return typeof this.map[A] != "undefined"
  157. },
  158. clear : function() {
  159. this.length = 0;
  160. this.items = [];
  161. this.keys = [];
  162. this.map = {};
  163. this.fireEvent("clear")
  164. },
  165. first : function() {
  166. return this.items[0]
  167. },
  168. last : function() {
  169. return this.items[this.length - 1]
  170. },
  171. _sort : function(I, A, H) {
  172. var C = String(A).toUpperCase() == "DESC" ? -1 : 1;
  173. H = H || function(K, J) {
  174. return K - J
  175. };
  176. var G = [], B = this.keys, F = this.items;
  177. for (var D = 0, E = F.length; D < E; D++) {
  178. G[G.length] = {
  179. key : B[D],
  180. value : F[D],
  181. index : D
  182. }
  183. }
  184. G.sort(function(K, J) {
  185. var L = H(K[I], J[I]) * C;
  186. if (L == 0) {
  187. L = (K.index < J.index ? -1 : 1)
  188. }
  189. return L
  190. });
  191. for (var D = 0, E = G.length; D < E; D++) {
  192. F[D] = G[D].value;
  193. B[D] = G[D].key
  194. }
  195. this.fireEvent("sort", this)
  196. },
  197. sort : function(A, B) {
  198. this._sort("value", A, B)
  199. },
  200. keySort : function(A, B) {
  201. this._sort("key", A, B || function(D, C) {
  202. return String(D).toUpperCase() - String(C).toUpperCase()
  203. })
  204. },
  205. getRange : function(E, A) {
  206. var B = this.items;
  207. if (B.length < 1) {
  208. return []
  209. }
  210. E = E || 0;
  211. A = Math.min(typeof A == "undefined" ? this.length - 1 : A, this.length
  212. - 1);
  213. var D = [];
  214. if (E <= A) {
  215. for (var C = E; C <= A; C++) {
  216. D[D.length] = B[C]
  217. }
  218. } else {
  219. for (var C = E; C >= A; C--) {
  220. D[D.length] = B[C]
  221. }
  222. }
  223. return D
  224. },
  225. filter : function(C, B, D, A) {
  226. if (Ext.isEmpty(B, false)) {
  227. return this.clone()
  228. }
  229. B = this.createValueMatcher(B, D, A);
  230. return this.filterBy(function(E) {
  231. return E && B.test(E[C])
  232. })
  233. },
  234. filterBy : function(F, E) {
  235. var G = new Ext.util.MixedCollection();
  236. G.getKey = this.getKey;
  237. var B = this.keys, D = this.items;
  238. for (var C = 0, A = D.length; C < A; C++) {
  239. if (F.call(E || this, D[C], B[C])) {
  240. G.add(B[C], D[C])
  241. }
  242. }
  243. return G
  244. },
  245. findIndex : function(C, B, E, D, A) {
  246. if (Ext.isEmpty(B, false)) {
  247. return -1
  248. }
  249. B = this.createValueMatcher(B, D, A);
  250. return this.findIndexBy(function(F) {
  251. return F && B.test(F[C])
  252. }, null, E)
  253. },
  254. findIndexBy : function(F, E, G) {
  255. var B = this.keys, D = this.items;
  256. for (var C = (G || 0), A = D.length; C < A; C++) {
  257. if (F.call(E || this, D[C], B[C])) {
  258. return C
  259. }
  260. }
  261. if (typeof G == "number" && G > 0) {
  262. for (var C = 0; C < G; C++) {
  263. if (F.call(E || this, D[C], B[C])) {
  264. return C
  265. }
  266. }
  267. }
  268. return -1
  269. },
  270. createValueMatcher : function(B, C, A) {
  271. if (!B.exec) {
  272. B = String(B);
  273. B = new RegExp((C === true ? "" : "^") + Ext.escapeRe(B), A
  274. ? ""
  275. : "i")
  276. }
  277. return B
  278. },
  279. clone : function() {
  280. var E = new Ext.util.MixedCollection();
  281. var B = this.keys, D = this.items;
  282. for (var C = 0, A = D.length; C < A; C++) {
  283. E.add(B[C], D[C])
  284. }
  285. E.getKey = this.getKey;
  286. return E
  287. }
  288. });
  289. Ext.util.MixedCollection.prototype.get = Ext.util.MixedCollection.prototype.item;