yanzhen-design-vue
Version:
163 lines (162 loc) • 4.94 kB
JavaScript
import { unref, mergeProps } from "vue";
import { isString, omit, mergeWith, isElement, isPlainObject, isFunction } from "lodash-es";
import { isEmptyValue, getNumeric } from "@yanzhen-libs/utils";
const commonKey = ["type", "formatter", "separator", "visible"];
function filterColumnUndefined(column) {
for (const [key, value] of Object.entries(column)) {
if (value === void 0) {
delete column[key];
}
}
return column;
}
function handleAColumnProps(props) {
const { field, key, title, width, showOverflow, align, sortable, className } = props;
const info = {
key: key ?? field,
dataIndex: field,
title,
width,
align,
sortable,
ellipsis: showOverflow,
className: isString(className) ? className : void 0,
resizable: true
};
const column = {
...info,
...omit(props, ...Object.keys(info), ...commonKey)
};
return filterColumnUndefined(column);
}
function handleElColumnProps(props) {
const { field, title, width, showOverflow, align, sortable, className } = props;
const info = {
prop: field,
label: title,
width,
align,
sortable,
showOverflowTooltip: showOverflow,
className: isString(className) ? className : void 0,
resizable: true
};
const column = {
...info,
...omit(props, ...Object.keys(info), ...commonKey)
};
return filterColumnUndefined(column);
}
function handleVxeColumnProps(props, tableRef) {
var _a, _b;
const scroll = (_b = (_a = unref(tableRef)) == null ? void 0 : _a.getScroll) == null ? void 0 : _b.call(_a);
const { minWidth, width } = props;
const vxeProps = {
showOverflow: props.showOverflow,
showHeaderOverflow: props.showHeaderOverflow,
showFooterOverflow: props.showFooterOverflow
};
switch (true) {
case (isEmptyValue(minWidth) && isEmptyValue(width)):
vxeProps.minWidth = 110;
break;
}
if ((scroll == null ? void 0 : scroll.virtualX) === true) {
vxeProps.showHeaderOverflow = true;
vxeProps.showFooterOverflow = true;
}
if ((scroll == null ? void 0 : scroll.virtualY) === true) {
console.log("isVirtualY");
vxeProps.showOverflow = true;
vxeProps.showHeaderOverflow = true;
vxeProps.showFooterOverflow = true;
}
const _props = omit(props, ...Object.keys(vxeProps));
return { ..._props, ...vxeProps };
}
function handleVxeTableProps(props, tableRef, ...mergeArgs) {
var _a, _b;
const el = (_a = unref(tableRef)) == null ? void 0 : _a.$el;
const scrollX = mergeWith(
{
enabled: true,
scrollToLeftOnChange: true,
gt: 5,
oSize: 2
},
props == null ? void 0 : props.scrollX
);
const scrollY = mergeWith(
{
enabled: true,
mode: "wheel",
scrollToLeftOnChange: true,
gt: 5,
oSize: 2
},
props == null ? void 0 : props.scrollY
);
if (isElement(el)) {
const clientRect = (_b = el == null ? void 0 : el.getBoundingClientRect) == null ? void 0 : _b.call(el);
if (clientRect && (clientRect == null ? void 0 : clientRect.width) > 0) {
const width = getNumeric(clientRect.width);
const gt = Math.floor(width / 60);
scrollX.gt = gt < 5 ? 5 : gt;
}
if (clientRect && (clientRect == null ? void 0 : clientRect.height) > 0) {
const height = getNumeric(clientRect.height);
const gt = Math.floor(height / 40);
scrollY.gt = gt < 5 ? 5 : gt;
}
}
const vxeProps = {
showOverflow: true,
showHeaderOverflow: true,
showFooterOverflow: true,
scrollX,
scrollY
};
const mergeData = mergeArgs.reduce((prev, value) => {
if (isPlainObject(value)) {
for (const [key, val] of Object.entries(value)) {
if (!isEmptyValue(val)) {
prev[key] = prev[key] ? prev[key].concat([val]) : [].concat([val]);
}
}
}
return prev;
}, {});
function createMergePropsFn(name = "class", datas = []) {
return function(...args) {
let data = {};
for (const _data of datas) {
switch (true) {
case isFunction(_data):
data = mergeProps(data, { [name]: _data(...args) });
break;
case (isString(_data) || isPlainObject(_data)):
data = mergeProps(data, { [name]: _data });
break;
}
}
return data[name];
};
}
for (const [key, datas] of Object.entries(mergeData)) {
const value = vxeProps[key];
if (key.toLowerCase().includes("class")) {
vxeProps[key] = createMergePropsFn("class", [value, props[key], ...datas]);
} else if (key.toLowerCase().includes("style")) {
vxeProps[key] = createMergePropsFn("style", [value, props[key], ...datas]);
}
}
const _props = omit(props, ...Object.keys(vxeProps));
console.log(vxeProps, _props, "handleVxeTableProps");
return { ..._props, ...vxeProps };
}
export {
handleAColumnProps,
handleElColumnProps,
handleVxeColumnProps,
handleVxeTableProps
};