UNPKG

yanzhen-design-vue

Version:

196 lines (195 loc) 6.68 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const vue = require("vue"); const utilsVue = require("@yanzhen-libs/utils-vue"); const utils = require("@yanzhen-libs/utils"); const lodashEs = require("lodash-es"); const index = require("../utils/index.cjs"); const constants = require("../constants.cjs"); function useVxeTableColumnSlotRecord(tableRef) { const contentRectRecords = vue.ref({}); const rowRectRecords = vue.ref({}); let resizeCache; function handleExtractArgs(...args) { const extract = { name: void 0, record: void 0 }; function isRecordParam(record) { if (!lodashEs.isPlainObject(record)) return false; const keys = Object.keys(record); switch (true) { case keys.includes("column"): case keys.includes("row"): return true; } return false; } for (const data of args) { switch (true) { case (utils.isEmptyValue(extract["name"]) && lodashEs.isString(data)): extract["name"] = data; break; case (utils.isEmptyValue(extract["record"]) && isRecordParam(data)): extract["record"] = data; break; } } return extract; } function isFixedRecord(...args) { var _a; const { record } = handleExtractArgs(...args); const fixed = (_a = record == null ? void 0 : record.column) == null ? void 0 : _a.fixed; return !utils.isEmptyValue(fixed); } function isHiddenRecord(...args) { const { record, name } = handleExtractArgs(...args); switch (true) { case (record == null ? void 0 : record.isHidden) === true: return true; case isFixedRecord(record): case name === "edit": return false; } return void 0; } function getRecordId(...args) { var _a, _b; const { record } = handleExtractArgs(...args); if (!record) return; const getRowid = (_a = vue.unref(tableRef)) == null ? void 0 : _a.getRowid; const getColid = (_b = vue.unref(tableRef)) == null ? void 0 : _b.getColid; let { $rowIndex = 0, row, column, type, rowid, colid } = record; if (utils.isEmptyValue(rowid)) rowid = getRowid == null ? void 0 : getRowid(row); if (utils.isEmptyValue(colid)) colid = getColid == null ? void 0 : getColid(column); const recordIds = !utils.isEmptyValue(rowid) ? [type, rowid, colid] : [type, $rowIndex, colid]; return recordIds.join(":"); } function getElementRect(el, callback) { function toJSON() { var _a; const contentRect = ((_a = el == null ? void 0 : el.getBoundingClientRect) == null ? void 0 : _a.call(el).toJSON()) ?? {}; return { ...contentRect, realHeight: (el == null ? void 0 : el.offsetHeight) ?? 0, realWidth: (el == null ? void 0 : el.offsetWidth) ?? 0, target: el }; } if (lodashEs.isElement(el)) { if (lodashEs.isFunction(callback)) { if (!resizeCache) resizeCache = utilsVue.createResizeObserver(); resizeCache.add(el, callback); } const target = toJSON(); const contentRect = new Proxy(toJSON(), { get(_target, p, receiver) { if (p === "target") return el; if (p === "toJSON") return toJSON; const json = toJSON(); const value = Reflect.get(_target, p, receiver); const newValue = Reflect.get(json, p, receiver); if (newValue !== value) { target[p] = newValue; } return newValue; } }); return { target: el, contentRect, realHeight: contentRect.realHeight, realWidth: contentRect.realWidth, toJSON() { return toJSON(); } }; } return null; } function getNodeRect(...args) { const { name = "default", record } = handleExtractArgs(...args); if (!record) return; const recordId = getRecordId(name, record); return vue.unref(contentRectRecords)[recordId]; } function updateNodeRect(...args) { var _a; const { name = "default", record } = handleExtractArgs(...args); if (!record) return; const recordId = getRecordId(name, record); const target = record == null ? void 0 : record.target; const newContentRect = (_a = getElementRect(target)) == null ? void 0 : _a.contentRect; const contentRect = vue.unref(contentRectRecords)[recordId]; if (newContentRect !== contentRect) { vue.unref(contentRectRecords)[recordId] = newContentRect; } } function updateRowsNodeRect(tbody) { const rows = index.unrefTbodyRowElements(tbody); if (rows && rows.length > 0) { index.unrefTbodyRowRecord(rows, tableRef, (record) => { updateRowNodeRect(record); }); } } function updateRowNodeRect(...args) { const { record } = handleExtractArgs(...args); if (!record || !record.rowid) return; const el = record == null ? void 0 : record.target; const cells = el == null ? void 0 : el.querySelectorAll(`td:not(.${constants.VIRTUAL_CLASS})`); const cellHeights = [...cells ?? []].reduce((prev, el2) => { var _a, _b; if (!index.isHiddenDom(el2)) { const cell = el2.querySelector(`div.${constants.CELL_CLASS}`); if (lodashEs.isElement(cell)) { const realHeight = (_b = (_a = getElementRect(cell)) == null ? void 0 : _a.contentRect) == null ? void 0 : _b.realHeight; prev.push(realHeight); } } return prev; }, []); const newValue = lodashEs.max(cellHeights); const value = vue.unref(rowRectRecords)[record.rowid]; if (newValue !== value) { vue.unref(rowRectRecords)[record.rowid] = newValue; } } function getRowNodeRectHeight(...args) { const { record } = handleExtractArgs(...args); if (!record || !record.rowid) return; return vue.unref(rowRectRecords)[record.rowid]; } function tableCellStyle(record) { const height = getRowNodeRectHeight(record); return { "--vxe-cell-min-height": `${height ?? 0}px` }; } const nodeRecords = vue.computed(() => vue.readonly(vue.unref(contentRectRecords))); const cellNodeRect = { updateNodeRect, updateRowNodeRect, updateRowsNodeRect, getNodeRect, nodeRecords, getRowNodeRectHeight, tableCellStyle }; function handleUnmounted() { resizeCache == null ? void 0 : resizeCache.stop(); } vue.onUnmounted(() => { handleUnmounted(); }); return { getRecordId, getElementRect, isFixedRecord, isHiddenRecord, cellNodeRect, stop: handleUnmounted }; } exports.useVxeTableColumnSlotRecord = useVxeTableColumnSlotRecord;