Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / widgets / grid / CheckboxSelectionModel.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.grid.CheckboxSelectionModel\r
9  * @extends Ext.grid.RowSelectionModel\r
10  * A custom selection model that renders a column of checkboxes that can be toggled to select or deselect rows.\r
11  * @constructor\r
12  * @param {Object} config The configuration options\r
13  */\r
14 Ext.grid.CheckboxSelectionModel = Ext.extend(Ext.grid.RowSelectionModel, {\r
15 \r
16     /**\r
17      * @cfg {Boolean} checkOnly <tt>true</tt> if rows can only be selected by clicking on the\r
18      * checkbox column (defaults to <tt>false</tt>).\r
19      */\r
20     /**\r
21      * @cfg {String} header Any valid text or HTML fragment to display in the header cell for the\r
22      * checkbox column.  Defaults to:<pre><code>\r
23      * '&lt;div class="x-grid3-hd-checker">&#38;#160;&lt;/div>'</tt>\r
24      * </code></pre>\r
25      * The default CSS class of <tt>'x-grid3-hd-checker'</tt> displays a checkbox in the header\r
26      * and provides support for automatic check all/none behavior on header click. This string\r
27      * can be replaced by any valid HTML fragment, including a simple text string (e.g.,\r
28      * <tt>'Select Rows'</tt>), but the automatic check all/none behavior will only work if the\r
29      * <tt>'x-grid3-hd-checker'</tt> class is supplied.\r
30      */\r
31     header : '<div class="x-grid3-hd-checker">&#160;</div>',\r
32     /**\r
33      * @cfg {Number} width The default width in pixels of the checkbox column (defaults to <tt>20</tt>).\r
34      */\r
35     width : 20,\r
36     /**\r
37      * @cfg {Boolean} sortable <tt>true</tt> if the checkbox column is sortable (defaults to\r
38      * <tt>false</tt>).\r
39      */\r
40     sortable : false,\r
41 \r
42     // private\r
43     menuDisabled : true,\r
44     fixed : true,\r
45     dataIndex : '',\r
46     id : 'checker',\r
47 \r
48     constructor : function(){\r
49         Ext.grid.CheckboxSelectionModel.superclass.constructor.apply(this, arguments);\r
50 \r
51         if(this.checkOnly){\r
52             this.handleMouseDown = Ext.emptyFn;\r
53         }\r
54     },\r
55 \r
56     // private\r
57     initEvents : function(){\r
58         Ext.grid.CheckboxSelectionModel.superclass.initEvents.call(this);\r
59         this.grid.on('render', function(){\r
60             var view = this.grid.getView();\r
61             view.mainBody.on('mousedown', this.onMouseDown, this);\r
62             Ext.fly(view.innerHd).on('mousedown', this.onHdMouseDown, this);\r
63 \r
64         }, this);\r
65     },\r
66 \r
67     // private\r
68     onMouseDown : function(e, t){\r
69         if(e.button === 0 && t.className == 'x-grid3-row-checker'){ // Only fire if left-click\r
70             e.stopEvent();\r
71             var row = e.getTarget('.x-grid3-row');\r
72             if(row){\r
73                 var index = row.rowIndex;\r
74                 if(this.isSelected(index)){\r
75                     this.deselectRow(index);\r
76                 }else{\r
77                     this.selectRow(index, true);\r
78                 }\r
79             }\r
80         }\r
81     },\r
82 \r
83     // private\r
84     onHdMouseDown : function(e, t){\r
85         if(t.className == 'x-grid3-hd-checker'){\r
86             e.stopEvent();\r
87             var hd = Ext.fly(t.parentNode);\r
88             var isChecked = hd.hasClass('x-grid3-hd-checker-on');\r
89             if(isChecked){\r
90                 hd.removeClass('x-grid3-hd-checker-on');\r
91                 this.clearSelections();\r
92             }else{\r
93                 hd.addClass('x-grid3-hd-checker-on');\r
94                 this.selectAll();\r
95             }\r
96         }\r
97     },\r
98 \r
99     // private\r
100     renderer : function(v, p, record){\r
101         return '<div class="x-grid3-row-checker">&#160;</div>';\r
102     }\r
103 });