c2a5177d11e2a0a02a4667f3dff5f501b0e69e98.svn-base 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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.data.Store = function(A) {
  7. this.data = new Ext.util.MixedCollection(false);
  8. this.data.getKey = function(B) {
  9. return B.id
  10. };
  11. this.baseParams = {};
  12. this.paramNames = {
  13. "start" : "start",
  14. "limit" : "limit",
  15. "sort" : "sort",
  16. "dir" : "dir"
  17. };
  18. if (A && A.data) {
  19. this.inlineData = A.data;
  20. delete A.data
  21. }
  22. Ext.apply(this, A);
  23. if (this.url && !this.proxy) {
  24. this.proxy = new Ext.data.HttpProxy({
  25. url : this.url
  26. })
  27. }
  28. if (this.reader) {
  29. if (!this.recordType) {
  30. this.recordType = this.reader.recordType
  31. }
  32. if (this.reader.onMetaChange) {
  33. this.reader.onMetaChange = this.onMetaChange.createDelegate(this)
  34. }
  35. }
  36. if (this.recordType) {
  37. this.fields = this.recordType.prototype.fields
  38. }
  39. this.modified = [];
  40. this.addEvents("datachanged", "metachange", "add", "remove", "update",
  41. "clear", "beforeload", "load", "loadexception");
  42. if (this.proxy) {
  43. this.relayEvents(this.proxy, ["loadexception"])
  44. }
  45. this.sortToggle = {};
  46. Ext.data.Store.superclass.constructor.call(this);
  47. if (this.storeId || this.id) {
  48. Ext.StoreMgr.register(this)
  49. }
  50. if (this.inlineData) {
  51. this.loadData(this.inlineData);
  52. delete this.inlineData
  53. } else {
  54. if (this.autoLoad) {
  55. this.load.defer(10, this, [typeof this.autoLoad == "object"
  56. ? this.autoLoad
  57. : undefined])
  58. }
  59. }
  60. };
  61. Ext.extend(Ext.data.Store, Ext.util.Observable, {
  62. remoteSort : false,
  63. pruneModifiedRecords : false,
  64. lastOptions : null,
  65. destroy : function() {
  66. if (this.id) {
  67. Ext.StoreMgr.unregister(this)
  68. }
  69. this.data = null;
  70. this.purgeListeners()
  71. },
  72. add : function(B) {
  73. B = [].concat(B);
  74. if (B.length < 1) {
  75. return
  76. }
  77. for (var D = 0, A = B.length; D < A; D++) {
  78. B[D].join(this)
  79. }
  80. var C = this.data.length;
  81. this.data.addAll(B);
  82. if (this.snapshot) {
  83. this.snapshot.addAll(B)
  84. }
  85. this.fireEvent("add", this, B, C)
  86. },
  87. addSorted : function(A) {
  88. var B = this.findInsertIndex(A);
  89. this.insert(B, A)
  90. },
  91. remove : function(A) {
  92. var B = this.data.indexOf(A);
  93. this.data.removeAt(B);
  94. if (this.pruneModifiedRecords) {
  95. this.modified.remove(A)
  96. }
  97. if (this.snapshot) {
  98. this.snapshot.remove(A)
  99. }
  100. this.fireEvent("remove", this, A, B)
  101. },
  102. removeAll : function() {
  103. this.data.clear();
  104. if (this.snapshot) {
  105. this.snapshot.clear()
  106. }
  107. if (this.pruneModifiedRecords) {
  108. this.modified = []
  109. }
  110. this.fireEvent("clear", this)
  111. },
  112. insert : function(C, B) {
  113. B = [].concat(B);
  114. for (var D = 0, A = B.length; D < A; D++) {
  115. this.data.insert(C, B[D]);
  116. B[D].join(this)
  117. }
  118. this.fireEvent("add", this, B, C)
  119. },
  120. indexOf : function(A) {
  121. return this.data.indexOf(A)
  122. },
  123. indexOfId : function(A) {
  124. return this.data.indexOfKey(A)
  125. },
  126. getById : function(A) {
  127. return this.data.key(A)
  128. },
  129. getAt : function(A) {
  130. return this.data.itemAt(A)
  131. },
  132. getRange : function(B, A) {
  133. return this.data.getRange(B, A)
  134. },
  135. storeOptions : function(A) {
  136. A = Ext.apply({}, A);
  137. delete A.callback;
  138. delete A.scope;
  139. this.lastOptions = A
  140. },
  141. load : function(B) {
  142. B = B || {};
  143. if (this.fireEvent("beforeload", this, B) !== false) {
  144. this.storeOptions(B);
  145. var C = Ext.apply(B.params || {}, this.baseParams);
  146. if (this.sortInfo && this.remoteSort) {
  147. var A = this.paramNames;
  148. C[A["sort"]] = this.sortInfo.field;
  149. C[A["dir"]] = this.sortInfo.direction
  150. }
  151. this.proxy.load(C, this.reader, this.loadRecords, this, B)
  152. }
  153. },
  154. reload : function(A) {
  155. this.load(Ext.applyIf(A || {}, this.lastOptions))
  156. },
  157. loadRecords : function(G, B, F) {
  158. if (!G || F === false) {
  159. if (F !== false) {
  160. this.fireEvent("load", this, [], B)
  161. }
  162. if (B.callback) {
  163. B.callback.call(B.scope || this, [], B, false)
  164. }
  165. return
  166. }
  167. var E = G.records, D = G.totalRecords || E.length;
  168. if (!B || B.add !== true) {
  169. if (this.pruneModifiedRecords) {
  170. this.modified = []
  171. }
  172. for (var C = 0, A = E.length; C < A; C++) {
  173. E[C].join(this)
  174. }
  175. if (this.snapshot) {
  176. this.data = this.snapshot;
  177. delete this.snapshot
  178. }
  179. this.data.clear();
  180. this.data.addAll(E);
  181. this.totalLength = D;
  182. this.applySort();
  183. this.fireEvent("datachanged", this)
  184. } else {
  185. this.totalLength = Math.max(D, this.data.length + E.length);
  186. this.add(E)
  187. }
  188. this.fireEvent("load", this, E, B);
  189. if (B.callback) {
  190. B.callback.call(B.scope || this, E, B, true)
  191. }
  192. },
  193. loadData : function(C, A) {
  194. var B = this.reader.readRecords(C);
  195. this.loadRecords(B, {
  196. add : A
  197. }, true)
  198. },
  199. getCount : function() {
  200. return this.data.length || 0
  201. },
  202. getTotalCount : function() {
  203. return this.totalLength || 0
  204. },
  205. getSortState : function() {
  206. return this.sortInfo
  207. },
  208. applySort : function() {
  209. if (this.sortInfo && !this.remoteSort) {
  210. var A = this.sortInfo, B = A.field;
  211. this.sortData(B, A.direction)
  212. }
  213. },
  214. sortData : function(C, D) {
  215. D = D || "ASC";
  216. var A = this.fields.get(C).sortType;
  217. var B = function(F, E) {
  218. var H = A(F.data[C]), G = A(E.data[C]);
  219. return H > G ? 1 : (H < G ? -1 : 0)
  220. };
  221. this.data.sort(D, B);
  222. if (this.snapshot && this.snapshot != this.data) {
  223. this.snapshot.sort(D, B)
  224. }
  225. },
  226. setDefaultSort : function(B, A) {
  227. A = A ? A.toUpperCase() : "ASC";
  228. this.sortInfo = {
  229. field : B,
  230. direction : A
  231. };
  232. this.sortToggle[B] = A
  233. },
  234. sort : function(C, A) {
  235. var B = this.fields.get(C);
  236. if (!B) {
  237. return false
  238. }
  239. if (!A) {
  240. if (this.sortInfo && this.sortInfo.field == B.name) {
  241. A = (this.sortToggle[B.name] || "ASC").toggle("ASC",
  242. "DESC")
  243. } else {
  244. A = B.sortDir
  245. }
  246. }
  247. this.sortToggle[B.name] = A;
  248. this.sortInfo = {
  249. field : B.name,
  250. direction : A
  251. };
  252. if (!this.remoteSort) {
  253. this.applySort();
  254. this.fireEvent("datachanged", this)
  255. } else {
  256. this.load(this.lastOptions)
  257. }
  258. },
  259. each : function(B, A) {
  260. this.data.each(B, A)
  261. },
  262. getModifiedRecords : function() {
  263. return this.modified
  264. },
  265. createFilterFn : function(C, B, D, A) {
  266. if (Ext.isEmpty(B, false)) {
  267. return false
  268. }
  269. B = this.data.createValueMatcher(B, D, A);
  270. return function(E) {
  271. return B.test(E.data[C])
  272. }
  273. },
  274. sum : function(E, F, A) {
  275. var C = this.data.items, B = 0;
  276. F = F || 0;
  277. A = (A || A === 0) ? A : C.length - 1;
  278. for (var D = F; D <= A; D++) {
  279. B += (C[D].data[E] || 0)
  280. }
  281. return B
  282. },
  283. filter : function(D, C, E, A) {
  284. var B = this.createFilterFn(D, C, E, A);
  285. return B ? this.filterBy(B) : this.clearFilter()
  286. },
  287. filterBy : function(B, A) {
  288. this.snapshot = this.snapshot || this.data;
  289. this.data = this.queryBy(B, A || this);
  290. this.fireEvent("datachanged", this)
  291. },
  292. query : function(D, C, E, A) {
  293. var B = this.createFilterFn(D, C, E, A);
  294. return B ? this.queryBy(B) : this.data.clone()
  295. },
  296. queryBy : function(B, A) {
  297. var C = this.snapshot || this.data;
  298. return C.filterBy(B, A || this)
  299. },
  300. find : function(D, C, F, E, A) {
  301. var B = this.createFilterFn(D, C, E, A);
  302. return B ? this.data.findIndexBy(B, null, F) : -1
  303. },
  304. findBy : function(B, A, C) {
  305. return this.data.findIndexBy(B, A, C)
  306. },
  307. collect : function(G, H, B) {
  308. var F = (B === true && this.snapshot)
  309. ? this.snapshot.items
  310. : this.data.items;
  311. var I, J, A = [], C = {};
  312. for (var D = 0, E = F.length; D < E; D++) {
  313. I = F[D].data[G];
  314. J = String(I);
  315. if ((H || !Ext.isEmpty(I)) && !C[J]) {
  316. C[J] = true;
  317. A[A.length] = I
  318. }
  319. }
  320. return A
  321. },
  322. clearFilter : function(A) {
  323. if (this.isFiltered()) {
  324. this.data = this.snapshot;
  325. delete this.snapshot;
  326. if (A !== true) {
  327. this.fireEvent("datachanged", this)
  328. }
  329. }
  330. },
  331. isFiltered : function() {
  332. return this.snapshot && this.snapshot != this.data
  333. },
  334. afterEdit : function(A) {
  335. if (this.modified.indexOf(A) == -1) {
  336. this.modified.push(A)
  337. }
  338. this.fireEvent("update", this, A, Ext.data.Record.EDIT)
  339. },
  340. afterReject : function(A) {
  341. this.modified.remove(A);
  342. this.fireEvent("update", this, A, Ext.data.Record.REJECT)
  343. },
  344. afterCommit : function(A) {
  345. this.modified.remove(A);
  346. this.fireEvent("update", this, A, Ext.data.Record.COMMIT)
  347. },
  348. commitChanges : function() {
  349. var B = this.modified.slice(0);
  350. this.modified = [];
  351. for (var C = 0, A = B.length; C < A; C++) {
  352. B[C].commit()
  353. }
  354. },
  355. rejectChanges : function() {
  356. var B = this.modified.slice(0);
  357. this.modified = [];
  358. for (var C = 0, A = B.length; C < A; C++) {
  359. B[C].reject()
  360. }
  361. },
  362. onMetaChange : function(B, A, C) {
  363. this.recordType = A;
  364. this.fields = A.prototype.fields;
  365. delete this.snapshot;
  366. this.sortInfo = B.sortInfo;
  367. this.modified = [];
  368. this.fireEvent("metachange", this, this.reader.meta)
  369. },
  370. findInsertIndex : function(A) {
  371. this.suspendEvents();
  372. var C = this.data.clone();
  373. this.data.add(A);
  374. this.applySort();
  375. var B = this.data.indexOf(A);
  376. this.data = C;
  377. this.resumeEvents();
  378. return B
  379. }
  380. });