source: trunk/web/addons/job_monarch/lib/extjs/examples/locale/PagingMemoryProxy.js @ 619

Last change on this file since 619 was 619, checked in by ramonb, 15 years ago

lib/:

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