UNPKG

@antv/s2

Version:

effective spreadsheet render core lib

143 lines 6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleDrillDown = exports.buildDrillDownOptions = exports.defaultPartDrillDownDisplayCondition = exports.handleActionIconClick = exports.getDrillDownCache = void 0; const tslib_1 = require("tslib"); const lodash_1 = require("lodash"); const common_1 = require("../../common"); /** * 获取下钻缓存 * @param spreadsheet * @param meta */ 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, }; }; exports.getDrillDownCache = getDrillDownCache; /** * 点击下钻Icon的响应 * @param params */ const handleActionIconClick = (params) => { const { meta, event, callback } = params; const { spreadsheet } = meta; spreadsheet.store.set('drillDownNode', meta); const { drillDownDataCache, drillDownCurrentCache } = (0, exports.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(common_1.S2Event.GLOBAL_ACTION_ICON_CLICK, event); } callback({ sheetInstance: spreadsheet, cacheDrillFields: cache, disabledFields: disabled, event, }); }; exports.handleActionIconClick = handleActionIconClick; /** * 下钻 icon 默认展示规则 * @param meta 节点 * @returns */ const defaultPartDrillDownDisplayCondition = (meta) => { const s2 = meta.spreadsheet; const { fields } = s2.dataCfg; const iconLevel = (0, lodash_1.size)(fields.rows) - 1; /* * 当 values 为空时, 会将 dataCfg.fields.valueInCols 强制置为 false, 导致下钻 icon 不显示 * 兼容初始 values 为空, 默认需要显示下钻 icon, 通过下钻动态改变 values 的场景 https://github.com/antvis/S2/issues/1514 */ const isValueInCols = !(0, lodash_1.isEmpty)(fields.values) ? s2.isValueInCols() : true; // 只有数值置于列头且为树状分层结构时才支持下钻 return (iconLevel <= meta.level && s2.isHierarchyTreeType() && isValueInCols && !meta.isGrandTotals); }; exports.defaultPartDrillDownDisplayCondition = defaultPartDrillDownDisplayCondition; /** * 构造下钻功能的 s2 options * @param options 原始 options * @param partDrillDown 下钻参数 * @param callback 下钻点击事件 * @returns 新 options */ 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 (!(0, lodash_1.isEmpty)(partDrillDown)) { const drillDownActionIcon = { icons: [ { name: 'DrillDownIcon', position: 'right', onClick: ({ meta, event }) => { meta.spreadsheet.store.set('drillDownNode', meta); (0, exports.handleActionIconClick)({ meta, event, callback, }); }, }, ], belongsCell: 'rowCell', defaultHide: true, displayCondition: (partDrillDown === null || partDrillDown === void 0 ? void 0 : partDrillDown.displayCondition) || exports.defaultPartDrillDownDisplayCondition, }; nextHeaderIcons.push(drillDownActionIcon); } return Object.assign(Object.assign({}, options), { headerActionIcons: nextHeaderIcons }); }; exports.buildDrillDownOptions = buildDrillDownOptions; const handleDrillDown = (params) => { const { fetchData, spreadsheet, drillFields, drillItemsNum = -1 } = params; spreadsheet.store.set('drillItemsNum', drillItemsNum); const meta = spreadsheet.store.get('drillDownNode'); const { drillDownDataCache, drillDownCurrentCache } = (0, exports.getDrillDownCache)(spreadsheet, meta); let newDrillDownDataCache = (0, lodash_1.clone)(drillDownDataCache); // 如果当前节点已有下钻缓存,需要清除 if (drillDownCurrentCache) { newDrillDownDataCache = (0, lodash_1.filter)(drillDownDataCache, (cache) => cache.rowId !== meta.id); } if (!fetchData) { return; } fetchData(meta, drillFields).then((info) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const { drillData, drillField } = info; spreadsheet.dataSet.transformDrillDownData(drillField, drillData, meta); if (!(0, lodash_1.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); })); }; exports.handleDrillDown = handleDrillDown; //# sourceMappingURL=drill-down.js.map