]>
git.proxmox.com Git - sencha-touch.git/blob - src/src/mixin/Selectable.js
2 * Tracks what records are currently selected in a databound widget. This class is mixed in to {@link Ext.dataview.DataView} and
6 Ext
.define('Ext.mixin.Selectable', {
7 extend
: 'Ext.mixin.Mixin',
12 updateStore
: 'updateStore'
17 * @event beforeselectionchange
18 * Fires before an item is selected.
19 * @param {Ext.mixin.Selectable} this
20 * @preventable selectionchange
21 * @deprecated 2.0.0 Please listen to the {@link #selectionchange} event with an order of `before` instead.
25 * @event selectionchange
26 * Fires when a selection changes.
27 * @param {Ext.mixin.Selectable} this
28 * @param {Ext.data.Model[]} records The records whose selection has changed.
33 * @cfg {Boolean} disableSelection `true` to disable selection.
34 * This configuration will lock the selection model that the DataView uses.
37 disableSelection
: null,
42 * Valid values are `'SINGLE'`, `'SIMPLE'`, and `'MULTI'`.
48 * @cfg {Boolean} allowDeselect
49 * Allow users to deselect a record in a DataView, List or Grid. Only applicable when the Selectable's `mode` is
56 * @cfg {Ext.data.Model} lastSelected
63 * @cfg {Ext.data.Model} lastFocused
70 * @cfg {Boolean} deselectOnContainerClick `true` to deselect current selection when the container body is
74 deselectOnContainerClick
: true
83 selectableEventHooks
: {
84 addrecords
: 'onSelectionStoreAdd',
85 removerecords
: 'onSelectionStoreRemove',
86 updaterecord
: 'onSelectionStoreUpdate',
87 load
: 'refreshSelection',
88 refresh
: 'refreshSelection'
91 constructor: function() {
92 this.selected
= new Ext
.util
.MixedCollection();
93 this.callParent(arguments
);
99 applyMode: function(mode
) {
100 mode
= mode
? mode
.toUpperCase() : 'SINGLE';
101 // set to mode specified unless it doesnt exist, in that case
103 return this.modes
[mode
] ? mode
: 'SINGLE';
109 updateStore: function(newStore
, oldStore
) {
111 bindEvents
= Ext
.apply({}, me
.selectableEventHooks
, { scope
: me
});
113 if (oldStore
&& Ext
.isObject(oldStore
) && oldStore
.isStore
) {
114 if (oldStore
.autoDestroy
) {
118 oldStore
.un(bindEvents
);
120 newStore
.un('clear', 'onSelectionStoreClear', this);
126 newStore
.on(bindEvents
);
127 newStore
.onBefore('clear', 'onSelectionStoreClear', this);
128 me
.refreshSelection();
133 * Selects all records.
134 * @param {Boolean} silent `true` to suppress all select events.
136 selectAll: function(silent
) {
138 selections
= me
.getStore().getRange();
140 me
.select(selections
, true, silent
);
144 * Deselects all records.
146 deselectAll: function(supress
) {
148 selections
= me
.getStore().getRange();
150 me
.deselect(selections
, supress
);
153 me
.setLastSelected(null);
154 me
.setLastFocused(null);
157 // Provides differentiation of logic between MULTI, SIMPLE and SINGLE
159 selectWithEvent: function(record
) {
161 isSelected
= me
.isSelected(record
);
162 switch (me
.getMode()) {
169 me
.select(record
, true);
173 if (me
.getAllowDeselect() && isSelected
) {
174 // if allowDeselect is on and this record isSelected, deselect it
177 // select the record and do NOT maintain existing selections
178 me
.select(record
, false);
185 * Selects a range of rows if the selection model {@link Ext.mixin.Selectable#getDisableSelection} is not locked.
186 * All rows in between `startRecord` and `endRecord` are also selected.
187 * @param {Number} startRecord The index of the first row in the range.
188 * @param {Number} endRecord The index of the last row in the range.
189 * @param {Boolean} [keepExisting] `true` to retain existing selections.
191 selectRange: function(startRecord
, endRecord
, keepExisting
) {
193 store
= me
.getStore(),
197 if (me
.getDisableSelection()) {
202 if (startRecord
> endRecord
) {
204 endRecord
= startRecord
;
208 for (i
= startRecord
; i
<= endRecord
; i
++) {
209 records
.push(store
.getAt(i
));
211 this.doMultiSelect(records
, keepExisting
);
215 * Adds the given records to the currently selected set.
216 * @param {Ext.data.Model/Array/Number} records The records to select.
217 * @param {Boolean} keepExisting If `true`, the existing selection will be added to (if not, the old selection is replaced).
218 * @param {Boolean} suppressEvent If `true`, the `select` event will not be fired.
220 select: function(records
, keepExisting
, suppressEvent
) {
224 if (me
.getDisableSelection()) {
228 if (typeof records
=== "number") {
229 records
= [me
.getStore().getAt(records
)];
236 if (me
.getMode() == "SINGLE" && records
) {
237 record
= records
.length
? records
[0] : records
;
238 me
.doSingleSelect(record
, suppressEvent
);
240 me
.doMultiSelect(records
, keepExisting
, suppressEvent
);
245 * Selects a single record.
248 doSingleSelect: function(record
, suppressEvent
) {
250 selected
= me
.selected
;
252 if (me
.getDisableSelection()) {
257 // should we also check beforeselect?
258 if (me
.isSelected(record
)) {
262 if (selected
.getCount() > 0) {
263 me
.deselect(me
.getLastSelected(), suppressEvent
);
266 selected
.add(record
);
267 me
.setLastSelected(record
);
268 me
.onItemSelect(record
, suppressEvent
);
269 me
.setLastFocused(record
);
271 if (!suppressEvent
) {
272 me
.fireSelectionChange([record
]);
277 * Selects a set of multiple records.
280 doMultiSelect: function(records
, keepExisting
, suppressEvent
) {
281 if (records
=== null || this.getDisableSelection()) {
284 records
= !Ext
.isArray(records
) ? [records
] : records
;
287 selected
= me
.selected
,
293 if (!keepExisting
&& selected
.getCount() > 0) {
295 me
.deselect(me
.getSelection(), true);
297 for (; i
< ln
; i
++) {
299 if (keepExisting
&& me
.isSelected(record
)) {
303 me
.setLastSelected(record
);
304 selected
.add(record
);
305 if (!suppressEvent
) {
306 me
.setLastFocused(record
);
309 me
.onItemSelect(record
, suppressEvent
);
311 if (change
&& !suppressEvent
) {
312 this.fireSelectionChange(records
);
317 * Deselects the given record(s). If many records are currently selected, it will only deselect those you pass in.
318 * @param {Number/Array/Ext.data.Model} records The record(s) to deselect. Can also be a number to reference by index.
319 * @param {Boolean} suppressEvent If `true` the `deselect` event will not be fired.
321 deselect: function(records
, suppressEvent
) {
324 if (me
.getDisableSelection()) {
328 records
= Ext
.isArray(records
) ? records
: [records
];
330 var selected
= me
.selected
,
333 store
= me
.getStore(),
337 for (; i
< ln
; i
++) {
340 if (typeof record
=== 'number') {
341 record
= store
.getAt(record
);
344 if (selected
.remove(record
)) {
345 if (me
.getLastSelected() == record
) {
346 me
.setLastSelected(selected
.last());
351 me
.onItemDeselect(record
, suppressEvent
);
355 if (change
&& !suppressEvent
) {
356 me
.fireSelectionChange(records
);
361 * Sets a record as the last focused record. This does NOT mean
362 * that the record has been selected.
363 * @param {Ext.data.Record} newRecord
364 * @param {Ext.data.Record} oldRecord
366 updateLastFocused: function(newRecord
, oldRecord
) {
367 this.onLastFocusChanged(oldRecord
, newRecord
);
370 fireSelectionChange: function(records
) {
372 //<deprecated product=touch since=2.0>
373 me
.fireAction('beforeselectionchange', [me
], function() {
375 me
.fireAction('selectionchange', [me
, records
], 'getSelection');
376 //<deprecated product=touch since=2.0>
382 * Returns an array of the currently selected records.
383 * @return {Array} An array of selected records.
385 getSelection: function() {
386 return this.selected
.getRange();
390 * Returns `true` if the specified row is selected.
391 * @param {Ext.data.Model/Number} record The record or index of the record to check.
394 isSelected: function(record
) {
395 record
= Ext
.isNumber(record
) ? this.getStore().getAt(record
) : record
;
396 return this.selected
.indexOf(record
) !== -1;
400 * Returns `true` if there is a selected record.
403 hasSelection: function() {
404 return this.selected
.getCount() > 0;
410 refreshSelection: function() {
412 selections
= me
.getSelection();
414 me
.deselectAll(true);
415 if (selections
.length
) {
416 me
.select(selections
, false, true);
420 // prune records from the SelectionModel if
421 // they were selected at the time they were
423 onSelectionStoreRemove: function(store
, records
) {
425 selected
= me
.selected
,
429 if (me
.getDisableSelection()) {
433 for (i
= 0; i
< ln
; i
++) {
435 if (selected
.remove(record
)) {
436 if (me
.getLastSelected() == record
) {
437 me
.setLastSelected(null);
439 if (me
.getLastFocused() == record
) {
440 me
.setLastFocused(null);
442 me
.fireSelectionChange([record
]);
447 onSelectionStoreClear: function(store
) {
448 var records
= store
.getData().items
;
449 this.onSelectionStoreRemove(store
, records
);
453 * Returns the number of selections.
456 getSelectionCount: function() {
457 return this.selected
.getCount();
460 onSelectionStoreAdd
: Ext
.emptyFn
,
461 onSelectionStoreUpdate
: Ext
.emptyFn
,
462 onItemSelect
: Ext
.emptyFn
,
463 onItemDeselect
: Ext
.emptyFn
,
464 onLastFocusChanged
: Ext
.emptyFn
,
465 onEditorKey
: Ext
.emptyFn
468 * Selects a record instance by record instance or index.
469 * @member Ext.mixin.Selectable
471 * @param {Ext.data.Model/Number} records An array of records or an index.
472 * @param {Boolean} keepExisting
473 * @param {Boolean} suppressEvent Set to `false` to not fire a select event.
474 * @deprecated 2.0.0 Please use {@link #select} instead.
478 * Deselects a record instance by record instance or index.
479 * @member Ext.mixin.Selectable
481 * @param {Ext.data.Model/Number} records An array of records or an index.
482 * @param {Boolean} suppressEvent Set to `false` to not fire a deselect event.
483 * @deprecated 2.0.0 Please use {@link #deselect} instead.
487 * Returns the selection mode currently used by this Selectable.
488 * @member Ext.mixin.Selectable
489 * @method getSelectionMode
490 * @return {String} The current mode.
491 * @deprecated 2.0.0 Please use {@link #getMode} instead.
495 * Returns the array of previously selected items.
496 * @member Ext.mixin.Selectable
497 * @method getLastSelected
498 * @return {Array} The previous selection.
503 * Returns `true` if the Selectable is currently locked.
504 * @member Ext.mixin.Selectable
506 * @return {Boolean} True if currently locked
507 * @deprecated 2.0.0 Please use {@link #getDisableSelection} instead.
511 * This was an internal function accidentally exposed in 1.x and now deprecated. Calling it has no effect
512 * @member Ext.mixin.Selectable
513 * @method setLastFocused
518 * Deselects any currently selected records and clears all stored selections.
519 * @member Ext.mixin.Selectable
520 * @method clearSelections
521 * @deprecated 2.0.0 Please use {@link #deselectAll} instead.
525 * Returns the number of selections.
526 * @member Ext.mixin.Selectable
529 * @deprecated 2.0.0 Please use {@link #getSelectionCount} instead.
533 * @cfg {Boolean} locked
534 * @inheritdoc Ext.mixin.Selectable#disableSelection
535 * @deprecated 2.0.0 Please use {@link #disableSelection} instead.