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