import { forEach, reduce, omit } from 'lodash'; import { Store } from '@/lib/store'; import { getCurrentRowData } from '@/lib/calculate'; import { parseWatcherFn } from '@/lib/utils/vues'; import { RowSelectionStatus } from '@/lib/types'; import { unFlatten } from '@/lib/utils/tree'; import { TREE_PARENT_ID_KEY } from '@/lib/constants/default-props'; export class RowSelectionOperator { private readonly store: Store; constructor(store: Store) { this.store = store; } /** * 从数据配置中获取行选择的信息 */ public flushRowSelection() { if (!this.store.state.featureConfig.rowSelection.active) return; const rowSelectionConfig = this.store.state.featureConfig.rowSelection; const columnId2Field = this.store.state.index.columnId2Field; const data2D = this.store.state.index.data2D; forEach(data2D, (rowData, rowId) => { const formattedRowData = getCurrentRowData(rowData, columnId2Field); this.store.mutation.setRowSelection(rowId, { disabled: parseWatcherFn(rowSelectionConfig.disabled)(formattedRowData), display: parseWatcherFn(rowSelectionConfig.display)(formattedRowData), selected: false, }); }); } /** * 根据变化列刷新可选状态 * @param rowId */ public flushRowSelectionById(rowId: string) { if (!this.store.state.featureConfig.rowSelection.active) return; const rowSelectionConfig = this.store.state.featureConfig.rowSelection; const columnId2Field = this.store.state.index.columnId2Field; const data2D = this.store.state.index.data2D; const currentRowData = data2D[rowId]; const formattedRowData = getCurrentRowData(currentRowData, columnId2Field); this.store.mutation.setRowSelection(rowId, { disabled: parseWatcherFn(rowSelectionConfig.disabled)(formattedRowData), display: parseWatcherFn(rowSelectionConfig.display)(formattedRowData), selected: false, }); } public getSelectedRowData() { const data2D = this.store.state.index.data2D; const rowRecord = this.store.state.index.rowRecord; const rowSelection = this.store.state.index.rowSelection; const rowIndex2Id = this.store.state.index.rowIndex2Id; const columnId2Field = this.store.state.index.columnId2Field; const pushNode = (result, rowId) => { const data = this.store.state.featureConfig.treeData.active ? omit(getCurrentRowData(data2D[rowId], columnId2Field), [ this.store.state.featureConfig.treeData.childrenKey, ]) : getCurrentRowData(data2D[rowId], columnId2Field); result.push(data); }; return reduce( rowIndex2Id, (result, rowId) => { // 对于有子节点的 if (rowRecord[rowId].hasChildren) { const currentSelection = rowSelection[rowId]; if (this.isSelected(currentSelection)) { pushNode(result, rowId); } return result; } else { const currentParentSelection = rowSelection[rowRecord[rowId].parentId]; if (currentParentSelection) { if (this.isSelected(currentParentSelection)) { pushNode(result, rowId); } return result; } else { const currentSelection = rowSelection[rowId]; if (this.isSelected(currentSelection)) { pushNode(result, rowId); } return result; } } }, [], ); } public getTreeSelectedRowData() { const data = this.getSelectedRowData(); const idKey = this.store.state.config.idKey; const childrenKey = this.store.state.featureConfig.treeData.childrenKey; return unFlatten(data, TREE_PARENT_ID_KEY, idKey, childrenKey); } public changeRowDisabled(rowId: string, disabled: boolean) { const rowSelection = this.store.state.index.rowSelection[rowId]; this.store.mutation.setRowSelection(rowId, { ...rowSelection, disabled, }); } public changeRowDisplay(rowId: string, display: boolean) { const rowSelection = this.store.state.index.rowSelection[rowId]; this.store.mutation.setRowSelection(rowId, { ...rowSelection, display, }); } public triggerRow(rowId: string) { const rowSelection = this.store.state.index.rowSelection[rowId]; if (rowSelection.display && !rowSelection.disabled) { this.store.mutation.setRowSelection(rowId, { ...rowSelection, selected: !rowSelection.selected, }); } } public selectAll() { this.triggerAll(true); } public unSelectAll() { this.triggerAll(false); } private isSelected(rowSelection: RowSelectionStatus) { return rowSelection.display && !rowSelection.disabled && rowSelection.selected; } private triggerAll(selected: boolean) { const rowSelections = this.store.state.index.rowSelection; forEach(rowSelections, (rowSelectionStatus, rowId) => { if (rowSelectionStatus.display && !rowSelectionStatus.disabled) { this.store.mutation.setRowSelection(rowId, { ...rowSelectionStatus, selected: selected, }); } }); } }