UNPKG

@imengyu/vue3-context-menu

Version:
1,582 lines 55 kB
import { defineComponent as q, toRefs as ce, ref as P, reactive as He, onMounted as pe, nextTick as me, onBeforeUnmount as ye, openBlock as p, createElementBlock as z, normalizeClass as te, createElementVNode as D, renderSlot as E, normalizeStyle as we, unref as n, createCommentVNode as F, inject as V, provide as Q, createBlock as A, mergeProps as ge, createVNode as re, toDisplayString as Ie, Transition as Ye, normalizeProps as Ve, withCtx as se, resolveComponent as Xe, Teleport as Ze, Fragment as We, renderList as _e, createSlots as Ge, useSlots as Ue, watch as Je, h as be, guardReactiveProps as lt, render as Te } from "vue"; let ne = null; function st() { return ne !== null; } function ut(e) { ne && Qe(), ne = e; } function Le(e) { e === ne && (ne = null); } function Qe() { ne && (ne.closeMenu(), ne = null); } const G = { defaultDirection: "br", defaultMinWidth: 100, defaultMaxWidth: 600, defaultZindex: 100, defaultZoom: 1, defaultAdjustPadding: { x: 0, y: 10 } }; function oe(e, o) { let l = e.offsetTop; return e.offsetParent != null && e.offsetParent != o && (l -= e.offsetParent.scrollTop, l += oe(e.offsetParent, o)), l; } function le(e, o) { let l = e.offsetLeft; return e.offsetParent != null && e.offsetParent != o && (l -= e.offsetParent.scrollLeft, l += le(e.offsetParent, o)), l; } function at(e, o, l, c) { return { x: le(e, c) + o, y: oe(e, c) + l }; } const Se = "mx-menu-default-container", it = "mx-menu-container-"; let rt = 0; function qe(e) { const { getContainer: o, zIndex: l } = e; if (o) { const m = typeof o == "function" ? o() : o; if (m) { let t = m.getAttribute("id"); return t || (t = it + rt++, m.setAttribute("id", t)), { eleId: t, container: m, isNew: !1 }; } } let c = document.getElementById(Se); return c || (c = document.createElement("div"), c.setAttribute("id", Se), c.setAttribute("class", "mx-menu-ghost-host fullscreen"), document.body.appendChild(c)), c.style.zIndex = (l == null ? void 0 : l.toString()) || G.defaultZindex.toString(), { eleId: Se, container: c, isNew: !0 }; } function je(e) { return typeof e == "number" ? `${e}px` : e; } const J = q({ props: { /** * Can be VNode or (data: unknown) => VNode */ vnode: { type: null }, /** * If vnode is a callback, this data will be passed to the callback first parameter. * @default null */ data: { type: null, default: null } }, setup(e) { const { vnode: o, data: l } = ce(e); return () => typeof o.value == "function" ? o.value(l.value) : o.value; } }); function ct(e, o) { const l = { ...e }; return delete l[o], l; } var dt = Object.defineProperty, ft = (e, o, l) => o in e ? dt(e, o, { enumerable: !0, configurable: !0, writable: !0, value: l }) : e[o] = l, Ae = (e, o, l) => ft(e, typeof o != "symbol" ? o + "" : o, l); class Fe { constructor(o, l) { Ae(this, "x", 0), Ae(this, "y", 0), this.x = o || 0, this.y = l || 0; } set(o, l) { this.x = o, this.y = l; } substract(o) { this.x -= o.x, this.y -= o.y; } } function Ee(e) { const { onDown: o, onMove: l, onUp: c } = e, m = new Fe(), t = new Fe(); let b; function d(f) { f.stopPropagation(), t.set(f.x, f.y), t.substract(m), l(m, t, f, b); } function y(f) { c(f, b), m.set(0, 0), document.removeEventListener("mousemove", d), document.removeEventListener("mouseup", y); } return (f, w) => o(f, w) ? (b = w, m.set(f.x, f.y), document.addEventListener("mousemove", d), document.addEventListener("mouseup", y), f.stopPropagation(), !0) : !1; } function vt(e, o) { let l = 0; return { start() { l > 0 && clearInterval(l), l = setInterval(() => { l = 0, o(); }, e); }, stop() { l > 0 && (clearInterval(l), l = 0); } }; } const ve = [], ze = vt(100, () => { for (const e of ve) e(); }); function ht(e, o, l, c) { let m = 0, t = 0; function b() { e.value && (o && m !== e.value.offsetWidth && o(e.value.offsetWidth), l && t !== e.value.offsetHeight && l(e.value.offsetHeight), m = e.value.offsetWidth, t = e.value.offsetHeight); } return { startResizeChecker() { ze.start(), ve.push(b); }, stopResizeChecker() { const d = ve.indexOf(b); d >= 0 && ve.splice(d, 1), ve.length === 0 && ze.stop(); } }; } const De = 140, mt = 70, pt = /* @__PURE__ */ q({ __name: "ScrollRect", props: { /** * Scroll direction * * * both : Scroll in both directions * * vertical : Scroll only in vertical direction * * horizontal : Scroll only in horizontal direction * * none : Disable scroll * * @default both */ scroll: { type: String, default: "both" }, /** * Show scroll bar always, otherwise show scroll bar when mouse over * @default false */ scrollBarAlwaysShow: { type: Boolean, default: !1 }, /** * Is able to click scroll bar background to set scroll position? (When `scrollBarAlwaysShow` is true) * @default true */ scrollBarBackgroundClickable: { type: Boolean, default: !1 }, /** * CSS class of inner container */ containerClass: { type: String, default: "" } }, emits: ["scroll"], setup(e, { expose: o, emit: l }) { const c = e, m = l, t = P(), b = P(), d = P(), y = P(), f = P(), w = P(), H = P(!1), x = He({ show: !1, size: 0, sizeRaw: 0, pos: 0 }), v = He({ show: !1, size: 0, sizeRaw: 0, pos: 0 }); let T = 0, L = 0, O = 0, r = 0, a = null; const g = { attributes: !0, childList: !0 }; function I() { if (t.value) { if (x.show) { const u = t.value.offsetWidth / t.value.scrollWidth; x.sizeRaw = u * t.value.offsetWidth, x.size = u * 100, x.pos = t.value.scrollLeft / (t.value.scrollWidth - t.value.offsetWidth) * (100 - x.size), u >= 1 && (x.show = !1); } if (v.show) { const u = t.value.offsetHeight / t.value.scrollHeight; v.sizeRaw = u * t.value.offsetHeight, v.size = u * 100, v.pos = t.value.scrollTop / (t.value.scrollHeight - t.value.offsetHeight) * (100 - v.size), u >= 1 && (v.show = !1); } m("scroll", t.value.scrollLeft, t.value.scrollTop); } } function R(u = !1) { if (!t.value) return; let S = c.scroll === "horizontal" || c.scroll === "both", W = c.scroll === "vertical" || c.scroll === "both"; const ue = S && (T !== t.value.scrollWidth || O !== t.value.offsetWidth), ae = W && (L !== t.value.scrollHeight || r !== t.value.offsetHeight); if (!u && !ue && !ae) return; const ie = window.getComputedStyle(t.value); (ie.overflow === "hidden" || ie.overflowX === "hidden") && (S = !1), (ie.overflow === "hidden" || ie.overflowY === "hidden") && (W = !1), x.show = S, v.show = W, I(), O = t.value.offsetWidth, r = t.value.offsetHeight, T = t.value.scrollWidth, L = t.value.scrollHeight; } function h(u) { var S; c.scroll == "horizontal" && (u.deltaMode == 0 && ((S = t.value) == null || S.scrollTo({ left: t.value.scrollLeft + (u.deltaY > 0 ? De : -140), behavior: "smooth" })), u.preventDefault(), u.stopPropagation()); } function _(u) { var S; u.deltaMode == 0 && ((S = t.value) == null || S.scrollTo({ left: t.value.scrollLeft + (u.deltaY > 0 ? De : -140), behavior: "smooth" }), u.preventDefault(), u.stopPropagation()); } function B(u) { var S; u.deltaMode == 0 && ((S = t.value) == null || S.scrollTo({ top: t.value.scrollTop + (u.deltaY > 0 ? mt : -70), behavior: "smooth" }), u.preventDefault(), u.stopPropagation()); } let N = 0, Z = 0, U = 0, K = 0; const M = Ee({ onDown(u) { return !d.value || !f.value ? !1 : (N = u.offsetX, Z = u.x - u.offsetX - f.value.offsetLeft, u.preventDefault(), H.value = !0, !0); }, onMove(u, S, W) { t.value && d.value && (Y(W.x - N - Z), W.preventDefault(), W.stopPropagation()); }, onUp() { H.value = !1; } }), j = Ee({ onDown(u) { return !y.value || !w.value ? !1 : (U = u.offsetY, K = u.y - u.offsetY - w.value.offsetTop, u.preventDefault(), H.value = !0, !0); }, onMove(u, S, W) { t.value && y.value && (ee(W.y - U - K), W.preventDefault(), W.stopPropagation()); }, onUp() { H.value = !1; } }); function X(u) { t.value && (t.value.scrollLeft = u / 100 * (t.value.scrollWidth - t.value.offsetWidth)); } function k(u) { t.value && (t.value.scrollLeft = u / 100 * (t.value.scrollHeight - t.value.offsetHeight)); } function Y(u) { t.value && (t.value.scrollLeft = u / (t.value.offsetWidth - x.sizeRaw) * (t.value.scrollWidth - t.value.offsetWidth)); } function ee(u) { t.value && (t.value.scrollTop = u / (t.value.offsetHeight - v.sizeRaw) * (t.value.scrollHeight - t.value.offsetHeight)); } function s(u) { c.scrollBarBackgroundClickable && Y(u.offsetX - x.sizeRaw / 2); } function C(u) { c.scrollBarBackgroundClickable && ee(u.offsetY - v.sizeRaw / 2); } const { startResizeChecker: $, stopResizeChecker: i } = ht( t, () => R(), () => R() ); return pe(() => { me(() => { setTimeout(() => R(!0), 200), R(!0), $(), a = new MutationObserver(() => R()), a.observe(t.value, g); }); }), ye(() => { i(), a && (a.disconnect(), a = null); }), o({ refreshScrollState() { R(!0); }, getScrollContainer() { return t.value; }, scrollTo(u, S) { var W; (W = t.value) == null || W.scrollTo(u, S); }, scrollToTop() { var u; (u = t.value) == null || u.scrollTo(0, 0); }, scrollToBottom() { var u; (u = t.value) == null || u.scrollTo(t.value.scrollWidth, t.value.scrollHeight); } }), (u, S) => (p(), z("div", { ref_key: "scrollrect", ref: b, class: te([ "vue-scroll-rect", e.scrollBarAlwaysShow ? "always-show-scrollbar" : "", e.scrollBarBackgroundClickable ? "background-clickable" : "", H.value ? "dragging" : "" ]), onWheel: h }, [ D("div", { ref_key: "container", ref: t, class: te(["scroll-content", e.scroll, e.containerClass]), onScroll: I }, [ E(u.$slots, "default") ], 34), x.show ? E(u.$slots, "scrollBarX", { key: 0, scrollBarValue: x, onScroll: X }, () => [ D("div", { ref_key: "scrollBarRefX", ref: d, class: "scrollbar horizontal", onClick: s, onWheel: _ }, [ D("div", { class: "thumb", ref_key: "scrollBarThumbRefX", ref: f, style: we({ left: `${x.pos}%`, width: `${x.size}%` }), onMousedown: S[0] || (S[0] = //@ts-ignore (...W) => n(M) && n(M)(...W)), onWheel: _ }, null, 36) ], 544) ]) : F("", !0), v.show ? E(u.$slots, "scrollBarY", { key: 1, scrollBarValue: v, onScroll: k }, () => [ v.show ? (p(), z("div", { key: 0, ref_key: "scrollBarRefY", ref: y, class: "scrollbar vertical", onClick: C, onWheel: B }, [ D("div", { class: "thumb", ref_key: "scrollBarThumbRefY", ref: w, style: we({ top: `${v.pos}%`, height: `${v.size}%` }), onMousedown: S[1] || (S[1] = //@ts-ignore (...W) => n(j) && n(j)(...W)), onWheel: B }, null, 36) ], 544)) : F("", !0) ]) : F("", !0) ], 34)); } }), Me = (e, o) => { const l = e.__vccOpts || e; for (const [c, m] of o) l[c] = m; return l; }, gt = {}, bt = { class: "mx-checked-mark", "aria-hidden": "true", viewBox: "0 0 1024 1024" }, Ct = /* @__PURE__ */ D("path", { d: "M129.3,428.6L52,512l345,372.5l575-620.8l-69.5-75L400.4,718.2L129.3,428.6z" }, null, -1), yt = [ Ct ]; function Mt(e, o) { return p(), z("svg", bt, yt); } const xt = /* @__PURE__ */ Me(gt, [["render", Mt]]), kt = {}, St = { class: "mx-right-arrow", "aria-hidden": "true", viewBox: "0 0 1024 1024" }, wt = /* @__PURE__ */ D("path", { d: "M307.018 49.445c11.517 0 23.032 4.394 31.819 13.18L756.404 480.18c8.439 8.438 13.181 19.885 13.181 31.82s-4.741 23.38-13.181 31.82L338.838 961.376c-17.574 17.573-46.065 17.573-63.64-0.001-17.573-17.573-17.573-46.065 0.001-63.64L660.944 512 275.198 126.265c-17.574-17.573-17.574-46.066-0.001-63.64C283.985 53.839 295.501 49.445 307.018 49.445z" }, null, -1), It = [ wt ]; function Wt(e, o) { return p(), z("svg", St, It); } const _t = /* @__PURE__ */ Me(kt, [["render", Wt]]), Pt = { class: "mx-item-row" }, Rt = ["xlink:href"], Bt = { key: 1, class: "label" }, Ot = { class: "mx-item-row" }, $t = { class: "mx-shortcut" }, Pe = /* @__PURE__ */ q({ __name: "ContextMenuItem", props: { /** * Is this menu disabled? */ disabled: { type: Boolean, default: !1 }, /** * Is this menu hidden? */ hidden: { type: Boolean, default: !1 }, customRender: { type: Function, default: null }, /** * Custom css class for submenu */ customClass: { type: String, default: "" }, clickHandler: { type: Function, default: null }, /** * Menu label */ label: { type: [String, Object, Function], default: "" }, /** * Menu icon (for icon class) */ icon: { type: [String, Object, Function], default: "" }, /** * Custom icon library font class name. * * Only for css font icon, If you use the svg icon, you do not need to use this. */ iconFontClass: { type: String, default: "iconfont" }, /** * Is this menu item checked? * * The check mark are displayed on the left side of the icon, so it is not recommended to display the icon at the same time. */ checked: { type: Boolean, default: !1 }, /** * Shortcut key text display on the right. * * The shortcut keys here are only for display. You need to handle the key events by yourself. */ shortcut: { type: String, default: "" }, /** * Display icons use svg symbol (`<use xlink:href="#icon-symbol-name">`) , only valid when icon attribute is empty. */ svgIcon: { type: String, default: "" }, /** * The user-defined attribute of the svg tag, which is valid when using `svgIcon`. */ svgProps: { type: Object, default: null }, /** * Should a fixed-width icon area be reserved for menu items without icon. (this item) * * Default is true . * * The width of icon area can be override with css var `--mx-menu-placeholder-width`. */ preserveIconWidth: { type: Boolean, default: !0 }, /** * Show right arrow on this menu? */ showRightArrow: { type: Boolean, default: !1 }, hasChildren: { type: Boolean, default: !1 }, /** * Should close menu when Click this menu item ? */ clickClose: { type: Boolean, default: !0 }, /** * When there are subitems in this item, is it allowed to trigger its own click event? Default is false */ clickableWhenHasChildren: { type: Boolean, default: !1 }, rawMenuItem: { type: Object, default: void 0 } }, emits: [ "click", "subMenuOpen", "subMenuClose" ], setup(e, { expose: o, emit: l }) { const c = e, m = l, { clickHandler: t, clickClose: b, clickableWhenHasChildren: d, disabled: y, hidden: f, label: w, icon: H, iconFontClass: x, showRightArrow: v, shortcut: T, hasChildren: L } = ce(c), O = P(!1), r = P(!1), a = P(), g = V("globalOptions"), I = V("globalHasSlot"), R = V("globalRenderSlot"), h = V("globalCloseMenu"), _ = V("menuContext"), B = { getSubMenuInstance: () => { }, showSubMenu: () => O.value ? (_.markActiveMenuItem(B, !0), !0) : L.value ? (Z(), !0) : !1, hideSubMenu: () => { _.closeOtherSubMenu(); }, isDisabledOrHidden: () => y.value || f.value, getElement: () => a.value, focus: () => r.value = !0, blur: () => r.value = !1, click: N }; Q("menuItemInstance", B), pe(() => { _.isMenuItemDataCollectedFlag() ? me(() => { let M = 0; const j = _.getElement(); if (j) { let X = 0; for (let k = 0; k < j.children.length; k++) { const Y = j.children[k]; if (Y.getAttribute("data-type") === "ContextMenuItem") { if (Y === a.value) { M = X; break; } X++; } } } _.addChildMenuItem(B, M); }) : _.addChildMenuItem(B); }), ye(() => { _.removeChildMenuItem(B); }); function N(M) { if (!y.value) { if (M) { const j = M.target; if (j.classList.contains("mx-context-no-clickable") || g.value.ignoreClickClassName && j.classList.contains(g.value.ignoreClickClassName)) return; if (g.value.clickCloseClassName && j.classList.contains(g.value.clickCloseClassName)) { M.stopPropagation(), h(c.rawMenuItem); return; } } L.value ? d.value ? (typeof t.value == "function" && t.value(M), m("click", M)) : O.value || Z() : (typeof t.value == "function" && t.value(M), m("click", M), b.value && h(c.rawMenuItem)); } } function Z(M) { r.value = !1, _.checkCloseOtherSubMenuTimeOut() || _.closeOtherSubMenu(), y.value || (_.markActiveMenuItem(B), L.value && (M || _.markThisOpenedByKeyBoard(), _.addOpenedSubMenu(U), O.value = !0, me(() => m("subMenuOpen", B)))); } function U() { r.value = !1, O.value = !1, m("subMenuClose", B); } function K() { return { disabled: y.value, label: w.value, icon: H.value, iconFontClass: x.value, showRightArrow: v.value, clickClose: b.value, clickableWhenHasChildren: d.value, shortcut: T.value, theme: g.value.theme, isOpen: O, hasChildren: L, onClick: N, onMouseEnter: Z, closeMenu: h }; } return o(B), (M, j) => n(f) ? F("", !0) : (p(), z("div", { key: 0, class: "mx-context-menu-item-wrapper", ref_key: "menuItemRef", ref: a, "data-type": "ContextMenuItem" }, [ n(I)("itemRender") ? (p(), A(n(J), { key: 0, vnode: () => n(R)("itemRender", K()) }, null, 8, ["vnode"])) : e.customRender ? (p(), A(n(J), { key: 1, vnode: e.customRender, data: K() }, null, 8, ["vnode", "data"])) : (p(), z("div", { key: 2, class: te([ "mx-context-menu-item", n(y) ? "disabled" : "", r.value ? "keyboard-focus" : "", e.customClass ? " " + e.customClass : "", O.value ? "open" : "" ]), onClick: N, onMouseenter: Z }, [ E(M.$slots, "default", {}, () => [ D("div", Pt, [ D("div", { class: te([ "mx-icon-placeholder", e.preserveIconWidth ? "preserve-width" : "" ]) }, [ E(M.$slots, "icon", {}, () => [ n(I)("itemIconRender") ? (p(), A(n(J), { key: 0, vnode: () => n(R)("itemIconRender", K()) }, null, 8, ["vnode"])) : typeof e.svgIcon == "string" && e.svgIcon ? (p(), z("svg", ge({ key: 1, class: "icon svg" }, e.svgProps), [ D("use", { "xlink:href": e.svgIcon }, null, 8, Rt) ], 16)) : typeof n(H) != "string" ? (p(), A(n(J), { key: 2, vnode: n(H), data: n(H) }, null, 8, ["vnode", "data"])) : typeof n(H) == "string" && n(H) !== "" ? (p(), z("i", { key: 3, class: te(n(H) + " icon " + n(x) + " " + n(g).iconFontClass) }, null, 2)) : F("", !0) ]), e.checked ? E(M.$slots, "check", { key: 0 }, () => [ n(I)("itemCheckRender") ? (p(), A(n(J), { key: 0, vnode: () => n(R)("itemCheckRender", K()) }, null, 8, ["vnode"])) : F("", !0), re(xt) ]) : F("", !0) ], 2), E(M.$slots, "label", {}, () => [ n(I)("itemLabelRender") ? (p(), A(n(J), { key: 0, vnode: () => n(R)("itemLabelRender", K()) }, null, 8, ["vnode"])) : typeof n(w) == "string" ? (p(), z("span", Bt, Ie(n(w)), 1)) : (p(), A(n(J), { key: 2, vnode: n(w), data: n(w) }, null, 8, ["vnode", "data"])) ]) ]), D("div", Ot, [ n(T) || M.$slots.shortcut ? E(M.$slots, "shortcut", { key: 0 }, () => [ n(I)("itemShortcutRender") ? (p(), A(n(J), { key: 0, vnode: () => n(R)("itemShortcutRender", K()) }, null, 8, ["vnode"])) : F("", !0), D("span", $t, Ie(n(T)), 1) ]) : F("", !0), n(v) ? E(M.$slots, "rightArrow", { key: 1 }, () => [ n(I)("itemRightArrowRender") ? (p(), A(n(J), { key: 0, vnode: () => n(R)("itemRightArrowRender", K()) }, null, 8, ["vnode"])) : F("", !0), re(_t) ]) : F("", !0) ]) ]) ], 34)), n(g).menuTransitionProps ? (p(), A(Ye, Ve(ge({ key: 3 }, n(g).menuTransitionProps)), { default: se(() => [ O.value ? E(M.$slots, "submenu", { key: 0, context: B }) : F("", !0) ]), _: 3 }, 16)) : O.value ? E(M.$slots, "submenu", { key: 4, context: B }) : F("", !0) ], 512)); } }), Ht = q({ name: "ContextMenuSperator", components: { VNodeRender: J }, setup() { const e = V("globalHasSlot"), o = V("globalRenderSlot"); return { globalHasSlot: e, globalRenderSlot: o }; } }), Tt = { key: 1, class: "mx-context-menu-item-sperator mx-context-no-clickable" }; function Lt(e, o, l, c, m, t) { const b = Xe("VNodeRender"); return e.globalHasSlot("separatorRender") ? (p(), A(b, { key: 0, vnode: () => e.globalRenderSlot("separatorRender", {}) }, null, 8, ["vnode"])) : (p(), z("div", Tt)); } const he = /* @__PURE__ */ Me(Ht, [["render", Lt]]), Ce = /* @__PURE__ */ q({ __name: "ContextSubMenu", props: { /** * Items from options */ items: { type: Object, default: null }, /** * Max width for this submenu */ maxWidth: { type: [String, Number], default: 0 }, /** * Min width for this submenu */ minWidth: { type: [String, Number], default: 0 }, /** * Specifies should submenu adjust it position * when the menu exceeds the screen. The default is true */ adjustPosition: { type: Boolean, default: !0 }, /** * Menu direction */ direction: { type: String, default: "br" }, parentMenuItemContext: { type: Object, default: null } }, setup(e, { expose: o }) { const l = e, c = P(!1), m = V("globalGetMenuHostId", ""), t = V("menuContext"), b = V("globalOptions"); V("globalHasSlot"), V("globalRenderSlot"); const { zIndex: d, getParentWidth: y, getParentHeight: f, getZoom: w } = t, { adjustPosition: H } = ce(l), x = P(), v = P(), T = P(), L = [], O = V("globalSetCurrentSubMenu"), r = []; let a = null, g = 0; function I() { a && a.blur(); } function R(s, C) { if (s) { for (let $ = C !== void 0 ? C : 0; $ < r.length; $++) if (!r[$].isDisabledOrHidden()) { h($); break; } } else for (let $ = C !== void 0 ? C : r.length - 1; $ >= 0; $--) if (!r[$].isDisabledOrHidden()) { h($); break; } } function h(s) { var C; if (a && I(), s !== void 0 && (a = r[Math.max(0, Math.min(s, r.length - 1))]), !!a && (a.focus(), X.value)) { const $ = a.getElement(); $ && ((C = x.value) == null || C.scrollTo(0, Math.min(Math.max(-j.value, -$.offsetTop - $.offsetHeight + Y.value), 0))); } } function _() { O(B); } const B = { isTopLevel: () => t.getParentContext() === null, closeSelfAndActiveParent: () => { const s = U.getParentContext(); if (s) { s.closeOtherSubMenu(); const C = s.getSubMenuInstanceContext(); if (C) return C.focusCurrentItem(), !0; } return !1; }, closeCurrentSubMenu: () => { var s; return (s = U.getParentContext()) == null ? void 0 : s.closeOtherSubMenu(); }, moveCurrentItemFirst: () => R(!0), moveCurrentItemLast: () => R(!1), moveCurrentItemDown: () => R(!0, a ? r.indexOf(a) + 1 : 0), moveCurrentItemUp: () => R(!1, a ? r.indexOf(a) - 1 : 0), focusCurrentItem: () => h(), openCurrentItemSubMenu: () => a ? a == null ? void 0 : a.showSubMenu() : !1, triggerCurrentItemClick: (s) => a == null ? void 0 : a.click(s) }; let N = !1, Z = !1; const U = { zIndex: d + 1, container: t.container, adjustPadding: b.value.adjustPadding || G.defaultAdjustPadding, getParentWidth: () => { var s; return ((s = T.value) == null ? void 0 : s.offsetWidth) || 0; }, getParentHeight: () => { var s; return ((s = T.value) == null ? void 0 : s.offsetHeight) || 0; }, getPositon: () => [k.value.x, k.value.y], getZoom: () => b.value.zoom || G.defaultZoom, addOpenedSubMenu(s) { L.push(s); }, closeOtherSubMenu() { L.forEach((s) => s()), L.splice(0, L.length), O(B); }, checkCloseOtherSubMenuTimeOut() { return g ? (clearTimeout(g), g = 0, !0) : !1; }, closeOtherSubMenuWithTimeOut() { g = setTimeout(() => { g = 0, this.closeOtherSubMenu(); }, 200); }, addChildMenuItem: (s, C) => { C === void 0 ? r.push(s) : r.splice(C, 0, s); }, removeChildMenuItem: (s) => { r.splice(r.indexOf(s), 1), s.getSubMenuInstance = () => { }; }, markActiveMenuItem: (s, C = !1) => { I(), a = s, C && h(); }, markThisOpenedByKeyBoard: () => { N = !0; }, isOpenedByKeyBoardFlag: () => N ? (N = !1, !0) : !1, isMenuItemDataCollectedFlag: () => Z, getElement: () => T.value || null, getParentContext: () => t, getSubMenuInstanceContext: () => B }; Q("menuContext", U); const K = { getChildItem: (s) => r[s], getMenuDimensions: () => v.value ? { width: v.value.offsetWidth, height: v.value.offsetHeight } : { width: 0, height: 0 }, getSubmenuRoot: () => v.value, getMenu: () => T.value, getScrollValue: () => { var s, C; return ((C = (s = x.value) == null ? void 0 : s.getScrollContainer()) == null ? void 0 : C.scrollTop) || 0; }, setScrollValue: (s) => { var C; return (C = x.value) == null ? void 0 : C.scrollTo(0, s); }, getScrollHeight: () => j.value, adjustPosition: () => { ee(); }, getMaxHeight: () => Y.value, getPosition: () => k.value, setPosition: (s, C) => { k.value.x = s, k.value.y = C; } }, M = V("menuItemInstance", void 0); M && (M.getSubMenuInstance = () => K); const j = P(0), X = P(!1), k = P({ x: 0, y: 0 }), Y = P(0); function ee() { me(() => { var C, $; const s = T.value; if (s && x.value) { const { container: i } = t, u = (y == null ? void 0 : y()) ?? 0, S = (f == null ? void 0 : f()) ?? 0, W = typeof t.adjustPadding == "number" ? t.adjustPadding : ((C = t.adjustPadding) == null ? void 0 : C.x) ?? 0, ue = typeof t.adjustPadding == "number" ? t.adjustPadding : (($ = t.adjustPadding) == null ? void 0 : $.y) ?? 0, ae = S > 0 ? ue : 0, ie = document.documentElement.scrollHeight / w(), tt = document.documentElement.scrollWidth / w(), nt = Math.min(tt, i.offsetWidth), Re = Math.min(ie, i.offsetHeight); let xe = le(s, i), ke = oe(s, i); l.direction.includes("l") ? k.value.x -= s.offsetWidth + W : l.direction.includes("r") ? k.value.x += u + W : (k.value.x += u / 2, k.value.x -= (s.offsetWidth + W) / 2), l.direction.includes("t") ? k.value.y -= (s.offsetHeight + ue * 2) / w() : l.direction.includes("b") ? k.value.y -= ue / w() : k.value.y -= (s.offsetHeight + ue) / 2 / w(), H.value && me(() => { var Oe, $e; xe = le(s, i), ke = oe(s, i), j.value = (($e = (Oe = x.value) == null ? void 0 : Oe.getScrollContainer()) == null ? void 0 : $e.scrollHeight) || 0; const ot = xe + s.offsetWidth - nt, Be = ke + j.value + ae * 2 - Re; if (X.value = Be > 0, ot > 0) { const de = u + s.offsetWidth - W, fe = xe; de > fe ? k.value.x -= fe : k.value.x -= de; } if (X.value) { const de = Be, fe = ke; de > fe ? k.value.y -= fe - ae : k.value.y -= de - ae, Y.value = Re - ae * 2; } else Y.value = 0; }); } s == null || s.focus({ preventScroll: !0 }), t.isOpenedByKeyBoardFlag() && R(!0), Z = !0; }); } return pe(() => { var C; c.value = !0; const s = (C = l.parentMenuItemContext) == null ? void 0 : C.getElement(); if (s) { const $ = le(s, t.container), i = oe(s, t.container); k.value.x = $, k.value.y = i; } else { const [$, i] = t.getPositon(); k.value.x = $, k.value.y = i; } O(B), ee(); }), ye(() => { c.value = !1, M && (M.getSubMenuInstance = () => { }); }), o(K), (s, C) => { const $ = Xe("ContextSubMenu", !0); return c.value ? (p(), A(Ze, { key: 0, to: `#${n(m)}` }, [ D("div", ge({ ref_key: "submenuRoot", ref: v }, s.$attrs, { class: [ "mx-context-menu", n(b).customClass ? n(b).customClass : "", n(b).theme ?? "" ], style: { maxWidth: e.maxWidth ? n(je)(e.maxWidth) : `${n(G).defaultMaxWidth}px`, minWidth: e.minWidth ? n(je)(e.minWidth) : `${n(G).defaultMinWidth}px`, maxHeight: X.value && Y.value > 0 ? `${Y.value}px` : void 0, zIndex: n(d), left: `${k.value.x}px`, top: `${k.value.y}px` }, "data-type": "ContextSubMenu", onClick: _ }), [ re(n(pt), { ref_key: "scrollRectRef", ref: x, scroll: X.value ? "vertical" : "none", style: we({ width: "auto", height: `${X.value ? Y.value : j.value}px` }) }, { default: se(() => [ D("div", { class: te(["mx-context-menu-items"]), ref_key: "menu", ref: T }, [ E(s.$slots, "default", {}, () => [ (p(!0), z(We, null, _e(e.items, (i, u) => (p(), z(We, { key: u }, [ i.hidden !== !0 && i.divided === "up" ? (p(), A(he, { key: 0 })) : F("", !0), i.hidden !== !0 && i.divided === "self" ? (p(), A(he, { key: 1 })) : (p(), A(Pe, { key: 2, clickHandler: i.onClick ? (S) => i.onClick(S) : void 0, disabled: typeof i.disabled == "object" ? i.disabled.value : i.disabled, hidden: typeof i.hidden == "object" ? i.hidden.value : i.hidden, icon: i.icon, iconFontClass: i.iconFontClass, svgIcon: i.svgIcon, svgProps: i.svgProps, label: i.label, customRender: i.customRender, customClass: i.customClass, checked: typeof i.checked == "object" ? i.checked.value : i.checked, shortcut: i.shortcut, clickClose: i.clickClose, clickableWhenHasChildren: i.clickableWhenHasChildren, preserveIconWidth: i.preserveIconWidth !== void 0 ? i.preserveIconWidth : n(b).preserveIconWidth, showRightArrow: i.children && i.children.length > 0, hasChildren: i.children && i.children.length > 0, rawMenuItem: i, onSubMenuOpen: (S) => { var W; return (W = i.onSubMenuOpen) == null ? void 0 : W.call(i, S); }, onSubMenuClose: (S) => { var W; return (W = i.onSubMenuClose) == null ? void 0 : W.call(i, S); } }, Ge({ _: 2 }, [ i.children && i.children.length > 0 ? { name: "submenu", fn: se(({ context: S }) => [ re($, { parentMenuItemContext: S, items: i.children, maxWidth: i.maxWidth, minWidth: i.minWidth, adjustPosition: i.adjustSubMenuPosition !== void 0 ? i.adjustSubMenuPosition : n(b).adjustPosition, direction: i.direction !== void 0 ? i.direction : n(b).direction }, null, 8, ["parentMenuItemContext", "items", "maxWidth", "minWidth", "adjustPosition", "direction"]) ]), key: "0" } : void 0 ]), 1032, ["clickHandler", "disabled", "hidden", "icon", "iconFontClass", "svgIcon", "svgProps", "label", "customRender", "customClass", "checked", "shortcut", "clickClose", "clickableWhenHasChildren", "preserveIconWidth", "showRightArrow", "hasChildren", "rawMenuItem", "onSubMenuOpen", "onSubMenuClose"])), i.hidden !== !0 && (i.divided === "down" || i.divided === !0) ? (p(), A(he, { key: 3 })) : F("", !0) ], 64))), 128)) ]) ], 512) ]), _: 3 }, 8, ["scroll", "style"]) ], 16) ], 8, ["to"])) : F("", !0); }; } }); const et = /* @__PURE__ */ q({ __name: "ContextSubMenuWrapper", props: { /** * Menu options */ options: { type: Object, default: null }, /** * Show menu? */ show: { type: Boolean, default: null }, /** * Current container, For calculation only */ container: { type: Object, default: null }, /** * Make sure is user set the custom container. */ isFullScreenContainer: { type: Boolean, default: !0 } }, emits: ["close", "closeAnimFinished"], setup(e, { expose: o, emit: l }) { const c = e, m = l, t = Ue(), b = P(), { options: d, show: y, container: f } = ce(c); pe(() => { y.value && x(); }), ye(() => { O(); }), Je(y, (h) => { h ? x() : (Le(w), O()); }); const w = { closeMenu: v, isClosed: T, getMenuRef: () => b.value, getMenuDimensions: () => { var h; return ((h = b.value) == null ? void 0 : h.getMenuDimensions()) ?? { width: 0, height: 0 }; } }; let H = !1; function x() { L(), ut(w); } function v(h) { H = !0, m("close", h), d.value.menuTransitionProps || m("closeAnimFinished"), Le(w); } function T() { return H; } function L() { setTimeout(() => { document.addEventListener("click", I, !0), document.addEventListener("contextmenu", I, !0), document.addEventListener("scroll", g, !0), !c.isFullScreenContainer && f.value && f.value.addEventListener("scroll", g, !0), d.value.keyboardControl !== !1 && document.addEventListener("keydown", a, !0); }, 50); } function O() { document.removeEventListener("contextmenu", I, !0), document.removeEventListener("click", I, !0), document.removeEventListener("scroll", g, !0), !c.isFullScreenContainer && f.value && f.value.removeEventListener("scroll", g, !0), d.value.keyboardControl !== !1 && document.removeEventListener("keydown", a, !0); } const r = P(); Q("globalSetCurrentSubMenu", (h) => r.value = h), Q("globalGetMenuHostId", f.value.id); function a(h) { var B, N, Z, U, K, M, j, X, k, Y, ee, s, C; let _ = !0; switch (h.key) { case "Escape": { ((B = r.value) == null ? void 0 : B.isTopLevel()) === !1 ? (N = r.value) == null || N.closeCurrentSubMenu() : v(); break; } case "ArrowDown": (Z = r.value) == null || Z.moveCurrentItemDown(); break; case "ArrowUp": (U = r.value) == null || U.moveCurrentItemUp(); break; case "Home": (K = r.value) == null || K.moveCurrentItemFirst(); break; case "End": (M = r.value) == null || M.moveCurrentItemLast(); break; case "ArrowLeft": { (j = r.value) != null && j.closeSelfAndActiveParent() || (k = (X = d.value).onKeyFocusMoveLeft) == null || k.call(X); break; } case "ArrowRight": (Y = r.value) != null && Y.openCurrentItemSubMenu() || (s = (ee = d.value).onKeyFocusMoveRight) == null || s.call(ee); break; case "Enter": (C = r.value) == null || C.triggerCurrentItemClick(h); break; default: _ = !1; break; } _ && r.value && (h.stopPropagation(), h.preventDefault()); } function g(h) { d.value.closeWhenScroll !== !1 && R(h.target, null); } function I(h) { R(h.target, h); } function R(h, _) { var B, N; for (; h; ) { if (h.classList && h.classList.contains("mx-context-menu")) return; h = h.parentNode; } _ ? d.value.clickCloseOnOutside !== !1 ? (O(), v()) : (N = (B = d.value).onClickOnOutside) == null || N.call(B, _) : (O(), v()); } return Q("globalOptions", d), Q("globalCloseMenu", v), Q("globalIsFullScreenContainer", c.isFullScreenContainer), Q("globalHasSlot", (h) => t[h] !== void 0), Q("globalRenderSlot", (h, _) => E(t, h, { ..._ }, () => [be("span", "Render slot failed")], !1)), Q("menuContext", { zIndex: d.value.zIndex || G.defaultZindex, container: f.value, adjustPadding: { x: 0, y: 0 }, getZoom: () => d.value.zoom || G.defaultZoom, getParentWidth: () => 0, getParentHeight: () => 0, getPositon: () => [d.value.x, d.value.y], closeOtherSubMenuWithTimeOut: () => { }, checkCloseOtherSubMenuTimeOut: () => !1, addOpenedSubMenu: () => { }, closeOtherSubMenu: () => { }, getParentContext: () => null, getSubMenuInstanceContext: () => null, getElement: () => null, addChildMenuItem: () => { }, removeChildMenuItem: () => { }, markActiveMenuItem: () => { }, markThisOpenedByKeyBoard: () => { }, isOpenedByKeyBoardFlag: () => !1, isMenuItemDataCollectedFlag: () => !1 }), o(w), (h, _) => n(d).menuTransitionProps ? (p(), A(Ye, ge({ key: 0, appear: "" }, n(d).menuTransitionProps, { onAfterLeave: _[0] || (_[0] = (B) => m("closeAnimFinished")) }), { default: se(() => [ n(y) ? (p(), A(Ce, { key: 0, ref_key: "submenuInstance", ref: b, items: n(d).items, adjustPosition: n(d).adjustPosition, maxWidth: n(d).maxWidth || n(G).defaultMaxWidth, minWidth: n(d).minWidth || n(G).defaultMinWidth, direction: n(d).direction || n(G).defaultDirection }, { default: se(() => [ E(h.$slots, "default") ]), _: 3 }, 8, ["items", "adjustPosition", "maxWidth", "minWidth", "direction"])) : F("", !0) ]), _: 3 }, 16)) : n(y) ? (p(), A(Ce, { key: 1, ref_key: "submenuInstance", ref: b, items: n(d).items, adjustPosition: n(d).adjustPosition, maxWidth: n(d).maxWidth || n(G).defaultMaxWidth, minWidth: n(d).minWidth || n(G).defaultMinWidth, direction: n(d).direction || n(G).defaultDirection }, { default: se(() => [ E(h.$slots, "default") ]), _: 3 }, 8, ["items", "adjustPosition", "maxWidth", "minWidth", "direction"])) : F("", !0); } }); const jt = /* @__PURE__ */ q({ __name: "ContextMenu", props: { /** * Menu options */ options: { type: Object, default: null }, /** * Show menu? */ show: { type: Boolean, default: !1 } }, emits: ["update:show", "close"], setup(e, { expose: o, emit: l }) { const c = l, m = e, { options: t, show: b } = ce(m), { isNew: d, container: y, eleId: f } = qe(t.value), w = P(null), H = Ue(); function x(v) { var T, L; c("update:show", !1), c("close"), (L = (T = t.value).onClose) == null || L.call(T, v); } return o({ closeMenu: () => c("update:show", !1), isClosed: () => !b.value, getMenuRef: () => { var v; return (v = w.value) == null ? void 0 : v.getMenuRef(); }, getMenuDimensions: () => { var v; return ((v = w.value) == null ? void 0 : v.getMenuDimensions()) ?? { width: 0, height: 0 }; } }), (v, T) => (p(), A(Ze, { to: `#${n(f)}` }, [ re(et, { ref_key: "menuRef", ref: w, options: n(t), show: n(b), container: n(y), isFullScreenContainer: !n(d), onClose: x }, Ge({ _: 2 }, [ _e(n(H), (L, O) => ({ name: O, fn: se((r) => [ E(v.$slots, O, Ve(lt(r))) ]) })) ]), 1032, ["options", "show", "container", "isFullScreenContainer"]) ], 8, ["to"])); } }), At = q({ name: "ContextMenuGroup", props: { /** * Is this menu disabled? */ disabled: { type: Boolean, default: !1 }, /** * Is this menu hidden? */ hidden: { type: Boolean, default: !1 }, /** * Is this menu disabled? */ clickHandler: { type: Function, default: null }, /** * Menu label */ label: { type: String, default: "" }, /** * Menu icon (for icon class) */ icon: { type: String, default: "" }, /** * Custom icon library font class name. * * Only for css font icon, If you use the svg icon, you do not need to use this. */ iconFontClass: { type: String, default: "iconfont" }, /** * Is this menu item checked? * * The check mark are displayed on the left side of the icon, so it is not recommended to display the icon at the same time. */ checked: { type: Boolean, default: !1 }, /** * Shortcut key text display on the right. * * The shortcut keys here are only for display. You need to handle the key events by yourself. */ shortcut: { type: String, default: "" }, /** * Display icons use svg symbol (`<use xlink:href="#icon-symbol-name">`) , only valid when icon attribute is empty. */ svgIcon: { type: String, default: "" }, /** * The user-defined attribute of the svg tag, which is valid when using `svgIcon`. */ svgProps: { type: Object, default: null }, /** * Should a fixed-width icon area be reserved for menu items without icon. (this item) * * Default is true . * * The width of icon area can be override with css var `--mx-menu-placeholder-width`. */ preserveIconWidth: { type: Boolean, default: !0 }, /** * Show right arrow on this menu? */ showRightArrow: { type: Boolean, default: !1 }, /** * Should close menu when Click this menu item ? */ clickClose: { type: Boolean, default: !0 }, /** * By default, the submenu will automatically adjust its position to prevent it overflow the container. * * If you allow menu overflow containers, you can set this to false. * * Default is inherit from `MenuOptions.adjustPosition` . */ adjustSubMenuPosition: { type: Boolean, default: void 0 }, /** * Max width of submenu */ maxWidth: { type: [String, Number], default: 0 }, /** * Min width of submenu */ minWidth: { type: [String, Number], default: 0 } }, setup(e, o) { const l = V("globalOptions"), { adjustSubMenuPosition: c, maxWidth: m, minWidth: t } = ce(e), b = typeof c.value < "u" ? c.value : l.value.adjustPosition, d = P(), y = P(); return o.expose({ getSubMenuRef: () => d.value, getMenuItemRef: () => y.value }), () => be(Pe, { ...e, ref: y, showRightArrow: !0, maxWidth: void 0, minWidth: void 0, adjustSubMenuPosition: void 0, hasChildren: typeof o.slots.default !== void 0 }, o.slots.default ? { //Create SubMenu submenu: (f) => be(Ce, { ref: d, maxWidth: m.value, minWidth: t.value, adjustPosition: b, parentMenuItemContext: f.context }, { default: o.slots.default }), //Add other slots ...ct(o.slots, "default") } : o.slots); } }); function Ft(e, o, l, c) { const m = P(!0), t = be(et, { options: e, show: m.value, container: o, isFullScreenContainer: !l, onCloseAnimFinished: () => { Te(null, o); }, onClose: (b) => { var d; (d = e.onClose) == null || d.call(e, b), m.value = !1; } }, c); return Te(t, o), t.component; } function Ne(e, o) { const l = qe(e); return Ft(e, l.container, l.isNew, o).exposed; } const Ke = { /** * For Vue install * @param app */ install(e) { e.config.globalProperties.$contextmenu = Ne, e.component("ContextMenu", jt), e.component("ContextMenuItem", Pe), e.component("ContextMenuGroup", At), e.component("ContextMenuSperator", he), e.component("ContextMenuSeparator", he), e.component("ContextSubMenu", Ce); }, /** * Show a ContextMenu in page, same as `this.$contextmenu` * * For example: * * ```ts * onContextMenu(e : MouseEvent) { * //prevent the browser's default menu * e.preventDefault(); * //show your menu * ContextMenu.showContextMenu({ * x: e.x, * y: e.y, * items: [ * { * label: "A menu item", * onClick: () => { * alert("You click a menu item"); * } * }, * { * label: "A submenu", * children: [ * { label: "Item1" }, * { label: "Item2" }, * { label: "Item3" }, * ] * }, * ] * }); * } * ``` * * You can pass customSlots to custom rendering this menu. * * For example, custom rendering #itemRender and #separatorRender: * ```ts * ContextMenu.showContextMenu({ * ... * } as MenuOptions, { * //Use slot in function mode * itemRender: ({ disabled, label, icon, showRightArrow, onClick, onMouseEnter }) => [ h('div', { * class: 'my-menu-item'+(disabled?' disabled':''), * onMouseenter: onMouseEnter, * onClick: onClick, * }, [ * icon ? h('img', { src: icon }) : h('div', { class: 'icon-place-holder' }), * h('span', label), * showRightArrow ? h('span', { class: 'right-arraw' }, '>>') : h('div'), * ]) ], * separatorRender: () => [ h('div', { class: 'my-menu-sperator' }) ] * }) * ``` * * @param options The options of ContextMenu * @param customSlots You can provide some custom slots to customize the rendering style of the menu. These slots are the same as the slots of component ContextMenu. * @returns Menu instance */ showContextMenu(e, o) { return Ne(e, o); }, /** * Get if there is a menu open now. */ isAnyContext