@antv/s2
Version:
effective spreadsheet render core lib
135 lines • 5.37 kB
JavaScript
import { __awaiter } from "tslib";
import { clone, filter, isEmpty, size } from 'lodash';
import { S2Event, } from '../../common';
/**
* 获取下钻缓存
* @param spreadsheet
* @param meta
*/
export const getDrillDownCache = (spreadsheet, meta) => {
const drillDownDataCache = spreadsheet.store.get('drillDownDataCache', []);
const cache = drillDownDataCache === null || drillDownDataCache === void 0 ? void 0 : drillDownDataCache.find(({ rowId }) => rowId === meta.id);
return {
drillDownDataCache,
drillDownCurrentCache: cache,
};
};
/**
* 点击下钻Icon的响应
* @param params
*/
export const handleActionIconClick = (params) => {
const { meta, event, callback } = params;
const { spreadsheet } = meta;
spreadsheet.store.set('drillDownNode', meta);
const { drillDownDataCache, drillDownCurrentCache } = getDrillDownCache(spreadsheet, meta);
const cache = (drillDownCurrentCache === null || drillDownCurrentCache === void 0 ? void 0 : drillDownCurrentCache.drillField)
? [drillDownCurrentCache === null || drillDownCurrentCache === void 0 ? void 0 : drillDownCurrentCache.drillField]
: [];
const disabled = [];
// 父节点已经下钻过的维度不应该再下钻
drillDownDataCache.forEach((val) => {
if (meta.id.includes(val.rowId) && meta.id !== val.rowId) {
disabled.push(val.drillField);
}
});
if (event) {
spreadsheet.emit(S2Event.GLOBAL_ACTION_ICON_CLICK, event);
}
callback({
sheetInstance: spreadsheet,
cacheDrillFields: cache,
disabledFields: disabled,
event,
});
};
/**
* 下钻 icon 默认展示规则
* @param meta 节点
* @returns
*/
export const defaultPartDrillDownDisplayCondition = (meta) => {
const s2 = meta.spreadsheet;
const { fields } = s2.dataCfg;
const iconLevel = size(fields.rows) - 1;
/*
* 当 values 为空时, 会将 dataCfg.fields.valueInCols 强制置为 false, 导致下钻 icon 不显示
* 兼容初始 values 为空, 默认需要显示下钻 icon, 通过下钻动态改变 values 的场景 https://github.com/antvis/S2/issues/1514
*/
const isValueInCols = !isEmpty(fields.values) ? s2.isValueInCols() : true;
// 只有数值置于列头且为树状分层结构时才支持下钻
return (iconLevel <= meta.level &&
s2.isHierarchyTreeType() &&
isValueInCols &&
!meta.isGrandTotals);
};
/**
* 构造下钻功能的 s2 options
* @param options 原始 options
* @param partDrillDown 下钻参数
* @param callback 下钻点击事件
* @returns 新 options
*/
export const buildDrillDownOptions = (options, partDrillDown, callback) => {
var _a;
const nextHeaderIcons = ((_a = options === null || options === void 0 ? void 0 : options.headerActionIcons) === null || _a === void 0 ? void 0 : _a.length)
? [...options.headerActionIcons]
: [];
if (!isEmpty(partDrillDown)) {
const drillDownActionIcon = {
icons: [
{
name: 'DrillDownIcon',
position: 'right',
onClick: ({ meta, event }) => {
meta.spreadsheet.store.set('drillDownNode', meta);
handleActionIconClick({
meta,
event,
callback,
});
},
},
],
belongsCell: 'rowCell',
defaultHide: true,
displayCondition: (partDrillDown === null || partDrillDown === void 0 ? void 0 : partDrillDown.displayCondition) || defaultPartDrillDownDisplayCondition,
};
nextHeaderIcons.push(drillDownActionIcon);
}
return Object.assign(Object.assign({}, options), { headerActionIcons: nextHeaderIcons });
};
export const handleDrillDown = (params) => {
const { fetchData, spreadsheet, drillFields, drillItemsNum = -1 } = params;
spreadsheet.store.set('drillItemsNum', drillItemsNum);
const meta = spreadsheet.store.get('drillDownNode');
const { drillDownDataCache, drillDownCurrentCache } = getDrillDownCache(spreadsheet, meta);
let newDrillDownDataCache = clone(drillDownDataCache);
// 如果当前节点已有下钻缓存,需要清除
if (drillDownCurrentCache) {
newDrillDownDataCache = filter(drillDownDataCache, (cache) => cache.rowId !== meta.id);
}
if (!fetchData) {
return;
}
fetchData(meta, drillFields).then((info) => __awaiter(void 0, void 0, void 0, function* () {
const { drillData, drillField } = info;
spreadsheet.dataSet.transformDrillDownData(drillField, drillData, meta);
if (!isEmpty(drillData)) {
// 缓存到表实例中
const drillLevel = meta.level + 1;
const newDrillDownData = {
rowId: meta.id,
drillLevel,
drillData,
drillField,
};
newDrillDownDataCache.push(newDrillDownData);
spreadsheet.store.set('drillDownDataCache', newDrillDownDataCache);
}
// 重置当前交互
spreadsheet.interaction.reset();
yield spreadsheet.render(false);
}));
};
//# sourceMappingURL=drill-down.js.map