@imengyu/vue3-context-menu
Version:
A context menu component for Vue3
1,600 lines • 56.1 kB
JavaScript
import { defineComponent as te, toRefs as re, ref as H, computed as ze, reactive as Ee, onMounted as pe, nextTick as me, onBeforeUnmount as ye, openBlock as C, createElementBlock as $, normalizeClass as oe, normalizeStyle as be, createElementVNode as E, renderSlot as z, unref as s, createCommentVNode as D, inject as N, provide as q, createBlock as F, mergeProps as Ie, createVNode as ie, toDisplayString as We, resolveComponent as Je, watch as He, Teleport as Qe, Transition as rt, withCtx as ve, Fragment as _e, renderList as Re, createSlots as qe, useSlots as et, h as Ce, normalizeProps as ct, guardReactiveProps as dt, render as je } from "vue";
let le = null;
function ft() {
return le !== null;
}
function vt(e) {
le && tt(), le = e;
}
function De(e) {
e === le && (le = null);
}
function tt() {
le && (le.closeMenu(), le = null);
}
const ee = {
defaultDirection: "br",
defaultMinWidth: 100,
defaultMaxWidth: 600,
defaultZindex: 100,
defaultZoom: 1,
defaultAdjustPadding: {
x: 0,
y: 10
}
};
function se(e, l) {
let u = e.offsetTop;
return e.offsetParent != null && e.offsetParent != l && (u -= e.offsetParent.scrollTop, u += se(e.offsetParent, l)), u;
}
function ue(e, l) {
let u = e.offsetLeft;
return e.offsetParent != null && e.offsetParent != l && (u -= e.offsetParent.scrollLeft, u += ue(e.offsetParent, l)), u;
}
function ht(e, l, u, i) {
return {
x: ue(e, i) + l,
y: se(e, i) + u
};
}
const we = "mx-menu-default-container", mt = "mx-menu-container-";
let pt = 0;
function nt(e) {
const { getContainer: l, zIndex: u } = e;
if (l) {
const v = typeof l == "function" ? l() : l;
if (v) {
let t = v.getAttribute("id");
return t || (t = mt + pt++, v.setAttribute("id", t)), {
eleId: t,
container: v,
isNew: !1
};
}
}
let i = document.getElementById(we);
return i || (i = document.createElement("div"), i.setAttribute("id", we), i.setAttribute("class", "mx-menu-ghost-host fullscreen"), document.body.appendChild(i)), i.style.zIndex = (u == null ? void 0 : u.toString()) || ee.defaultZindex.toString(), {
eleId: we,
container: i,
isNew: !0
};
}
function Ne(e) {
return typeof e == "number" ? `${e}px` : e;
}
const Q = te({
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: l, data: u } = re(e);
return () => typeof l.value == "function" ? l.value(u.value) : l.value;
}
});
function gt(e, l) {
const u = { ...e };
return delete u[l], u;
}
var bt = Object.defineProperty, Ct = (e, l, u) => l in e ? bt(e, l, { enumerable: !0, configurable: !0, writable: !0, value: u }) : e[l] = u, Ke = (e, l, u) => Ct(e, typeof l != "symbol" ? l + "" : l, u);
class Ye {
constructor(l, u) {
Ke(this, "x", 0), Ke(this, "y", 0), this.x = l || 0, this.y = u || 0;
}
set(l, u) {
this.x = l, this.y = u;
}
substract(l) {
this.x -= l.x, this.y -= l.y;
}
}
function Ve(e) {
const { onDown: l, onMove: u, onUp: i } = e, v = new Ye(), t = new Ye();
let W;
function c(f) {
f.stopPropagation(), t.set(f.x, f.y), t.substract(v), u(v, t, f, W);
}
function g(f) {
i(f, W), v.set(0, 0), document.removeEventListener("mousemove", c), document.removeEventListener("mouseup", g);
}
return (f, S) => l(f, S) ? (W = S, v.set(f.x, f.y), document.addEventListener("mousemove", c), document.addEventListener("mouseup", g), f.stopPropagation(), !0) : !1;
}
function yt(e, l) {
let u = 0;
return {
start() {
u > 0 && clearInterval(u), u = setInterval(() => {
u = 0, l();
}, e);
},
stop() {
u > 0 && (clearInterval(u), u = 0);
}
};
}
const fe = [], Xe = yt(100, () => {
for (const e of fe)
e();
});
function xt(e, l, u, i) {
let v = 0, t = 0;
function W() {
e.value && (l && v !== e.value.offsetWidth && l(e.value.offsetWidth), u && t !== e.value.offsetHeight && u(e.value.offsetHeight), i && (v !== e.value.offsetWidth || t !== e.value.offsetHeight) && i(e.value.offsetWidth, e.value.offsetHeight), v = e.value.offsetWidth, t = e.value.offsetHeight);
}
return {
startResizeChecker() {
Xe.start(), fe.push(W);
},
stopResizeChecker() {
const c = fe.indexOf(W);
c >= 0 && fe.splice(c, 1), fe.length === 0 && Xe.stop();
}
};
}
const Ze = 140, Mt = 70, kt = /* @__PURE__ */ te({
__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
},
height: {
type: Number,
default: void 0
},
width: {
type: Number,
default: void 0
},
/**
*
*/
maxHeight: {
type: Number,
default: void 0
},
maxWidth: {
type: Number,
default: void 0
},
/**
* CSS class of inner container
*/
containerClass: {
type: String,
default: ""
},
/**
* Container style
*/
containerStyle: {
type: null
}
},
emits: ["scroll", "resized"],
setup(e, { expose: l, emit: u }) {
const i = e, v = u, t = H(), W = H(), c = H(), g = H(), f = H(), S = H(), R = H(!1), L = ze(() => i.scroll === "horizontal" || i.scroll === "both"), w = ze(() => i.scroll === "vertical" || i.scroll === "both"), M = Ee({
show: !1,
size: 0,
sizeRaw: 0,
pos: 0
}), y = Ee({
show: !1,
size: 0,
sizeRaw: 0,
pos: 0
});
let _ = 0, r = 0, h = 0, m = 0, d = null;
const B = { attributes: !0, childList: !0 };
function p() {
if (t.value) {
if (M.show) {
const n = t.value.offsetWidth / t.value.scrollWidth;
M.sizeRaw = n * t.value.offsetWidth, M.size = n * 100, M.pos = t.value.scrollLeft / (t.value.scrollWidth - t.value.offsetWidth) * (100 - M.size), n >= 1 && (M.show = !1);
}
if (y.show) {
const n = t.value.offsetHeight / t.value.scrollHeight;
y.sizeRaw = n * t.value.offsetHeight, y.size = n * 100, y.pos = t.value.scrollTop / (t.value.scrollHeight - t.value.offsetHeight) * (100 - y.size), n >= 1 && (y.show = !1);
}
v("scroll", t.value.scrollLeft, t.value.scrollTop);
}
}
function b(n = !1) {
if (!t.value)
return;
let o = L.value, P = w.value;
const U = o && (_ !== t.value.scrollWidth || h !== t.value.offsetWidth), V = w && (r !== t.value.scrollHeight || m !== t.value.offsetHeight);
if (!n && !U && !V)
return;
const J = window.getComputedStyle(t.value);
(J.overflow === "hidden" || J.overflowX === "hidden") && (o = !1), (J.overflow === "hidden" || J.overflowY === "hidden") && (P = !1), M.show = o, y.show = P, p(), h = t.value.offsetWidth, m = t.value.offsetHeight, _ = t.value.scrollWidth, r = t.value.scrollHeight, v("resized", _, r);
}
function O(n) {
var o;
i.scroll == "horizontal" && (n.deltaMode == 0 && ((o = t.value) == null || o.scrollTo({
left: t.value.scrollLeft + (n.deltaY > 0 ? Ze : -140),
behavior: "smooth"
})), n.preventDefault(), n.stopPropagation());
}
function K(n) {
var o;
n.deltaMode == 0 && ((o = t.value) == null || o.scrollTo({
left: t.value.scrollLeft + (n.deltaY > 0 ? Ze : -140),
behavior: "smooth"
}), n.preventDefault(), n.stopPropagation());
}
function j(n) {
var o;
n.deltaMode == 0 && ((o = t.value) == null || o.scrollTo({
top: t.value.scrollTop + (n.deltaY > 0 ? Mt : -70),
behavior: "smooth"
}), n.preventDefault(), n.stopPropagation());
}
let G = 0, T = 0, k = 0, A = 0;
const X = Ve({
onDown(n) {
return !c.value || !f.value ? !1 : (G = n.offsetX, T = n.x - n.offsetX - f.value.offsetLeft, n.preventDefault(), R.value = !0, !0);
},
onMove(n, o, P) {
t.value && c.value && (I(P.x - G - T), P.preventDefault(), P.stopPropagation());
},
onUp() {
R.value = !1;
}
}), Y = Ve({
onDown(n) {
return !g.value || !S.value ? !1 : (k = n.offsetY, A = n.y - n.offsetY - S.value.offsetTop, n.preventDefault(), R.value = !0, !0);
},
onMove(n, o, P) {
t.value && g.value && (ne(P.y - k - A), P.preventDefault(), P.stopPropagation());
},
onUp() {
R.value = !1;
}
});
function Z(n) {
t.value && (t.value.scrollLeft = n / 100 * (t.value.scrollWidth - t.value.offsetWidth));
}
function ae(n) {
t.value && (t.value.scrollLeft = n / 100 * (t.value.scrollHeight - t.value.offsetHeight));
}
function I(n) {
t.value && (t.value.scrollLeft = n / (t.value.offsetWidth - M.sizeRaw) * (t.value.scrollWidth - t.value.offsetWidth));
}
function ne(n) {
t.value && (t.value.scrollTop = n / (t.value.offsetHeight - y.sizeRaw) * (t.value.scrollHeight - t.value.offsetHeight));
}
function Pe(n) {
i.scrollBarBackgroundClickable && I(n.offsetX - M.sizeRaw / 2);
}
function ge(n) {
i.scrollBarBackgroundClickable && ne(n.offsetY - y.sizeRaw / 2);
}
const {
startResizeChecker: a,
stopResizeChecker: x
} = xt(
t,
() => b(),
() => b()
);
return pe(() => {
me(() => {
setTimeout(() => b(!0), 200), b(!0), a(), d = new MutationObserver(() => b()), d.observe(t.value, B);
});
}), ye(() => {
x(), d && (d.disconnect(), d = null);
}), l({
refreshScrollState() {
b(!0);
},
getScrollContainer() {
return t.value;
},
scrollTo(n, o) {
var P;
(P = t.value) == null || P.scrollTo(n, o);
},
scrollToTop() {
var n;
(n = t.value) == null || n.scrollTo(0, 0);
},
scrollToBottom() {
var n;
(n = t.value) == null || n.scrollTo(t.value.scrollWidth, t.value.scrollHeight);
}
}), (n, o) => (C(), $("div", {
ref_key: "scrollrect",
ref: W,
class: oe([
"vue-scroll-rect",
e.scrollBarAlwaysShow ? "always-show-scrollbar" : "",
e.scrollBarBackgroundClickable ? "background-clickable" : "",
R.value ? "dragging" : ""
]),
style: be({
width: e.width ? `${e.width}px` : void 0,
height: e.height ? `${e.height}px` : void 0
}),
onWheel: O
}, [
E("div", {
ref_key: "container",
ref: t,
class: oe(["scroll-content", e.scroll, e.containerClass]),
style: be({
maxWidth: e.maxWidth ? `${e.maxWidth}px` : void 0,
maxHeight: e.maxHeight ? `${e.maxHeight}px` : void 0,
...e.containerStyle
}),
onScroll: p
}, [
z(n.$slots, "default")
], 38),
M.show ? z(n.$slots, "scrollBarX", {
key: 0,
scrollBarValue: M,
onScroll: Z
}, () => [
E("div", {
ref_key: "scrollBarRefX",
ref: c,
class: "scrollbar horizontal",
onClick: Pe,
onWheel: K
}, [
E("div", {
class: "thumb",
ref_key: "scrollBarThumbRefX",
ref: f,
style: be({ left: `${M.pos}%`, width: `${M.size}%` }),
onMousedown: o[0] || (o[0] = //@ts-ignore
(...P) => s(X) && s(X)(...P)),
onWheel: K
}, null, 36)
], 544)
]) : D("", !0),
y.show ? z(n.$slots, "scrollBarY", {
key: 1,
scrollBarValue: y,
onScroll: ae
}, () => [
y.show ? (C(), $("div", {
key: 0,
ref_key: "scrollBarRefY",
ref: g,
class: "scrollbar vertical",
onClick: ge,
onWheel: j
}, [
E("div", {
class: "thumb",
ref_key: "scrollBarThumbRefY",
ref: S,
style: be({ top: `${y.pos}%`, height: `${y.size}%` }),
onMousedown: o[1] || (o[1] = //@ts-ignore
(...P) => s(Y) && s(Y)(...P)),
onWheel: j
}, null, 36)
], 544)) : D("", !0)
]) : D("", !0)
], 38));
}
}), xe = (e, l) => {
const u = e.__vccOpts || e;
for (const [i, v] of l)
u[i] = v;
return u;
}, St = {}, wt = {
class: "mx-checked-mark",
"aria-hidden": "true",
viewBox: "0 0 1024 1024"
}, It = /* @__PURE__ */ E("path", { d: "M129.3,428.6L52,512l345,372.5l575-620.8l-69.5-75L400.4,718.2L129.3,428.6z" }, null, -1), Wt = [
It
];
function _t(e, l) {
return C(), $("svg", wt, Wt);
}
const Ht = /* @__PURE__ */ xe(St, [["render", _t]]), Rt = {}, Ot = {
class: "mx-right-arrow",
"aria-hidden": "true",
viewBox: "0 0 1024 1024"
}, Bt = /* @__PURE__ */ E("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), Pt = [
Bt
];
function $t(e, l) {
return C(), $("svg", Ot, Pt);
}
const Lt = /* @__PURE__ */ xe(Rt, [["render", $t]]), At = { class: "mx-item-row" }, Ft = ["xlink:href"], Tt = {
key: 1,
class: "label"
}, zt = { class: "mx-item-row" }, Et = { class: "mx-shortcut" }, Oe = /* @__PURE__ */ te({
__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: l, emit: u }) {
const i = e, v = u, {
clickHandler: t,
clickClose: W,
clickableWhenHasChildren: c,
disabled: g,
hidden: f,
label: S,
icon: R,
iconFontClass: L,
showRightArrow: w,
shortcut: M,
hasChildren: y
} = re(i), _ = H(!1), r = H(!1), h = H(), m = N("globalOptions"), d = N("globalHasSlot"), B = N("globalRenderSlot"), p = N("globalCloseMenu"), b = N("menuContext"), O = {
getSubMenuInstance: () => {
},
showSubMenu: () => _.value ? (b.markActiveMenuItem(O, !0), !0) : y.value ? (j(), !0) : !1,
hideSubMenu: () => {
b.closeOtherSubMenu();
},
isDisabledOrHidden: () => g.value || f.value,
getElement: () => h.value,
focus: () => r.value = !0,
blur: () => r.value = !1,
click: K
};
q("menuItemInstance", O), pe(() => {
b.isMenuItemDataCollectedFlag() ? me(() => {
let k = 0;
const A = b.getElement();
if (A) {
let X = 0;
for (let Y = 0; Y < A.children.length; Y++) {
const Z = A.children[Y];
if (Z.getAttribute("data-type") === "ContextMenuItem") {
if (Z === h.value) {
k = X;
break;
}
X++;
}
}
}
b.addChildMenuItem(O, k);
}) : b.addChildMenuItem(O);
}), ye(() => {
b.removeChildMenuItem(O);
});
function K(k) {
if (!g.value) {
if (k) {
const A = k.target;
if (A.classList.contains("mx-context-no-clickable") || m.value.ignoreClickClassName && A.classList.contains(m.value.ignoreClickClassName))
return;
if (m.value.clickCloseClassName && A.classList.contains(m.value.clickCloseClassName)) {
k.stopPropagation(), p(i.rawMenuItem);
return;
}
}
y.value ? c.value ? (typeof t.value == "function" && t.value(k), v("click", k)) : _.value || j() : (typeof t.value == "function" && t.value(k), v("click", k), W.value && p(i.rawMenuItem));
}
}
function j(k) {
r.value = !1, b.checkCloseOtherSubMenuTimeOut() || b.closeOtherSubMenu(), g.value || (b.markActiveMenuItem(O), y.value && (k || b.markThisOpenedByKeyBoard(), b.addOpenedSubMenu(G), _.value = !0, me(() => v("subMenuOpen", O))));
}
function G() {
r.value = !1, _.value = !1, v("subMenuClose", O);
}
function T() {
return {
disabled: g.value,
label: S.value,
icon: R.value,
iconFontClass: L.value,
showRightArrow: w.value,
clickClose: W.value,
clickableWhenHasChildren: c.value,
shortcut: M.value,
theme: m.value.theme,
isOpen: _,
hasChildren: y,
onClick: K,
onMouseEnter: j,
closeMenu: p
};
}
return l(O), (k, A) => s(f) ? D("", !0) : (C(), $("div", {
key: 0,
class: "mx-context-menu-item-wrapper",
ref_key: "menuItemRef",
ref: h,
"data-type": "ContextMenuItem"
}, [
s(d)("itemRender") ? (C(), F(s(Q), {
key: 0,
vnode: () => s(B)("itemRender", T())
}, null, 8, ["vnode"])) : e.customRender ? (C(), F(s(Q), {
key: 1,
vnode: e.customRender,
data: T()
}, null, 8, ["vnode", "data"])) : (C(), $("div", {
key: 2,
class: oe([
"mx-context-menu-item",
s(g) ? "disabled" : "",
r.value ? "keyboard-focus" : "",
e.customClass ? " " + e.customClass : "",
_.value ? "open" : ""
]),
onClick: K,
onMouseenter: j
}, [
z(k.$slots, "default", {}, () => [
E("div", At, [
E("div", {
class: oe([
"mx-icon-placeholder",
e.preserveIconWidth ? "preserve-width" : ""
])
}, [
z(k.$slots, "icon", {}, () => [
s(d)("itemIconRender") ? (C(), F(s(Q), {
key: 0,
vnode: () => s(B)("itemIconRender", T())
}, null, 8, ["vnode"])) : typeof e.svgIcon == "string" && e.svgIcon ? (C(), $("svg", Ie({
key: 1,
class: "icon svg"
}, e.svgProps), [
E("use", { "xlink:href": e.svgIcon }, null, 8, Ft)
], 16)) : typeof s(R) != "string" ? (C(), F(s(Q), {
key: 2,
vnode: s(R),
data: s(R)
}, null, 8, ["vnode", "data"])) : typeof s(R) == "string" && s(R) !== "" ? (C(), $("i", {
key: 3,
class: oe(s(R) + " icon " + s(L) + " " + s(m).iconFontClass)
}, null, 2)) : D("", !0)
]),
e.checked ? z(k.$slots, "check", { key: 0 }, () => [
s(d)("itemCheckRender") ? (C(), F(s(Q), {
key: 0,
vnode: () => s(B)("itemCheckRender", T())
}, null, 8, ["vnode"])) : D("", !0),
ie(Ht)
]) : D("", !0)
], 2),
z(k.$slots, "label", {}, () => [
s(d)("itemLabelRender") ? (C(), F(s(Q), {
key: 0,
vnode: () => s(B)("itemLabelRender", T())
}, null, 8, ["vnode"])) : typeof s(S) == "string" ? (C(), $("span", Tt, We(s(S)), 1)) : (C(), F(s(Q), {
key: 2,
vnode: s(S),
data: s(S)
}, null, 8, ["vnode", "data"]))
])
]),
E("div", zt, [
s(M) || k.$slots.shortcut ? z(k.$slots, "shortcut", { key: 0 }, () => [
s(d)("itemShortcutRender") ? (C(), F(s(Q), {
key: 0,
vnode: () => s(B)("itemShortcutRender", T())
}, null, 8, ["vnode"])) : D("", !0),
E("span", Et, We(s(M)), 1)
]) : D("", !0),
s(w) ? z(k.$slots, "rightArrow", { key: 1 }, () => [
s(d)("itemRightArrowRender") ? (C(), F(s(Q), {
key: 0,
vnode: () => s(B)("itemRightArrowRender", T())
}, null, 8, ["vnode"])) : D("", !0),
ie(Lt)
]) : D("", !0)
])
])
], 34)),
z(k.$slots, "submenu", {
context: O,
show: _.value
})
], 512));
}
}), jt = te({
name: "ContextMenuSperator",
components: {
VNodeRender: Q
},
setup() {
const e = N("globalHasSlot"), l = N("globalRenderSlot");
return {
globalHasSlot: e,
globalRenderSlot: l
};
}
}), Dt = {
key: 1,
class: "mx-context-menu-item-sperator mx-context-no-clickable"
};
function Nt(e, l, u, i, v, t) {
const W = Je("VNodeRender");
return e.globalHasSlot("separatorRender") ? (C(), F(W, {
key: 0,
vnode: () => e.globalRenderSlot("separatorRender", {})
}, null, 8, ["vnode"])) : (C(), $("div", Dt));
}
const he = /* @__PURE__ */ xe(jt, [["render", Nt]]), Be = /* @__PURE__ */ te({
__name: "ContextSubMenu",
props: {
/**
* Items from options
*/
items: {
type: Object,
default: null
},
/**
* Show
*/
show: {
type: Boolean,
default: !1
},
/**
* Max height for this submenu
*/
maxHeight: {
type: Number,
default: 0
},
/**
* 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
}
},
emits: ["closeAnimFinished"],
setup(e, { expose: l, emit: u }) {
const i = e, v = u, t = H(!1), W = N("globalGetMenuHostId", ""), c = N("menuContext"), g = N("globalOptions");
N("globalHasSlot"), N("globalRenderSlot");
const { zIndex: f, getParentWidth: S, getParentHeight: R, getZoom: L } = c, { adjustPosition: w } = re(i), M = H(), y = H(), _ = H(), r = [], h = N("globalSetCurrentSubMenu"), m = [];
let d = null, B = 0;
function p() {
d && d.blur();
}
function b(a, x) {
if (a) {
for (let n = x !== void 0 ? x : 0; n < m.length; n++)
if (!m[n].isDisabledOrHidden()) {
O(n);
break;
}
} else
for (let n = x !== void 0 ? x : m.length - 1; n >= 0; n--)
if (!m[n].isDisabledOrHidden()) {
O(n);
break;
}
}
function O(a) {
var n;
if (d && p(), a !== void 0 && (d = m[Math.max(0, Math.min(a, m.length - 1))]), !d)
return;
d.focus();
const x = d.getElement();
x && ((n = M.value) == null || n.scrollTo(0, Math.min(Math.max(-Y.value, -x.offsetTop - x.offsetHeight + Z.value), 0)));
}
function K() {
h(j);
}
const j = {
isTopLevel: () => c.getParentContext() === null,
closeSelfAndActiveParent: () => {
const a = k.getParentContext();
if (a) {
a.closeOtherSubMenu();
const x = a.getSubMenuInstanceContext();
if (x)
return x.focusCurrentItem(), !0;
}
return !1;
},
closeCurrentSubMenu: () => {
var a;
return (a = k.getParentContext()) == null ? void 0 : a.closeOtherSubMenu();
},
moveCurrentItemFirst: () => b(!0),
moveCurrentItemLast: () => b(!1),
moveCurrentItemDown: () => b(!0, d ? m.indexOf(d) + 1 : 0),
moveCurrentItemUp: () => b(!1, d ? m.indexOf(d) - 1 : 0),
focusCurrentItem: () => O(),
openCurrentItemSubMenu: () => d ? d == null ? void 0 : d.showSubMenu() : !1,
triggerCurrentItemClick: (a) => d == null ? void 0 : d.click(a)
};
let G = !1, T = !1;
const k = {
zIndex: f + 1,
container: c.container,
adjustPadding: g.value.adjustPadding || ee.defaultAdjustPadding,
getParentWidth: () => {
var a;
return ((a = _.value) == null ? void 0 : a.offsetWidth) || 0;
},
getParentHeight: () => {
var a;
return ((a = _.value) == null ? void 0 : a.offsetHeight) || 0;
},
getPositon: () => [I.value.x, I.value.y],
getZoom: () => g.value.zoom || ee.defaultZoom,
addOpenedSubMenu(a) {
r.push(a);
},
closeOtherSubMenu() {
r.forEach((a) => a()), r.splice(0, r.length), h(j);
},
checkCloseOtherSubMenuTimeOut() {
return B ? (clearTimeout(B), B = 0, !0) : !1;
},
closeOtherSubMenuWithTimeOut() {
B = setTimeout(() => {
B = 0, this.closeOtherSubMenu();
}, 200);
},
addChildMenuItem: (a, x) => {
x === void 0 ? m.push(a) : m.splice(x, 0, a);
},
removeChildMenuItem: (a) => {
m.splice(m.indexOf(a), 1), a.getSubMenuInstance = () => {
};
},
markActiveMenuItem: (a, x = !1) => {
p(), d = a, x && O();
},
markThisOpenedByKeyBoard: () => {
G = !0;
},
isOpenedByKeyBoardFlag: () => G ? (G = !1, !0) : !1,
isMenuItemDataCollectedFlag: () => T,
getElement: () => _.value || null,
getParentContext: () => c,
getSubMenuInstanceContext: () => j
};
q("menuContext", k);
const A = {
getChildItem: (a) => m[a],
getMenuDimensions: () => y.value ? {
width: y.value.offsetWidth,
height: y.value.offsetHeight
} : { width: 0, height: 0 },
getSubmenuRoot: () => y.value,
getMenu: () => _.value,
getScrollValue: () => {
var a, x;
return ((x = (a = M.value) == null ? void 0 : a.getScrollContainer()) == null ? void 0 : x.scrollTop) || 0;
},
setScrollValue: (a) => {
var x;
return (x = M.value) == null ? void 0 : x.scrollTo(0, a);
},
getScrollHeight: () => Y.value,
adjustPosition: () => {
ne();
},
getMaxHeight: () => Z.value,
getPosition: () => I.value,
setPosition: (a, x) => {
I.value.x = a, I.value.y = x;
}
}, X = N("menuItemInstance", void 0);
X && (X.getSubMenuInstance = () => A);
const Y = H(0), Z = H(0), ae = H(!1), I = H({ x: 0, y: 0 });
function ne() {
me(() => {
const a = _.value, x = y.value;
if (a && x && M.value) {
const { container: n } = c, o = (S == null ? void 0 : S()) ?? 0, P = (R == null ? void 0 : R()) ?? 0, U = getComputedStyle(x), V = parseFloat(U.paddingLeft), J = parseFloat(U.paddingTop), Me = P > 0 ? J : 0, lt = document.documentElement.scrollHeight / L(), st = document.documentElement.scrollWidth / L(), ut = Math.min(st, n.offsetWidth), $e = Math.min(lt, n.offsetHeight);
let ke = ue(a, n), Se = se(a, n);
i.direction.includes("l") ? I.value.x -= a.offsetWidth + V : i.direction.includes("r") ? I.value.x += o + V : (I.value.x += o / 2, I.value.x -= (a.offsetWidth + V) / 2), i.direction.includes("t") ? I.value.y -= (a.offsetHeight + J * 2) / L() : i.direction.includes("b") ? I.value.y -= J / L() : I.value.y -= (a.offsetHeight + J) / 2 / L(), me(() => {
var Fe, Te;
ke = ue(a, n), Se = se(a, n);
const Le = ((Te = (Fe = M.value) == null ? void 0 : Fe.getScrollContainer()) == null ? void 0 : Te.scrollHeight) || 0, at = i.maxHeight;
Y.value = i.maxHeight ? Math.min(Le, i.maxHeight) : Le;
const it = ke + a.offsetWidth - ut, Ae = Se + Y.value + Me * 2 - $e;
if (ae.value = Ae > 0, w.value && it > 0) {
const ce = o + a.offsetWidth - V, de = ke;
ce > de ? I.value.x -= de : I.value.x -= ce;
}
if (ae.value) {
if (w.value) {
const ce = Ae, de = Se;
ce > de ? I.value.y -= de - Me : I.value.y -= ce - Me;
}
Z.value = $e - (I.value.y + J);
} else
Z.value = at || 0;
});
}
a == null || a.focus({
preventScroll: !0
}), c.isOpenedByKeyBoardFlag() && b(!0), T = !0;
});
}
function Pe() {
}
function ge() {
var x;
const a = (x = i.parentMenuItemContext) == null ? void 0 : x.getElement();
if (a) {
const n = ue(a, c.container), o = se(a, c.container);
I.value.x = n, I.value.y = o;
} else {
const [n, o] = c.getPositon();
I.value.x = n, I.value.y = o;
}
ne();
}
return He(() => i.show, (a) => {
a ? ge() : void 0;
}), pe(() => {
t.value = !0, h(j), ge();
}), ye(() => {
t.value = !1, X && (X.getSubMenuInstance = () => {
});
}), l(A), (a, x) => {
const n = Je("ContextSubMenu", !0);
return t.value ? (C(), F(Qe, {
key: 0,
to: `#${s(W)}`
}, [
ie(rt, Ie({ appear: "" }, s(g).menuTransitionProps || {
duration: 10
}, {
onAfterLeave: x[0] || (x[0] = (o) => v("closeAnimFinished"))
}), {
default: ve(() => [
e.show ? (C(), $("div", Ie({
key: 0,
ref_key: "submenuRoot",
ref: y
}, a.$attrs, {
class: [
"mx-context-menu",
s(g).customClass ? s(g).customClass : "",
s(g).theme ?? ""
],
style: {
maxWidth: e.maxWidth ? s(Ne)(e.maxWidth) : `${s(ee).defaultMaxWidth}px`,
minWidth: e.minWidth ? s(Ne)(e.minWidth) : `${s(ee).defaultMinWidth}px`,
zIndex: s(f),
left: `${I.value.x}px`,
top: `${I.value.y}px`
},
"data-type": "ContextSubMenu",
onClick: K
}), [
ie(s(kt), {
ref_key: "scrollRectRef",
ref: M,
scroll: "vertical",
maxHeight: Z.value,
containerClass: "mx-context-menu-scroll"
}, {
default: ve(() => [
E("div", {
class: oe(["mx-context-menu-items"]),
ref_key: "menu",
ref: _
}, [
z(a.$slots, "default", {}, () => [
(C(!0), $(_e, null, Re(e.items, (o, P) => (C(), $(_e, { key: P }, [
o.hidden !== !0 && o.divided === "up" ? (C(), F(he, { key: 0 })) : D("", !0),
o.hidden !== !0 && o.divided === "self" ? (C(), F(he, { key: 1 })) : (C(), F(Oe, {
key: 2,
clickHandler: o.onClick ? (U) => o.onClick(U) : void 0,
disabled: typeof o.disabled == "object" ? o.disabled.value : o.disabled,
hidden: typeof o.hidden == "object" ? o.hidden.value : o.hidden,
icon: o.icon,
iconFontClass: o.iconFontClass,
svgIcon: o.svgIcon,
svgProps: o.svgProps,
label: o.label,
customRender: o.customRender,
customClass: o.customClass,
checked: typeof o.checked == "object" ? o.checked.value : o.checked,
shortcut: o.shortcut,
clickClose: o.clickClose,
clickableWhenHasChildren: o.clickableWhenHasChildren,
preserveIconWidth: o.preserveIconWidth !== void 0 ? o.preserveIconWidth : s(g).preserveIconWidth,
showRightArrow: o.children && o.children.length > 0,
hasChildren: o.children && o.children.length > 0,
rawMenuItem: o,
onSubMenuOpen: (U) => {
var V;
return (V = o.onSubMenuOpen) == null ? void 0 : V.call(o, U);
},
onSubMenuClose: (U) => {
var V;
return (V = o.onSubMenuClose) == null ? void 0 : V.call(o, U);
}
}, qe({ _: 2 }, [
o.children && o.children.length > 0 ? {
name: "submenu",
fn: ve(({ context: U, show: V }) => [
ie(n, {
show: V,
parentMenuItemContext: U,
items: o.children,
maxWidth: o.maxWidth,
minWidth: o.minWidth,
maxHeight: o.maxHeight,
adjustPosition: o.adjustSubMenuPosition !== void 0 ? o.adjustSubMenuPosition : s(g).adjustPosition,
direction: o.direction !== void 0 ? o.direction : s(g).direction
}, null, 8, ["show", "parentMenuItemContext", "items", "maxWidth", "minWidth", "maxHeight", "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"])),
o.hidden !== !0 && (o.divided === "down" || o.divided === !0) ? (C(), F(he, { key: 3 })) : D("", !0)
], 64))), 128))
])
], 512)
]),
_: 3
}, 8, ["maxHeight"])
], 16)) : D("", !0)
]),
_: 3
}, 16)
], 8, ["to"])) : D("", !0);
};
}
});
const ot = /* @__PURE__ */ te({
__name: "ContextSubMenuWrapper",
props: {
/**
* Menu options
*/
options: {
type: Object,
default: null
},
/**
* Show menu?
*/
show: {
type: null,
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: l, emit: u }) {
const i = e, v = u, t = et(), W = H(), {
options: c,
show: g,
container: f
} = re(i);
pe(() => {
g.value && L();
}), ye(() => {
_();
}), He(g, (p) => {
p ? L() : (De(S), _());
});
const S = {
closeMenu: w,
isClosed: M,
getMenuRef: () => W.value,
getMenuDimensions: () => {
var p;
return ((p = W.value) == null ? void 0 : p.getMenuDimensions()) ?? { width: 0, height: 0 };
}
};
let R = !1;
function L() {
y(), vt(S);
}
function w(p) {
R = !0, v("close", p), c.value.menuTransitionProps || v("closeAnimFinished"), De(S);
}
function M() {
return R;
}
function y() {
setTimeout(() => {
document.addEventListener("click", d, !0), document.addEventListener("contextmenu", d, !0), document.addEventListener("scroll", m, !0), !i.isFullScreenContainer && f.value && f.value.addEventListener("scroll", m, !0), c.value.keyboardControl !== !1 && document.addEventListener("keydown", h, !0);
}, 50);
}
function _() {
document.removeEventListener("contextmenu", d, !0), document.removeEventListener("click", d, !0), document.removeEventListener("scroll", m, !0), !i.isFullScreenContainer && f.value && f.value.removeEventListener("scroll", m, !0), c.value.keyboardControl !== !1 && document.removeEventListener("keydown", h, !0);
}
const r = H();
q("globalSetCurrentSubMenu", (p) => r.value = p), q("globalGetMenuHostId", f.value.id);
function h(p) {
var O, K, j, G, T, k, A, X, Y, Z, ae, I, ne;
let b = !0;
switch (p.key) {
case "Escape": {
((O = r.value) == null ? void 0 : O.isTopLevel()) === !1 ? (K = r.value) == null || K.closeCurrentSubMenu() : w();
break;
}
case "ArrowDown":
(j = r.value) == null || j.moveCurrentItemDown();
break;
case "ArrowUp":
(G = r.value) == null || G.moveCurrentItemUp();
break;
case "Home":
(T = r.value) == null || T.moveCurrentItemFirst();
break;
case "End":
(k = r.value) == null || k.moveCurrentItemLast();
break;
case "ArrowLeft": {
(A = r.value) != null && A.closeSelfAndActiveParent() || (Y = (X = c.value).onKeyFocusMoveLeft) == null || Y.call(X);
break;
}
case "ArrowRight":
(Z = r.value) != null && Z.openCurrentItemSubMenu() || (I = (ae = c.value).onKeyFocusMoveRight) == null || I.call(ae);
break;
case "Enter":
(ne = r.value) == null || ne.triggerCurrentItemClick(p);
break;
default:
b = !1;
break;
}
b && r.value && (p.stopPropagation(), p.preventDefault());
}
function m(p) {
c.value.closeWhenScroll !== !1 && B(p.target, null);
}
function d(p) {
B(p.target, p);
}
function B(p, b) {
var O, K;
for (; p; ) {
if (p.classList && p.classList.contains("mx-context-menu"))
return;
p = p.parentNode;
}
b ? c.value.clickCloseOnOutside !== !1 ? (_(), w()) : (K = (O = c.value).onClickOnOutside) == null || K.call(O, b) : (_(), w());
}
return q("globalOptions", c), q("globalCloseMenu", w), q("globalIsFullScreenContainer", i.isFullScreenContainer), q("globalHasSlot", (p) => t[p] !== void 0), q("globalRenderSlot", (p, b) => z(t, p, { ...b }, () => [Ce("span", "Render slot failed")], !1)), q("menuContext", {
zIndex: c.value.zIndex || ee.defaultZindex,
container: f.value,
adjustPadding: { x: 0, y: 0 },
getZoom: () => c.value.zoom || ee.defaultZoom,
getParentWidth: () => 0,
getParentHeight: () => 0,
getPositon: () => [c.value.x, c.value.y],
closeOtherSubMenuWithTimeOut: () => {
},
checkCloseOtherSubMenuTimeOut: () => !1,
addOpenedSubMenu: () => {
},
closeOtherSubMenu: () => {
},
getParentContext: () => null,
getSubMenuInstanceContext: () => null,
getElement: () => null,
addChildMenuItem: () => {
},
removeChildMenuItem: () => {
},
markActiveMenuItem: () => {
},
markThisOpenedByKeyBoard: () => {
},
isOpenedByKeyBoardFlag: () => !1,
isMenuItemDataCollectedFlag: () => !1
}), l(S), (p, b) => (C(), F(Be, {
ref_key: "submenuInstance",
ref: W,
show: s(g),
items: s(c).items,
adjustPosition: s(c).adjustPosition,
maxWidth: s(c).maxWidth || s(ee).defaultMaxWidth,
minWidth: s(c).minWidth || s(ee).defaultMinWidth,
maxHeight: s(c).maxHeight,
direction: s(c).direction || s(ee).defaultDirection,
onCloseAnimFinished: b[0] || (b[0] = (O) => v("closeAnimFinished"))
}, {
default: ve(() => [
z(p.$slots, "default")
]),
_: 3
}, 8, ["show", "items", "adjustPosition", "maxWidth", "minWidth", "maxHeight", "direction"]));
}
});
const Kt = /* @__PURE__ */ te({
__name: "ContextMenu",
props: {
/**
* Menu options
*/
options: {
type: Object,
default: null
},
/**
* Show menu?
*/
show: {
type: Boolean,
default: !1
}
},
emits: ["update:show", "close"],
setup(e, { expose: l, emit: u }) {
const i = u, v = e, { options: t, show: W } = re(v), { isNew: c, container: g, eleId: f } = nt(t.value), S = H(null), R = et();
function L(w) {
var M, y;
i("update:show", !1), i("close"), (y = (M = t.value).onClose) == null || y.call(M, w);
}
return l({
closeMenu: () => i("update:show", !1),
isClosed: () => !W.value,
getMenuRef: () => {
var w;
return (w = S.value) == null ? void 0 : w.getMenuRef();
},
getMenuDimensions: () => {
var w;
return ((w = S.value) == null ? void 0 : w.getMenuDimensions()) ?? { width: 0, height: 0 };
}
}), (w, M) => (C(), F(Qe, {
to: `#${s(f)}`
}, [
ie(ot, {
ref_key: "menuRef",
ref: S,
options: s(t),
show: s(W),
container: s(g),
isFullScreenContainer: !s(c),
onClose: L
}, qe({ _: 2 }, [
Re(s(R), (y, _) => ({
name: _,
fn: ve((r) => [
z(w.$slots, _, ct(dt(r)))
])
}))
]), 1032, ["options", "show", "container", "isFullScreenContainer"])
], 8, ["to"]));
}
}), Yt = te({
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 height of submenu
*/
maxHeight: {
type: [String, Number],
default: 0
},
/**
* Max width of submenu
*/
maxWidth: {
type: [String, Number],
default: 0
},
/**
* Min width of submenu
*/
minWidth: {
type: [String, Number],
default: 0
}
},
setup(e, l) {
const u = N("globalOptions"), { adjustSubMenuPosition: i, maxWidth: v, minWidth: t, maxHeight: W } = re(e), c = typeof i.value < "u" ? i.value : u.value.adjustPosition, g = H(), f = H();
return l.expose({
getSubMenuRef: () => g.value,
getMenuItemRef: () => f.value
}), () => Ce(Oe, {
...e,
ref: f,
showRightArrow: !0,
maxWidth: void 0,
minWidth: void 0,
maxHeight: void 0,
adjustSubMenuPosition: void 0,
hasChildren: typeof l.slots.default !== void 0
}, l.slots.default ? {
//Create SubMenu
submenu: (S) => Ce(Be, {
ref: g,
show: S.show,
maxWidth: v.value,
minWidth: t.value,
maxHeight: W.value,
adjustPosition: c,
parentMenuItemContext: S.context
}, {
default: l.slots.default
}),
//Add other slots
...gt(l.slots, "default")
} : l.slots);
}
});
function Vt(e, l, u, i) {
const v = H(!0), t = Ce(ot, {
options: e,
show: v,
container: l,
isFullScreenContainer: !u,
onCloseAnimFinished: () => {
je(null, l);
},
onClose: (W) => {
var c;
(c = e.onClose) == null || c.call(e, W), v.value = !1;
}
}, i);
return je(t, l), t.component;
}
function Ue(e, l) {
const u = nt(e);
return Vt(e, u.container, u.isNew, l).exposed;
}
const Ge = {
/**
* For Vue install
* @param app
*/
install(e) {
e.config.globalProperties.$contextmenu = Ue, e.component("ContextMenu", Kt), e.component("ContextMenuItem", Oe), e.component("ContextMenuGroup", Yt), e.component("ContextMenuSperator", he), e.component("ContextMenuSeparator", he), e.component("ContextSubMenu", Be);
},
/**
* 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 cus