@antv/s2
Version:
effective spreadsheet render core lib
150 lines • 6.39 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PivotSheet = void 0;
const tslib_1 = require("tslib");
const lodash_1 = require("lodash");
const cell_1 = require("../cell");
const constant_1 = require("../common/constant");
const data_set_1 = require("../data-set");
const custom_grid_pivot_data_set_1 = require("../data-set/custom-grid-pivot-data-set");
const facet_1 = require("../facet");
const spread_sheet_1 = require("./spread-sheet");
class PivotSheet extends spread_sheet_1.SpreadSheet {
isCustomRowFields() {
return this.isCustomHeaderFields('rows');
}
getDataSet() {
const { dataSet } = this.options;
if (dataSet) {
return dataSet(this);
}
if (this.isCustomRowFields()) {
return new custom_grid_pivot_data_set_1.CustomGridPivotDataSet(this);
}
return new data_set_1.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 tslib_1.__awaiter(this, void 0, void 0, function* () {
if (this.dataSet instanceof data_set_1.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 cell_1.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 facet_1.PivotFacet(this);
this.facet.render();
}
bindEvents() {
this.off(constant_1.S2Event.ROW_CELL_COLLAPSED__PRIVATE);
this.off(constant_1.S2Event.ROW_CELL_ALL_COLLAPSED__PRIVATE);
this.on(constant_1.S2Event.ROW_CELL_COLLAPSED__PRIVATE, this.handleRowCellCollapsed);
this.on(constant_1.S2Event.ROW_CELL_ALL_COLLAPSED__PRIVATE, this.handleRowCellToggleCollapseAll);
}
handleRowCellCollapsed(data) {
return tslib_1.__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(constant_1.S2Event.ROW_CELL_COLLAPSED, {
isCollapsed,
collapseFields,
node,
});
});
}
handleRowCellToggleCollapseAll(isCollapsed) {
return tslib_1.__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(constant_1.S2Event.ROW_CELL_ALL_COLLAPSED, collapseAll);
});
}
groupSortByMethod(sortMethod, meta) {
return tslib_1.__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() ? (0, lodash_1.last)(rows) : (0, lodash_1.last)(columns);
const { query, field, value, extra } = meta;
const sortQuery = (0, lodash_1.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[constant_1.EXTRA_FIELD] = sortValue;
}
const sortFieldId = (0, lodash_1.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(constant_1.S2Event.RANGE_SORT, sortParams);
this.setDataCfg(Object.assign(Object.assign({}, this.dataCfg), { sortParams }));
yield this.render();
});
}
}
exports.PivotSheet = PivotSheet;
//# sourceMappingURL=pivot-sheet.js.map