Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / view / TableChunker.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.view.TableChunker
17  * 
18  * Produces optimized XTemplates for chunks of tables to be
19  * used in grids, trees and other table based widgets.
20  *
21  * @singleton
22  */
23 Ext.define('Ext.view.TableChunker', {
24     singleton: true,
25     requires: ['Ext.XTemplate'],
26     metaTableTpl: [
27         '{[this.openTableWrap()]}',
28         '<table class="' + Ext.baseCSSPrefix + 'grid-table ' + Ext.baseCSSPrefix + 'grid-table-resizer" border="0" cellspacing="0" cellpadding="0" {[this.embedFullWidth()]}>',
29             '<tbody>',
30             '<tr class="' + Ext.baseCSSPrefix + 'grid-header-row">',
31             '<tpl for="columns">',
32                 '<th class="' + Ext.baseCSSPrefix + 'grid-col-resizer-{id}" style="width: {width}px; height: 0px;"></th>',
33             '</tpl>',
34             '</tr>',
35             '{[this.openRows()]}',
36                 '{row}',
37                 '<tpl for="features">',
38                     '{[this.embedFeature(values, parent, xindex, xcount)]}',
39                 '</tpl>',
40             '{[this.closeRows()]}',
41             '</tbody>',
42         '</table>',
43         '{[this.closeTableWrap()]}'
44     ],
45
46     constructor: function() {
47         Ext.XTemplate.prototype.recurse = function(values, reference) {
48             return this.apply(reference ? values[reference] : values);
49         };
50     },
51
52     embedFeature: function(values, parent, x, xcount) {
53         var tpl = '';
54         if (!values.disabled) {
55             tpl = values.getFeatureTpl(values, parent, x, xcount);
56         }
57         return tpl;
58     },
59
60     embedFullWidth: function() {
61         return 'style="width: {fullWidth}px;"';
62     },
63
64     openRows: function() {
65         return '<tpl for="rows">';
66     },
67
68     closeRows: function() {
69         return '</tpl>';
70     },
71
72     metaRowTpl: [
73         '<tr class="' + Ext.baseCSSPrefix + 'grid-row {addlSelector} {[this.embedRowCls()]}" {[this.embedRowAttr()]}>',
74             '<tpl for="columns">',
75                 '<td class="{cls} ' + Ext.baseCSSPrefix + 'grid-cell ' + Ext.baseCSSPrefix + 'grid-cell-{columnId} {{id}-modified} {{id}-tdCls} {[this.firstOrLastCls(xindex, xcount)]}" {{id}-tdAttr}><div unselectable="on" class="' + Ext.baseCSSPrefix + 'grid-cell-inner ' + Ext.baseCSSPrefix + 'unselectable" style="{{id}-style}; text-align: {align};">{{id}}</div></td>',
76             '</tpl>',
77         '</tr>'
78     ],
79     
80     firstOrLastCls: function(xindex, xcount) {
81         var cssCls = '';
82         if (xindex === 1) {
83             cssCls = Ext.baseCSSPrefix + 'grid-cell-first';
84         } else if (xindex === xcount) {
85             cssCls = Ext.baseCSSPrefix + 'grid-cell-last';
86         }
87         return cssCls;
88     },
89     
90     embedRowCls: function() {
91         return '{rowCls}';
92     },
93     
94     embedRowAttr: function() {
95         return '{rowAttr}';
96     },
97     
98     openTableWrap: function() {
99         return '';
100     },
101     
102     closeTableWrap: function() {
103         return '';
104     },
105
106     getTableTpl: function(cfg, textOnly) {
107         var tpl,
108             tableTplMemberFns = {
109                 openRows: this.openRows,
110                 closeRows: this.closeRows,
111                 embedFeature: this.embedFeature,
112                 embedFullWidth: this.embedFullWidth,
113                 openTableWrap: this.openTableWrap,
114                 closeTableWrap: this.closeTableWrap
115             },
116             tplMemberFns = {},
117             features = cfg.features || [],
118             ln = features.length,
119             i  = 0,
120             memberFns = {
121                 embedRowCls: this.embedRowCls,
122                 embedRowAttr: this.embedRowAttr,
123                 firstOrLastCls: this.firstOrLastCls
124             },
125             // copy the default
126             metaRowTpl = Array.prototype.slice.call(this.metaRowTpl, 0),
127             metaTableTpl;
128             
129         for (; i < ln; i++) {
130             if (!features[i].disabled) {
131                 features[i].mutateMetaRowTpl(metaRowTpl);
132                 Ext.apply(memberFns, features[i].getMetaRowTplFragments());
133                 Ext.apply(tplMemberFns, features[i].getFragmentTpl());
134                 Ext.apply(tableTplMemberFns, features[i].getTableFragments());
135             }
136         }
137         
138         metaRowTpl = Ext.create('Ext.XTemplate', metaRowTpl.join(''), memberFns);
139         cfg.row = metaRowTpl.applyTemplate(cfg);
140         
141         metaTableTpl = Ext.create('Ext.XTemplate', this.metaTableTpl.join(''), tableTplMemberFns);
142         
143         tpl = metaTableTpl.applyTemplate(cfg);
144         
145         // TODO: Investigate eliminating.
146         if (!textOnly) {
147             tpl = Ext.create('Ext.XTemplate', tpl, tplMemberFns);
148         }
149         return tpl;
150         
151     }
152 });
153