Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / grid / feature / AbstractSummary.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.grid.feature.AbstractSummary
17  * @extends Ext.grid.feature.Feature
18  * A small abstract class that contains the shared behaviour for any summary
19  * calculations to be used in the grid.
20  */
21 Ext.define('Ext.grid.feature.AbstractSummary', {
22     
23     /* Begin Definitions */
24    
25     extend: 'Ext.grid.feature.Feature',
26     
27     alias: 'feature.abstractsummary',
28    
29     /* End Definitions */
30    
31    /**
32     * @cfg {Boolean} showSummaryRow True to show the summary row. Defaults to <tt>true</tt>.
33     */
34     showSummaryRow: true,
35     
36     // @private
37     nestedIdRe: /\{\{id\}([\w\-]*)\}/g,
38     
39     /**
40      * Toggle whether or not to show the summary row.
41      * @param {Boolean} visible True to show the summary row
42      */
43     toggleSummaryRow: function(visible){
44         this.showSummaryRow = !!visible;
45     },
46     
47     /**
48      * Gets any fragments to be used in the tpl
49      * @private
50      * @return {Object} The fragments
51      */
52     getSummaryFragments: function(){
53         var fragments = {};
54         if (this.showSummaryRow) {
55             Ext.apply(fragments, {
56                 printSummaryRow: Ext.bind(this.printSummaryRow, this)
57             });
58         }
59         return fragments;
60     },
61     
62     /**
63      * Prints a summary row
64      * @private
65      * @param {Object} index The index in the template
66      * @return {String} The value of the summary row
67      */
68     printSummaryRow: function(index){
69         var inner = this.view.getTableChunker().metaRowTpl.join(''),
70             prefix = Ext.baseCSSPrefix;
71         
72         inner = inner.replace(prefix + 'grid-row', prefix + 'grid-row-summary');
73         inner = inner.replace('{{id}}', '{gridSummaryValue}');
74         inner = inner.replace(this.nestedIdRe, '{id$1}');  
75         inner = inner.replace('{[this.embedRowCls()]}', '{rowCls}');
76         inner = inner.replace('{[this.embedRowAttr()]}', '{rowAttr}');
77         inner = Ext.create('Ext.XTemplate', inner, {
78             firstOrLastCls: Ext.view.TableChunker.firstOrLastCls
79         });
80         
81         return inner.applyTemplate({
82             columns: this.getPrintData(index)
83         });
84     },
85     
86     /**
87      * Gets the value for the column from the attached data.
88      * @param {Ext.grid.column.Column} column The header
89      * @param {Object} data The current data
90      * @return {String} The value to be rendered
91      */
92     getColumnValue: function(column, summaryData){
93         var comp     = Ext.getCmp(column.id),
94             value    = summaryData[column.id],
95             renderer = comp.summaryRenderer;
96
97         if (renderer) {
98             value = renderer.call(
99                 comp.scope || this,
100                 value,
101                 summaryData,
102                 column.dataIndex
103             );
104         }
105         return value;
106     },
107     
108     /**
109      * Get the summary data for a field.
110      * @private
111      * @param {Ext.data.Store} store The store to get the data from
112      * @param {String/Function} type The type of aggregation. If a function is specified it will
113      * be passed to the stores aggregate function.
114      * @param {String} field The field to aggregate on
115      * @param {Boolean} group True to aggregate in grouped mode 
116      * @return {Number/String/Object} See the return type for the store functions.
117      */
118     getSummary: function(store, type, field, group){
119         if (type) {
120             if (Ext.isFunction(type)) {
121                 return store.aggregate(type, null, group);
122             }
123             
124             switch (type) {
125                 case 'count':
126                     return store.count(group);
127                 case 'min':
128                     return store.min(field, group);
129                 case 'max':
130                     return store.max(field, group);
131                 case 'sum':
132                     return store.sum(field, group);
133                 case 'average':
134                     return store.average(field, group);
135                 default:
136                     return group ? {} : '';
137                     
138             }
139         }
140     }
141     
142 });
143