diff --git a/app/public/build/kernel/Application.js b/app/public/build/kernel/Application.js index 690a41c..8f2fd48 100644 --- a/app/public/build/kernel/Application.js +++ b/app/public/build/kernel/Application.js @@ -540,6 +540,10 @@ class Application { return this.controllers[name]; } + getControllers() { + return this.controllers; + } + /** * Given a route, this function will change the current page to the page specified by the route * @param route - The route to change to. diff --git a/app/public/build/kernel/Application.min.js b/app/public/build/kernel/Application.min.js index 6fd6965..51f386e 100644 --- a/app/public/build/kernel/Application.min.js +++ b/app/public/build/kernel/Application.min.js @@ -1 +1 @@ -class Application{constructor(t){if(this.controllers={},this.useSockets=!1,this.socketsURL=!1,this.useSession=!0,this.appStarted=!1,this.useLocalStorage=!0,this.socketConfigs={},this.viewsLoadedCount=0,this.firstLoadElementId=t.firstLoadElementId,void 0!==t.routing?this.routing=t.routing:(this.routing=[],void 0!==t.controllers&&t.controllers.forEach(t=>{this.routing[`/${t.name}`]=t.name})),void 0!==t.useSession&&(this.useSession=t.useSession),void 0!==t.useLocalStorage&&(this.useLocalStorage=t.useLocalStorage),this.useLocalStorage){let t=JSON.parse(localStorage.getItem("views"));globalThis.views=[],t&&(globalThis.views=t),window.onbeforeunload=(()=>{this.onClose(),localStorage.setItem("views",JSON.stringify(globalThis.views))})}void 0!==t.useSockets&&void 0!==t.socketsURL&&(this.useSockets=t.useSockets,this.socketsURL=t.socketsURL,void 0!==t.socketConfigs&&(this.socketConfigs=t.socketConfigs)),void 0!==t.controllers&&(this.controllers_configuration=t.controllers);try{return this.loadControllers().then(()=>{window.onpopstate=(t=>this.changePath(t.target.location.href))}).catch(t=>console.error(t)),this}catch{return!1}}setCookie(t,e,s){const o=new Date;o.setTime(o.getTime()+s);let i="expires="+o.toUTCString();document.cookie=t+"="+e+";"+i+";path=/"}deleteCookie(t){return this.setCookie(t,"",-1)}getCookie(t){let e=t+"=",s=decodeURIComponent(document.cookie).split(";");for(let t=0;te.url==t);return globalThis.views.length>0&&e?e:""}async loadControllers(){this.controllersToLoad=[],this.urlConfiguration=[],Object.keys(this.routing).includes(new URL(location.href).pathname)&&(this.urlConfiguration=this.routing[new URL(location.href).pathname],this.controllersToLoad=this.routing[new URL(location.href).pathname].controllers);let t=new URL(location.href).pathname.split("/");if(Object.keys(this.routing).forEach(e=>{if(!e.match(/{.*}+/g))return;let s=e.split("/");if(s.length==t.length&&s.length==t.length){for(let e=0;e{this.setMetaTag(t)}),this.urlConfiguration.styles&&this.urlConfiguration.styles.forEach(t=>{this.addStyle(t)})}catch(t){console.error(t)}for(let t=0;t{e.settings.app=this,e.settings.name=e.name,this.controllers[e.name]=new t.default(e.settings),this.controllers[e.name].init(),this.searchParams=this.getSearchParams()}).catch(t=>console.error(t))}}setTitle(t){return document.title=t}addStyle(t){if("string"!=typeof t)return void console.error("Incorrect style url");let e=document.createElement("link");e.rel="stylesheet",e.href=t,document.getElementsByTagName("head")[0].appendChild(e)}setMetaTag(t){let e=document.createElement("meta");for(const[s,o]of Object.entries(t))e[s]=o;document.getElementsByTagName("head")[0].appendChild(e)}viewLoaded(t){1!=this.appStarted&&(this.getController(t).showOnLoad?this.getController(t).show():this.getController(t).hide(),this.viewsLoadedCount++,this.onControllerLoaded(t),this.controllersToLoad.length===this.viewsLoadedCount&&(this.firstLoadElementId&&(document.getElementById(this.firstLoadElementId).style.display="none"),console.info("All views loaded"),this.appStarted=!0,this.startApplication()))}onControllerLoaded(t){}onStartApp(){console.info("Redefine startup function")}onSocketConnected(){console.info("Custom callback on socket connected")}onSocketDisconnected(){console.info("Custom callback on socket disconnected")}onSocketConnectionError(){console.info("Custom callback on socket connection Error")}startApplication(){this.socket=void 0,this.socketConnected=!1,console.info("Application started"),this.onStartApp(),this.useSockets&&this.socketsURL&&(this.socket=io(this.socketsURL,this.socketConfigs),this.socket.on("connect",()=>{this.useSession?(this.sessionKey=this.getCookie("sessionKey"),this.socket.emit("getSession",{sessionKey:this.sessionKey}),this.socket.on("getSession",t=>{this.session=t,t.sessionKey&&t.sessionKey!==this.sessionKey&&(null==t.liveTime&&(t.liveTime=216e6),this.setCookie("sessionKey",t.sessionKey,t.liveTime),this.sessionKey=t.sessionKey),1!=this.socketConnected&&(this.socketConnected=!0,this.onSocketConnected(),this.registerSocketRequests())})):(this.socketConnected=!0,this.onSocketConnected())}),this.socket.on("disconnect",()=>{this.socketConnected=!1,this.onSocketDisconnected()}),this.socket.on("connect_error",()=>{this.socketConnected=!1,this.onSocketConnectionError()}))}registerSocketRequests(){return Array.isArray(this.socketListeners)&&this.socketListeners.forEach(t=>{"function"==typeof t.callback&&this.socket.once(t.name,t.callback)}),Array.isArray(this.socketRequests)&&this.socketRequests.forEach(t=>{this.socket.emit(t.name,t.data)}),this}addSocketListener(t,e){return void 0===this.socketListeners&&(this.socketListeners=[]),this.socketListeners.push({name:t,callback:e}),this}addSocketRequest(t,e){return void 0===this.socketRequests&&(this.socketRequests=[]),this.socketRequests.push({name:t,data:e}),this}request(t,e,s){return"object"==typeof e&&void 0===e.sessionKey&&this.useSession&&this.sessionKey&&(e.sessionKey=this.sessionKey),this.socketConnected?(this.socket.once(t,s),this.socket.emit(t,e),this):(this.addSocketListener(t,s),this.addSocketRequest(t,e),console.info("Sockets not registered yet! Added to onConnect"),this)}getController(t){return this.controllers[t]}changePath(t){let e;e=t instanceof URL?t:new URL(t,location.href);let s=[];e.searchParams.forEach((t,e)=>{s.push({name:e,value:t})});let o=[];o="string"==typeof this.routing[e.pathname]?this.routing[e.pathname].split(","):this.routing[e.pathname],void 0===e.pathname&&this.show404();try{o.forEach(t=>{this.getController(t.replace(/\s/g,"")).show().onLoad(s),this.searchParams=s,this.pathname=e.pathname,this.onPageChange(this.searchParams,this.pathname)})}catch{this.show404()}}onPageChange(){}show404(){console.error("This method is for 404 error")}redirect(t="",e=[]){try{let s=new URL(t,location.href);return Object.keys(e).forEach(t=>{s.searchParams.append(t,e[t])}),location.href=s.href,!0}catch{return console.error("Error on change page"),!1}}onClose(){}} \ No newline at end of file +class Application{constructor(t){if(this.controllers={},this.useSockets=!1,this.socketsURL=!1,this.useSession=!0,this.appStarted=!1,this.useLocalStorage=!0,this.socketConfigs={},this.viewsLoadedCount=0,this.firstLoadElementId=t.firstLoadElementId,void 0!==t.routing?this.routing=t.routing:(this.routing=[],void 0!==t.controllers&&t.controllers.forEach(t=>{this.routing[`/${t.name}`]=t.name})),void 0!==t.useSession&&(this.useSession=t.useSession),void 0!==t.useLocalStorage&&(this.useLocalStorage=t.useLocalStorage),this.useLocalStorage){let t=JSON.parse(localStorage.getItem("views"));globalThis.views=[],t&&(globalThis.views=t),window.onbeforeunload=(()=>{this.onClose(),localStorage.setItem("views",JSON.stringify(globalThis.views))})}void 0!==t.useSockets&&void 0!==t.socketsURL&&(this.useSockets=t.useSockets,this.socketsURL=t.socketsURL,void 0!==t.socketConfigs&&(this.socketConfigs=t.socketConfigs)),void 0!==t.controllers&&(this.controllers_configuration=t.controllers);try{return this.loadControllers().then(()=>{window.onpopstate=(t=>this.changePath(t.target.location.href))}).catch(t=>console.error(t)),this}catch{return!1}}setCookie(t,e,s){const o=new Date;o.setTime(o.getTime()+s);let i="expires="+o.toUTCString();document.cookie=t+"="+e+";"+i+";path=/"}deleteCookie(t){return this.setCookie(t,"",-1)}getCookie(t){let e=t+"=",s=decodeURIComponent(document.cookie).split(";");for(let t=0;te.url==t);return globalThis.views.length>0&&e?e:""}async loadControllers(){this.controllersToLoad=[],this.urlConfiguration=[],Object.keys(this.routing).includes(new URL(location.href).pathname)&&(this.urlConfiguration=this.routing[new URL(location.href).pathname],this.controllersToLoad=this.routing[new URL(location.href).pathname].controllers);let t=new URL(location.href).pathname.split("/");if(Object.keys(this.routing).forEach(e=>{if(!e.match(/{.*}+/g))return;let s=e.split("/");if(s.length==t.length&&s.length==t.length){for(let e=0;e{this.setMetaTag(t)}),this.urlConfiguration.styles&&this.urlConfiguration.styles.forEach(t=>{this.addStyle(t)})}catch(t){console.error(t)}for(let t=0;t{e.settings.app=this,e.settings.name=e.name,this.controllers[e.name]=new t.default(e.settings),this.controllers[e.name].init(),this.searchParams=this.getSearchParams()}).catch(t=>console.error(t))}}setTitle(t){return document.title=t}addStyle(t){if("string"!=typeof t)return void console.error("Incorrect style url");let e=document.createElement("link");e.rel="stylesheet",e.href=t,document.getElementsByTagName("head")[0].appendChild(e)}setMetaTag(t){let e=document.createElement("meta");for(const[s,o]of Object.entries(t))e[s]=o;document.getElementsByTagName("head")[0].appendChild(e)}viewLoaded(t){1!=this.appStarted&&(this.getController(t).showOnLoad?this.getController(t).show():this.getController(t).hide(),this.viewsLoadedCount++,this.onControllerLoaded(t),this.controllersToLoad.length===this.viewsLoadedCount&&(this.firstLoadElementId&&(document.getElementById(this.firstLoadElementId).style.display="none"),console.info("All views loaded"),this.appStarted=!0,this.startApplication()))}onControllerLoaded(t){}onStartApp(){console.info("Redefine startup function")}onSocketConnected(){console.info("Custom callback on socket connected")}onSocketDisconnected(){console.info("Custom callback on socket disconnected")}onSocketConnectionError(){console.info("Custom callback on socket connection Error")}startApplication(){this.socket=void 0,this.socketConnected=!1,console.info("Application started"),this.onStartApp(),this.useSockets&&this.socketsURL&&(this.socket=io(this.socketsURL,this.socketConfigs),this.socket.on("connect",()=>{this.useSession?(this.sessionKey=this.getCookie("sessionKey"),this.socket.emit("getSession",{sessionKey:this.sessionKey}),this.socket.on("getSession",t=>{this.session=t,t.sessionKey&&t.sessionKey!==this.sessionKey&&(null==t.liveTime&&(t.liveTime=216e6),this.setCookie("sessionKey",t.sessionKey,t.liveTime),this.sessionKey=t.sessionKey),1!=this.socketConnected&&(this.socketConnected=!0,this.onSocketConnected(),this.registerSocketRequests())})):(this.socketConnected=!0,this.onSocketConnected())}),this.socket.on("disconnect",()=>{this.socketConnected=!1,this.onSocketDisconnected()}),this.socket.on("connect_error",()=>{this.socketConnected=!1,this.onSocketConnectionError()}))}registerSocketRequests(){return Array.isArray(this.socketListeners)&&this.socketListeners.forEach(t=>{"function"==typeof t.callback&&this.socket.once(t.name,t.callback)}),Array.isArray(this.socketRequests)&&this.socketRequests.forEach(t=>{this.socket.emit(t.name,t.data)}),this}addSocketListener(t,e){return void 0===this.socketListeners&&(this.socketListeners=[]),this.socketListeners.push({name:t,callback:e}),this}addSocketRequest(t,e){return void 0===this.socketRequests&&(this.socketRequests=[]),this.socketRequests.push({name:t,data:e}),this}request(t,e,s){return"object"==typeof e&&void 0===e.sessionKey&&this.useSession&&this.sessionKey&&(e.sessionKey=this.sessionKey),this.socketConnected?(this.socket.once(t,s),this.socket.emit(t,e),this):(this.addSocketListener(t,s),this.addSocketRequest(t,e),console.info("Sockets not registered yet! Added to onConnect"),this)}getController(t){return this.controllers[t]}getControllers(){return this.controllers}changePath(t){let e;e=t instanceof URL?t:new URL(t,location.href);let s=[];e.searchParams.forEach((t,e)=>{s.push({name:e,value:t})});let o=[];o="string"==typeof this.routing[e.pathname]?this.routing[e.pathname].split(","):this.routing[e.pathname],void 0===e.pathname&&this.show404();try{o.forEach(t=>{this.getController(t.replace(/\s/g,"")).show().onLoad(s),this.searchParams=s,this.pathname=e.pathname,this.onPageChange(this.searchParams,this.pathname)})}catch{this.show404()}}onPageChange(){}show404(){console.error("This method is for 404 error")}redirect(t="",e=[]){try{let s=new URL(t,location.href);return Object.keys(e).forEach(t=>{s.searchParams.append(t,e[t])}),location.href=s.href,!0}catch{return console.error("Error on change page"),!1}}onClose(){}} \ No newline at end of file diff --git a/app/public/kernel/Application.js b/app/public/kernel/Application.js index 690a41c..8f2fd48 100644 --- a/app/public/kernel/Application.js +++ b/app/public/kernel/Application.js @@ -540,6 +540,10 @@ class Application { return this.controllers[name]; } + getControllers() { + return this.controllers; + } + /** * Given a route, this function will change the current page to the page specified by the route * @param route - The route to change to.