phx-react
Version:
PHX REACT
115 lines • 4.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyUrlQuery = exports.findColumnKeyMerge = exports.renderCell = exports.countLeaf = exports.buildHeaderRows = exports.getMaxDepth = void 0;
exports.buildRowSpan = buildRowSpan;
const getMaxDepth = (headers) => {
let max = 1;
for (const item of headers) {
if (item.children && item.children.length > 0) {
max = Math.max(max, 1 + (0, exports.getMaxDepth)(item.children));
}
}
return max;
};
exports.getMaxDepth = getMaxDepth;
const buildHeaderRows = (headers, level = 0, rows = [], maxDepth = 1, startIndex = 0) => {
if (!rows[level])
rows[level] = [];
let currentIndex = startIndex;
for (const item of headers) {
const hasChildren = item.children && item.children.length > 0;
const colSpan = hasChildren ? (0, exports.countLeaf)(item) : 1;
const rowSpan = hasChildren ? 1 : maxDepth - level;
rows[level].push({
name: item.name,
colSpan,
rowSpan,
colIndex: currentIndex,
});
if (hasChildren) {
(0, exports.buildHeaderRows)(item.children, level + 1, rows, maxDepth, currentIndex);
}
currentIndex += colSpan;
}
return rows;
};
exports.buildHeaderRows = buildHeaderRows;
const countLeaf = (header) => {
if (!header.children || header.children.length === 0)
return 1;
return header.children.reduce((t, c) => t + (0, exports.countLeaf)(c), 0);
};
exports.countLeaf = countLeaf;
const renderCell = (data, columnKey, thComponent) => {
const value = data[columnKey];
if (thComponent && (thComponent === null || thComponent === void 0 ? void 0 : thComponent[columnKey])) {
const componentCell = thComponent[columnKey](value, data);
return {
value: componentCell.value,
row: componentCell.row || 1,
children: (componentCell === null || componentCell === void 0 ? void 0 : componentCell.children) || null,
};
}
return { value: value !== null && value !== void 0 ? value : '', row: 1, children: null };
};
exports.renderCell = renderCell;
const findColumnKeyMerge = (mergeConfig, columnIndex) => {
const { columnPosition } = mergeConfig;
const currentKey = columnPosition.find((item) => item.position === columnIndex + 1);
return (currentKey === null || currentKey === void 0 ? void 0 : currentKey.key) || '';
};
exports.findColumnKeyMerge = findColumnKeyMerge;
function buildRowSpan(tableData, mergeConfig) {
const firstRowMap = {};
const dataLength = tableData.length;
for (let index = 0; index < dataLength; index++) {
const data = tableData[index];
for (const config of mergeConfig.columnPosition) {
const { key } = config;
const value = data[key];
if (!firstRowMap[`${key}${value || `idx${index}`}`]) {
firstRowMap[`${key}${value || `idx${index}`}`] = true;
data[`${key}_first_row`] = true;
}
else {
data[`${key}_first_row`] = false;
}
}
}
return tableData;
}
exports.applyUrlQuery = {
get: (searchParams, key) => {
const params = new URLSearchParams(searchParams.toString());
return params.get(key);
},
set: (routerPush, searchParams, data) => {
const params = new URLSearchParams(searchParams.toString());
for (const item of data) {
const { key, value } = item;
params.set(key, value);
}
routerPush(`?${params.toString()}`);
},
delete: (routerPush, searchParams, key) => {
const params = new URLSearchParams(searchParams.toString());
for (const item of key) {
params.delete(item);
}
routerPush(`?${params.toString()}`);
},
multipleMethod: (routerPush, searchParams, data) => {
const params = new URLSearchParams(searchParams.toString());
for (const item of data) {
const { key, value, method } = item;
if (method === 'delete') {
params[method](key);
}
else {
params[method](key, value);
}
}
routerPush(`?${params.toString()}`);
},
};
//# sourceMappingURL=utils.js.map