@antv/s2
Version:
effective spreadsheet render core lib
97 lines • 3.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formattedFieldValue = exports.groupIconsByPosition = exports.getIconTotalWidth = exports.getActionIconConfig = void 0;
const lodash_1 = require("lodash");
const constant_1 = require("../../common/constant");
const normalizeIcons = (icons = [], position = 'right') => {
return icons.map((icon) => {
if (typeof icon === 'string') {
return { name: icon, position };
}
return icon;
});
};
const normalizeActionIconCfg = (actionIconList = []) => actionIconList.map((actionIcon) => (Object.assign(Object.assign({}, actionIcon), { icons: normalizeIcons(actionIcon.icons) })));
const shouldShowActionIcons = (actionIconCfg, meta, cellType) => {
if (!actionIconCfg) {
return false;
}
const { icons, displayCondition, belongsCell } = actionIconCfg;
if ((0, lodash_1.isEmpty)(icons)) {
return false;
}
if (belongsCell !== cellType) {
return false;
}
// 有任意 iconName 命中展示,则使用当前 headerActionIcon config
return icons.some((icon) => {
var _a, _b, _c;
return (_c = (_b = (_a = icon.displayCondition) === null || _a === void 0 ? void 0 : _a.call(icon, meta, icon.name)) !== null && _b !== void 0 ? _b : displayCondition === null || displayCondition === void 0 ? void 0 : displayCondition(meta, icon.name)) !== null && _c !== void 0 ? _c : true;
});
};
/**
* 返回可用的 icon 配置
* @param actionIconCfgList icon 配置列表
* @param meta 单元格 meta
* @param cellType 单元格类型
* @returns icon 配置
*/
const getActionIconConfig = (actionIconCfgList = [], meta, cellType) => {
const normalizedList = normalizeActionIconCfg(actionIconCfgList);
const iconConfig = (0, lodash_1.find)(normalizedList, (cfg) => shouldShowActionIcons(cfg, meta, cellType));
if (!iconConfig) {
return;
}
// 使用配置的 displayCondition 进一步筛选需要展示的 icon
const displayIcons = iconConfig.icons.filter((icon) => {
var _a, _b, _c, _d;
return (_d = (_b = (_a = icon.displayCondition) === null || _a === void 0 ? void 0 : _a.call(icon, meta, icon.name)) !== null && _b !== void 0 ? _b : (_c = iconConfig.displayCondition) === null || _c === void 0 ? void 0 : _c.call(iconConfig, meta, icon.name)) !== null && _d !== void 0 ? _d : true;
});
return Object.assign(Object.assign({}, iconConfig), { icons: displayIcons });
};
exports.getActionIconConfig = getActionIconConfig;
const getIconTotalWidth = (icons = [], iconTheme) => {
if ((0, lodash_1.isEmpty)(icons)) {
return 0;
}
const { margin, size } = iconTheme;
return icons.reduce((acc, { position }) => acc + size + (position === 'left' ? margin.right : margin.left), 0);
};
exports.getIconTotalWidth = getIconTotalWidth;
const groupIconsByPosition = (icons = [], conditionIcon) => {
const groupedIcons = (0, lodash_1.merge)({
left: [],
right: [],
}, (0, lodash_1.groupBy)(icons, 'position'));
// 基于 condition icon 和 value 是强关联的,所以最好将 condition icon 放在 value 的左右侧
if (conditionIcon) {
if (conditionIcon.position === 'left') {
groupedIcons.left.push(conditionIcon);
}
else {
groupedIcons.right.unshift(conditionIcon);
}
}
return groupedIcons;
};
exports.groupIconsByPosition = groupIconsByPosition;
/**
* 格式化行列头维度名称
* @param meta
* @param fieldName
*/
const formattedFieldValue = (meta, fieldName) => {
const { value, field } = meta;
if (!(0, lodash_1.isEqual)(field, constant_1.EXTRA_FIELD)) {
return {
formattedValue: value,
value,
};
}
return {
formattedValue: fieldName || value,
value,
};
};
exports.formattedFieldValue = formattedFieldValue;
//# sourceMappingURL=header-cell.js.map