Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / examples / ux / CheckColumn.js
index 59ceb99..e3dafc0 100644 (file)
@@ -1,84 +1,92 @@
-/*!
- * Ext JS Library 3.1.1
- * Copyright(c) 2006-2010 Ext JS, LLC
- * licensing@extjs.com
- * http://www.extjs.com/license
+/*
+
+This file is part of Ext JS 4
+
+Copyright (c) 2011 Sencha Inc
+
+Contact:  http://www.sencha.com/contact
+
+GNU General Public License Usage
+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.
+
+If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
+
+*/
+/**
+ * @class Ext.ux.CheckColumn
+ * @extends Ext.grid.column.Column
+ * <p>A Header subclass which renders a checkbox in each column cell which toggles the truthiness of the associated data field on click.</p>
+ * <p><b>Note. As of ExtJS 3.3 this no longer has to be configured as a plugin of the GridPanel.</b></p>
+ * <p>Example usage:</p>
+ * <pre><code>
+// create the grid
+var grid = Ext.create('Ext.grid.Panel', {
+    ...
+    columns: [{
+           text: 'Foo',
+           ...
+        },{
+           xtype: 'checkcolumn',
+           text: 'Indoor?',
+           dataIndex: 'indoor',
+           width: 55
+        }
+    ]
+    ...
+});
+ * </code></pre>
+ * In addition to toggling a Boolean value within the record data, this
+ * class adds or removes a css class <tt>'x-grid-checked'</tt> on the td
+ * based on whether or not it is checked to alter the background image used
+ * for a column.
  */
-Ext.ns('Ext.ux.grid');\r
-\r
-/**\r
- * @class Ext.ux.grid.CheckColumn\r
- * @extends Object\r
- * GridPanel plugin to add a column with check boxes to a grid.\r
- * <p>Example usage:</p>\r
- * <pre><code>\r
-// create the column\r
-var checkColumn = new Ext.grid.CheckColumn({\r
-   header: 'Indoor?',\r
-   dataIndex: 'indoor',\r
-   id: 'check',\r
-   width: 55\r
-});\r
-\r
-// add the column to the column model\r
-var cm = new Ext.grid.ColumnModel([{\r
-       header: 'Foo',\r
-       ...\r
-    },\r
-    checkColumn\r
-]);\r
-\r
-// create the grid\r
-var grid = new Ext.grid.EditorGridPanel({\r
-    ...\r
-    cm: cm,\r
-    plugins: [checkColumn], // include plugin\r
-    ...\r
-});\r
- * </code></pre>\r
- * In addition to storing a Boolean value within the record data, this\r
- * class toggles a css class between <tt>'x-grid3-check-col'</tt> and\r
- * <tt>'x-grid3-check-col-on'</tt> to alter the background image used for\r
- * a column.\r
- */\r
-Ext.ux.grid.CheckColumn = function(config){\r
-    Ext.apply(this, config);\r
-    if(!this.id){\r
-        this.id = Ext.id();\r
-    }\r
-    this.renderer = this.renderer.createDelegate(this);\r
-};\r
-\r
-Ext.ux.grid.CheckColumn.prototype ={\r
-    init : function(grid){\r
-        this.grid = grid;\r
-        this.grid.on('render', function(){\r
-            var view = this.grid.getView();\r
-            view.mainBody.on('mousedown', this.onMouseDown, this);\r
-        }, this);\r
-    },\r
-\r
-    onMouseDown : function(e, t){\r
-        if(Ext.fly(t).hasClass(this.createId())){\r
-            e.stopEvent();\r
-            var index = this.grid.getView().findRowIndex(t);\r
-            var record = this.grid.store.getAt(index);\r
-            record.set(this.dataIndex, !record.data[this.dataIndex]);\r
-        }\r
-    },\r
-\r
-    renderer : function(v, p, record){\r
-        p.css += ' x-grid3-check-col-td'; \r
-        return String.format('<div class="x-grid3-check-col{0} {1}">&#160;</div>', v ? '-on' : '', this.createId());\r
-    },\r
-    \r
-    createId : function(){\r
-        return 'x-grid3-cc-' + this.id;\r
-    }\r
-};\r
-\r
-// register ptype\r
-Ext.preg('checkcolumn', Ext.ux.grid.CheckColumn);\r
-\r
-// backwards compat\r
-Ext.grid.CheckColumn = Ext.ux.grid.CheckColumn;
\ No newline at end of file
+Ext.define('Ext.ux.CheckColumn', {
+    extend: 'Ext.grid.column.Column',
+    alias: 'widget.checkcolumn',
+    
+    constructor: function() {
+        this.addEvents(
+            /**
+             * @event checkchange
+             * Fires when the checked state of a row changes
+             * @param {Ext.ux.CheckColumn} this
+             * @param {Number} rowIndex The row index
+             * @param {Boolean} checked True if the box is checked
+             */
+            'checkchange'
+        );
+        this.callParent(arguments);
+    },
+
+    /**
+     * @private
+     * Process and refire events routed from the GridView's processEvent method.
+     */
+    processEvent: function(type, view, cell, recordIndex, cellIndex, e) {
+        if (type == 'mousedown' || (type == 'keydown' && (e.getKey() == e.ENTER || e.getKey() == e.SPACE))) {
+            var record = view.panel.store.getAt(recordIndex),
+                dataIndex = this.dataIndex,
+                checked = !record.get(dataIndex);
+                
+            record.set(dataIndex, checked);
+            this.fireEvent('checkchange', this, recordIndex, checked);
+            // cancel selection.
+            return false;
+        } else {
+            return this.callParent(arguments);
+        }
+    },
+
+    // Note: class names are not placed on the prototype bc renderer scope
+    // is not in the header.
+    renderer : function(value){
+        var cssPrefix = Ext.baseCSSPrefix,
+            cls = [cssPrefix + 'grid-checkheader'];
+
+        if (value) {
+            cls.push(cssPrefix + 'grid-checkheader-checked');
+        }
+        return '<div class="' + cls.join(' ') + '">&#160;</div>';
+    }
+});
+