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