1export const selectedRows = [];
2export const tableHeaderCheckboxModel = false;
3export const tableHeaderCheckboxIndeterminate = false;
4
5const BVTableSelectableMixin = {
6  methods: {
7    clearSelectedRows(tableRef) {
8      if (tableRef) tableRef.clearSelected();
9    },
10    toggleSelectRow(tableRef, rowIndex) {
11      if (tableRef && rowIndex !== undefined) {
12        tableRef.isRowSelected(rowIndex)
13          ? tableRef.unselectRow(rowIndex)
14          : tableRef.selectRow(rowIndex);
15      }
16    },
17    onRowSelected(selectedRows, totalRowsCount) {
18      if (selectedRows && totalRowsCount !== undefined) {
19        this.selectedRows = selectedRows;
20        if (selectedRows.length === 0) {
21          this.tableHeaderCheckboxIndeterminate = false;
22          this.tableHeaderCheckboxModel = false;
23        } else if (selectedRows.length === totalRowsCount) {
24          this.tableHeaderCheckboxIndeterminate = false;
25          this.tableHeaderCheckboxModel = true;
26        } else {
27          this.tableHeaderCheckboxIndeterminate = true;
28          this.tableHeaderCheckboxModel = true;
29        }
30      }
31    },
32    onChangeHeaderCheckbox(tableRef) {
33      if (tableRef) {
34        if (this.tableHeaderCheckboxModel) tableRef.selectAllRows();
35        else tableRef.clearSelected();
36      }
37    },
38  },
39};
40
41export default BVTableSelectableMixin;
42