3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.ux.ajax.JsonSimlet
18 Ext.define('Ext.ux.ajax.JsonSimlet', function () {
20 function makeSortFn (def, cmp) {
21 var order = def.direction,
22 sign = (order && order.toUpperCase() == 'DESC') ? -1 : 1;
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);
33 return cmp(leftRec, rightRec);
37 function makeSortFns (defs, cmp) {
38 for (var sortFn = cmp, i = defs && defs.length; i; ) {
39 sortFn = makeSortFn(defs[--i], sortFn);
45 extend: 'Ext.ux.ajax.Simlet',
48 getData: function (ctx) {
52 order = (params.group || '') + (params.sort || ''),
61 if (order == me.currentOrder) {
65 ctx.sortSpec = fields = params.sort && Ext.decode(params.sort);
66 sortFn = makeSortFns(fields);
68 ctx.groupSpec = fields = params.group && Ext.decode(params.group);
69 sortFn = makeSortFns(fields, sortFn);
71 data = data.slice(0); // preserve 'physical' order of raw data...
75 me.currentOrder = order;
80 getPage: function (ctx, data) {
83 start = ctx.params.start || 0,
84 end = ctx.params.limit ? Math.min(length, start + ctx.params.limit) : length;
86 if (start || end < length) {
87 ret = ret.slice(start, end);
93 getGroupSummary: function (groupField, rows, ctx) {
97 getSummary: function (ctx, data, page) {
99 groupField = ctx.groupSpec[0].property,
106 Ext.each(page, function (rec) {
107 fieldValue = rec[groupField];
108 todo[fieldValue] = true;
113 summary.push(me.getGroupSummary(groupField, accum, ctx));
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];
123 if (lastFieldValue !== fieldValue) {
125 lastFieldValue = fieldValue;
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;
143 flush(); // make sure that last pesky summary goes...
148 doGet: function (ctx) {
150 data = me.getData(ctx),
151 page = me.getPage(ctx, data),
154 totalRecords: data.length
156 ret = this.callParent(arguments); // pick up status/statusText
159 response.summaryData = me.getSummary(ctx, data, page);
162 ret.responseText = Ext.encode(response);