UNPKG

@antv/s2

Version:

effective spreadsheet render core lib

146 lines 6.02 kB
import { __awaiter } from "tslib"; import { clone, isString, last } from 'lodash'; import { DataCell } from '../cell'; import { EXTRA_FIELD, S2Event } from '../common/constant'; import { PivotDataSet } from '../data-set'; import { CustomGridPivotDataSet } from '../data-set/custom-grid-pivot-data-set'; import { PivotFacet } from '../facet'; import { SpreadSheet } from './spread-sheet'; export class PivotSheet extends SpreadSheet { isCustomRowFields() { return this.isCustomHeaderFields('rows'); } getDataSet() { const { dataSet } = this.options; if (dataSet) { return dataSet(this); } if (this.isCustomRowFields()) { return new CustomGridPivotDataSet(this); } return new PivotDataSet(this); } getContentHeight() { return this.facet.getContentHeight(); } /** * Check if is pivot mode */ isPivotMode() { return true; } isTableMode() { return false; } isHierarchyTreeType() { return this.options.hierarchyType === 'tree'; } /** * Scroll Freeze Row Header */ isFrozenRowHeader() { var _a, _b; return !!((_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.frozen) === null || _b === void 0 ? void 0 : _b.rowHeader); } /** * Check if the value is in the columns */ isValueInCols() { var _a, _b; return (_b = (_a = this.dataSet) === null || _a === void 0 ? void 0 : _a.fields) === null || _b === void 0 ? void 0 : _b.valueInCols; } clearDrillDownData(rowNodeId, preventRender) { return __awaiter(this, void 0, void 0, function* () { if (this.dataSet instanceof PivotDataSet) { const cleaned = this.dataSet.clearDrillDownData(rowNodeId); if (cleaned && !preventRender) { // 重置当前交互 this.interaction.reset(); yield this.render(false); } } }); } buildFacet() { var _a, _b, _c, _d, _e; var _f; const defaultCell = (viewMeta) => new DataCell(viewMeta, this); (_a = (_f = this.options).dataCell) !== null && _a !== void 0 ? _a : (_f.dataCell = defaultCell); (_b = this.facet) === null || _b === void 0 ? void 0 : _b.destroy(); this.facet = (_e = (_d = (_c = this.options).facet) === null || _d === void 0 ? void 0 : _d.call(_c, this)) !== null && _e !== void 0 ? _e : new PivotFacet(this); this.facet.render(); } bindEvents() { this.off(S2Event.ROW_CELL_COLLAPSED__PRIVATE); this.off(S2Event.ROW_CELL_ALL_COLLAPSED__PRIVATE); this.on(S2Event.ROW_CELL_COLLAPSED__PRIVATE, this.handleRowCellCollapsed); this.on(S2Event.ROW_CELL_ALL_COLLAPSED__PRIVATE, this.handleRowCellToggleCollapseAll); } handleRowCellCollapsed(data) { return __awaiter(this, void 0, void 0, function* () { var _a; const { isCollapsed, node } = data; const { collapseFields: defaultCollapsedFields } = (_a = this.options.style) === null || _a === void 0 ? void 0 : _a.rowCell; const collapseFields = Object.assign(Object.assign({}, defaultCollapsedFields), { [node.id]: isCollapsed }); this.setOptions({ style: { rowCell: { collapseFields, }, }, }); yield this.render(false); this.emit(S2Event.ROW_CELL_COLLAPSED, { isCollapsed, collapseFields, node, }); }); } handleRowCellToggleCollapseAll(isCollapsed) { return __awaiter(this, void 0, void 0, function* () { const collapseAll = !isCollapsed; this.setOptions({ style: { rowCell: { collapseAll, collapseFields: null, expandDepth: null, }, }, }); yield this.render(false); this.emit(S2Event.ROW_CELL_ALL_COLLAPSED, collapseAll); }); } groupSortByMethod(sortMethod, meta) { return __awaiter(this, void 0, void 0, function* () { var _a; const { rows, columns } = this.dataCfg.fields; const { hideValue } = this.options.style.colCell; const sortField = this.isValueInCols() ? last(rows) : last(columns); const { query, field, value, extra } = meta; const sortQuery = clone(query); let sortValue = (extra === null || extra === void 0 ? void 0 : extra.isCustomNode) ? field : value; // 数值置于列头且隐藏了指标列头的情况, 会默认取第一个指标做组内排序, 需要还原指标列的 query, 所以多指标时请不要这么用…… if (hideValue && this.isValueInCols()) { sortValue = this.dataSet.fields.values[0]; sortQuery[EXTRA_FIELD] = sortValue; } const sortFieldId = isString(sortField) ? sortField : sortField.field; const sortParam = { sortFieldId, sortMethod, sortByMeasure: sortValue, query: sortQuery, }; const prevSortParams = (_a = this.dataCfg.sortParams) === null || _a === void 0 ? void 0 : _a.filter((item) => (item === null || item === void 0 ? void 0 : item.sortFieldId) !== sortField); this.updateSortMethodMap(meta.id, sortMethod, true); const sortParams = [...prevSortParams, sortParam]; this.emit(S2Event.RANGE_SORT, sortParams); this.setDataCfg(Object.assign(Object.assign({}, this.dataCfg), { sortParams })); yield this.render(); }); } } //# sourceMappingURL=pivot-sheet.js.map