Upgrade to ExtJS 4.0.2 - Released 06/09/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 {Boolan} 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         
71         inner = inner.replace('x-grid-row', 'x-grid-row-summary');
72         inner = inner.replace('{{id}}', '{gridSummaryValue}');
73         inner = inner.replace(this.nestedIdRe, '{id$1}');  
74         inner = inner.replace('{[this.embedRowCls()]}', '{rowCls}');
75         inner = inner.replace('{[this.embedRowAttr()]}', '{rowAttr}');
76         inner = Ext.create('Ext.XTemplate', inner, {
77             firstOrLastCls: Ext.view.TableChunker.firstOrLastCls
78         });
79         
80         return inner.applyTemplate({
81             columns: this.getPrintData(index)
82         });
83     },
84     
85     /**
86      * Gets the value for the column from the attached data.
87      * @param {Ext.grid.column.Column} column The header
88      * @param {Object} data The current data
89      * @return {String} The value to be rendered
90      */
91     getColumnValue: function(column, summaryData){
92         var comp     = Ext.getCmp(column.id),
93             value    = summaryData[column.id],
94             renderer = comp.summaryRenderer;
95
96         if (renderer) {
97             value = renderer.call(
98                 comp.scope || this,
99                 value,
100                 summaryData,
101                 column.dataIndex
102             );
103         }
104         return value;
105     },
106     
107     /**
108      * Get the summary data for a field.
109      * @private
110      * @param {Ext.data.Store} store The store to get the data from
111      * @param {String/Function} type The type of aggregation. If a function is specified it will
112      * be passed to the stores aggregate function.
113      * @param {String} field The field to aggregate on
114      * @param {Boolean} group True to aggregate in grouped mode 
115      * @return {Mixed} See the return type for the store functions.
116      */
117     getSummary: function(store, type, field, group){
118         if (type) {
119             if (Ext.isFunction(type)) {
120                 return store.aggregate(type, null, group);
121             }
122             
123             switch (type) {
124                 case 'count':
125                     return store.count(group);
126                 case 'min':
127                     return store.min(field, group);
128                 case 'max':
129                     return store.max(field, group);
130                 case 'sum':
131                     return store.sum(field, group);
132                 case 'average':
133                     return store.average(field, group);
134                 default:
135                     return group ? {} : '';
136                     
137             }
138         }
139     }
140     
141 });
142