@byloth/vuert
Version:
The headless alerts, modals, notifications & popups library for Vue.js craftsmen. ℹ
183 lines (182 loc) • 6.66 kB
JavaScript
import { computed as e, createBlock as t, createCommentVNode as n, defineComponent as r, getCurrentScope as i, inject as a, markRaw as o, nextTick as s, onBeforeUnmount as c, onMounted as l, openBlock as u, ref as d, renderSlot as f, resolveDynamicComponent as p, shallowRef as m, withCtx as h } from "vue";
import { DeferredPromise as g, Publisher as _, RuntimeException as v, ValueException as y, delay as b } from "@byloth/core";
//#region src/core.ts
var x = { $vuert: Symbol("[vuert]: vuert") }, S = class extends v {
alert;
constructor(e, t, n, r = "AlertThrottledException") {
t === void 0 && (t = "The alert has been throttled to prevent spamming the user with too many alerts."), super(t, n, r), this.alert = e;
}
}, C = class e {
static VERSION = "1.4.5";
static get DEFAULT_OPTS() {
return {
useThrottling: !0,
throttlingDuration: 100,
transitionDuration: 200
};
}
_subscribers;
_throttlers;
_options;
get options() {
return { ...this._options };
}
_throttle;
constructor(t) {
this._subscribers = [], this._throttlers = /* @__PURE__ */ new Map(), this._options = {
...e.DEFAULT_OPTS,
...t
}, this._options.useThrottling ? this._throttle = (e) => {
if (!e.id) return !1;
let t = Date.now();
return t - (this._throttlers.get(e.id) ?? 0) > this._options.throttlingDuration ? (this._throttlers.set(e.id, t), !1) : !0;
} : this._throttle = () => !1;
}
emit(e) {
if (this._throttle(e)) throw new S(e);
let t = this._subscribers.slice().map((t) => t(e)).filter((e) => !!e);
if (!t.length) throw new v("Unable to handle the emitted alert properly. There wasn't found any supported subscribers.");
if (t.length > 1) throw new v("Unable to handle the emitted alert properly. There were found too many supported subscribers.");
return t[0];
}
subscribe(e) {
return this._subscribers.push(e), () => {
let t = this._subscribers.indexOf(e);
return this._subscribers.splice(t, 1)[0];
};
}
}, w = void 0, T = (e) => {
w = e;
}, E = () => i() ? a(x.$vuert) : w, D = (e) => o({ install: ({ config: t, provide: n }) => {
let r = new C(e);
T(r), t.globalProperties.$vuert = r, n(x.$vuert, r);
} }), O = () => {
let e = E();
if (!e) throw new v("`useVuert()` was called but there was not active Vuert. Did you forget to install `Vuert` plugin in your App?");
return e;
}, k = class {
id;
type;
icon;
label;
callback;
constructor(e) {
this.id = e.id ?? Symbol(), this.type = e.type ?? "secondary", this.icon = e.icon, this.label = e.label, this.callback = e.callback ?? (() => e.value);
}
}, A = class {
id;
type;
priority;
icon;
title;
subtitle;
message;
payload;
actions;
dismissible;
timeout;
constructor(e) {
if (this.id = e.id ?? Symbol(), this.type = e.type ?? "info", this.priority = e.priority ?? "normal", this.icon = e.icon, this.title = e.title, this.subtitle = e.subtitle, e.message !== void 0 && e.component !== void 0) throw new y("The `message` and `component` properties cannot both be valued at the same time.");
if (this.message = e.message, this.payload = e.payload, this.actions = e.actions?.map((e) => new k(e)) ?? [], this.dismissible = e.dismissible || !1, e.timeout !== void 0) {
if (e.timeout <= 0) throw new y("The `timeout` property must be a positive integer or -at least- `undefined`.");
this.timeout = e.timeout;
} else this.timeout = 0;
}
}, j = class extends g {
_duration;
_timeoutId;
_publisher;
_isOpen;
alert;
isOpen;
component;
constructor(t, n) {
let r = async () => {
if (!this._isOpen.value) throw Error("Unable to close the alert. It has already been closed or not even opened yet.");
this._isOpen.value = !1, this._timeoutId !== void 0 && (clearTimeout(this._timeoutId), this._timeoutId = void 0), this._publisher.publish("closing"), await b(this._duration.leave), this._publisher.publish("closed");
};
if (super((e) => (r(), e instanceof k ? e.callback() : e instanceof Function ? e() : e), (e) => {
throw r(), e;
}), typeof n == "object") this._duration = {
enter: Number(n.enter),
leave: Number(n.leave)
};
else {
let e = Number(n);
this._duration = {
enter: e,
leave: e
};
}
this._publisher = new _(), this.alert = new A(t), this._isOpen = d(!1), this.isOpen = e(() => this._isOpen.value), this.component = t.component;
}
async open() {
if (this._isOpen.value) throw Error("Unable to open the alert. It has already been opened.");
this._isOpen.value = !0, this._publisher.publish("opening"), await b(this._duration.enter), this._publisher.publish("opened"), this.alert.timeout && (this._timeoutId = setTimeout(this.resolve, this.alert.timeout));
}
onOpening(e) {
this._publisher.subscribe("opening", e);
}
onOpened(e) {
this._publisher.subscribe("opened", e);
}
onClosing(e) {
this._publisher.subscribe("closing", e);
}
onClosed(e) {
this._publisher.subscribe("closed", e);
}
}, M = /* @__PURE__ */ r({
__name: "AlertHandler",
props: {
is: {
default: "div",
type: [String, Object]
},
filter: {
default: () => !0,
type: Function
},
transitionDuration: {
default: () => O().options.transitionDuration,
type: [Number, Object],
validator: (e) => e instanceof Object ? "enter" in e && "leave" in e ? isFinite(Number(e.enter)) && isFinite(Number(e.leave)) : !1 : isFinite(Number(e))
}
},
emits: {
opening: (e) => e instanceof A,
opened: (e) => e instanceof A,
closing: (e) => e instanceof A,
closed: (e) => e instanceof A
},
setup(e, { emit: r }) {
let i = e, a = r, o = [], g = d(0), _ = m(), v = async () => {
let e = o[0];
e.onOpening(() => a("opening", e.alert)), e.onOpened(() => a("opened", e.alert)), e.onClosing(() => a("closing", e.alert)), e.onClosed(async () => {
a("closed", e.alert), o.shift(), _.value = void 0, --g.value, await s(), o.length > 0 && v();
}), _.value = e, await e.open();
}, y = (e) => {
let t = new j(e, i.transitionDuration);
return o.push(t), o.length === 1 && v(), g.value += 1, t;
}, b;
return l(() => {
b = O().subscribe((e) => {
if (i.filter(e)) return y(e);
});
}), c(() => b()), (r, i) => (u(), t(p(e.is), null, {
default: h(() => [_.value ? f(r.$slots, "default", {
key: 0,
alert: _.value.alert,
customComponent: _.value.component,
isOpen: _.value.isOpen.value,
queue: g.value,
resolve: _.value.resolve,
reject: _.value.reject
}) : n("", !0)]),
_: 3
}));
}
}), N = C;
//#endregion
export { k as Action, A as Alert, M as AlertHandler, S as AlertThrottledException, j as Context, D as createVuert, N as default, O as useVuert };
//# sourceMappingURL=vuert.esm.js.map