UNPKG

@antv/s2

Version:

effective spreadsheet render core lib

125 lines 5.28 kB
import { isEmpty } from 'lodash'; import { CornerNodeType, InteractionName, } from '../../../common'; import { InteractionStateName, InterceptType, S2Event, } from '../../../common/constant'; import { BaseEvent, } from '../../../interaction/base-event'; export class CornerCellClick extends BaseEvent { bindEvents() { this.bindCornerCellClick(); } bindCornerCellClick() { this.spreadsheet.on(S2Event.CORNER_CELL_CLICK, (event) => { const cornerCell = this.spreadsheet.getCell(event.target); if (!cornerCell) { return; } const cornerCellMeta = cornerCell.getMeta(); switch (cornerCellMeta === null || cornerCellMeta === void 0 ? void 0 : cornerCellMeta.cornerType) { case CornerNodeType.Row: this.onRowCornerClick(cornerCellMeta === null || cornerCellMeta === void 0 ? void 0 : cornerCellMeta.field, event); break; case CornerNodeType.Col: this.onColCornerClick(cornerCellMeta === null || cornerCellMeta === void 0 ? void 0 : cornerCellMeta.field, event); break; case CornerNodeType.Series: this.onSeriesCornerClick(cornerCellMeta === null || cornerCellMeta === void 0 ? void 0 : cornerCellMeta.field, event); break; default: break; } }); } onRowCornerClick(field, event) { const rowNodes = this.getSelectedRowNodes(field); this.selectCells(rowNodes, event); } onColCornerClick(field, event) { const colNodes = this.getSelectedColNodes(field); this.selectCells(colNodes, event); } onSeriesCornerClick(field, event) { const seriesNodes = this.spreadsheet.facet.getSeriesNumberNodes(); this.selectCells(seriesNodes, event); } getSelectedRowNodes(field) { const { facet } = this.spreadsheet; // 树状模式只有一列 if (this.spreadsheet.isHierarchyTreeType()) { return facet.getRowNodes(); } if (!this.spreadsheet.isCustomRowFields()) { return facet.getRowNodesByField(field); } // 自定义行头 field 都是独立的, 需要根据 level 区查找. const sampleNode = facet.getRowNodesByField(field)[0]; return facet.getRowNodes(sampleNode === null || sampleNode === void 0 ? void 0 : sampleNode.level); } getSelectedColNodes(field) { const { facet } = this.spreadsheet; if (!this.spreadsheet.isCustomColumnFields()) { return facet.getColNodesByField(field); } // 自定义列头 field 都是独立的, 需要根据 level 区查找. const sampleNode = facet.getColNodesByField(field)[0]; return facet.getColNodes(sampleNode === null || sampleNode === void 0 ? void 0 : sampleNode.level); } getCellMetas(nodes, cellType) { return nodes.map((node) => { return { id: node.id, // 选中角头而高亮的行头, 不需要联动数值单元格, 所以索引设置为 -1 colIndex: -1, rowIndex: -1, type: cellType, }; }); } emitSelectEvent(event, targetCell) { this.spreadsheet.interaction.emitSelectEvent({ event, targetCell, interactionName: InteractionName.CORNER_CELL_CLICK, }); } selectCells(nodes, event) { var _a; const { interaction } = this.spreadsheet; const sample = (_a = nodes[0]) === null || _a === void 0 ? void 0 : _a.belongsCell; const cells = this.getCellMetas(nodes, sample === null || sample === void 0 ? void 0 : sample.cellType); const cornerCell = this.spreadsheet.getCell(event.target); if (sample && interaction.isSelectedCell(sample)) { interaction.reset(); this.emitSelectEvent(event, cornerCell); return; } if (isEmpty(nodes) || isEmpty(cells)) { return; } interaction.addIntercepts([InterceptType.HOVER]); interaction.changeState({ cells, stateName: InteractionStateName.SELECTED, }); interaction.highlightNodes(nodes, InteractionStateName.SELECTED); cornerCell === null || cornerCell === void 0 ? void 0 : cornerCell.updateByState(InteractionStateName.SELECTED); this.showTooltip(event); this.emitSelectEvent(event, cornerCell); } showTooltip(event) { // 角头的选中是维值, 不需要计算数值总和, 显示 [`xx 项已选中`] 即可 const selectedData = this.spreadsheet.interaction.getActiveCells(); const operator = this.getTooltipOperator(event); this.spreadsheet.showTooltipWithInfo(event, [], { operator, data: { summaries: [ { selectedData: selectedData, name: '', value: null, }, ], }, }); } } //# sourceMappingURL=corner-cell-click.js.map