Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / grid / View.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.View
17  * @extends Ext.view.Table
18
19 The grid View class provides extra {@link Ext.grid.Panel} specific functionality to the
20 {@link Ext.view.Table}. In general, this class is not instanced directly, instead a viewConfig
21 option is passed to the grid:
22
23     Ext.create('Ext.grid.Panel', {
24         // other options
25         viewConfig: {
26             stripeRows: false
27         }
28     });
29     
30 __Drag Drop__
31 Drag and drop functionality can be achieved in the grid by attaching a {@link Ext.grid.plugin.DragDrop} plugin
32 when creating the view.
33
34     Ext.create('Ext.grid.Panel', {
35         // other options
36         viewConfig: {
37             plugins: {
38                 ddGroup: 'people-group',
39                 ptype: 'gridviewdragdrop',
40                 enableDrop: false
41             }
42         }
43     });
44
45  * @markdown
46  */
47 Ext.define('Ext.grid.View', {
48     extend: 'Ext.view.Table',
49     alias: 'widget.gridview',
50
51     /**
52      * @cfg {Boolean} stripeRows <tt>true</tt> to stripe the rows. Default is <tt>false</tt>.
53      * <p>This causes the CSS class <tt><b>x-grid-row-alt</b></tt> to be added to alternate rows of
54      * the grid. A default CSS rule is provided which sets a background color, but you can override this
55      * with a rule which either overrides the <b>background-color</b> style using the '!important'
56      * modifier, or which uses a CSS selector of higher specificity.</p>
57      */
58     stripeRows: true,
59     
60     invalidateScrollerOnRefresh: true,
61     
62     /**
63      * Scroll the GridView to the top by scrolling the scroller.
64      * @private
65      */
66     scrollToTop : function(){
67         if (this.rendered) {
68             var section = this.ownerCt,
69                 verticalScroller = section.verticalScroller;
70                 
71             if (verticalScroller) {
72                 verticalScroller.scrollToTop();
73             }
74         }
75     },
76
77     // after adding a row stripe rows from then on
78     onAdd: function(ds, records, index) {
79         this.callParent(arguments);
80         this.doStripeRows(index);
81     },
82     
83     // after removing a row stripe rows from then on
84     onRemove: function(ds, records, index) {
85         this.callParent(arguments);
86         this.doStripeRows(index);
87     },
88     
89     onUpdate: function(ds, record, operation) {
90         var index = ds.indexOf(record);
91         this.callParent(arguments);
92         this.doStripeRows(index, index);
93     },
94     
95     /**
96      * Stripe rows from a particular row index
97      * @param {Number} startRow
98      * @param {Number} endRow Optional argument specifying the last row to process. By default process up to the last row.
99      * @private
100      */
101     doStripeRows: function(startRow, endRow) {
102         // ensure stripeRows configuration is turned on
103         if (this.stripeRows) {
104             var rows   = this.getNodes(startRow, endRow),
105                 rowsLn = rows.length,
106                 i      = 0,
107                 row;
108                 
109             for (; i < rowsLn; i++) {
110                 row = rows[i];
111                 // Remove prior applied row classes.
112                 row.className = row.className.replace(this.rowClsRe, ' ');
113                 startRow++;
114                 // Every odd row will get an additional cls
115                 if (startRow % 2 === 0) {
116                     row.className += (' ' + this.altRowCls);
117                 }
118             }
119         }
120     },
121     
122     refresh: function(firstPass) {
123         this.callParent(arguments);
124         this.doStripeRows(0);
125         // TODO: Remove gridpanel dependency
126         var g = this.up('gridpanel');
127         if (g && this.invalidateScrollerOnRefresh) {
128             g.invalidateScroller();
129         }
130     }
131 });
132