@byloth/vuert
Version:
The headless alerts, modals, notifications & popups library for Vue.js craftsmen. ℹ
3 lines (2 loc) • 6 kB
JavaScript
var Vuert=function(l,s,a){"use strict";var D=Object.defineProperty;var j=(l,s,a)=>s in l?D(l,s,{enumerable:!0,configurable:!0,writable:!0,value:a}):l[s]=a;var t=(l,s,a)=>j(l,typeof s!="symbol"?s+"":s,a);const g={$vuert:Symbol("[vuert]: vuert")};class w extends a.RuntimeException{constructor(n,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=n}}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 n;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=((n=e.actions)==null?void 0:n.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(n,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(n),this._isOpen=s.ref(!1),this.isOpen=s.computed(()=>this._isOpen.value),this.component=n.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(n){this._publisher.subscribe("opening",n)}onOpened(n){this._publisher.subscribe("opened",n)}onClosing(n){this._publisher.subscribe("closing",n)}onClosed(n){this._publisher.subscribe("closed",n)}}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=n=>{if(!n.id)return!1;const r=Date.now(),u=this._throttlers.get(n.id)??0;return r-u>this._options.throttlingDuration?(this._throttlers.set(n.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 w(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 n=this._subscribers.indexOf(e);return this._subscribers.splice(n,1)[0]}}};t(f,"VERSION","1.3.5");let b=f,O;const x=i=>{O=i},A=()=>s.getCurrentScope()?s.inject(g.$vuert):O,E=i=>({install:({config:e,provide:n})=>{const r=new b(i);x(r),e.globalProperties.$vuert=r,n(g.$vuert,r)}}),_=()=>{const i=A();if(!i)throw new a.RuntimeException("`useVuert()` was called but there was not active Vuert. Did you forget to install `Vuert` plugin in your App?");return i},V=s.defineComponent({__name:"AlertHandler",props:{is:{default:"div",type:[String,Object]},filter:{default:()=>!0,type:Function},transitionDuration:{default:()=>_().options.transitionDuration,type:[Number,Object],validator:i=>i instanceof Object?"enter"in i&&"leave"in i?isFinite(Number(i.enter))&&isFinite(Number(i.leave)):!1:isFinite(Number(i))}},emits:{opening:i=>i instanceof p,opened:i=>i instanceof p,closing:i=>i instanceof p,closed:i=>i instanceof p},setup(i,{emit:e}){const n=i,r=e,u=[],h=s.ref(0),d=s.shallowRef(),c=async()=>{const o=u[0];o.onOpening(()=>r("opening",o.alert)),o.onOpened(()=>r("opened",o.alert)),o.onClosing(()=>r("closing",o.alert)),o.onClosed(async()=>{r("closed",o.alert),u.shift(),d.value=void 0,h.value-=1,await s.nextTick(),u.length>0&&c()}),d.value=o,await o.open()},C=o=>{const y=new v(o,n.transitionDuration);return u.push(y),u.length===1&&c(),h.value+=1,y};let T;return s.onMounted(()=>{T=_().subscribe(o=>{if(n.filter(o))return C(o)})}),s.onBeforeUnmount(()=>T()),(o,y)=>(s.openBlock(),s.createBlock(s.resolveDynamicComponent(i.is),null,{default:s.withCtx(()=>[d.value?s.renderSlot(o.$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}):s.createCommentVNode("",!0)]),_:3}))}});return l.Action=m,l.Alert=p,l.AlertHandler=V,l.AlertThrottledException=w,l.Context=v,l.createVuert=E,l.default=b,l.useVuert=_,Object.defineProperties(l,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),l}({},Vue,Core);
//# sourceMappingURL=vuert.global.js.map