@antv/s2
Version:
effective spreadsheet render core lib
202 lines • 9.27 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseDataSet = void 0;
/* eslint-disable import/order */
// eslint-disable-next-line prettier/prettier
const lodash_1 = require("lodash");
const common_1 = require("../common");
const state_controller_1 = require("../utils/condition/state-controller");
const pivot_data_set_1 = require("../utils/dataset/pivot-data-set");
const text_1 = require("../utils/text");
class BaseDataSet {
constructor(spreadsheet) {
/**
* 单元格所对应格式化后的值(用于编辑表)
*/
this.displayFormattedValueMap = new Map();
/**
* 获取字段
*/
this.getField = (field) => {
const realField = (0, lodash_1.isString)(field) ? field : field === null || field === void 0 ? void 0 : field.field;
return realField || field;
};
/**
* 获取字段信息
*/
this.getFieldMeta = (0, lodash_1.memoize)((field, meta) => {
const realField = this.getField(field);
return (0, lodash_1.find)(this.meta || meta, ({ field: currentField }) => {
if (currentField instanceof RegExp) {
return currentField.test(realField);
}
if ((0, lodash_1.isArray)(currentField)) {
return currentField.includes(realField);
}
return currentField === realField;
});
});
/**
* 获取自定义单元格字段描述
* @param cell
*/
this.getCustomFieldDescription = (cell) => {
var _a, _b;
if (!cell) {
return;
}
const meta = (_a = cell.getMeta) === null || _a === void 0 ? void 0 : _a.call(cell);
if (meta === null || meta === void 0 ? void 0 : meta.isTotals) {
return;
}
// 数值单元格
if (cell.cellType === common_1.CellType.DATA_CELL) {
const currentMeta = (0, lodash_1.find)(meta === null || meta === void 0 ? void 0 : meta.spreadsheet.dataCfg.meta, {
field: (meta === null || meta === void 0 ? void 0 : meta.field) || (meta === null || meta === void 0 ? void 0 : meta.value) || (meta === null || meta === void 0 ? void 0 : meta.valueField),
});
return this.getFieldDescription(currentMeta === null || currentMeta === void 0 ? void 0 : currentMeta.field);
}
// 行/列头单元格, 取节点本身描述
return (((_b = meta === null || meta === void 0 ? void 0 : meta.extra) === null || _b === void 0 ? void 0 : _b['description']) ||
this.getFieldDescription(meta === null || meta === void 0 ? void 0 : meta.field));
};
this.spreadsheet = spreadsheet;
}
/**
* 获取字段名称
* @param field
*/
getFieldName(field, defaultValue) {
var _a, _b, _c, _d;
if (field === common_1.SERIES_NUMBER_FIELD) {
return (_a = this.spreadsheet) === null || _a === void 0 ? void 0 : _a.getSeriesNumberText();
}
const realField = this.getField(field);
let headerNode;
if ((_b = this.spreadsheet) === null || _b === void 0 ? void 0 : _b.isCustomHeaderFields()) {
// 兼容自定义行列头场景
headerNode = (_d = (_c = this.spreadsheet) === null || _c === void 0 ? void 0 : _c.facet) === null || _d === void 0 ? void 0 : _d.getHeaderNodes().find((node) => {
var _a;
return node.field === realField && ((_a = node === null || node === void 0 ? void 0 : node.extra) === null || _a === void 0 ? void 0 : _a.isCustomNode);
});
}
const realDefaultValue = (headerNode === null || headerNode === void 0 ? void 0 : headerNode.value) ||
((0, lodash_1.isString)(field) ? field : field === null || field === void 0 ? void 0 : field.title) ||
field;
return (0, lodash_1.get)(this.getFieldMeta(realField, this.meta), 'name', (0, text_1.replaceEmptyFieldValue)(defaultValue !== null && defaultValue !== void 0 ? defaultValue : realDefaultValue));
}
/**
* 获取自定义单元格字段名称
* @param cell
*/
getCustomRowFieldName(cell) {
var _a;
if (!cell) {
return;
}
const meta = (_a = cell.getMeta) === null || _a === void 0 ? void 0 : _a.call(cell);
// 数值单元格, 根据 rowIndex 匹配所对应的行头单元格名字
if (cell.cellType === common_1.CellType.DATA_CELL) {
const rowNode = this.spreadsheet.facet.getRowNodeByIndex(meta === null || meta === void 0 ? void 0 : meta.rowIndex);
return ((rowNode === null || rowNode === void 0 ? void 0 : rowNode.value) || this.getFieldName(rowNode === null || rowNode === void 0 ? void 0 : rowNode.field));
}
// 行/列头单元格, 取节点本身标题
return ((meta === null || meta === void 0 ? void 0 : meta.value) ||
this.getFieldName(meta === null || meta === void 0 ? void 0 : meta.field));
}
/**
* 获得字段格式化方法
* @param field
*/
getFieldFormatter(field) {
const realField = this.getField(field);
const formatter = (0, lodash_1.get)(this.getFieldMeta(realField, this.meta), 'formatter', lodash_1.identity);
// 如果格式化后的值是空,则兜底占位符, 保证导出结果和表格一致: https://github.com/antvis/S2/issues/2808
return (value, data, meta) => {
var _a, _b;
const placeholder = (0, text_1.getEmptyPlaceholder)(meta, (_b = (_a = this.spreadsheet) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.placeholder);
return (0, text_1.getDisplayText)(formatter === null || formatter === void 0 ? void 0 : formatter(value, data, meta), placeholder);
};
}
/**
* 获得字段描述
* @param field
*/
getFieldDescription(field) {
const realField = this.getField(field);
return (0, lodash_1.get)(this.getFieldMeta(realField, this.meta), 'description');
}
/**
* 设置数据配置
* @param dataCfg
*/
setDataCfg(dataCfg) {
var _a, _b, _c;
(_c = (_b = (_a = this.getFieldMeta) === null || _a === void 0 ? void 0 : _a.cache) === null || _b === void 0 ? void 0 : _b.clear) === null || _c === void 0 ? void 0 : _c.call(_b);
const { fields, meta, data, sortParams, filterParams } = this.processDataCfg(dataCfg);
this.fields = fields;
this.meta = meta;
this.originData = data;
this.sortParams = sortParams;
this.filterParams = filterParams;
this.displayData = this.originData;
this.indexesData = {};
}
/**
* 添加 (角头/数值虚拟字段) 格式化信息
*/
processMeta(meta = [], defaultExtraFieldText) {
var _a, _b;
const newMeta = [
...meta,
(0, pivot_data_set_1.generateExtraFieldMeta)(meta, (_b = (_a = this.spreadsheet) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.cornerExtraFieldText, defaultExtraFieldText),
];
return newMeta;
}
getDisplayDataSet() {
return this.displayData || [];
}
isEmpty() {
return (0, lodash_1.isEmpty)(this.getDisplayDataSet());
}
// https://github.com/antvis/S2/issues/2255
getEmptyViewIndexes() {
return [];
}
/**
* 获取 field 的取值范围
* ! 取值范围是以传入的 data 作为的值做计算
* ! 没有考虑总计、小计的情况,如果存在总计、小计,但是 data 里面并没有传,这里计算出来的范围就会不准确
* ! 表格会采用计算明细数据得出,而这些计算出来的总计、小计是和布局结构强相关
* ! 而要根据 totals 配置来覆盖所有情况,场景非常复杂
* @param field values 中的具体数值字段
* @returns 对应 field 的取值范围
*/
getValueRangeByField(field) {
const cacheRange = (0, state_controller_1.getValueRangeState)(this.spreadsheet, field);
if (cacheRange) {
return cacheRange;
}
const fieldValues = (0, lodash_1.compact)((0, lodash_1.map)(this.originData, (item) => {
const value = item[field];
return (0, lodash_1.isNil)(value) ? null : Number.parseFloat(value);
}));
const range = {
maxValue: (0, lodash_1.max)(fieldValues),
minValue: (0, lodash_1.min)(fieldValues),
};
(0, state_controller_1.setValueRangeState)(this.spreadsheet, {
[field]: range,
});
return range;
}
/**
* 是否超过 1 个数值
*/
moreThanOneValue() {
var _a, _b;
return ((_b = (_a = this.fields) === null || _a === void 0 ? void 0 : _a.values) === null || _b === void 0 ? void 0 : _b.length) > 1;
}
}
exports.BaseDataSet = BaseDataSet;
//# sourceMappingURL=base-data-set.js.map