Upgrade to ExtJS 3.1.0 - Released 12/16/2009
[extjs.git] / examples / ux / PagingMemoryProxy.js
1 /*!
2  * Ext JS Library 3.1.0
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7
8 /* Fix for Opera, which does not seem to include the map function on Array's */
9 if (!Array.prototype.map) {
10     Array.prototype.map = function(fun){
11         var len = this.length;
12         if (typeof fun != 'function') {
13             throw new TypeError();
14         }
15         var res = new Array(len);
16         var thisp = arguments[1];
17         for (var i = 0; i < len; i++) {
18             if (i in this) {
19                 res[i] = fun.call(thisp, this[i], i, this);
20             }
21         }
22         return res;
23     };
24 }
25
26 Ext.ns('Ext.ux.data');
27
28 /**
29  * @class Ext.ux.data.PagingMemoryProxy
30  * @extends Ext.data.MemoryProxy
31  * <p>Paging Memory Proxy, allows to use paging grid with in memory dataset</p>
32  */
33 Ext.ux.data.PagingMemoryProxy = Ext.extend(Ext.data.MemoryProxy, {
34     constructor : function(data){
35         Ext.ux.data.PagingMemoryProxy.superclass.constructor.call(this);
36         this.data = data;
37     },
38     doRequest : function(action, rs, params, reader, callback, scope, options){
39         params = params ||
40         {};
41         var result;
42         try {
43             result = reader.readRecords(this.data);
44         } 
45         catch (e) {
46             this.fireEvent('loadexception', this, options, null, e);
47             callback.call(scope, null, options, false);
48             return;
49         }
50         
51         // filtering
52         if (params.filter !== undefined) {
53             result.records = result.records.filter(function(el){
54                 if (typeof(el) == 'object') {
55                     var att = params.filterCol || 0;
56                     return String(el.data[att]).match(params.filter) ? true : false;
57                 }
58                 else {
59                     return String(el).match(params.filter) ? true : false;
60                 }
61             });
62             result.totalRecords = result.records.length;
63         }
64         
65         // sorting
66         if (params.sort !== undefined) {
67             // use integer as params.sort to specify column, since arrays are not named
68             // params.sort=0; would also match a array without columns
69             var dir = String(params.dir).toUpperCase() == 'DESC' ? -1 : 1;
70             var fn = function(v1, v2){
71                 return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
72             };
73             result.records.sort(function(a, b){
74                 var v = 0;
75                 if (typeof(a) == 'object') {
76                     v = fn(a.data[params.sort], b.data[params.sort]) * dir;
77                 }
78                 else {
79                     v = fn(a, b) * dir;
80                 }
81                 if (v == 0) {
82                     v = (a.index < b.index ? -1 : 1);
83                 }
84                 return v;
85             });
86         }
87         // paging (use undefined cause start can also be 0 (thus false))
88         if (params.start !== undefined && params.limit !== undefined) {
89             result.records = result.records.slice(params.start, params.start + params.limit);
90         }
91         callback.call(scope, result, options, true);
92     }
93 });
94
95 //backwards compat.
96 Ext.data.PagingMemoryProxy = Ext.ux.data.PagingMemoryProxy;