UNPKG

@byloth/vuert

Version:

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

3 lines (2 loc) 6.24 kB
(function(o,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("vue"),require("@byloth/core")):typeof define=="function"&&define.amd?define(["exports","vue","@byloth/core"],i):(o=typeof globalThis<"u"?globalThis:o||self,i(o.Vuert={},o.Vue,o.Core))})(this,function(o,i,a){"use strict";var j=Object.defineProperty;var D=(o,i,a)=>i in o?j(o,i,{enumerable:!0,configurable:!0,writable:!0,value:a}):o[i]=a;var t=(o,i,a)=>D(o,typeof i!="symbol"?i+"":i,a);const w={$vuert:Symbol("[vuert]: vuert")};class g extends a.RuntimeException{constructor(s,r,u,h="AlertThrottledException"){r===void 0&&(r="The alert has been throttled to prevent spamming the user with too many alerts.");super(r,u,h);t(this,"alert");this.alert=s}}class m{constructor(e){t(this,"id");t(this,"type");t(this,"icon");t(this,"label");t(this,"callback");this.id=e.id??Symbol(),this.type=e.type??"secondary",this.icon=e.icon,this.label=e.label,this.callback=e.callback??(()=>e.value)}}class p{constructor(e){t(this,"id");t(this,"type");t(this,"priority");t(this,"icon");t(this,"title");t(this,"message");t(this,"payload");t(this,"actions");t(this,"dismissible");t(this,"timeout");var s;if(this.id=e.id??Symbol(),this.type=e.type??"info",this.priority=e.priority??"normal",this.icon=e.icon,this.title=e.title,e.message!==void 0&&e.component!==void 0)throw new a.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=((s=e.actions)==null?void 0:s.map(r=>new m(r)))??[],this.dismissible=e.dismissible||!1,e.timeout!==void 0){if(e.timeout<=0)throw new a.ValueException("The `timeout` property must be a positive integer or -at least- `undefined`.");this.timeout=e.timeout}else this.timeout=0}}class v extends a.DeferredPromise{constructor(s,r){const u=async()=>{if(!this._isOpen.value)throw new 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 a.delay(this._duration.leave),this._publisher.publish("closed")};super(c=>(u(),c instanceof m?c.callback():c instanceof Function?c():c),c=>{throw u(),c});t(this,"_duration");t(this,"_timeoutId");t(this,"_publisher");t(this,"_isOpen");t(this,"alert");t(this,"isOpen");t(this,"component");if(typeof r=="object")this._duration={enter:Number(r.enter),leave:Number(r.leave)};else{const c=Number(r);this._duration={enter:c,leave:c}}this._publisher=new a.Publisher,this.alert=new p(s),this._isOpen=i.ref(!1),this.isOpen=i.computed(()=>this._isOpen.value),this.component=s.component}async open(){if(this._isOpen.value)throw new Error("Unable to open the alert. It has already been opened.");this._isOpen.value=!0,this._publisher.publish("opening"),await a.delay(this._duration.enter),this._publisher.publish("opened"),this.alert.timeout&&(this._timeoutId=setTimeout(this.resolve,this.alert.timeout))}onOpening(s){this._publisher.subscribe("opening",s)}onOpened(s){this._publisher.subscribe("opened",s)}onClosing(s){this._publisher.subscribe("closing",s)}onClosed(s){this._publisher.subscribe("closed",s)}}const f=class f{constructor(e){t(this,"_subscribers");t(this,"_throttlers");t(this,"_options");t(this,"_throttle");this._subscribers=[],this._throttlers=new Map,this._options={...f.DEFAULT_OPTS,...e},this._options.useThrottling?this._throttle=s=>{if(!s.id)return!1;const r=Date.now(),u=this._throttlers.get(s.id)??0;return r-u>this._options.throttlingDuration?(this._throttlers.set(s.id,r),!1):!0}:this._throttle=()=>!1}static get DEFAULT_OPTS(){return{useThrottling:!0,throttlingDuration:100,transitionDuration:200}}get options(){return{...this._options}}emit(e){if(this._throttle(e))throw new g(e);const u=this._subscribers.slice().map(h=>h(e)).filter(h=>!!h);if(!u.length)throw new a.RuntimeException("Unable to handle the emitted alert properly. There wasn't found any supported subscribers.");if(u.length>1)throw new a.RuntimeException("Unable to handle the emitted alert properly. There were found too many supported subscribers.");return u[0]}subscribe(e){return this._subscribers.push(e),()=>{const s=this._subscribers.indexOf(e);return this._subscribers.splice(s,1)[0]}}};t(f,"VERSION","1.3.5");let b=f,O;const x=n=>{O=n},A=()=>i.getCurrentScope()?i.inject(w.$vuert):O,E=n=>({install:({config:e,provide:s})=>{const r=new b(n);x(r),e.globalProperties.$vuert=r,s(w.$vuert,r)}}),_=()=>{const n=A();if(!n)throw new a.RuntimeException("`useVuert()` was called but there was not active Vuert. Did you forget to install `Vuert` plugin in your App?");return n},V=i.defineComponent({__name:"AlertHandler",props:{is:{default:"div",type:[String,Object]},filter:{default:()=>!0,type:Function},transitionDuration:{default:()=>_().options.transitionDuration,type:[Number,Object],validator:n=>n instanceof Object?"enter"in n&&"leave"in n?isFinite(Number(n.enter))&&isFinite(Number(n.leave)):!1:isFinite(Number(n))}},emits:{opening:n=>n instanceof p,opened:n=>n instanceof p,closing:n=>n instanceof p,closed:n=>n instanceof p},setup(n,{emit:e}){const s=n,r=e,u=[],h=i.ref(0),d=i.shallowRef(),c=async()=>{const l=u[0];l.onOpening(()=>r("opening",l.alert)),l.onOpened(()=>r("opened",l.alert)),l.onClosing(()=>r("closing",l.alert)),l.onClosed(async()=>{r("closed",l.alert),u.shift(),d.value=void 0,h.value-=1,await i.nextTick(),u.length>0&&c()}),d.value=l,await l.open()},C=l=>{const y=new v(l,s.transitionDuration);return u.push(y),u.length===1&&c(),h.value+=1,y};let T;return i.onMounted(()=>{T=_().subscribe(l=>{if(s.filter(l))return C(l)})}),i.onBeforeUnmount(()=>T()),(l,y)=>(i.openBlock(),i.createBlock(i.resolveDynamicComponent(n.is),null,{default:i.withCtx(()=>[d.value?i.renderSlot(l.$slots,"default",{key:0,alert:d.value.alert,customComponent:d.value.component,isOpen:d.value.isOpen.value,queue:h.value,resolve:d.value.resolve,reject:d.value.reject}):i.createCommentVNode("",!0)]),_:3}))}});o.Action=m,o.Alert=p,o.AlertHandler=V,o.AlertThrottledException=g,o.Context=v,o.createVuert=E,o.default=b,o.useVuert=_,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); //# sourceMappingURL=vuert.umd.cjs.map