2 lines
5.5 KiB
JavaScript
2 lines
5.5 KiB
JavaScript
const i={passive:!0};document.addEventListener("livewire:init",()=>{Livewire.hook("request",({fail:e})=>{e(({status:t,preventDefault:n})=>{t===419&&(window.location.reload(),n())})})});Alpine.store("notifications",{notifications:[],init(){Livewire.on("notify",e=>{this.addNotification(e)})},addNotification(e){e=e[0],e.show=!1,e.id=Date.now()+Math.floor(Math.random()*1e3),this.notifications.push(e),requestAnimationFrame(()=>{this.notifications=this.notifications.map(t=>(t.id===e.id&&(t.show=!0),t))}),setTimeout(()=>{this.removeNotification(e.id)},e.timeout||5e3)},removeNotification(e){this.notifications=this.notifications.filter(t=>(t.id===e&&(t.show=!1),t.show))}});let d;const p=document.documentElement.lang||"en",h={ar:"ar-SA",de:"de-DE",en:"en-US",es:"es-ES",fi:"fi-FI",fr:"fr-FR",it:"it-IT",sv:"sv-SE",uk:"uk-UA",ko:"ko-KR",lv:"lv-LV",nl:"nl-NL",no:"no-NO",pt:"pt-PT",sr:"sr-RS",id:"id-ID"};function m(){const e=new Date,t=document.getElementById("current-time"),n=document.getElementById("current-date"),o=h[p]||"en-US";t&&(t.textContent=e.toLocaleTimeString(o,{hour:"2-digit",minute:"2-digit",hour12:!0})),n&&(n.textContent=e.toLocaleDateString(o,{weekday:"long",year:"numeric",month:"long",day:"numeric"}))}const g=(e,t)=>{let n;return(...o)=>{clearTimeout(n),n=setTimeout(()=>e.apply(void 0,o),t)}},w=(e,t)=>{let n;return function(...o){n||(e.apply(this,o),n=!0,setTimeout(()=>n=!1,t))}};let a;function y(){const e=document.querySelectorAll(".reveal-on-scroll");if(e.length===0)return;const t={root:null,rootMargin:"0px 0px -10% 0px",threshold:.1};a=new IntersectionObserver(n=>{n.forEach(o=>{o.isIntersecting&&(o.target.style.transform="translateY(0)",o.target.style.opacity="1",o.target.classList.add("revealed"),a.unobserve(o.target))})},t),e.forEach(n=>{n.style.transform="translateY(20px)",n.style.opacity="0",n.style.transition="transform 0.6s ease-out, opacity 0.6s ease-out",a.observe(n)})}const s=new WeakMap;function b(e){document.querySelectorAll(e).forEach(n=>{if(s.has(n))return;const o=function(r){if(s.get(n))return r.preventDefault(),r.stopPropagation(),!1;s.set(n,!0),n.classList.add("btn-loading");const u=()=>{s.set(n,!1),n.classList.remove("btn-loading")};window.requestIdleCallback?setTimeout(()=>requestIdleCallback(u),2e3):setTimeout(u,2e3)};n.addEventListener("click",o,i),s.set(n,!1)})}let c;function E(){const e=document.querySelectorAll("img[data-src]");e.length!==0&&(c=new IntersectionObserver(t=>{t.forEach(n=>{if(n.isIntersecting){const o=n.target;o.src=o.dataset.src,o.classList.remove("lazy"),o.removeAttribute("data-src"),c.unobserve(o)}})},{rootMargin:"50px"}),e.forEach(t=>c.observe(t)))}let l=null;function v(){if(l!==null)return l;const e=[];if(navigator.hardwareConcurrency&&e.push(navigator.hardwareConcurrency>=4?1:0),navigator.deviceMemory&&e.push(navigator.deviceMemory>=4?1:0),navigator.connection){const o=navigator.connection;e.push(o.effectiveType==="4g"?1:0)}const t=/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);return e.push(t?0:1),l=e.reduce((o,r)=>o+r,0)/e.length>.5?"high":"low",l}function L(){document.querySelectorAll("form").forEach(t=>{const n=t.querySelector('button[type="submit"], input[type="submit"]');if(!n)return;const o=function(r){n.disabled=!0,n.classList.add("btn-loading"),setTimeout(()=>{n.disabled=!1,n.classList.remove("btn-loading")},8e3)};t.addEventListener("submit",o,i)})}function A(){if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){const t=document.querySelector("meta[name=viewport]");document.querySelectorAll("input, select, textarea").forEach(o=>{o.addEventListener("focus",()=>{t&&t.setAttribute("content","width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0")},i),o.addEventListener("blur",()=>{t&&t.setAttribute("content","width=device-width, initial-scale=1.0")},i)}),document.body.classList.add("mobile-optimized")}}function S(){window.addEventListener("error",e=>{console.error("Error:",e.error)}),window.addEventListener("unhandledrejection",e=>{console.error("Unhandled promise rejection:",e.reason)})}function k(e){const t=document.createElement("div");t.className="fixed top-4 right-4 bg-red-500 text-white px-4 py-2 rounded shadow-lg z-50 transition-opacity duration-300",t.textContent=e,document.body.appendChild(t),requestAnimationFrame(()=>{t.style.opacity="1"}),setTimeout(()=>{t.style.opacity="0",setTimeout(()=>t.remove(),300)},4e3)}function x(){let e=null;window.addEventListener("offline",()=>{e||(e=document.createElement("div"),e.className="fixed top-4 right-4 bg-yellow-500 text-white px-4 py-2 rounded shadow-lg z-50",e.textContent="You are offline. Some features may not work.",document.body.appendChild(e))}),window.addEventListener("online",()=>{e&&(e.remove(),e=null)})}function f(){m(),d=setInterval(m,1e3);const e=v();e==="high"?(y(),E()):(document.body.classList.add("low-performance"),document.querySelectorAll(".reveal-on-scroll").forEach(t=>{t.classList.add("revealed")})),b('.btn-primary, [href*="checkout"], [href*="add-to-cart"]'),L(),A(),S(),x(),e==="low"&&document.querySelectorAll(".animate-pulse-slowest, .animate-pulse-slow, .animate-pulse-fast").forEach(n=>{n.style.animation="none",n.style.opacity="0.1"})}document.addEventListener("touchstart",()=>{},i);document.addEventListener("touchmove",()=>{},i);document.readyState==="loading"?document.addEventListener("DOMContentLoaded",f):f();window.addEventListener("beforeunload",()=>{d&&clearInterval(d),a&&a.disconnect(),c&&c.disconnect()});window.AppOptimizer={debounce:g,throttle:w,getDevicePerformance:v,showErrorNotification:k};
|