UNPKG

@farris/ui-vue

Version:

Farris Vue, a Farris Design based Vue3 component library.

777 lines (776 loc) 22.7 kB
var we = Object.defineProperty; var xe = (r, s, i) => s in r ? we(r, s, { enumerable: !0, configurable: !0, writable: !0, value: i }) : r[s] = i; var h = (r, s, i) => xe(r, typeof s != "symbol" ? s + "" : s, i); import { getCustomClass as H, isMobilePhone as ve, useGuid as Ce, withInstall as ye } from "../common/index.esm.js"; import { computed as K, createVNode as u, Teleport as ke, getCurrentInstance as De, reactive as te, withDirectives as oe, resolveDirective as se, ref as $, defineComponent as Te, watch as ae, onMounted as Se, onUnmounted as Re, nextTick as ue } from "vue"; import { LocaleService as Oe } from "../locale/index.esm.js"; const Me = { id: { type: String, default: "" }, /** 组件自定义样式 */ customClass: { type: String, default: "" }, alignment: { Type: String, default: "right" }, items: { Type: Array, default: [] }, /** 控制是否可见 */ visible: { type: Boolean, default: !0 }, /** 更多按钮的样式 */ moreButtonClass: { type: String, default: "btn-secondary" } }; class fe { constructor(s) { /** 工具栏项标识 */ h(this, "id", ""); /** 工具栏项自定义样式 */ h(this, "class", "btn-secondary"); /** 图标 */ h(this, "icon", ""); /** 所属分组 */ h(this, "groupId", ""); /** 收藏顺序 */ h(this, "order", -1); /** 是否作为下拉菜单的顶层按钮 */ h(this, "asDropDownTop", !1); /** 文本 */ h(this, "text", ""); /** 是否可见原值 */ h(this, "visible", !0); /** 控制界面是否显示 */ h(this, "shown", !0); h(this, "responsed", !1); /** 是否启用提示消息 */ h(this, "tipsEnable", !1); /** 提示消息内容 */ h(this, "tipsText", ""); /** 记录宽度 */ h(this, "width", 0); /** 下拉时的样式名 */ h(this, "dropdownClass", ""); h(this, "onClick", () => { }); h(this, "options"); var g, p; this.options = s; const i = [ "id", "class", "icon", "groupId", "order", "asDropDownTop", "text", "isDP", "visible", "responsed", "width", "tipsEnable", "tipsText", "dropdownClass", "onClick" ]; Object.keys(s).filter((w) => i.indexOf(w) > -1).forEach((w) => { w === "visible" ? this[w] = s[w] === void 0 ? !0 : s[w] : this[w] = s[w]; }), this.shown = this.visible, (g = s.appearance) != null && g.class && (this.class = (p = s.appearance) == null ? void 0 : p.class); } /** 是否可用 */ get enable() { return Object.keys(this.options).indexOf("disabled") > -1 ? !this.options.disabled : K(() => !0); } /** 设置宽度 */ setWidth(s) { this.width = parseInt(s, 10); } /** 获取宽度 */ getWidth() { return this.visible ? this.width : !1; } } class Be extends fe { constructor(s) { super(s); } } function he() { function r(s) { const i = []; return s.reduce((g, p) => (p.children && p.children.length > 0 ? g.push(new ne(p)) : g.push(new Be(p)), g), i), i; } return { buildResponseToolbarItems: r }; } const { buildResponseToolbarItems: Ee } = he(); class ne extends fe { constructor(i) { super(i); h(this, "placement", ""); // 下拉位置 /** 是否可见 */ h(this, "visible", !0); /** 下拉class */ h(this, "dropdownClass", ""); /** 下拉菜单class */ h(this, "menuClass", ""); /** 是否用分开的下拉按钮 */ h(this, "split", !1); h(this, "children", []); h(this, "expanded", !1); h(this, "shown", !0); const g = ["visible", "icon", "isDP", "class", "dropdownClass", "menuClass", "placement", "split", "expanded"]; Object.keys(i).filter((p) => g.indexOf(p) > -1).forEach((p) => { p === "visible" ? this[p] = i[p] === void 0 ? !0 : i[p] : this[p] = i[p]; }), this.shown = this.visible, i.children && i.children.length && (this.children = Ee(i.children)); } } function je() { function r(i) { const g = { "f-icon": !0 }; if (i.icon) { const p = i.icon.trim().split(" "); p && p.length && p.reduce((w, R) => (w[R] = !0, w), g); } return g; } function s(i) { return !!(i.icon && i.icon.trim()); } return { iconClass: r, shouldShowIcon: s }; } function Pe(r, s) { function i(t) { return H({ "dropdown-menu": !0 }, t.menuClass); } function g(t) { const e = { disabled: !t.enable, "dropdown-submenu": !0, "f-rt-dropdown": !0, "text-truncate": !0 }; return H(e, t.dropdownClass); } function p(t) { const e = { disabled: !t.enable, "dropdown-item": !0, "f-rt-btn": !0, "text-truncate": !0 }; return H(e, t.dropdownClass); } function w(t) { const e = { disabled: !t.enable, "dropdown-item": !0, "f-rt-btn": !0, "text-truncate": !0, "f-rt-toggle": !t.split, "btn-icontext": !!(t.icon && t.icon.trim()), "dropdown-toggle": !t.split, "btn-secondary": !0 }; return H(e, t.dropdownClass); } function R(t) { const e = { "f-rt-btn": !0, "text-truncate": !0, "btn-icontext": !!(t.icon && t.icon.trim()), "d-block": !0 }; return H(e, t.dropdownClass); } let M; const x = /* @__PURE__ */ new Map(); let _ = ""; function k(t) { _ = "#" + t; } function B(t, e, a) { t.stopPropagation(); const l = a ? a.id : "__top_item__"; if (e.children && e.children.length && (e.expanded = !e.expanded, e.expanded)) { if (x.has(l) && x.get(l) !== e) { const b = x.get(l); b && (b.expanded = !1, x.delete(l)); } x.set(l, e); } } function z(t, e) { document.body.click(), e.enable && (t.stopPropagation(), typeof e.onClick == "function" && e.onClick(t, e.id), r.emit("click", t, e.id)); } function V(t, e) { const a = t.target; a.className.indexOf("dropdown-item") < 0 || (a.title = a.scrollWidth > a.clientWidth ? e : ""); } function U(t, e, a) { if (e.tipsEnable && e.tipsText) { const l = te({ content: e.tipsText, enable: e.tipsEnable }); return oe(u("li", { class: p(e), id: e.id, key: e.id + "_" + a, onClick: (b) => z(b, e) }, [u("span", { class: R(e) }, [s.shouldShowIcon(e) && u("i", { class: s.iconClass(e) }, null), e.text])]), [[se("tooltip"), l]]); } return u("li", { class: p(e), id: e.id, onClick: (l) => z(l, e) }, [u("span", { class: R(e), onMouseenter: (l) => V(l, e.text) }, [s.shouldShowIcon(e) && u("i", { class: s.iconClass(e) }, null), e.text])]); } function o(t, e, a) { e.enable && (typeof e.onClick == "function" && a.onClick(t, e.id), r.emit("click", t, e.id), a.split || B(t, e, a)); } function f(t, e, a) { t && t.stopPropagation(), B(t, e, a); } function D(t, e) { t.stopPropagation(); const a = e.id; if (e.children && e.children.length && e.expanded && x.has(a)) { const l = x.get(a); l && (l.expanded = !1, x.delete(a)); } } function W(t, e, a) { if (e.tipsEnable && e.tipsText) { const l = te({ content: e.tipsText, enable: e.tipsEnable }); return u("li", { class: g(e), id: e.id, key: e.id + "_" + a, onClick: (b) => o(b, e, t) }, [oe(u("span", { class: w(e) }, [s.shouldShowIcon(e) && u("i", { class: s.iconClass(e) }, null), e.text]), [[se("tooltip"), l]]), e.split && u("span", { class: "dropdown-toggle dropdown-toggle-split f-rt-toggle", onClick: (b) => f(b, e, t) }, null), M(e, e)]); } return u("li", { class: g(e), id: e.id, onClick: (l) => o(l, e, t), onMouseenter: (l) => V(l, e.text) }, [u("span", { class: w(e) }, [s.shouldShowIcon(e) && u("i", { class: s.iconClass(e) }, null), e.text]), e.split && u("span", { class: "dropdown-toggle dropdown-toggle-split f-rt-toggle", onClick: (l) => f(l, e, t) }, null), M(e, e)]); } function le(t) { return t.children.filter((e) => e.visible).map((e, a) => e.children && e.children.length ? W(t, e, a) : U(t, e, a)); } function re(t, e) { if (!t) return 1080; let a = -1; const l = t.children.length; for (let b = 0; b < l; b++) if (t.children[b].id === e) { a = b; break; } return 1080 + l - a; } function ie(t, e, a) { var q, Y; const l = { display: t.expanded ? "block" : "none", position: "fixed", maxWidth: "300px", minWidth: "120px", zIndex: 1080, // 先用固定zIndex,后期会统一调用动态的zIndex overflowY: "auto", maxHeight: "none" }, b = document.querySelector(_); if (!b) return l; const N = De(); let T = (Y = (q = N == null ? void 0 : N.exposed) == null ? void 0 : q.elementRef.value) == null ? void 0 : Y.querySelector("#" + e); if (T || (T = b == null ? void 0 : b.querySelector("#" + e)), !T) return l; const ce = window.innerWidth, A = window.innerHeight, y = T == null ? void 0 : T.getClientRects(); if (T && y && y.length) { const { top: E, width: Z, left: F, right: m, height: L } = y[0], I = Math.ceil(L + E); l.top = `${I}px`, l.left = `${F}px`; const J = t.id + "_menu", P = (T == null ? void 0 : T.querySelector("#" + J)) || (b == null ? void 0 : b.querySelector("#" + J)) || null; if (P) { P.style.visibility = "hidden", P.style.display = "block"; const G = P.offsetHeight; l.zIndex = t.expanded ? re(b, J) : 1080, l.display === "block" && (P.style.display = "block"); const n = P.getBoundingClientRect(); if (a) { l.top = `${E - 6}px`; const d = Math.ceil(Z + F); l.left = `${d}px`; } ce - F - Z < n.width && (l.left = `${(a ? F : m) - n.width}px`), A - E - L < G ? E > G ? (l.top = `${E - G - 6}px`, l.maxHeight = `${E - 16}px`) : (l.maxHeight = `${A - I}px`, E > A - E - L && (l.top = "10px", l.maxHeight = `${E - 16}px`)) : l.maxHeight = `${A - I}px`, P.style.visibility = "visible"; } } return l; } M = function(t, e) { const a = e ? e.id : t.id; return u(ke, { to: _, disabled: !t.expanded }, { default: () => [u("ul", { class: i(t), style: ie(t, a, e), id: t.id + "_menu", onWheel: (l) => D(l, t) }, [le(t)])] }); }; function de() { x.forEach((t) => { t.expanded = !1; }), x.clear(); } return { renderDropdownMenu: M, clearAllDropDownMenu: de, updateContainerId: k }; } function He(r, s, i) { const g = $(r.alignment), { renderDropdownMenu: p, clearAllDropDownMenu: w, updateContainerId: R } = Pe(s, i); function M(o) { const f = { "btn-group": !0, "f-rt-dropdown": !0, "f-btn-ml": g.value === "right" || g.value === "center", "f-btn-mr": g.value === "left" }; return H(f, o.dropdownClass); } function x(o) { const f = { btn: !0, disabled: !o.enable, "f-rt-btn": !0, "btn-icontext": !!(o.icon && o.icon.trim()) && o.text, // 图标+文本 "f-rt-toggle": !o.split, "dropdown-toggle": !o.split, "f-btn-icon": o.icon && !o.text // 只有图标 }; return H(f, o.class); } function _(o) { return H({ btn: !0, "dropdown-toggle": !0, "dropdown-toggle-split": !0 }, o.class); } function k(o) { return o.split ? { display: "flex" } : { display: "flex", "border-radius": "6px" }; } function B(o, f) { o.stopPropagation(); const D = f.expanded; document.body.click(), typeof f.onClick == "function" && f.onClick(o, f.id), s.emit("click", o, f.id), f.split || (f.expanded = !D); } function z(o, f) { o.stopPropagation(); const D = f.expanded; document.body.click(), f.expanded = !D; } function V(o, f = !0) { if (o.tipsEnable && o.tipsText) { const D = te({ content: o.tipsText, enable: o.tipsEnable }); return u("div", { id: o.id, class: M(o) }, [oe(u("div", { class: x(o), style: k(o), onClick: (W) => o.enable && B(W, o) }, [i.shouldShowIcon(o) && u("i", { class: i.iconClass(o) }, null), o.text && u("span", null, [o.text])]), [[se("tooltip"), D]]), o.split && u("button", { class: _(o), style: "border-radius:0 6px 6px 0;", onClick: (W) => z(W, o) }, null), f && p(o)]); } return u("div", { id: o.id, class: M(o) }, [u("div", { class: x(o), style: k(o), onClick: (D) => o.enable && B(D, o) }, [i.shouldShowIcon(o) && u("i", { class: i.iconClass(o) }, null), o.text && u("span", null, [o.text])]), o.split && u("button", { class: _(o), style: "border-radius:0 6px 6px 0;", onClick: (D) => z(D, o) }, null), f && p(o)]); } function U() { w(); } return { renderToolbarDropdown: V, clearAllDropDown: U, updateContainerId: R }; } const { buildResponseToolbarItems: pe } = he(), _e = /* @__PURE__ */ Te({ name: "FResponseToolbar", props: Me, emits: ["click"], setup(r, s) { const i = $(pe(r.items)), g = $(), p = $(), w = $(), R = ve(), M = { id: "__more_buttons__", text: R ? "" : Oe.getLocaleValue("responseToolbar.more"), icon: R ? "f-icon-more-horizontal" : "", class: r.moreButtonClass }, x = $(new ne(M)), _ = Object.assign({}, M, { id: M.id + "width" }), k = $(-1), B = je(); let z = !1; const { guid: V, uuid: U } = Ce(), o = r.id ? r.id : "toolbar-" + V(), f = $(), D = "dropdown-container-" + o + "-" + U(4), { renderToolbarDropdown: W, clearAllDropDown: le, updateContainerId: re } = He(r, s, B); re(D); const ie = K(() => x.value.children.length > 0), de = K(() => r.visible ? x.value.children.length > 0 ? !0 : i.value.findIndex((n) => n.shown && n.children && n.children.length > 0) > -1 : !1); function t() { r.visible && !f.value && document && (f.value = document.createElement("div"), f.value.id = D, document.body.appendChild(f.value)); } t(); function e() { f.value && document.body.contains(f.value) && (document.body.removeChild(f.value), f.value = null); } ae(de, (n, d) => { n ? t() : e(); }); const a = K(() => { const n = { "f-toolbar": !0, "f-response-toolbar": !0, "position-relative": !0, "f-response-toolbar-sm": r.buttonSize === "sm", "f-response-toolbar-lg": r.buttonSize === "lg", "d-none": !r.visible }; return H(n, r.customClass); }), l = K(() => ({ "w-100": !0, "d-flex": !0, "flex-nowrap": !0, "justify-content-end": r.alignment === "right", "justify-content-start": r.alignment === "left", "justify-content-center": r.alignment === "center" })); function b(n) { n && n.target.closest("ul.dropdown-menu") && n.type === "wheel" || (i.value.filter((d) => d.children && d.children.length > 0).forEach((d) => { d.expanded = !1; }), x.value.expanded = !1, le()); } function N(n, d) { var v; const c = { btn: !0, "f-rt-btn": !0, "f-btn-ml": r.alignment === "right" || d > 0 && r.alignment === "center", "f-btn-mr": r.alignment === "left", "btn-icontext": !!(n.icon && n.icon.trim()) }, C = ((v = n == null ? void 0 : n.appearance) == null ? void 0 : v.class) || n.class || "btn-secondary"; return H(c, C); } function T(n, d) { document.body.click(), typeof d.onClick == "function" && d.onClick(n, d.id), s.emit("click", n, d.id); } function ce(n, d) { if (n.tipsEnable && n.tipsText) { const c = te({ content: n.tipsText, enable: n.tipsEnable }); return oe(u("button", { type: "button", class: N(n, d), id: n.id, disabled: !n.enable, onClick: (C) => T(C, n) }, [B.shouldShowIcon(n) && u("i", { class: B.iconClass(n) }, null), n.text]), [[se("tooltip"), c]]); } return u("button", { type: "button", class: N(n, d), id: n.id, disabled: !n.enable, onClick: (c) => T(c, n) }, [B.shouldShowIcon(n) && u("i", { class: B.iconClass(n) }, null), n.text]); } let A = /* @__PURE__ */ new Map(), y = []; function q(n) { const d = Math.ceil(parseFloat(getComputedStyle(n).getPropertyValue("margin-left"))), c = Math.ceil(parseFloat(getComputedStyle(n).getPropertyValue("margin-right"))); return (d || 0) + n.getBoundingClientRect().width + (c || 0); } function Y(n, d, c) { if (d < k.value) { let C = c - 1; for (; C >= 0; C--) { const v = n[C], S = q(v); if (d += S, d >= k.value) break; } c = C; } return d -= k.value, c = c < 0 ? 0 : c, { start: c, space: d }; } function E(n) { const d = w.value; let c = n; const v = Array.from(d.children).filter((j) => j.id !== "__more_buttons__"), S = v.length; let ee = !1, O = 0; for (; O < S; O++) { const j = v[O], Q = q(j); if (Q <= c) c -= Q; else { const X = Y(v, c, O); O = X.start, c = X.space, ee = !0; break; } } if (!ee && y.length > 0) { const j = Y(v, c, O); O = j.start, c = j.space; } const be = y.length > 0; for (; O < S; O++) { const j = v[O], Q = q(j); A.set(j.id, !0); const X = y.find((ge) => ge.id === j.id); X ? X.width = Q : y[be ? "unshift" : "push"]({ id: j.id, width: Q }); } return c; } function Z() { A = /* @__PURE__ */ new Map(), y = []; } function F(n = !1) { const d = Object.assign({}, x.value); d.children = []; const c = new ne(d), C = i.value.reduce((v, S) => { const ee = r.items.find((O) => O.id === S.id); return n ? (c.children.push(S), S.shown = !1, v) : (A.has(S.id) ? (c.children.push(S), S.shown = !1) : S.shown = ee.visible !== !1, v.push(S), v); }, []); x.value = c, i.value = C; } function m(n) { if (R) { Z(), F(!0); return; } let d = E(n); if (y.length) { let c = 0; for (; c < y.length; c++) { const C = r.items.find((v) => v.id === y[c].id); if (C && C.visible !== !1) { const v = y[c].width; if (d >= v || c === y.length - 1 && d + k.value >= v) d -= v; else break; } } for (let C = 0; C < c; C++) A.delete(y[C].id); y.splice(0, c - 1); } F(); } function L() { if (g.value && k.value < 0) { const n = g.value.querySelector("#" + _.id); if (!n) return; n.getBoundingClientRect().width === 0 ? k.value = -1 : k.value = q(n); } } function I(n = -1) { if (n === 0 || !w.value || !p.value) return; n < 0 && (n = p.value.getBoundingClientRect().width), n = Math.ceil(n); const d = Math.ceil(w.value.getBoundingClientRect().width); if (R) { m(n), b(); return; } (n >= 0 && n < d || y.length) && (L(), m(n), b()); } function J() { return k.value < 0 && u("div", { style: "position:fixed;top:-40px;visibility:hidden;" }, [W(new ne(_))]); } const P = new ResizeObserver((n) => { if (n.length) { if (R) return; const c = n[0].contentRect.width; I(c); } }); function G() { p.value && !z && (P.observe(p.value), z = !0); } return Se(() => { L(), I(), G(), document.body.addEventListener("click", b), document.body.addEventListener("wheel", b); }), Re(() => { P.disconnect(), e(), document.body.removeEventListener("click", b), document.body.removeEventListener("wheel", b); }), ae(() => r.items, () => { i.value = pe(r.items), x.value.children = [], ue(() => { L(), Z(), I(); }); }, { deep: !0 }), ae(() => r.visible, (n) => { n && ue(() => { L(), I(), G(); }); }), s.expose({ elementRef: g }), () => u("div", { class: a.value, id: o, ref: g, style: { "overflow-y": k.value < 0 ? "hidden" : "initial" } }, [r.visible && J(), r.visible && u("div", { ref: p, class: l.value }, [u("div", { ref: w, class: "d-inline-block f-response-content", style: "white-space: nowrap;" }, [i.value.filter((n) => n.shown).map((n, d) => n.children && n.children.length > 0 ? W(n) : ce(n, d)), ie.value && W(x.value)])])]); } }); class Ie { constructor(s, i) { h(this, "id"); h(this, "name"); /** 记录元素的Id */ h(this, "presetId", []); /** 记录转变为下拉时元素的位置 */ h(this, "responsedIndex", []); /** 记录宽度 */ h(this, "width"); this.id = s, this.name = i, this.width = 0; } // 设置宽度 setWidth(s) { this.width = parseInt(s + "", 10); } // 获取宽度 getWidth() { return this.width; } // 更新presetIndex setPreset(s) { Array.isArray(s) ? this.presetId = this.presetId.concat(s) : this.presetId.push(s); } // 清除preset delPreset() { this.presetId = []; } // 删除 removeResponsed(s) { this.responsedIndex.splice(s, 1); } // 更新responsedIndex setResponsed(s) { Array.isArray(s) ? this.responsedIndex = this.responsedIndex.concat(s) : this.responsedIndex.push(s); } // 是否已经开始处理响应式 isResponsing() { return this.responsedIndex.length > 0; } // 是否已经处理完响应式 isResponsed() { return this.presetId.length === this.responsedIndex.length; } } const $e = ye(_e); export { _e as FResponseToolbar, ne as ResponseToolbarDropDownItem, Ie as ResponseToolbarGroup, Be as ResponseToolbarItem, $e as default, Me as responseToolbarProps };