Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / selection / DataViewModel.js
index 561b386..d26873d 100644 (file)
@@ -1,23 +1,55 @@
+/*
+
+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.selection.DataViewModel
  * @ignore
  */
 Ext.define('Ext.selection.DataViewModel', {
     extend: 'Ext.selection.Model',
-    
+
     requires: ['Ext.util.KeyNav'],
 
     deselectOnContainerClick: true,
-    
+
     /**
      * @cfg {Boolean} enableKeyNav
-     * 
-     * Turns on/off keyboard navigation within the DataView. Defaults to true.
+     *
+     * Turns on/off keyboard navigation within the DataView.
      */
     enableKeyNav: true,
-    
+
     constructor: function(cfg){
         this.addEvents(
+            /**
+             * @event beforedeselect
+             * Fired before a record is deselected. If any listener returns false, the
+             * deselection is cancelled.
+             * @param {Ext.selection.DataViewModel} this
+             * @param {Ext.data.Model} record The deselected record
+             */
+            'beforedeselect',
+
+            /**
+             * @event beforeselect
+             * Fired before a record is selected. If any listener returns false, the
+             * selection is cancelled.
+             * @param {Ext.selection.DataViewModel} this
+             * @param {Ext.data.Model} record The selected record
+             */
+            'beforeselect',
+
             /**
              * @event deselect
              * Fired after a record is deselected
@@ -25,7 +57,7 @@ Ext.define('Ext.selection.DataViewModel', {
              * @param  {Ext.data.Model} record The deselected record
              */
             'deselect',
-            
+
             /**
              * @event select
              * Fired after a record is selected
@@ -36,7 +68,7 @@ Ext.define('Ext.selection.DataViewModel', {
         );
         this.callParent(arguments);
     },
-    
+
     bindComponent: function(view) {
         var me = this,
             eventListeners = {
@@ -66,15 +98,15 @@ Ext.define('Ext.selection.DataViewModel', {
             this.deselectAll();
         }
     },
-    
+
     initKeyNav: function(view) {
         var me = this;
-        
+
         if (!view.rendered) {
             view.on('render', Ext.Function.bind(me.initKeyNav, me, [view], 0), me, {single: true});
             return;
         }
-        
+
         view.el.set({
             tabIndex: -1
         });
@@ -86,7 +118,7 @@ Ext.define('Ext.selection.DataViewModel', {
             scope: me
         });
     },
-    
+
     onNavKey: function(step) {
         step = step || 1;
         var me = this,
@@ -94,43 +126,46 @@ Ext.define('Ext.selection.DataViewModel', {
             selected = me.getSelection()[0],
             numRecords = me.view.store.getCount(),
             idx;
-                
+
         if (selected) {
             idx = view.indexOf(view.getNode(selected)) + step;
         } else {
             idx = 0;
         }
-        
+
         if (idx < 0) {
             idx = numRecords - 1;
         } else if (idx >= numRecords) {
             idx = 0;
         }
-        
+
         me.select(idx);
     },
 
     // Allow the DataView to update the ui
-    onSelectChange: function(record, isSelected, suppressEvent) {
+    onSelectChange: function(record, isSelected, suppressEvent, commitFn) {
         var me = this,
             view = me.view,
-            allowSelect = true;
-        
-        if (isSelected) {
-            if (!suppressEvent) {
-                allowSelect = me.fireEvent('beforeselect', me, record) !== false;
-            }
-            if (allowSelect) {
+            eventName = isSelected ? 'select' : 'deselect';
+
+        if ((suppressEvent || me.fireEvent('before' + eventName, me, record)) !== false &&
+                commitFn() !== false) {
+
+            if (isSelected) {
                 view.onItemSelect(record);
-                if (!suppressEvent) {
-                    me.fireEvent('select', me, record);
-                }
+            } else {
+                view.onItemDeselect(record);
             }
-        } else {
-            view.onItemDeselect(record);
+
             if (!suppressEvent) {
-                me.fireEvent('deselect', me, record);
+                me.fireEvent(eventName, me, record);
             }
         }
+    },
+    
+    destroy: function(){
+        Ext.destroy(this.keyNav);
+        this.callParent();
     }
 });
+