UNPKG

winbox-ui-next

Version:

We Design Vue 2.0: A Vue.js 3 UI Library

289 lines (288 loc) 8.01 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); import { isUndefined, isArray, isNull } from "../_utils/is.js"; import "../_utils/vue-utils.js"; const getDataColumnsNumber = (columns) => { let count = 0; const travelColumns = (columns2) => { if (isArray(columns2) && columns2.length > 0) { for (const item of columns2) { if (!item.children) { count += 1; } else { travelColumns(item.children); } } } }; travelColumns(columns); return count; }; const getTotalHeaderRows = (columns) => { let count = 0; if (isArray(columns) && columns.length > 0) { count = 1; for (const item of columns) { if (item.children) { const depth = getTotalHeaderRows(item.children); if (depth > 0) { count = Math.max(count, depth + 1); } } } } return count; }; const setParentFixed = (column, fixed) => { let { parent } = column; while (parent) { if (parent.fixed === fixed) { if (fixed === "left") { parent.isLastLeftFixed = true; } else { parent.isFirstRightFixed = true; } } parent = parent.parent; } }; const getGroupColumns = (columns, columnMap) => { const totalHeaderRows = getTotalHeaderRows(columns); columnMap.clear(); const dataColumns = []; const groupColumns = [...Array(totalHeaderRows)].map(() => []); let lastLeftFixedIndex; let firstRightFixedIndex; const travelColumns = (columns2, { level = 0, parent, fixed } = {}) => { var _a; for (const item of columns2) { const cell = __spreadProps(__spreadValues({}, item), { parent }); if (isArray(cell.children)) { const colSpan = getDataColumnsNumber(cell.children); if (colSpan > 1) { cell.colSpan = colSpan; } groupColumns[level].push(cell); travelColumns(cell.children, { level: level + 1, parent: cell, fixed: cell.fixed }); } else { const rowSpan = totalHeaderRows - level; if (rowSpan > 1) { cell.rowSpan = rowSpan; } if (fixed || cell.fixed) { cell.fixed = (_a = cell.fixed) != null ? _a : fixed; if (cell.fixed === "left") { lastLeftFixedIndex = dataColumns.length; } else if (isUndefined(firstRightFixedIndex)) { firstRightFixedIndex = dataColumns.length; } } if (isUndefined(cell.dataIndex) || isNull(cell.dataIndex)) { cell.dataIndex = `__w_data_index_${dataColumns.length}`; } columnMap.set(cell.dataIndex, cell); dataColumns.push(cell); groupColumns[level].push(cell); } } }; travelColumns(columns); if (!isUndefined(lastLeftFixedIndex)) { dataColumns[lastLeftFixedIndex].isLastLeftFixed = true; setParentFixed(dataColumns[lastLeftFixedIndex], "left"); } if (!isUndefined(firstRightFixedIndex)) { dataColumns[firstRightFixedIndex].isFirstRightFixed = true; setParentFixed(dataColumns[firstRightFixedIndex], "right"); } return { dataColumns, groupColumns }; }; const getOperationColumnIndex = (operations, name) => { for (let i = 0; i < operations.length; i++) { if (operations[i].name === name) { return i; } } return -1; }; const getOperationFixedNumber = (column, operations) => { var _a; const index = getOperationColumnIndex(operations, column.name); if (index <= 0) { return 0; } let count = 0; const _operations = operations.slice(0, index); for (const item of _operations) { count += (_a = item.width) != null ? _a : 0; } return count; }; const getFirstDataColumn = (column) => { if (column.children && column.children.length > 0) return getFirstDataColumn(column.children[0]); return column; }; const getLastDataColumn = (column) => { if (column.children && column.children.length > 0) return getFirstDataColumn(column.children[column.children.length - 1]); return column; }; const getFixedNumber = (column, { dataColumns, operations }) => { var _a, _b; let count = 0; if (column.fixed === "left") { for (const item of operations) { count += (_a = item.width) != null ? _a : 40; } const first = getFirstDataColumn(column); for (const item of dataColumns) { if (first.dataIndex === item.dataIndex) { break; } count += (_b = item.width) != null ? _b : 0; } return count; } const last = getLastDataColumn(column); for (let i = dataColumns.length - 1; i > 0; i--) { const item = dataColumns[i]; if (last.dataIndex === item.dataIndex) { break; } if (item.fixed === "right") { count += item.width; } } return count; }; const getOperationFixedCls = (prefixCls, column) => { if (column.fixed) { return [ `${prefixCls}-col-fixed-left`, { [`${prefixCls}-col-fixed-left-last`]: column.isLastLeftFixed } ]; } return []; }; const getFixedCls = (prefixCls, column) => { if (column.fixed === "left") { return [ `${prefixCls}-col-fixed-left`, { [`${prefixCls}-col-fixed-left-last`]: column.isLastLeftFixed } ]; } if (column.fixed === "right") { return [ `${prefixCls}-col-fixed-right`, { [`${prefixCls}-col-fixed-right-first`]: column.isFirstRightFixed } ]; } return []; }; const getStyle = (column, { dataColumns, operations }) => { if (column.fixed) { const offset = `${getFixedNumber(column, { dataColumns, operations })}px`; if (column.fixed === "left") { return { left: offset }; } return { right: offset }; } return {}; }; const getOperationStyle = (column, operations) => { if (column.fixed) { return { left: `${getOperationFixedNumber(column, operations)}px` }; } return {}; }; const spliceFromPath = (data, path, item) => { var _a; let parent = data; for (let i = 0; i < path.length; i++) { const index = path[i]; const isLast = i >= path.length - 1; if (isLast) { if (item) { parent.splice(index, 0, item); } else { return parent.splice(index, 1)[0]; } } parent = (_a = parent[index].children) != null ? _a : []; } return void 0; }; const getLeafKeys = (record) => { const keys = []; if (record.children) { for (const item of record.children) { if (item.isLeaf) { keys.push(item.key); } else { keys.push(...getLeafKeys(item)); } } } return keys; }; const getSelectionStatus = (selectedRowKeys, leafKeys) => { let checked = false; let indeterminate = false; const selectedLeafKeys = leafKeys.filter((key) => selectedRowKeys.includes(key)); if (selectedLeafKeys.length > 0) { if (selectedLeafKeys.length >= leafKeys.length) { checked = true; } else { indeterminate = true; } } return { checked, indeterminate }; }; export { getFixedCls, getFixedNumber, getGroupColumns, getLeafKeys, getOperationFixedCls, getOperationFixedNumber, getOperationStyle, getSelectionStatus, getStyle, spliceFromPath };