Newer
Older
smart-home-server / webclient / dist / js / main.js
(()=>{var u=Object.defineProperty;var o=Object.getOwnPropertySymbols;var a=Object.prototype.hasOwnProperty,h=Object.prototype.propertyIsEnumerable;var i=(t,r,e)=>r in t?u(t,r,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[r]=e,c=(t,r)=>{for(var e in r||(r={}))a.call(r,e)&&i(t,e,r[e]);if(o)for(var e of o(r))h.call(r,e)&&i(t,e,r[e]);return t};function d(){console.log("HUD init")}var s=class{constructor(r,e,n){this.screens={},this.routesMap={},this.currentScreen=null,this.screensContainer=document.querySelector(r),this.loader=document.querySelector(e),this.errorScreen=document.querySelector(n),console.log("Screens Init")}add(r,e){if(typeof e!="object")return console.error("Screens: screens must be object");if(typeof(e==null?void 0:e.alias)=="undefined")return console.error("Screens: undefined alias");if(typeof(e==null?void 0:e.renderer)!="function")return console.error("Screens: renderer must be function");this.screens[e.alias]=c({route:r},e),this.routesMap[r]=e.alias}switch(r){var n;if(this.showLoader(),(n=this.currentScreen)==null||n.DOMObject.remove(),typeof this.screens[r]=="undefined"){console.error(`Screens: "${r}" not found`);return}this.currentScreen=this.screens[r];let e=document.createElement("div");e.classList.add("screen"),e.id=r,e.dataset.alias=r,e.innerHTML=this.currentScreen.renderer(),this.currentScreen.DOMObject=e,this.screensContainer.append(this.currentScreen.DOMObject),this.currentScreen.initer(this)}routing(){setInterval(()=>{let r=document.location.hash.split("#!")[1];if(typeof r=="undefined"||r=="")return;let e=typeof this.routesMap[r]=="undefined"?"not-found-screen":this.routesMap[r];(!this.currentScreen||this.currentScreen.alias!=e)&&this.switch(e)},50)}ready(){this.currentScreen!=null&&(this.currentScreen.DOMObject||(this.currentScreen.DOMObject=document.getElementsById(this.currentScreen.alias)),this.hideLoader(),this.currentScreen.DOMObject.classList.add("a-show"))}error(){var r;(r=this.currentScreen)==null||r.DOMObject.remove(),this.showErrorScreen()}hideLoader(){this.loader.classList.remove("a-show")}showLoader(){this.loader.classList.add("a-show")}showErrorScreen(r){this.errorScreen.classList.add("a-show")}hideErrorScreen(r){this.errorScreen.classList.remove("a-show")}getScreens(){return this.screens}getRoutesMap(){return this.routesMap}};document.addEventListener("DOMContentLoaded",t=>{console.log("App init"),d();let r=new s(".screens",".load-screen",".error-screen");r.add("/",{alias:"home",renderer:()=>'<h2 class="mt-4">Hello world</h2>',initer:e=>{setTimeout(()=>e.ready(),1e3),setTimeout(()=>e.error(),2e3)}}),r.add("-",{alias:"not-found-screen",renderer:()=>'<h2 class="mt-4">404 NOT FOUND</h2>',initer:e=>{e.ready()}}),console.log(r.getScreens()),r.routing()});})();
//# sourceMappingURL=main.js.map