Files
3dealer/public/js/filament/notifications/notifications.js
T

2 lines
11 KiB
JavaScript

(()=>{function C(i,t=()=>{}){let e=!1;return function(){e?t.apply(this,arguments):(e=!0,i.apply(this,arguments))}}function g({notification:i}){return{isShown:!1,computedStyle:null,transitionDuration:null,transitionEasing:null,closeTimeout:null,durationTimeout:null,remainingDuration:null,durationStartedAt:null,isHovered:!1,isFocusedWithin:!1,isClosing:!1,focusOrigin:null,unsubscribeLivewireHook:null,transitionEffect:null,isDestroyed:!1,init(){this.$el.classList.contains("fi-inline")?this.isShown=!0:(this.computedStyle=window.getComputedStyle(this.$el),this.transitionDuration=parseFloat(this.computedStyle.transitionDuration)*1e3,this.transitionEasing=this.computedStyle.transitionTimingFunction),this.configureTransitions(),this.configureAnimations(),i.duration&&i.duration!=="persistent"&&(this.$root.classList.contains("fi-inline")?this.durationTimeout=setTimeout(()=>{if(!this.$el.matches(":hover")){this.close();return}this.$el.addEventListener("mouseleave",()=>this.close())},i.duration):(this.remainingDuration=i.duration,this.isHovered=this.$el.matches(":hover"),this.isFocusedWithin=this.$el.contains(document.activeElement),!this.isHovered&&!this.isFocusedWithin&&this.resumeDuration())),this.isShown=!0},pauseDuration(t){if(this.isClosing)return;let e=this.isHovered||this.isFocusedWithin;t==="hover"?this.isHovered=!0:this.isFocusedWithin=!0,!(e||!this.durationTimeout)&&(this.remainingDuration=Math.max(0,this.remainingDuration-(performance.now()-this.durationStartedAt)),clearTimeout(this.durationTimeout),this.durationTimeout=null,this.durationStartedAt=null)},resumeDuration(t=null){t==="hover"?this.isHovered=!1:t==="focus"&&(this.isFocusedWithin=!1),!(this.isClosing||this.isHovered||this.isFocusedWithin||this.remainingDuration===null||this.durationTimeout)&&(this.durationStartedAt=performance.now(),this.durationTimeout=setTimeout(()=>{this.durationTimeout=null,this.durationStartedAt=null,this.remainingDuration=0,this.close()},this.remainingDuration))},handleFocusIn(t){this.$el.contains(t.relatedTarget)||(this.focusOrigin=t.relatedTarget instanceof HTMLElement?t.relatedTarget:null),this.pauseDuration("focus")},handleFocusOut(t){this.$el.contains(t.relatedTarget)||this.resumeDuration("focus")},handleEscape(t){this.$el.contains(document.activeElement)&&(t.preventDefault(),t.stopImmediatePropagation(),this.dismiss())},configureTransitions(){if(this.$el.classList.contains("fi-inline")){let r=this.$el.style.getPropertyValue("display"),h=this.$el.style.getPropertyPriority("display");this.transitionEffect=Alpine.effect(()=>{if(this.isDestroyed)return;let u=this.isShown;Alpine.mutateDom(()=>{u?(this.$el.style.setProperty("display",r,h),this.$el.style.setProperty("visibility","visible")):this.$el.style.setProperty("display","none","important")}),this.$el._x_isShown=u});return}let t=this.computedStyle.display,e=()=>{Alpine.mutateDom(()=>{this.$el.style.setProperty("display",t),this.$el.style.setProperty("visibility","visible")}),this.$el._x_isShown=!0},s=()=>{Alpine.mutateDom(()=>{this.$el._x_isShown?this.$el.style.setProperty("visibility","hidden"):this.$el.style.setProperty("display","none")})},n=C(r=>r?e():s(),r=>{this.$el._x_toggleAndCascadeWithTransitions(this.$el,r,e,s)});this.transitionEffect=Alpine.effect(()=>{this.isDestroyed||n(this.isShown)})},configureAnimations(){this.$el.classList.contains("fi-inline")||(this.unsubscribeLivewireHook=Livewire.hook("commit",({component:t,succeed:e})=>{t.snapshot.data.isFilamentNotificationsComponent&&requestAnimationFrame(()=>{if(this.isDestroyed)return;let s=()=>this.$el.getBoundingClientRect().top,n=s();e(()=>{this.isDestroyed||queueMicrotask(()=>queueMicrotask(()=>{if(this.isDestroyed||!this.isShown)return;this.$el.getAnimations().forEach(h=>h.finish());let r=s();n!==r&&(window.matchMedia("(prefers-reduced-motion: reduce)").matches||this.$el.animate([{transform:`translateY(${n-r}px)`},{transform:"translateY(0px)"}],{duration:this.transitionDuration,easing:this.transitionEasing}))}))})})}))},close(t=!1){if(this.isClosing)return;this.isClosing=!0,clearTimeout(this.closeTimeout),clearTimeout(this.durationTimeout),this.durationTimeout=null,this.durationStartedAt=null;let e=()=>window.dispatchEvent(new CustomEvent("notificationClosed",{detail:{id:i.id}}));if(t===!0){this.isShown=!1,e();return}if(this.$root.classList.contains("fi-inline")){e();return}let s=this.$el.closest("[wire\\:id]");this.isShown=!1,this.closeTimeout=setTimeout(()=>{s&&!s.isConnected||e()},this.transitionDuration)},dismiss(){let t=this.focusOrigin;this.focusOrigin=null,this.$el.contains(document.activeElement)&&t instanceof HTMLElement&&t.isConnected&&!this.$el.contains(t)&&t.focus({preventScroll:!0}),this.close()},markAsRead(){window.dispatchEvent(new CustomEvent("markedNotificationAsRead",{detail:{id:i.id}}))},markAsUnread(){window.dispatchEvent(new CustomEvent("markedNotificationAsUnread",{detail:{id:i.id}}))},destroy(){this.isDestroyed=!0,clearTimeout(this.durationTimeout),this.durationTimeout=null,this.unsubscribeLivewireHook?.(),this.unsubscribeLivewireHook=null,Alpine.release(this.transitionEffect)}}}var b=new Set,a=new Map,T="filament.notifications.redirect",x=/^[a-f0-9]{40}$/,c=0,D=!1,S=!1,y=!1,m=0,$=0,l=null;try{let i=JSON.parse(sessionStorage.getItem(T)??"[]");Array.isArray(i)&&i.filter(t=>typeof t=="string"&&x.test(t)).slice(-50).forEach(t=>a.set(t,{redirect:!0,ready:!0}))}catch{}function E(){try{let i=[...a].filter(([,t])=>t.redirect).map(([t])=>t);i.length?sessionStorage.setItem(T,JSON.stringify(i)):sessionStorage.removeItem(T)}catch{}}function L(i,t){for(a.set(i,t);a.size>50;)a.delete(a.keys().next().value);E()}function f(i){l===i&&(i.batches.forEach(t=>a.delete(t)),$=Math.max($,i.wake),l=null,E(),p())}function p(){if(y)return;y=!0;let i=c;window.Alpine.nextTick(()=>{if(y=!1,D||l)return;if(i!==c){p();return}let t=[...b].find(n=>n.$el.isConnected);if(!t)return;let e=[...a].filter(([,n])=>n.ready&&(n.redirect||n.generation===c)).map(([n])=>n);if(!e.length&&m===$)return;let s={host:t,batches:e,wake:m};l=s;try{t.$wire.pullNotifications(e).then(()=>f(s),()=>f(s))}catch(n){throw f(s),n}})}function k({respond:i,succeed:t,fail:e}){let s=c,n=null,r=!1,h=(o,d)=>{s!==c||!x.test(o??"")||(n=o,r=d,r&&L(n,{redirect:!0,ready:!1}))},u=({json:o})=>h(o.filamentNotificationBatch,o.components.some(d=>d.effects?.redirect));i(({response:o})=>{if(!(!o.ok||o.redirected||s!==c))if(o.headers)h(o.headers.get("X-Filament-Notifications"),o.headers.get("X-Filament-Notifications-Redirect")==="true");else{let d=o.text.bind(o);o.text=async()=>{let A=await d();return o.ok&&u({json:JSON.parse(A)}),A}}}),t(({json:o})=>{s!==c||r||o.components.some(d=>d.effects?.redirect)||(n&&L(n,{generation:s,redirect:!1,ready:!0}),o.filamentNotifications&&(m++,p()))}),e(()=>{n&&r&&(a.delete(n),E())})}function P(){Livewire.hook("request",k),Livewire.hook("commit",({component:i,commit:t,fail:e})=>{let s=l;s&&i.id===s.host.$wire.$id&&t.calls.some(n=>["pullNotifications","pullNotificationsFromSession"].includes(n.method)&&JSON.stringify(n.params)===JSON.stringify([s.batches]))&&e(()=>f(s))}),document.addEventListener("livewire:navigating",()=>{D=!0,c++;for(let[i,t]of a)t.redirect||a.delete(i);l&&f(l)}),document.addEventListener("livewire:navigated",()=>{S&&m++,S=!0,D=!1;for(let i of a.values())i.redirect&&(i.ready=!0);p()})}function N(){let i;return{init(){i=this,b.add(i),(S||this.$el.dataset.filamentNotifications!=="mounted")&&m++,p()},destroy(){b.delete(i),l?.host===i&&f(l)}}}var w=class{constructor(){return this.id(crypto.randomUUID?.()??"10000000-1000-4000-8000-100000000000".replace(/[018]/g,t=>(+t^crypto.getRandomValues(new Uint8Array(1))[0]&15>>+t/4).toString(16))),this}id(t){return this._id=t,this}title(t){return this._title=t,this}body(t){return this._body=t,this}actions(t){return this._actions=t,this}status(t){return this._status=t,this}color(t){return this._color=t,this}icon(t){return this._icon=t,this}iconColor(t){return this._iconColor=t,this}duration(t){return this._duration=t,this}seconds(t){return this.duration(t*1e3),this}persistent(){return this.duration("persistent"),this}danger(){return this.status("danger"),this}info(){return this.status("info"),this}success(){return this.status("success"),this}warning(){return this.status("warning"),this}view(t){return this._view=t,this}viewData(t){return this._viewData=t,this}toJSON(){return{id:this._id,title:this._title,body:this._body,actions:this._actions?.map(t=>t.toJSON()),status:this._status,color:this._color,icon:this._icon,iconColor:this._iconColor,duration:this._duration,view:this._view,viewData:this._viewData}}send(){return window.dispatchEvent(new CustomEvent("notificationSent",{detail:{notification:this.toJSON()}})),this}},v=class{constructor(t){return this.name(t),this}name(t){return this._name=t,this}color(t){return this._color=t,this}dispatch(t,e){return this.event(t),this.eventData(e),this}dispatchSelf(t,e){return this.dispatch(t,e),this._dispatchDirection="self",this}dispatchTo(t,e,s){return this.dispatch(e,s),this._dispatchDirection="to",this._dispatchToComponent=t,this}emit(t,e){return this.dispatch(t,e),this}emitSelf(t,e){return this.dispatchSelf(t,e),this}emitTo(t,e,s){return this.dispatchTo(t,e,s),this}dispatchDirection(t){return this._dispatchDirection=t,this}dispatchToComponent(t){return this._dispatchToComponent=t,this}event(t){return this._event=t,this}eventData(t){return this._eventData=t,this}extraAttributes(t){return this._extraAttributes=t,this}icon(t){return this._icon=t,this}iconPosition(t){return this._iconPosition=t,this}outlined(t=!0){return this._isOutlined=t,this}disabled(t=!0){return this._isDisabled=t,this}label(t){return this._label=t,this}close(t=!0){return this._shouldClose=t,this}openUrlInNewTab(t=!0){return this._shouldOpenUrlInNewTab=t,this}size(t){return this._size=t,this}url(t){return this._url=t,this}view(t){return this._view=t,this}button(){return this.view("filament::components.button.index"),this}grouped(){return this.view("filament::components.dropdown.list.item"),this}iconButton(){return this.view("filament::components.icon-button"),this}link(){return this.view("filament::components.link"),this}toJSON(){return{name:this._name,color:this._color,event:this._event,eventData:this._eventData,dispatchDirection:this._dispatchDirection,dispatchToComponent:this._dispatchToComponent,extraAttributes:this._extraAttributes,icon:this._icon,iconPosition:this._iconPosition,isOutlined:this._isOutlined,isDisabled:this._isDisabled,label:this._label,shouldClose:this._shouldClose,shouldOpenUrlInNewTab:this._shouldOpenUrlInNewTab,size:this._size,url:this._url,view:this._view}}},_=class{constructor(t){return this.actions(t),this}actions(t){return this._actions=t.map(e=>e.grouped()),this}color(t){return this._color=t,this}icon(t){return this._icon=t,this}iconPosition(t){return this._iconPosition=t,this}label(t){return this._label=t,this}tooltip(t){return this._tooltip=t,this}toJSON(){return{actions:this._actions?.map(t=>t.toJSON()),color:this._color,icon:this._icon,iconPosition:this._iconPosition,label:this._label,tooltip:this._tooltip}}};window.setUpFilamentBroadcastNotifications=({$wire:i,broadcastChannel:t,event:e,onNotification:s})=>{let n,r=!1,h=o=>{setTimeout(()=>{r||s(o)},500)},u=()=>{n||r||(n=window.Echo.private(t),n.listen(e,h))};window.addEventListener("EchoLoaded",u),i.__instance.addCleanup(()=>{r=!0,window.removeEventListener("EchoLoaded",u),n?.stopListening(e,h)}),window.Echo&&u()};window.FilamentNotificationAction=v;window.FilamentNotificationActionGroup=_;window.FilamentNotification=w;document.addEventListener("alpine:init",()=>{window.Alpine.data("notificationComponent",g),window.Alpine.data("notificationsComponent",N)});document.addEventListener("livewire:init",P);})();