+/*
+
+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.Model
* @extends Ext.util.Observable
*/
Ext.define('Ext.selection.Model', {
extend: 'Ext.util.Observable',
- alternateClassName: 'Ext.AbstractStoreSelectionModel',
+ alternateClassName: 'Ext.AbstractSelectionModel',
requires: ['Ext.data.StoreManager'],
// lastSelected
* Modes of selection.
* Valid values are SINGLE, SIMPLE, and MULTI. Defaults to 'SINGLE'
*/
-
+
/**
* @cfg {Boolean} allowDeselect
* Allow users to deselect a record in a DataView, List or Grid. Only applicable when the SelectionModel's mode is 'SINGLE'. Defaults to false.
* records.
*/
selected: null,
-
-
+
+
/**
* Prune records when they are removed from the store from the selection.
* This is a private flag. For an example of its usage, take a look at
constructor: function(cfg) {
var me = this;
-
+
cfg = cfg || {};
Ext.apply(me, cfg);
-
+
me.addEvents(
/**
* @event selectionchange
// maintains the currently selected records.
me.selected = Ext.create('Ext.util.MixedCollection');
-
+
me.callParent(arguments);
},
// binds the store to the selModel.
bind : function(store, initial){
var me = this;
-
+
if(!initial && me.store){
if(store !== me.store && me.store.autoDestroy){
me.store.destroy();
}
},
- selectAll: function(silent) {
- var selections = this.store.getRange(),
+ /**
+ * Select all records in the view.
+ * @param {Boolean} suppressEvent True to suppress any selects event
+ */
+ selectAll: function(suppressEvent) {
+ var me = this,
+ selections = me.store.getRange(),
i = 0,
- len = selections.length;
-
+ len = selections.length,
+ start = me.getSelection().length;
+
+ me.bulkChange = true;
for (; i < len; i++) {
- this.doSelect(selections[i], true, silent);
+ me.doSelect(selections[i], true, suppressEvent);
}
+ delete me.bulkChange;
+ // fire selection change only if the number of selections differs
+ me.maybeFireSelectionChange(me.getSelection().length !== start);
},
- deselectAll: function() {
- var selections = this.getSelection(),
+ /**
+ * Deselect all records in the view.
+ * @param {Boolean} suppressEvent True to suppress any deselect events
+ */
+ deselectAll: function(suppressEvent) {
+ var me = this,
+ selections = me.getSelection(),
i = 0,
- len = selections.length;
-
+ len = selections.length,
+ start = me.getSelection().length;
+
+ me.bulkChange = true;
for (; i < len; i++) {
- this.doDeselect(selections[i]);
+ me.doDeselect(selections[i], suppressEvent);
}
+ delete me.bulkChange;
+ // fire selection change only if the number of selections differs
+ me.maybeFireSelectionChange(me.getSelection().length !== start);
},
// Provides differentiation of logic between MULTI, SIMPLE and SINGLE
// selection modes. Requires that an event be passed so that we can know
// if user held ctrl or shift.
- selectWithEvent: function(record, e) {
+ selectWithEvent: function(record, e, keepExisting) {
var me = this;
-
+
switch (me.selectionMode) {
case 'MULTI':
if (e.ctrlKey && me.isSelected(record)) {
} else if (e.ctrlKey) {
me.doSelect(record, true, false);
} else if (me.isSelected(record) && !e.shiftKey && !e.ctrlKey && me.selected.getCount() > 1) {
- me.doSelect(record, false, false);
+ me.doSelect(record, keepExisting, false);
} else {
me.doSelect(record, false);
}
tmp,
dontDeselect,
records = [];
-
+
if (me.isLocked()){
return;
}
-
+
if (!keepExisting) {
- me.clearSelections();
+ me.deselectAll(true);
}
-
+
if (!Ext.isNumber(startRow)) {
startRow = store.indexOf(startRow);
- }
+ }
if (!Ext.isNumber(endRow)) {
endRow = store.indexOf(endRow);
}
-
+
// swap values
if (startRow > endRow){
tmp = endRow;
} else {
dontDeselect = (dir == 'up') ? startRow : endRow;
}
-
+
for (i = startRow; i <= endRow; i++){
if (selectedCount == (endRow - startRow + 1)) {
if (i != dontDeselect) {
}
me.doMultiSelect(records, true);
},
-
+
/**
* Selects a record instance by record instance or index.
* @param {Ext.data.Model/Index} records An array of records or an index
deselect: function(records, suppressEvent) {
this.doDeselect(records, suppressEvent);
},
-
+
doSelect: function(records, keepExisting, suppressEvent) {
var me = this,
record;
-
+
if (me.locked) {
return;
}
change = false,
i = 0,
len, record;
-
+
if (me.locked) {
return;
}
-
+
records = !Ext.isArray(records) ? [records] : records;
len = records.length;
if (!keepExisting && selected.getCount() > 0) {
+ if (me.doDeselect(me.getSelection(), suppressEvent) === false) {
+ return;
+ }
+ // TODO - coalesce the selectionchange event in deselect w/the one below...
+ }
+
+ function commit () {
+ selected.add(record);
change = true;
- me.doDeselect(me.getSelection(), true);
}
for (; i < len; i++) {
if (keepExisting && me.isSelected(record)) {
continue;
}
- change = true;
me.lastSelected = record;
- selected.add(record);
- me.onSelectChange(record, true, suppressEvent);
+ me.onSelectChange(record, true, suppressEvent, commit);
}
me.setLastFocused(record, suppressEvent);
// fire selchange if there was a change and there is no suppressEvent flag
doDeselect: function(records, suppressEvent) {
var me = this,
selected = me.selected,
- change = false,
i = 0,
- len, record;
-
+ len, record,
+ attempted = 0,
+ accepted = 0;
+
if (me.locked) {
- return;
+ return false;
}
if (typeof records === "number") {
records = [me.store.getAt(records)];
+ } else if (!Ext.isArray(records)) {
+ records = [records];
+ }
+
+ function commit () {
+ ++accepted;
+ selected.remove(record);
}
- records = !Ext.isArray(records) ? [records] : records;
len = records.length;
+
for (; i < len; i++) {
record = records[i];
- if (selected.remove(record)) {
+ if (me.isSelected(record)) {
if (me.lastSelected == record) {
me.lastSelected = selected.last();
}
- me.onSelectChange(record, false, suppressEvent);
- change = true;
+ ++attempted;
+ me.onSelectChange(record, false, suppressEvent, commit);
}
}
+
// fire selchange if there was a change and there is no suppressEvent flag
- me.maybeFireSelectionChange(change && !suppressEvent);
+ me.maybeFireSelectionChange(accepted > 0 && !suppressEvent);
+ return accepted === attempted;
},
doSingleSelect: function(record, suppressEvent) {
var me = this,
+ changed = false,
selected = me.selected;
-
+
if (me.locked) {
return;
}
if (me.isSelected(record)) {
return;
}
- if (selected.getCount() > 0) {
- me.doDeselect(me.lastSelected, suppressEvent);
+
+ function commit () {
+ me.bulkChange = true;
+ if (selected.getCount() > 0 && me.doDeselect(me.lastSelected, suppressEvent) === false) {
+ delete me.bulkChange;
+ return false;
+ }
+ delete me.bulkChange;
+
+ selected.add(record);
+ me.lastSelected = record;
+ changed = true;
}
- selected.add(record);
- me.lastSelected = record;
- me.onSelectChange(record, true, suppressEvent);
- if (!suppressEvent) {
- me.setLastFocused(record);
+
+ me.onSelectChange(record, true, suppressEvent, commit);
+
+ if (changed) {
+ if (!suppressEvent) {
+ me.setLastFocused(record);
+ }
+ me.maybeFireSelectionChange(!suppressEvent);
}
- me.maybeFireSelectionChange(!suppressEvent);
},
/**
me.lastFocused = record;
me.onLastFocusChanged(recordBeforeLast, record, supressFocus);
},
-
+
/**
* Determines if this record is currently focused.
* @param Ext.data.Record record
// fire selection change as long as true is not passed
// into maybeFireSelectionChange
maybeFireSelectionChange: function(fireEvent) {
- if (fireEvent) {
- var me = this;
+ var me = this;
+ if (fireEvent && !me.bulkChange) {
me.fireEvent('selectionchange', me, me.getSelection());
}
},
getLastSelected: function() {
return this.lastSelected;
},
-
+
getLastFocused: function() {
return this.lastFocused;
},
/**
* Returns an array of the currently selected records.
+ * @return {Array} The selected records
*/
getSelection: function() {
return this.selected.getRange();
/**
* Returns the current selectionMode. SINGLE, MULTI or SIMPLE.
+ * @return {String} The selectionMode
*/
getSelectionMode: function() {
return this.selectionMode;
record = Ext.isNumber(record) ? this.store.getAt(record) : record;
return this.selected.indexOf(record) !== -1;
},
-
+
/**
- * Returns true if there is a selected record.
+ * Returns true if there are any a selected records.
* @return {Boolean}
*/
hasSelection: function() {
}
me.clearSelections();
-
+
if (me.store.indexOf(lastFocused) !== -1) {
// restore the last focus but supress restoring focus
this.setLastFocused(lastFocused, true);
// perform the selection again
me.doSelect(toBeSelected, false, true);
}
-
+
me.maybeFireSelectionChange(change);
},
+ /**
+ * A fast reset of the selections without firing events, updating the ui, etc.
+ * For private usage only.
+ * @private
+ */
clearSelections: function() {
// reset the entire selection to nothing
- var me = this;
- me.selected.clear();
- me.lastSelected = null;
- me.setLastFocused(null);
+ this.selected.clear();
+ this.lastSelected = null;
+ this.setLastFocused(null);
},
// when a record is added to a store
// when a store is cleared remove all selections
// (if there were any)
onStoreClear: function() {
- var me = this,
- selected = this.selected;
-
- if (selected.getCount > 0) {
- selected.clear();
- me.lastSelected = null;
- me.setLastFocused(null);
- me.maybeFireSelectionChange(true);
+ if (this.selected.getCount > 0) {
+ this.clearSelections();
+ this.maybeFireSelectionChange(true);
}
},
onStoreRemove: function(store, record) {
var me = this,
selected = me.selected;
-
+
if (me.locked || !me.pruneRemoved) {
return;
}
}
},
+ /**
+ * Gets the count of selected records.
+ * @return {Number} The number of selected records
+ */
getCount: function() {
return this.selected.getCount();
},
bindComponent: function(cmp) {
}
-});
\ No newline at end of file
+});