1 line
8.5 KiB
JavaScript
1 line
8.5 KiB
JavaScript
"use strict";window.$=window.aero=(n=>document.getElementById(n)),$.lastRequest=null,$.currentUrl=$.originalUrl=window.location.pathname,$.classes={fadeIn:"fade-in",fadeOut:"fade-out"},$.init=function(){document.removeEventListener("DOMContentLoaded",$.init),$.content=$("content"),$.loadingAnimation=$("loading-animation"),$.content.addEventListener($.getTransitionEventName(),(n=>{return n.target===$.content&&n.target.classList.contains($.classes.fadeOut)?$.content.response?($.setContent($.content.response),void $.scrollToTop()):void($.content.transitionEnded=!0):void 0})),$.ajaxifyLinks(),$.markActiveLinks()},document.addEventListener("DOMContentLoaded",$.init),window.addEventListener("popstate",(n=>{n.state?$.load(n.state,!1):$.currentUrl!==$.originalUrl&&$.load($.originalUrl,!1)}));;let onLinkClick=function(i){if(2!==i.which){let n=this.getAttribute("href");i.preventDefault(),i.stopPropagation(),n!==window.location.pathname&&$.load(n)}};$.ajaxifyLinks=(i=>{i||(i=document.body);for(let n=i.querySelectorAll(".ajax"),o=0;o<n.length;o++){let t=n[o];t.classList.remove("ajax"),t.onclick=onLinkClick}});;$.emit=(e=>{document.dispatchEvent(new Event(e,{bubbles:!0,cancelable:!0}))});;$.executeScripts=(n=>{n||(n=$.content);for(let t=n.getElementsByTagName("script"),e=0;e<t.length;e++){let i=t[e];"application/json"!==i.type&&"application/ld+json"!==i.type&&new Function(i.innerHTML)()}});;$.fadeIn=(s=>{s&&(s.classList.remove($.classes.fadeOut),s.classList.add($.classes.fadeIn))});;$.fadeOut=(s=>{s&&(s.classList.remove($.classes.fadeIn),s.classList.add($.classes.fadeOut))});;$.get=(e=>{return new Promise(((t,o)=>{let n=new XMLHttpRequest;n.onerror=(()=>o(new Error("You are either offline or the requested page doesn't exist."))),n.ontimeout=(()=>o(new Error("The page took too much time to respond."))),n.onload=(()=>{n.status<200||n.status>=400?o(n.responseText):t(n.responseText)}),n.open("GET",e,!0),n.send(),$.lastRequest=n}))});;$.getJSON=(e=>$.get(e).then(JSON.parse));;$.getTransitionEventName=(()=>{let n=document.createElement("fakeelement"),i={transition:"transitionend",OTransition:"oTransitionEnd",MozTransition:"transitionend",WebkitTransition:"webkitTransitionEnd"};for(let t in i)if(void 0!==n.style[t])return i[t]});;$.load=((t,n=true)=>{$.lastRequest&&($.lastRequest.abort(),$.lastRequest=null),$.currentUrl=t,n&&history.pushState(t,null,t),$.content.response=null,$.content.transitionEnded=!1,$.fadeIn($.loadingAnimation),$.fadeOut($.content);let e=n=>{return t===$.currentUrl?$.content.transitionEnded?($.setContent(n),void $.scrollToTop()):void($.content.response=n):void 0};$.get("/_"+t).then(e).catch(e),$.markActiveLinks(t)});;$.markActiveLinks=((e,t)=>{"object"==typeof e&&(t=e,e=void 0),void 0===e&&(e=window.location.pathname),void 0===t&&(t=document.body);for(let i=t.querySelectorAll("a"),o=0;o<i.length;o++){let a=i[o],c=a.getAttribute("href");e===c?a.classList.add("active"):a.classList.remove("active")}$.emit("ActiveLinksMarked")});;$.post=((e,t)=>{return new Promise(((n,o)=>{let r=new XMLHttpRequest;r.onerror=(()=>o(new Error("Error requesting "+e))),r.ontimeout=(()=>o(new Error("Timeout requesting "+e))),r.onload=(()=>{r.status<200||r.status>=400?o(r.responseText):n(r.responseText)}),r.open("POST",e,!0),"object"==typeof t?(r.setRequestHeader("Content-type","application/json"),r.send(JSON.stringify(t))):r.send(t),$.lastRequest=r}))});;$.scrollToTop=(()=>{for(let o=$.content;o=o.parentElement;)o.scrollTop=0});;$.setContent=(n=>{$.content.innerHTML=n,$.fadeIn($.content),$.fadeOut($.loadingAnimation),$.ajaxifyLinks($.content),$.markActiveLinks($.content),$.executeScripts($.content),$.emit("DOMContentLoaded")});document.addEventListener("keydown",(e=>{if(65===e.keyCode&&e.altKey){let l=$("staff-info");"block"!==l.style.display?l.style.display="block":l.style.display="none"}}));;function updateAvatars(){for(let t=document.querySelectorAll(".user-image"),e=0;e<t.length;++e){let a=t[e];0===a.naturalWidth?(a.onload=function(){this.style.opacity=1},a.onerror=function(){this.src="/images/elements/no-gravatar.svg",this.style.opacity=1}):a.style.opacity=1}};window.location.hash&&"#_=_"===window.location.hash&&window.history.pushState("",document.title,window.location.pathname),document.addEventListener("DOMContentLoaded",function(t){$.loadingAnimation.classList.add("fade-out"),updateAvatars()});;window.like=((e,n)=>{$.post(`/api/${e}/like/`+n),$("like-"+n).style.display="none",$("unlike-"+n).style.display="inline-block";let l=$("likes-"+n);l.innerHTML=parseInt(l.textContent)+1}),window.unlike=((e,n)=>{$.post(`/api/${e}/unlike/`+n),$("like-"+n).style.display="inline-block",$("unlike-"+n).style.display="none";let l=$("likes-"+n);l.innerHTML=parseInt(l.textContent)-1}),window.edit=(e=>{$("source-"+e).style.display="block",$("save-"+e).style.display="block",$("render-"+e).style.display="none",$("toolbar-"+e).style.display="none"}),window.cancelEdit=(e=>{$("source-"+e).style.display="none",$("save-"+e).style.display="none",$("render-"+e).style.display="",$("toolbar-"+e).style.display=""}),window.saveEdit=((e,n)=>{let l=$("source-"+n).value;$.post(`/api/${e}/edit/`+n,{id:n,text:l}).then((e=>{$("render-"+n).innerHTML=e,cancelEdit(n)}))});;function subscribeOnServer(n){console.log("Send subscription to server..."),console.log(n),$.post("/api/notifications/subscribe",n).then(function(n){console.log(n)})}function unsubscribeOnServer(n){console.log("Send unsubscription to server..."),console.log(n),$.post("/api/notifications/unsubscribe",n).then(function(n){console.log(n)})}function sendTestNotification(){console.log("Sending test notification..."),$.get("/api/notifications/test").then(function(n){})}function subscribe(){var n=document.querySelector(".push-button");n&&(n.disabled=!0),navigator.serviceWorker.ready.then(function(e){e.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:new TextEncoder("binary").encode("BLxjquZGLvRnYGkV_xlkuAIilZzHJLDdGUZAmvq4pqev5uQHBmYRxJqfbFQFQn2kYfe5SRBwiNfuiakHn-3KR_k")}).then(function(e){return isPushEnabled=!0,n&&(n.textContent="Disable Notifications",n.disabled=!1),document.querySelector(".test-notification").disabled=!1,subscribeOnServer(e)}).catch(function(e){"denied"===Notification.permission?(console.warn("Permission for Notifications was denied"),n&&(n.disabled=!0)):(console.error("Unable to subscribe to push.",e),n&&(n.disabled=!1,n.textContent="Enable Notifications"))})})}function unsubscribe(){var n=document.querySelector(".push-button");n.disabled=!0,document.querySelector(".test-notification").disabled=!0,navigator.serviceWorker.ready.then(function(e){e.pushManager.getSubscription().then(function(e){return e?(unsubscribeOnServer(e),void e.unsubscribe().then(function(e){n&&(n.disabled=!1,n.textContent="Enable Notifications"),isPushEnabled=!1}).catch(function(e){console.log("Unsubscription error: ",e),n&&(n.disabled=!1,n.textContent="Enable Notifications")})):(isPushEnabled=!1,void(n&&(n.disabled=!1,n.textContent="Enable Notifications")))}).catch(function(n){console.error("Error thrown while unsubscribing from push messaging.",n)})})}function initialiseState(n){return console.log("Initialise state..."),console.log("Scope:",n.scope),"showNotification"in ServiceWorkerRegistration.prototype?"denied"===Notification.permission?void console.warn("The user has blocked notifications."):"PushManager"in window?void navigator.serviceWorker.ready.then(function(n){console.log("Get subscription..."),n.pushManager.getSubscription().then(function(n){console.log("Enable Push UI...");var e=document.querySelector(".push-button");e&&(e.disabled=!1),n&&(subscribeOnServer(n),e&&(e.textContent="Disable Notifications"),isPushEnabled=!0,document.querySelector(".test-notification").disabled=!1)}).catch(function(n){console.warn("Error during getSubscription()",n)})}):void console.warn("Push messaging isn't supported."):void console.warn("Notifications aren't supported.")}var isPushEnabled=!1;;function makeSaveable(e,t){window.save=function(n){var a=n.target;if(!document.saving){document.saving=!0;var o=a.id,c=a.value?a.value:"",s=a.dataset.old?a.dataset.old:"";a.classList.add("saving"),$.content.style.cursor="wait",$.post(e,{"function":"save",key:o,value:c,old:s}).then(function(e){t&&t(o,e)}).catch(function(e){console.error(e)}).then(function(){$.get("/_"+location.pathname).then(function(e){var t=document.activeElement.id,n=document.activeElement.value;if($.setContent(e),t){var a=$(t);a&&(a.value=n,a.select?a.select():a.focus&&a.focus())}$.content.style.cursor="auto",document.saving=!1})})}};for(var n=document.querySelectorAll(".save-on-change"),a=0;a<n.length;++a){var o=n[a];o.onchange=window.save,o.dataset.old=o.value}} |