UNPKG

@byloth/vuert

Version:

The headless alerts, modals, notifications & popups library for Vue.js craftsmen. ℹ

2 lines 5.84 kB
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("vue"),require("@byloth/core")):typeof define==`function`&&define.amd?define([`exports`,`vue`,`@byloth/core`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.Vuert={},e.Vue,e.Core))})(this,function(e,t,n){Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});var r={$vuert:Symbol(`[vuert]: vuert`)},i=class extends n.RuntimeException{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}},a=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=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 i(e);let t=this._subscribers.slice().map(t=>t(e)).filter(e=>!!e);if(!t.length)throw new n.RuntimeException(`Unable to handle the emitted alert properly. There wasn't found any supported subscribers.`);if(t.length>1)throw new n.RuntimeException(`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]}}},o=void 0,s=e=>{o=e},c=()=>(0,t.getCurrentScope)()?(0,t.inject)(r.$vuert):o,l=e=>(0,t.markRaw)({install:({config:t,provide:n})=>{let i=new a(e);s(i),t.globalProperties.$vuert=i,n(r.$vuert,i)}}),u=()=>{let e=c();if(!e)throw new n.RuntimeException("`useVuert()` was called but there was not active Vuert. Did you forget to install `Vuert` plugin in your App?");return e},d=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)}},f=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 n.ValueException("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 d(e))??[],this.dismissible=e.dismissible||!1,e.timeout!==void 0){if(e.timeout<=0)throw new n.ValueException("The `timeout` property must be a positive integer or -at least- `undefined`.");this.timeout=e.timeout}else this.timeout=0}},p=class extends n.DeferredPromise{_duration;_timeoutId;_publisher;_isOpen;alert;isOpen;component;constructor(e,r){let i=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(0,n.delay)(this._duration.leave),this._publisher.publish(`closed`)};if(super(e=>(i(),e instanceof d?e.callback():e instanceof Function?e():e),e=>{throw i(),e}),typeof r==`object`)this._duration={enter:Number(r.enter),leave:Number(r.leave)};else{let e=Number(r);this._duration={enter:e,leave:e}}this._publisher=new n.Publisher,this.alert=new f(e),this._isOpen=(0,t.ref)(!1),this.isOpen=(0,t.computed)(()=>this._isOpen.value),this.component=e.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(0,n.delay)(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=(0,t.defineComponent)({__name:`AlertHandler`,props:{is:{default:`div`,type:[String,Object]},filter:{default:()=>!0,type:Function},transitionDuration:{default:()=>u().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 f,opened:e=>e instanceof f,closing:e=>e instanceof f,closed:e=>e instanceof f},setup(e,{emit:n}){let r=e,i=n,a=[],o=(0,t.ref)(0),s=(0,t.shallowRef)(),c=async()=>{let e=a[0];e.onOpening(()=>i(`opening`,e.alert)),e.onOpened(()=>i(`opened`,e.alert)),e.onClosing(()=>i(`closing`,e.alert)),e.onClosed(async()=>{i(`closed`,e.alert),a.shift(),s.value=void 0,--o.value,await(0,t.nextTick)(),a.length>0&&c()}),s.value=e,await e.open()},l=e=>{let t=new p(e,r.transitionDuration);return a.push(t),a.length===1&&c(),o.value+=1,t},d;return(0,t.onMounted)(()=>{d=u().subscribe(e=>{if(r.filter(e))return l(e)})}),(0,t.onBeforeUnmount)(()=>d()),(n,r)=>((0,t.openBlock)(),(0,t.createBlock)((0,t.resolveDynamicComponent)(e.is),null,{default:(0,t.withCtx)(()=>[s.value?(0,t.renderSlot)(n.$slots,`default`,{key:0,alert:s.value.alert,customComponent:s.value.component,isOpen:s.value.isOpen.value,queue:o.value,resolve:s.value.resolve,reject:s.value.reject}):(0,t.createCommentVNode)(``,!0)]),_:3}))}}),h=a;e.Action=d,e.Alert=f,e.AlertHandler=m,e.AlertThrottledException=i,e.Context=p,e.createVuert=l,e.default=h,e.useVuert=u}); //# sourceMappingURL=vuert.umd.cjs.map