95c4b8e05287b40aef3db129af73344b48d43eb9.svn-base 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /* Fix for Opera, which does not seem to include the map function on Array's */
  7. if (!Array.prototype.map) {
  8. Array.prototype.map = function(fun) {
  9. var len = this.length;
  10. if (typeof fun != "function") {
  11. throw new TypeError();
  12. }
  13. var res = new Array(len);
  14. var thisp = arguments[1];
  15. for (var i = 0; i < len; i++) {
  16. if (i in this) {
  17. res[i] = fun.call(thisp, this[i], i, this);
  18. }
  19. }
  20. return res;
  21. };
  22. }
  23. /* Paging Memory Proxy, allows to use paging grid with in memory dataset */
  24. Ext.data.PagingMemoryProxy = function(data) {
  25. Ext.data.PagingMemoryProxy.superclass.constructor.call(this);
  26. this.data = data;
  27. };
  28. Ext.extend(Ext.data.PagingMemoryProxy, Ext.data.MemoryProxy, {
  29. load : function(params, reader, callback, scope, arg) {
  30. params = params || {};
  31. var result;
  32. try {
  33. result = reader.readRecords(this.data);
  34. } catch (e) {
  35. this.fireEvent("loadexception", this, arg, null, e);
  36. callback.call(scope, null, arg, false);
  37. return;
  38. }
  39. // filtering
  40. if (params.filter !== undefined) {
  41. result.records = result.records.filter(function(el) {
  42. if (typeof(el) == "object") {
  43. var att = params.filterCol || 0;
  44. return String(el.data[att])
  45. .match(params.filter)
  46. ? true
  47. : false;
  48. } else {
  49. return String(el).match(params.filter)
  50. ? true
  51. : false;
  52. }
  53. });
  54. result.totalRecords = result.records.length;
  55. }
  56. // sorting
  57. if (params.sort !== undefined) {
  58. // use integer as params.sort to specify column, since
  59. // arrays are not named
  60. // params.sort=0; would also match a array without columns
  61. var dir = String(params.dir).toUpperCase() == "DESC"
  62. ? -1
  63. : 1;
  64. var fn = function(r1, r2) {
  65. return r1 < r2;
  66. };
  67. result.records.sort(function(a, b) {
  68. var v = 0;
  69. if (typeof(a) == "object") {
  70. v = fn(a.data[params.sort],
  71. b.data[params.sort])
  72. * dir;
  73. } else {
  74. v = fn(a, b) * dir;
  75. }
  76. if (v == 0) {
  77. v = (a.index < b.index ? -1 : 1);
  78. }
  79. return v;
  80. });
  81. }
  82. // paging (use undefined cause start can also be 0 (thus false))
  83. if (params.start !== undefined && params.limit !== undefined) {
  84. result.records = result.records.slice(params.start,
  85. params.start + params.limit);
  86. }
  87. callback.call(scope, result, arg, true);
  88. }
  89. });