@opentiny/vue-renderless
Version:
An enterprise-class UI component library, support both Vue.js 2 and Vue.js 3, as well as PC and mobile.
196 lines (195 loc) • 6.27 kB
JavaScript
import {
__spreadValues
} from "../../chunk-G2ADBYYC.js";
import { isNull, find, isFunction } from "@opentiny/utils";
import { get, set } from "../static";
const gridSize = ["medium", "small", "mini"];
const getSize = ({ size, $parent }) => size || ($parent && gridSize.includes($parent.size) ? $parent.size : null);
const getFuncText = (content) => isFunction(content) ? content() : content;
const getRowkey = ($table) => $table.rowId;
const getRowid = ($table, row) => {
const rowId = get(row, getRowkey($table));
return rowId ? encodeURIComponent(rowId) : "";
};
const getColumnList = (columns, options = {}, level = 0) => {
const result = [];
columns.forEach((column, index) => {
var _a;
const hasChildren = (_a = column.children) == null ? void 0 : _a.length;
if (!options.hasFixed && column.fixed) {
options.hasFixed = true;
}
if (!options.isCheckable && column.type === "selection") {
options.isCheckable = true;
}
if (level === 0 && !options.isGroup && hasChildren) {
options.isGroup = true;
}
options.columnCaches.push({ colid: column.id, column, index });
result.push.apply(result, hasChildren ? getColumnList(column.children, options, level + 1) : [column]);
});
return result;
};
const repairFixed = (root) => {
const subtree = [];
let fixed;
const recursive = (col) => {
subtree.push(col);
if (!fixed && col.fixed) {
fixed = col.fixed;
}
if (Array.isArray(col.children) && col.children.length > 0) {
col.children.forEach((col2) => recursive(col2));
}
};
recursive(root);
if (fixed) {
subtree.forEach((c) => c.fixed = fixed);
}
};
const getClass = (property, params) => property ? isFunction(property) ? property(params) : property : "";
const getFilters = (filters) => (filters || []).map(({ label, value, data, checked }) => ({
label,
value,
data,
_data: data,
checked: !!checked
}));
const initFilter = (filter) => {
var _a, _b, _c;
const {
values,
value: valueKey = "value",
checked: checkedKey = "checked",
condition,
enumable,
multi,
inputFilter
} = filter;
const value = ((_a = values == null ? void 0 : values.filter) == null ? void 0 : _a.call(values, (i) => i[checkedKey]).map((i) => i[valueKey])) || [];
const hasChecked = (_c = (_b = values == null ? void 0 : values.some) == null ? void 0 : _b.call(values, (i) => i[checkedKey])) != null ? _c : false;
const filterOptions = {
condition: { input: "", relation: "equals", empty: null, type: null, value },
hasFilter: inputFilter && !!(condition == null ? void 0 : condition.input) || enumable && multi && hasChecked || false,
custom: null,
showClear: true
};
return __spreadValues(__spreadValues({}, filterOptions), filter);
};
const formatText = (value) => `${isNull(value) ? "" : value}`;
const setCellValue = (row, column, value) => {
const { format, property } = column;
if (format && format.async && Array.isArray(format.data) && format.data.length > 0 && value) {
let labelText = "";
const { enabled, valueSplit, textSplit } = format.async.splitConfig || {};
const findCellValue = (optionValue) => find(format.data, (col) => {
if (typeof col === "object") {
const colLabel = get(col, format.async.text || "label");
const colValue = get(col, format.async.value || "value");
col.label = colLabel;
return optionValue === colValue || optionValue === colLabel;
}
return optionValue === col;
});
if (enabled) {
const labelTexts = [];
value.split(valueSplit || ",").forEach((item) => {
const findValue = findCellValue(item);
if (findValue) {
labelTexts.push(findValue.label);
}
});
labelText = labelTexts.join(textSplit || ",");
} else {
labelText = findCellValue(value);
}
set(row, column.asyncPrefix + property, labelText ? labelText.label : labelText);
}
set(row, property, value);
};
const hasChildrenList = (item) => item && item.children && item.children.length > 0;
const emitEvent = (vm, type, args) => {
if (vm.tableListeners[type]) {
const params = [].concat(args);
vm.$emit(type, ...params);
}
};
const assemColumn = ($table) => {
const collectColumn = [];
const assem = (columnVms, columns) => {
if (Array.isArray(columnVms)) {
columnVms.forEach((columnVm) => {
const column = columnVm.columnConfig;
const children = [];
if (column) {
columns.push(column);
assem(columnVm.childColumns, children);
column.children = children.length > 0 ? children : null;
}
});
}
};
assem($table.childColumns, collectColumn);
$table.collectColumn = collectColumn;
};
const getCellValue = (row, column) => get(row, column.own.field);
const getListeners = ($attrs, $listeners) => {
const regHyphenate = /\B([A-Z])/g;
const regEventPrefix = /^on[A-Z]/;
const listeners = {};
if ($listeners) {
return $listeners;
}
Object.keys($attrs).forEach((name) => {
const event = $attrs[name];
if (regEventPrefix.test(name) && typeof event === "function") {
listeners[name.slice(2).replace(regHyphenate, "-$1").toLowerCase()] = event;
}
});
return listeners;
};
function dfsCopy(tree, callback, parent = void 0, isTree = false, childrenKey = "children") {
let copy;
if (Array.isArray(tree)) {
copy = [];
tree.forEach((node, index) => {
const copyItem = callback(node, index, parent);
if (copyItem) {
copy.push(copyItem);
}
if (isTree) {
const children = node[childrenKey];
if (children) {
const childrenCopy = dfsCopy(children, callback, node, isTree, childrenKey);
if (copyItem) {
copyItem[childrenKey] = childrenCopy;
}
}
}
});
}
return copy;
}
let rowUniqueId = 0;
const getRowUniqueId = () => `row_${++rowUniqueId}`;
export {
assemColumn,
dfsCopy,
emitEvent,
formatText,
getCellValue,
getClass,
getColumnList,
getFilters,
getFuncText,
getListeners,
getRowUniqueId,
getRowid,
getRowkey,
getSize,
gridSize,
hasChildrenList,
initFilter,
repairFixed,
setCellValue
};