@antv/s2
Version:
effective spreadsheet render core lib
129 lines • 5.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CornerCellClick = void 0;
const lodash_1 = require("lodash");
const common_1 = require("../../../common");
const constant_1 = require("../../../common/constant");
const base_event_1 = require("../../../interaction/base-event");
class CornerCellClick extends base_event_1.BaseEvent {
bindEvents() {
this.bindCornerCellClick();
}
bindCornerCellClick() {
this.spreadsheet.on(constant_1.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 common_1.CornerNodeType.Row:
this.onRowCornerClick(cornerCellMeta === null || cornerCellMeta === void 0 ? void 0 : cornerCellMeta.field, event);
break;
case common_1.CornerNodeType.Col:
this.onColCornerClick(cornerCellMeta === null || cornerCellMeta === void 0 ? void 0 : cornerCellMeta.field, event);
break;
case common_1.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: common_1.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 ((0, lodash_1.isEmpty)(nodes) || (0, lodash_1.isEmpty)(cells)) {
return;
}
interaction.addIntercepts([constant_1.InterceptType.HOVER]);
interaction.changeState({
cells,
stateName: constant_1.InteractionStateName.SELECTED,
});
interaction.highlightNodes(nodes, constant_1.InteractionStateName.SELECTED);
cornerCell === null || cornerCell === void 0 ? void 0 : cornerCell.updateByState(constant_1.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,
},
],
},
});
}
}
exports.CornerCellClick = CornerCellClick;
//# sourceMappingURL=corner-cell-click.js.map