@antv/s2-react-components
Version:
React components for S2
100 lines • 4.14 kB
JavaScript
import { __rest } from "tslib";
import { getClassNameWithPrefix, i18n } from '@antv/s2';
import { filter, flatten, map, mapValues } from 'lodash';
import { ColIcon, RowIcon, ValueIcon } from '../common/icons';
import { DroppableType, FieldType, SWITCHER_PREFIX_CLS } from './constant';
// 是否开启行列维度相互切换
export const getSwitcherConfig = (allowExchangeHeader = true) => {
return {
[FieldType.Rows]: {
text: i18n('行头'),
icon: RowIcon,
droppableType: allowExchangeHeader
? DroppableType.Dimensions
: DroppableType.Rows,
},
[FieldType.Cols]: {
text: i18n('列头'),
icon: ColIcon,
droppableType: allowExchangeHeader
? DroppableType.Dimensions
: DroppableType.Cols,
},
[FieldType.Values]: {
text: i18n('值'),
icon: ValueIcon,
droppableType: DroppableType.Measures,
},
};
};
export const getSwitcherClassName = (...classNames) => getClassNameWithPrefix(SWITCHER_PREFIX_CLS, ...classNames);
export const getMainLayoutClassName = (sheetType) => {
switch (sheetType) {
case 'table':
return getSwitcherClassName('content', 'one-dimension');
default:
return getSwitcherClassName('content', 'three-dimensions');
}
};
export const shouldCrossRows = (sheetType, type) => sheetType === 'table' || type === FieldType.Values;
export 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,
};
};
export 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 = map(target.children, (item) => {
return Object.assign(Object.assign({}, item), { checked: item.id === id ? checked : item.checked });
});
}
else {
target.checked = checked;
target.children = map(target.children, (item) => {
return Object.assign(Object.assign({}, item), { checked });
});
}
return source.map((item) => (item.id === target.id ? target : item));
};
export const generateSwitchResult = (state) => {
const generateFieldResult = (items = []) => {
const flattenValues = (list = []) => flatten(map(list, (_a) => {
var { children } = _a, rest = __rest(_a, ["children"]);
return [
Object.assign({}, rest),
...flattenValues(children),
];
}));
const allItems = flattenValues(items);
// get all hidden values
const hideItems = filter(allItems, (item) => item.checked === false);
return {
items: allItems,
hideItems,
};
};
return {
[FieldType.Rows]: generateFieldResult(state[FieldType.Rows]),
[FieldType.Cols]: generateFieldResult(state[FieldType.Cols]),
[FieldType.Values]: generateFieldResult(state[FieldType.Values]),
};
};
export const getSwitcherState = (fields) => mapValues(fields, 'items');
//# sourceMappingURL=util.js.map