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