yanzhen-design-vue
Version:
145 lines (144 loc) • 4.83 kB
JavaScript
import { unref } from "vue";
import { parseJson } from "@yanzhen-libs/utils";
import { isElement, isArray, isFunction, isBoolean } from "lodash-es";
import { VIRTUAL_CLASS, HIDDEN_CLASS } from "../constants.mjs";
function unrefTbodyElement(target) {
var _a;
const el = unref(target);
if (!el || !isElement(el)) return null;
return ((_a = el.querySelector) == null ? void 0 : _a.call(el, ".vxe-table--body-wrapper table.vxe-table--body tbody")) ?? null;
}
function isTableRow(element) {
if (!element || !isElement(element)) return false;
return element.tagName.toLowerCase() === "tr";
}
function isTableCol(element) {
if (!element || !isElement(element)) return false;
return element.tagName.toLowerCase() === "td";
}
function isTableTbody(element) {
if (!element || !isElement(element)) return false;
return element.tagName.toLowerCase() === "tbody";
}
function unrefTbodyRowElements(target) {
var _a;
const tbody = isTableTbody(target) ? target : unrefTbodyElement(target);
if (!tbody || !isElement(tbody)) return null;
const rows = (_a = tbody == null ? void 0 : tbody.getElementsByTagName) == null ? void 0 : _a.call(tbody, "tr");
return (rows == null ? void 0 : rows.length) > 0 ? [...rows] : null;
}
function unrefTbodyColElements(target) {
var _a;
const tbody = isTableTbody(target) || isTableRow(target) ? target : unrefTbodyElement(target);
if (!tbody || !isElement(tbody)) return null;
const cols = (_a = tbody == null ? void 0 : tbody.getElementsByTagName) == null ? void 0 : _a.call(tbody, "td");
return (cols == null ? void 0 : cols.length) > 0 ? [...cols] : null;
}
const ROW_RECORD_ENUMS = {
$rowIndex: "index",
row: "item",
items: true,
rowid: true
};
function unrefTbodyRowRecord(target, tableRef, callback) {
var _a;
if (isArray(target)) {
return target.map((item) => {
const record = unrefTbodyRowRecord(item, tableRef);
if (record && isFunction(callback)) {
callback(record);
}
return record;
});
}
const getRowNode = (_a = unref(tableRef)) == null ? void 0 : _a.getRowNode;
if (!target || !isTableRow(target) || !isFunction(getRowNode)) return null;
const rowNode = getRowNode(target);
const _record = Object.entries(ROW_RECORD_ENUMS).reduce((prev, [k1, k2]) => {
prev[k1] = k2 === true ? rowNode[k1] : rowNode[k2];
return prev;
}, {});
return {
..._record,
type: "body",
target
};
}
const COL_RECORD_ENUMS = {
$columnIndex: "index",
column: "item",
colid: true
};
function unrefTbodyColRecord(target, tableRef, ...args) {
var _a;
const callback = args.find((value) => isFunction(value));
const isRowRecord = args.find((value) => isBoolean(value));
if (isArray(target)) {
const rowRecords = /* @__PURE__ */ new Map();
return target.map((item) => {
const colRecord2 = unrefTbodyColRecord(item, tableRef, callback);
if (!colRecord2) return null;
const parent = item.parentElement;
let rowRecord = rowRecords.get(parent);
if (isRowRecord) {
if (!rowRecord) {
rowRecord = unrefTbodyRowRecord(parent, tableRef);
rowRecords.set(parent, rowRecord);
}
}
const record = {
...rowRecord ?? {},
...colRecord2
};
if (isFunction(callback)) {
callback(record);
}
return record;
});
}
const getColumnNode = (_a = unref(tableRef)) == null ? void 0 : _a.getColumnNode;
if (!target || !isTableCol(target) || !isFunction(getColumnNode)) return null;
const columnNode = getColumnNode(target);
const colRecord = Object.entries(COL_RECORD_ENUMS).reduce((prev, [k1, k2]) => {
prev[k1] = k2 === true ? columnNode[k1] : columnNode[k2];
return prev;
}, {});
if (isRowRecord) {
const parent = target.parentElement;
const rowRecord = unrefTbodyRowRecord(parent, tableRef) ?? {};
return {
...rowRecord,
...colRecord,
parent,
target,
type: "body"
};
}
return {
...colRecord,
target,
type: "body"
};
}
function isVirtualDom(target) {
var _a;
if (!target || !isElement(target)) return false;
return parseJson((_a = target.getAttribute) == null ? void 0 : _a.call(target, "data-virtual")) ?? String(target.className).includes(VIRTUAL_CLASS) ?? target.classList.contains(VIRTUAL_CLASS);
}
function isHiddenDom(target) {
var _a;
if (!target || !isElement(target)) return false;
return parseJson((_a = target.getAttribute) == null ? void 0 : _a.call(target, "data-hidden")) ?? String(target.className).includes(HIDDEN_CLASS) ?? target.classList.contains(HIDDEN_CLASS);
}
export {
isHiddenDom,
isTableCol,
isTableRow,
isTableTbody,
isVirtualDom,
unrefTbodyColElements,
unrefTbodyColRecord,
unrefTbodyElement,
unrefTbodyRowElements,
unrefTbodyRowRecord
};