vxe-table
Version:
A PC-end table component based on Vxe UI, supporting copy-paste, data pivot table, and high-performance virtual list table solution.
328 lines (327 loc) • 14.7 kB
JavaScript
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var import_vue = require("vue");
var import_xe_utils = __toESM(require("xe-utils"));
var import_core = require("@vxe-ui/core");
var import_dom = require("../../../ui/src/dom");
var import_utils = require("../../../ui/src/utils");
const { menus, hooks, globalEvents, GLOBAL_EVENT_KEYS } = import_core.VxeUI;
const tableMenuMethodKeys = ["closeMenu"];
hooks.add("tableMenuModule", {
setupTable($xeTable) {
const { xID, props, reactData, internalData } = $xeTable;
const { refElem, refTableFilter, refTableMenu } = $xeTable.getRefMaps();
const { computeMouseOpts, computeIsContentMenu, computeMenuOpts } = $xeTable.getComputeMaps();
const $xeGrid = $xeTable.xeGrid;
const $xeGantt = $xeTable.xeGantt;
const $xeGGWrapper = $xeGrid || $xeGantt;
let menuMethods = {};
let menuPrivateMethods = {};
const handleOpenMenuEvent = (evnt, type, params) => {
const { ctxMenuStore } = reactData;
const isContentMenu = computeIsContentMenu.value;
const menuOpts = computeMenuOpts.value;
const config = menuOpts[type];
const { zIndex, transfer, width, visibleMethod } = menuOpts;
if (config) {
const { options, disabled } = config;
if (disabled) {
evnt.preventDefault();
} else if (isContentMenu && options && options.length) {
params.options = options;
$xeTable.preventEvent(evnt, "event.showMenu", params, () => {
if (!visibleMethod || visibleMethod(params)) {
evnt.preventDefault();
$xeTable.updateZindex();
const el = $xeGGWrapper ? $xeGGWrapper.getRefMaps().refElem.value : refElem.value;
if (!el) {
return;
}
const tableRect = el.getBoundingClientRect();
const { scrollTop, scrollLeft, visibleHeight, visibleWidth } = (0, import_dom.getDomNode)();
let top = evnt.clientY - tableRect.y;
let left = evnt.clientX - tableRect.x;
if (transfer) {
top = evnt.clientY + scrollTop;
left = evnt.clientX + scrollLeft;
}
const handleVisible = () => {
internalData._currMenuParams = params;
const ctxStyle = {
zIndex: zIndex || internalData.tZindex,
top: `${top}px`,
left: `${left}px`
};
if (width) {
ctxStyle["--vxe-ui-table-menu-item-width"] = (0, import_dom.toCssUnit)(width);
}
Object.assign(ctxMenuStore, {
visible: true,
list: options,
selected: null,
selectChild: null,
showChild: false,
style: ctxStyle
});
(0, import_vue.nextTick)(() => {
const tableMenu = refTableMenu.value;
const ctxElem = tableMenu.getRefMaps().refElem.value;
const clientHeight = ctxElem.clientHeight;
const clientWidth = ctxElem.clientWidth;
const { boundingTop, boundingLeft } = (0, import_dom.getAbsolutePos)(ctxElem);
const offsetTop = boundingTop + clientHeight - visibleHeight;
const offsetLeft = boundingLeft + clientWidth - visibleWidth;
if (offsetTop > -10) {
ctxMenuStore.style.top = `${Math.max(scrollTop + 2, top - clientHeight - 2)}px`;
}
if (offsetLeft > -10) {
ctxMenuStore.style.left = `${Math.max(scrollLeft + 2, left - clientWidth - 2)}px`;
}
});
};
const { keyboard, row, column } = params;
if (keyboard && row && column) {
$xeTable.scrollToRow(row, column).then(() => {
const cell = $xeTable.getCellElement(row, column);
if (cell) {
const { boundingTop, boundingLeft } = (0, import_dom.getAbsolutePos)(cell);
top = boundingTop + scrollTop + Math.floor(cell.offsetHeight / 2);
left = boundingLeft + scrollLeft + Math.floor(cell.offsetWidth / 2);
}
handleVisible();
});
} else {
handleVisible();
}
} else {
menuMethods.closeMenu();
}
});
}
}
$xeTable.closeFilter();
};
menuMethods = {
/**
* 关闭快捷菜单
*/
closeMenu() {
Object.assign(reactData.ctxMenuStore, {
visible: false,
selected: null,
selectChild: null,
showChild: false
});
return (0, import_vue.nextTick)();
}
};
menuPrivateMethods = {
/**
* 处理菜单的移动
*/
moveCtxMenu(evnt, ctxMenuStore, property, hasOper, operRest, menuList) {
let selectItem;
const selectIndex = import_xe_utils.default.findIndexOf(menuList, (item) => ctxMenuStore[property] === item);
if (hasOper) {
if (operRest && (0, import_utils.hasChildrenList)(ctxMenuStore.selected)) {
ctxMenuStore.showChild = true;
} else {
ctxMenuStore.showChild = false;
ctxMenuStore.selectChild = null;
}
} else if (globalEvents.hasKey(evnt, GLOBAL_EVENT_KEYS.ARROW_UP)) {
for (let len = selectIndex - 1; len >= 0; len--) {
if (menuList[len].visible !== false) {
selectItem = menuList[len];
break;
}
}
ctxMenuStore[property] = selectItem || menuList[menuList.length - 1];
} else if (globalEvents.hasKey(evnt, GLOBAL_EVENT_KEYS.ARROW_DOWN)) {
for (let index = selectIndex + 1; index < menuList.length; index++) {
if (menuList[index].visible !== false) {
selectItem = menuList[index];
break;
}
}
ctxMenuStore[property] = selectItem || menuList[0];
} else if (ctxMenuStore[property] && (globalEvents.hasKey(evnt, GLOBAL_EVENT_KEYS.ENTER) || globalEvents.hasKey(evnt, GLOBAL_EVENT_KEYS.SPACEBAR))) {
$xeTable.ctxMenuLinkEvent(evnt, ctxMenuStore[property]);
}
},
handleOpenMenuEvent,
/**
* 快捷菜单事件处理
*/
handleGlobalContextmenuEvent(evnt) {
const $xeGrid2 = $xeTable.xeGrid;
const $xeGantt2 = $xeTable.xeGantt;
const { mouseConfig, menuConfig } = props;
const { editStore, ctxMenuStore } = reactData;
const { visibleColumn } = internalData;
const tableFilter = refTableFilter.value;
const tableMenu = refTableMenu.value;
const mouseOpts = computeMouseOpts.value;
const menuOpts = computeMenuOpts.value;
const el = refElem.value;
const { selected } = editStore;
const layoutList = ["header", "body", "footer"];
if ((0, import_utils.isEnableConf)(menuConfig)) {
if (ctxMenuStore.visible && tableMenu && (0, import_dom.getEventTargetNode)(evnt, tableMenu.getRefMaps().refElem.value).flag) {
evnt.preventDefault();
return;
}
if (internalData._keyCtx) {
const type = "body";
const params = { source: "table", type, $table: $xeTable, $grid: $xeGrid2, $gantt: $xeGantt2, keyboard: true, columns: visibleColumn.slice(0), $event: evnt };
if ((0, import_utils.hasEnableConf)(mouseConfig, mouseOpts) && mouseOpts.area) {
const activeArea = $xeTable.getActiveCellArea();
if (activeArea && activeArea.row && activeArea.column) {
params.row = activeArea.row;
params.column = activeArea.column;
handleOpenMenuEvent(evnt, type, params);
return;
}
} else if ((0, import_utils.hasEnableConf)(mouseConfig, mouseOpts) && mouseOpts.selected) {
if (selected.row && selected.column) {
params.row = selected.row;
params.column = selected.column;
handleOpenMenuEvent(evnt, type, params);
return;
}
}
}
for (let index = 0; index < layoutList.length; index++) {
const layout = layoutList[index];
const columnTargetNode = (0, import_dom.getEventTargetNode)(evnt, el, `vxe-${layout}--column`, (target) => {
return target.parentNode.parentNode.parentNode.getAttribute("xid") === xID;
});
const params = { source: "table", type: layout, $table: $xeTable, $grid: $xeGrid2, $gantt: $xeGantt2, columns: visibleColumn.slice(0), $event: evnt };
if (columnTargetNode.flag) {
const cell = columnTargetNode.targetElem;
const columnNodeRest = $xeTable.getColumnNode(cell);
const column = columnNodeRest ? columnNodeRest.item : null;
let typePrefix = `${layout}-`;
if (column) {
Object.assign(params, { column, columnIndex: $xeTable.getColumnIndex(column), cell });
}
if (layout === "body") {
const rowNodeRest = $xeTable.getRowNode(cell.parentNode);
const row = rowNodeRest ? rowNodeRest.item : null;
typePrefix = "";
if (row) {
params.row = row;
params.rowIndex = $xeTable.getRowIndex(row);
}
}
const eventType = `${typePrefix}cell-menu`;
handleOpenMenuEvent(evnt, layout, params);
$xeTable.dispatchEvent(eventType, params, evnt);
return;
} else if ((0, import_dom.getEventTargetNode)(evnt, el, `vxe-table--${layout}-wrapper`, (target) => target.getAttribute("xid") === xID).flag || layout === "body" && (0, import_dom.getEventTargetNode)(evnt, el, "vxe-table--empty-place-wrapper", (target) => target.getAttribute("xid") === xID).flag) {
if (menuOpts.trigger === "cell") {
evnt.preventDefault();
} else {
handleOpenMenuEvent(evnt, layout, params);
}
return;
}
}
}
if (tableFilter && !(0, import_dom.getEventTargetNode)(evnt, tableFilter.getRefMaps().refElem.value).flag) {
$xeTable.closeFilter();
}
menuMethods.closeMenu();
},
ctxMenuMouseoverEvent(evnt, item, child) {
const menuElem = evnt.currentTarget;
const { ctxMenuStore } = reactData;
evnt.preventDefault();
evnt.stopPropagation();
ctxMenuStore.selected = item;
ctxMenuStore.selectChild = child;
if (!child) {
ctxMenuStore.showChild = (0, import_utils.hasChildrenList)(item);
if (ctxMenuStore.showChild) {
(0, import_vue.nextTick)(() => {
const childWrapperElem = menuElem.nextElementSibling;
if (childWrapperElem) {
const { boundingTop, boundingLeft, visibleHeight, visibleWidth } = (0, import_dom.getAbsolutePos)(menuElem);
const posTop = boundingTop + menuElem.offsetHeight;
const posLeft = boundingLeft + menuElem.offsetWidth;
let left = "";
let right = "";
if (posLeft + childWrapperElem.offsetWidth > visibleWidth - 10) {
left = "auto";
right = `${menuElem.offsetWidth}px`;
}
let top = "";
let bottom = "";
if (posTop + childWrapperElem.offsetHeight > visibleHeight - 10) {
top = "auto";
bottom = "0";
}
childWrapperElem.style.left = left;
childWrapperElem.style.right = right;
childWrapperElem.style.top = top;
childWrapperElem.style.bottom = bottom;
}
});
}
}
},
ctxMenuMouseoutEvent(evnt, item) {
const { ctxMenuStore } = reactData;
if (!item.children) {
ctxMenuStore.selected = null;
}
ctxMenuStore.selectChild = null;
},
/**
* 快捷菜单点击事件
*/
ctxMenuLinkEvent(evnt, menu) {
const $xeGrid2 = $xeTable.xeGrid;
const $xeGantt2 = $xeTable.xeGantt;
if (!menu.loading && !menu.disabled && (menu.code || !menu.children || !menu.children.length)) {
const gMenuOpts = menus.get(menu.code);
const params = Object.assign({}, internalData._currMenuParams, { menu, $table: $xeTable, $grid: $xeGrid2, $gantt: $xeGantt2, $event: evnt });
const tmMethod = gMenuOpts ? gMenuOpts.tableMenuMethod || gMenuOpts.menuMethod : null;
if (tmMethod) {
tmMethod(params, evnt);
}
$xeTable.dispatchEvent("menu-click", params, evnt);
menuMethods.closeMenu();
}
}
};
return { ...menuMethods, ...menuPrivateMethods };
},
setupGrid($xeGrid) {
return $xeGrid.extendTableMethods(tableMenuMethodKeys);
},
setupGantt($xeGantt) {
return $xeGantt.extendTableMethods(tableMenuMethodKeys);
}
});