Skip to content

Commit

Permalink
Table: make toggleAllSelection method an instance method(#14075)
Browse files Browse the repository at this point in the history
  • Loading branch information
riho authored and island205 committed Feb 20, 2019
1 parent 4a0e926 commit 7b1d0e2
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions packages/table/src/table-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,35 @@ const TableStore = function(table, initialState = {}) {
selectOnIndeterminate: false
};

this._toggleAllSelection = debounce(10, function(states) {
const data = states.data || [];
if (data.length === 0) return;
const selection = this.states.selection;
// when only some rows are selected (but not all), select or deselect all of them
// depending on the value of selectOnIndeterminate
const value = states.selectOnIndeterminate
? !states.isAllSelected
: !(states.isAllSelected || selection.length);
let selectionChanged = false;
data.forEach((item, index) => {
if (states.selectable) {
if (states.selectable.call(null, item, index) && toggleRowSelection(states, item, value)) {
selectionChanged = true;
}
} else {
if (toggleRowSelection(states, item, value)) {
selectionChanged = true;
}
}
});
const table = this.table;
if (selectionChanged) {
table.$emit('selection-change', selection ? selection.slice() : []);
}
table.$emit('select-all', selection);
states.isAllSelected = value;
});

for (let prop in initialState) {
if (initialState.hasOwnProperty(prop) && this.states.hasOwnProperty(prop)) {
this.states[prop] = initialState[prop];
Expand Down Expand Up @@ -335,36 +364,9 @@ TableStore.prototype.mutations = {
this.updateAllSelected();
},

toggleAllSelection: debounce(10, function(states) {
const data = states.data || [];
if (data.length === 0) return;
const selection = this.states.selection;
// when only some rows are selected (but not all), select or deselect all of them
// depending on the value of selectOnIndeterminate
const value = states.selectOnIndeterminate
? !states.isAllSelected
: !(states.isAllSelected || selection.length);
let selectionChanged = false;

data.forEach((item, index) => {
if (states.selectable) {
if (states.selectable.call(null, item, index) && toggleRowSelection(states, item, value)) {
selectionChanged = true;
}
} else {
if (toggleRowSelection(states, item, value)) {
selectionChanged = true;
}
}
});

const table = this.table;
if (selectionChanged) {
table.$emit('selection-change', selection ? selection.slice() : []);
}
table.$emit('select-all', selection);
states.isAllSelected = value;
})
toggleAllSelection(state) {
this._toggleAllSelection(state);
}
};

const doFlattenColumns = (columns) => {
Expand Down

0 comments on commit 7b1d0e2

Please sign in to comment.