Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / examples / ux / CheckColumn.js
index d421f25..bbe3421 100644 (file)
@@ -1,84 +1,77 @@
-/*!
- * Ext JS Library 3.2.0
- * Copyright(c) 2006-2010 Ext JS, Inc.
- * licensing@extjs.com
- * http://www.extjs.com/license
- */
-Ext.ns('Ext.ux.grid');
-
 /**
- * @class Ext.ux.grid.CheckColumn
- * @extends Object
- * GridPanel plugin to add a column with check boxes to a grid.
+ * @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 column
-var checkColumn = new Ext.grid.CheckColumn({
-   header: 'Indoor?',
-   dataIndex: 'indoor',
-   id: 'check',
-   width: 55
-});
-
-// add the column to the column model
-var cm = new Ext.grid.ColumnModel([{
-       header: 'Foo',
-       ...
-    },
-    checkColumn
-]);
-
 // create the grid
-var grid = new Ext.grid.EditorGridPanel({
+var grid = Ext.create('Ext.grid.Panel', {
     ...
-    cm: cm,
-    plugins: [checkColumn], // include plugin
+    columns: [{
+           text: 'Foo',
+           ...
+        },{
+           xtype: 'checkcolumn',
+           text: 'Indoor?',
+           dataIndex: 'indoor',
+           width: 55
+        }
+    ]
     ...
 });
  * </code></pre>
- * In addition to storing a Boolean value within the record data, this
- * class toggles a css class between <tt>'x-grid3-check-col'</tt> and
- * <tt>'x-grid3-check-col-on'</tt> to alter the background image used for
- * a column.
+ * 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.ux.grid.CheckColumn = function(config){
-    Ext.apply(this, config);
-    if(!this.id){
-        this.id = Ext.id();
-    }
-    this.renderer = this.renderer.createDelegate(this);
-};
-
-Ext.ux.grid.CheckColumn.prototype ={
-    init : function(grid){
-        this.grid = grid;
-        this.grid.on('render', function(){
-            var view = this.grid.getView();
-            view.mainBody.on('mousedown', this.onMouseDown, this);
-        }, this);
+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);
     },
 
-    onMouseDown : function(e, t){
-        if(Ext.fly(t).hasClass(this.createId())){
-            e.stopEvent();
-            var index = this.grid.getView().findRowIndex(t);
-            var record = this.grid.store.getAt(index);
-            record.set(this.dataIndex, !record.data[this.dataIndex]);
+    /**
+     * @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);
         }
     },
 
-    renderer : function(v, p, record){
-        p.css += ' x-grid3-check-col-td'; 
-        return String.format('<div class="x-grid3-check-col{0} {1}">&#160;</div>', v ? '-on' : '', this.createId());
-    },
-    
-    createId : function(){
-        return 'x-grid3-cc-' + this.id;
-    }
-};
-
-// register ptype
-Ext.preg('checkcolumn', Ext.ux.grid.CheckColumn);
+    // 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'];
 
-// backwards compat
-Ext.grid.CheckColumn = Ext.ux.grid.CheckColumn;
\ No newline at end of file
+        if (value) {
+            cls.push(cssPrefix + 'grid-checkheader-checked');
+        }
+        return '<div class="' + cls.join(' ') + '">&#160;</div>';
+    }
+});