UNPKG

@zhsz/cool-design-crud

Version:

369 lines (368 loc) 12.2 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); const vue = require("vue"); const Sortable = require("sortablejs"); const vue$1 = require("@iconify/vue"); const XEUtils = require("xe-utils"); require("../../utils/test.js"); const core = require("../../hooks/core.js"); require("clone-deep"); require("array.prototype.flat"); require("merge"); require("@formily/core"); const lodashEs = require("lodash-es"); require("../../hooks/table.js"); const tdesignVueNext = require("tdesign-vue-next"); const naiveUi = require("naive-ui"); const utils = require("./utils.js"); const orderStorageKey = "VXE_TABLE_CUSTOM_COLUMN_ORDER"; const index = /* @__PURE__ */ vue.defineComponent({ name: "cl-toolbar", props: {}, setup: function(props) { const { crud } = core.useCore(); let $xetable; const reactData = vue.reactive({ isRefresh: false, columns: [] }); const customStore = vue.reactive({ isAll: false, isIndeterminate: false, visible: false }); const indexMap = {}; const checkCustomStatus = () => { const { columns } = reactData; const { computeCustomOpts: computeTableCustomOpts } = $xetable.getComputeMaps(); const tableCustomOpts = computeTableCustomOpts.value; const { checkMethod } = tableCustomOpts; customStore.isAll = columns.filter((item) => !item.type && item.title !== "操作").every((column) => (checkMethod ? !checkMethod({ column }) : false) || column.visible); customStore.isIndeterminate = !customStore.isAll && columns.some((column) => (!checkMethod || checkMethod({ column })) && (column.visible || column.halfVisible)); }; const handleOptionCheck = (column) => { const { columns } = reactData; const matchObj = XEUtils.findTree(columns, (item) => item === column); if (matchObj && matchObj.parent) { const { parent } = matchObj; if (parent.children && parent.children.length) { parent.visible = parent.children.every((column2) => column2.visible); parent.halfVisible = !parent.visible && parent.children.some((column2) => column2.visible || column2.halfVisible); handleOptionCheck(parent); } } }; const changeCheckboxOption = (column) => { const isChecked = !column.visible; XEUtils.eachTree([column], (item) => { item.visible = isChecked; item.halfVisible = false; }); handleOptionCheck(column); checkCustomStatus(); }; const allCustomEvent = () => { const { columns } = reactData; const { computeCustomOpts: computeTableCustomOpts } = $xetable.getComputeMaps(); const tableCustomOpts = computeTableCustomOpts.value; const { checkMethod } = tableCustomOpts; const isAll = !customStore.isAll; XEUtils.eachTree(columns, (column) => { if (!checkMethod || checkMethod({ column })) { column.visible = isAll; column.halfVisible = false; } }); customStore.isAll = isAll; checkCustomStatus(); }; const getCustomStorageMap = (key2) => { const version = utils.default.version; const rest = XEUtils.toStringJSON(localStorage.getItem(key2) || ""); return rest && rest._v === version ? rest : { _v: version }; }; const handleTableCustom = async () => { await $xetable.handleCustom(); }; const closeCustom = async () => { if (customStore.visible) { customStore.visible = false; await handleTableCustom(); } }; const checkTable = () => { if ($xetable) { return true; } }; const calcIndexMap = () => { let index2 = 0; const { columns } = reactData; columns.forEach((t, i) => { var _a; indexMap[index2] = i; if ((_a = t.children) == null ? void 0 : _a.length) { index2 += t.children.length + 1; } else { index2++; } }); }; const showCustom = () => { customStore.visible = true; calcIndexMap(); checkCustomStatus(); }; const customOpenEvent = (evnt) => { if (checkTable()) { if (!customStore.visible) { showCustom(); emitCustomEvent("open", evnt); } } }; const emitCustomEvent = (type, evnt) => { $xetable.dispatchEvent("custom", { type }, evnt); }; const confirmCustomEvent = async (evnt) => { var _a; const id = (_a = $xetable.props) == null ? void 0 : _a.id; if (id) { const columnOrderStorage = getCustomStorageMap(orderStorageKey); const colOrders = []; reactData.columns.forEach((column) => { colOrders.push(column.field || column.type); }); columnOrderStorage[id] = [colOrders.join(",")]; localStorage.setItem(orderStorageKey, XEUtils.toJSONString(columnOrderStorage)); } await closeCustom(); emitCustomEvent("confirm", evnt); }; const changeFixedOption = async (column, colFixed) => { const { computeIsMaxFixedColumn } = $xetable.getComputeMaps(); const isMaxFixedColumn = computeIsMaxFixedColumn.value; if (column.fixed === colFixed) { await $xetable.clearColumnFixed(column); } else { if (!isMaxFixedColumn || column.fixed) { await $xetable.setColumnFixed(column, colFixed); } } }; const resetCustomEvent = async (evnt) => { var _a, _b; localStorage.removeItem("VXE_TABLE_CUSTOM_COLUMN_ORDER"); if ((_a = $xetable.xegrid) == null ? void 0 : _a.props.columns) { await $xetable.loadColumn((_b = $xetable.xegrid) == null ? void 0 : _b.props.columns); } await $xetable.resetColumn(true); await closeCustom(); emitCustomEvent("reset", evnt); }; const key = vue.ref(0); const popUpdateShow = (value) => { customStore.visible = value; vue.nextTick(() => { if (!$xetable.props.id) { return; } const tbody = document.querySelector(".cl-toolbar.panel-wrapper .body"); new Sortable(tbody, { animation: 200, handle: ".operations.level--1", ghostClass: "sortable-ghost", direction: "vertical", onEnd: async ({ oldIndex, newIndex, item }) => { const { columns } = reactData; const tempItem = columns.splice(indexMap[oldIndex] || 0, 1); columns.splice(indexMap[newIndex] || 0, 0, tempItem[0]); key.value++; calcIndexMap(); } }); }); }; const count = vue.ref(0); async function restoreCustomStorage() { count.value++; if (count.value > 5) { return; } const id = $xetable.props.id; if (!id) { console.error("vxe.error.reqProp", ["id"]); return; } const columnOrderStorageKey = getCustomStorageMap(orderStorageKey)[id || ""]; if (columnOrderStorageKey) { const colOrder = columnOrderStorageKey[0].split(",").filter(Boolean); const { columns } = reactData; const oldColOrder = columns.map((item) => item.field || item.type); if (!(colOrder == null ? void 0 : colOrder.length) || !(oldColOrder == null ? void 0 : oldColOrder.length) || JSON.stringify(oldColOrder) === JSON.stringify(colOrder)) { return; } const sortedList = lodashEs.sortBy(columns, (column) => { const index2 = colOrder.indexOf(column.field || column.type); return index2 === -1 ? colOrder.length : index2; }); reactData.columns = sortedList; await $xetable.loadColumn(sortedList); } } vue.nextTick(async () => { if (crud["cl-table"]) { await crud["cl-table"].connect({ syncUpdate(params) { const { collectColumn } = params; $xetable = params.$table; reactData.columns = collectColumn; restoreCustomStorage(); } }); } }); return () => { const colVNs = []; const { columns } = reactData; XEUtils.eachTree(columns, (column, index2, items, path, parent) => { const colTitle = utils.formatText(column.getTitle(), 1); const colKey = column.getKey(); const isColGroup = column.children && column.children.length; if (isColGroup || colKey || colTitle) { const isChecked = column.visible; const isIndeterminate = column.halfVisible; colVNs.push(vue.h("div", { key: key.value + Math.random(), class: ["operations", `level--${column.level}`, { hidden: ["seq", "checkbox", "radio", "expand"].includes(column.type || ""), "is--group": isColGroup }] }, [vue.h(tdesignVueNext.Checkbox, { label: colTitle, indeterminate: isIndeterminate, checked: isChecked, class: ["vxe-custom--checkbox-option"], onChange: () => { changeCheckboxOption(column); } }, void 0), !parent ? vue.h("div", { class: "custom--fixed-option" }, [vue.h(vue$1.Icon, { icon: "icon-park-outline:left-c", class: ["vxe-custom--fixed-left-option", column.fixed === "left" ? "active" : ""], onClick: async () => { await changeFixedOption(column, "left"); } }), vue.h(vue$1.Icon, { icon: "icon-park-outline:right-c", class: ["vxe-custom--fixed-right-option", column.fixed === "right" ? "active" : ""], size: "26px", onClick: async () => { await changeFixedOption(column, "right"); } }), vue.h(vue$1.Icon, { icon: "icon-park-outline:direction-adjustment-three", class: ["move"] })]) : null])); } }); return vue.createVNode(naiveUi.NPopover, { "trigger": "click", "scrollable": true, "show": customStore.visible, "onUpdateShow": popUpdateShow }, { trigger: () => vue.createVNode(tdesignVueNext.Button, { "shape": "circle", "theme": "default", "variant": "outline", "size": "medium", "onClick": customOpenEvent }, { icon: () => vue.createVNode(vue$1.Icon, { "icon": "icon-park-solid:system" }, null) }), default: () => vue.createVNode("div", { "class": "cl-toolbar panel-wrapper" }, [vue.createVNode("div", { "class": "top" }, [vue.createVNode(tdesignVueNext.Checkbox, { "checked": customStore.isAll, "indeterminate": customStore.isIndeterminate, "on-change": allCustomEvent }, { default: () => [vue.createTextVNode("全选")] })]), vue.createVNode(naiveUi.NScrollbar, { "style": "max-height: 300px" }, { default: () => [vue.createVNode("div", { "class": "body" }, { default: () => colVNs })] }), vue.createVNode("div", { "class": "bottom" }, [vue.createVNode(tdesignVueNext.Button, { "theme": "default", "variant": "text", "onClick": resetCustomEvent }, { default: () => [vue.createTextVNode("重置")] }), vue.createVNode(tdesignVueNext.Button, { "theme": "default", "variant": "text", "onClick": confirmCustomEvent }, { default: () => [vue.createTextVNode("确认")] })])]) }); }; } }); exports.default = index;