Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / examples / ux / ajax / JsonSimlet.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.ux.ajax.JsonSimlet
17  */
18 Ext.define('Ext.ux.ajax.JsonSimlet', function () {
19
20     function makeSortFn (def, cmp) {
21         var order = def.direction,
22             sign = (order && order.toUpperCase() == 'DESC') ? -1 : 1;
23
24         return function (leftRec, rightRec) {
25             var lhs = leftRec[def.property],
26                 rhs = rightRec[def.property],
27                 c = (lhs < rhs) ? -1 : ((rhs < lhs) ? 1 : 0);
28
29             if (c || !cmp) {
30                 return c * sign;
31             }
32
33             return cmp(leftRec, rightRec);
34         }
35     }
36
37     function makeSortFns (defs, cmp) {
38         for (var sortFn = cmp, i = defs && defs.length; i; ) {
39             sortFn = makeSortFn(defs[--i], sortFn);
40         }
41         return sortFn;
42     }
43
44     return {
45         extend: 'Ext.ux.ajax.Simlet',
46         alias: 'simlet.json',
47
48         getData: function (ctx) {
49             var me = this,
50                 data = me.data,
51                 params = ctx.params,
52                 order = (params.group || '') + (params.sort || ''),
53                 fields,
54                 sortFn,
55                 i;
56
57             if (!order) {
58                 return data;
59             }
60
61             if (order == me.currentOrder) {
62                 return me.sortedData;
63             }
64
65             ctx.sortSpec = fields = params.sort && Ext.decode(params.sort);
66             sortFn = makeSortFns(fields);
67
68             ctx.groupSpec = fields = params.group && Ext.decode(params.group);
69             sortFn = makeSortFns(fields, sortFn);
70
71             data = data.slice(0); // preserve 'physical' order of raw data...
72             data.sort(sortFn);
73
74             me.sortedData = data;
75             me.currentOrder = order;
76
77             return data;
78         },
79
80         getPage: function (ctx, data) {
81             var ret = data,
82                 length = data.length,
83                 start = ctx.params.start || 0,
84                 end = ctx.params.limit ? Math.min(length, start + ctx.params.limit) : length;
85
86             if (start || end < length) {
87                 ret = ret.slice(start, end);
88             }
89
90             return ret;
91         },
92
93         getGroupSummary: function (groupField, rows, ctx) {
94             return rows[0];
95         },
96
97         getSummary: function (ctx, data, page) {
98             var me = this,
99                 groupField = ctx.groupSpec[0].property,
100                 accum,
101                 todo = {},
102                 summary = [],
103                 fieldValue,
104                 lastFieldValue;
105
106             Ext.each(page, function (rec) {
107                 fieldValue = rec[groupField];
108                 todo[fieldValue] = true;
109             });
110
111             function flush () {
112                 if (accum) {
113                     summary.push(me.getGroupSummary(groupField, accum, ctx));
114                     accum = null;
115                 }
116             }
117
118             // data is ordered primarily by the groupField, so one pass can pick up all
119             // the summaries one at a time.
120             Ext.each(data, function (rec) {
121                 fieldValue = rec[groupField];
122
123                 if (lastFieldValue !== fieldValue) {
124                     flush();
125                     lastFieldValue = fieldValue;
126                 }
127
128                 if (!todo[fieldValue]) {
129                     // if we have even 1 summary, we have summarized all that we need
130                     // (again because data and page are ordered by groupField)
131                     return !summary.length;
132                 }
133
134                 if (accum) {
135                     accum.push(rec);
136                 } else {
137                     accum = [rec];
138                 }
139
140                 return true;
141             });
142
143             flush(); // make sure that last pesky summary goes...
144
145             return summary;
146         },
147
148         doGet: function (ctx) {
149             var me = this,
150                 data = me.getData(ctx),
151                 page = me.getPage(ctx, data),
152                 response = {
153                     data: page,
154                     totalRecords: data.length
155                 },
156                 ret = this.callParent(arguments); // pick up status/statusText
157
158             if (ctx.groupSpec) {
159                 response.summaryData = me.getSummary(ctx, data, page);
160             }
161
162             ret.responseText = Ext.encode(response);
163             return ret;
164         }
165     };
166 }());
167