@antv/s2-react-components
Version:
React components for S2
111 lines • 5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSwitcherState = exports.generateSwitchResult = exports.checkItem = exports.moveItem = exports.shouldCrossRows = exports.getMainLayoutClassName = exports.getSwitcherClassName = exports.getSwitcherConfig = void 0;
const tslib_1 = require("tslib");
const s2_1 = require("@antv/s2");
const lodash_1 = require("lodash");
const icons_1 = require("../common/icons");
const constant_1 = require("./constant");
// 是否开启行列维度相互切换
const getSwitcherConfig = (allowExchangeHeader = true) => {
return {
[constant_1.FieldType.Rows]: {
text: (0, s2_1.i18n)('行头'),
icon: icons_1.RowIcon,
droppableType: allowExchangeHeader
? constant_1.DroppableType.Dimensions
: constant_1.DroppableType.Rows,
},
[constant_1.FieldType.Cols]: {
text: (0, s2_1.i18n)('列头'),
icon: icons_1.ColIcon,
droppableType: allowExchangeHeader
? constant_1.DroppableType.Dimensions
: constant_1.DroppableType.Cols,
},
[constant_1.FieldType.Values]: {
text: (0, s2_1.i18n)('值'),
icon: icons_1.ValueIcon,
droppableType: constant_1.DroppableType.Measures,
},
};
};
exports.getSwitcherConfig = getSwitcherConfig;
const getSwitcherClassName = (...classNames) => (0, s2_1.getClassNameWithPrefix)(constant_1.SWITCHER_PREFIX_CLS, ...classNames);
exports.getSwitcherClassName = getSwitcherClassName;
const getMainLayoutClassName = (sheetType) => {
switch (sheetType) {
case 'table':
return (0, exports.getSwitcherClassName)('content', 'one-dimension');
default:
return (0, exports.getSwitcherClassName)('content', 'three-dimensions');
}
};
exports.getMainLayoutClassName = getMainLayoutClassName;
const shouldCrossRows = (sheetType, type) => sheetType === 'table' || type === constant_1.FieldType.Values;
exports.shouldCrossRows = shouldCrossRows;
const moveItem = (source = [], destination = [], droppableSource, droppableDestination) => {
// change order in same column
if (droppableDestination.droppableId === droppableSource.droppableId) {
const updatingDestination = [...destination];
const [removed] = updatingDestination.splice(droppableSource.index, 1);
updatingDestination.splice(droppableDestination.index, 0, removed);
return {
[droppableDestination.droppableId]: updatingDestination,
};
}
// move to other column
const updatingSource = [...source];
const updatingDestination = [...destination];
const [removed] = updatingSource.splice(droppableSource.index, 1);
updatingDestination.splice(droppableDestination.index, 0, removed);
return {
[droppableSource.droppableId]: updatingSource,
[droppableDestination.droppableId]: updatingDestination,
};
};
exports.moveItem = moveItem;
const checkItem = (source = [], checked, id, parentId) => {
const target = Object.assign({}, source.find((item) => item.id === (parentId !== null && parentId !== void 0 ? parentId : id)));
// 有 parentId 时,说明是第二层级的改变
if (parentId) {
target.children = (0, lodash_1.map)(target.children, (item) => {
return Object.assign(Object.assign({}, item), { checked: item.id === id ? checked : item.checked });
});
}
else {
target.checked = checked;
target.children = (0, lodash_1.map)(target.children, (item) => {
return Object.assign(Object.assign({}, item), { checked });
});
}
return source.map((item) => (item.id === target.id ? target : item));
};
exports.checkItem = checkItem;
const generateSwitchResult = (state) => {
const generateFieldResult = (items = []) => {
const flattenValues = (list = []) => (0, lodash_1.flatten)((0, lodash_1.map)(list, (_a) => {
var { children } = _a, rest = tslib_1.__rest(_a, ["children"]);
return [
Object.assign({}, rest),
...flattenValues(children),
];
}));
const allItems = flattenValues(items);
// get all hidden values
const hideItems = (0, lodash_1.filter)(allItems, (item) => item.checked === false);
return {
items: allItems,
hideItems,
};
};
return {
[constant_1.FieldType.Rows]: generateFieldResult(state[constant_1.FieldType.Rows]),
[constant_1.FieldType.Cols]: generateFieldResult(state[constant_1.FieldType.Cols]),
[constant_1.FieldType.Values]: generateFieldResult(state[constant_1.FieldType.Values]),
};
};
exports.generateSwitchResult = generateSwitchResult;
const getSwitcherState = (fields) => (0, lodash_1.mapValues)(fields, 'items');
exports.getSwitcherState = getSwitcherState;
//# sourceMappingURL=util.js.map