
/* Smart UI v18.0.1 (2024-01-22) 
Copyright (c) 2011-2023 jQWidgets. 
License: https://htmlelements.com/license/ */ //

Smart.Utilities.Assign("Router",class{constructor(e,t="/"){const i=this,n=document.createElement("a");if(e instanceof HTMLElement)i.view=e;else if("string"==typeof e){if(!(e=document.getElementById(e)))throw new Error("Router: Provided view does not exist in the DOM.");i.view=e}"/"!==t.charAt(t.length-1)&&(t+="/"),i.base=t,n.href=t,i._resolvedBase=n.pathname,i.cache=!1,i.routes=[],i._visited={},i._titleElement=document.querySelector("title"),i._addWindowEventListeners()}setRoutes(e){const t=[];for(let i=0;i<e.length;i++){const n=e[i];n.path&&n.template&&t.push(n)}this.routes=t}route(e,t,i=!0){const n=this;let l=n.routes.find((t=>t.path===e));if(l)l.id="";else{let t={};l=[...n.routes].find((i=>{if(i.path.indexOf("{")>=0){const n=i.path.substring(0,i.path.indexOf("{")-1);if(e.indexOf(n)>=0){let l=e.replace(n,"").replaceAll("/","").replaceAll("#","").replaceAll("?id=","");return t=JSON.parse(JSON.stringify(i)),l&&(t.id=l),t.path=n,i}}})),l=t}if(!l)return;const o=l.template,s=document.createElement("div");if(-1!==o.indexOf("/")||-1!==o.indexOf("."))new Smart.Ajax({url:o,type:"GET",dataType:"text/html"},(function(e,t){t>=200&&t<=299&&(l.id&&(e=e.replace("{id}",l.id)),s.innerHTML=e,n._templateLoadComplete(l,s))}));else{const e=document.getElementById(o);if(!o)return;const t=document.importNode(e.content,!0);s.appendChild(t),n._templateLoadComplete(l,s)}let a=n.base+l.path.slice(1);l.id&&(a+="?id="+l.id),t&&(window.history.pushState({},"",a),t.preventDefault()),i&&window.history.replaceState({},"",a)}_templateLoadComplete(e,t){const i=this,n=document.createElement("div"),l=e.style,o=e.script,s=[];let a=e.styleUrls,r=e.scriptUrls,d=!1,c=0;function p(){t.style.opacity=1,t.style.visibility="visible",i._visited[e.path]=!0,i.templateApplied&&"function"==typeof i.templateApplied&&i.templateApplied(e)}if(e.title&&i._titleElement&&(i._titleElement.innerHTML=e.title),(!i.cache||!i._visited[e.path])&&r&&Array.isArray(r)&&r.length>0&&(d=!0,c+=r.length,function e(t){const i=document.createElement("script"),l=r[t];"string"==typeof l?(i.src=l,i.type="module"):"object"==typeof l&&l.src&&(i.src=l.src,i.type=l.type||"module"),i.onload=function(){-1===s.indexOf(i)&&(s.push(i),c===s.length?p():t<r.length-1&&e(t+1))},n.appendChild(i)}(0)),o&&"string"==typeof o){const e=document.createElement("script");e.type="text/javascript",e.innerHTML=o,n.appendChild(e),d=!0}if(a&&Array.isArray(a)&&a.length>0){d=!0;for(let e=0;e<=a.length-1;e++){const t=document.createElement("link");c++,t.rel="stylesheet",t.type="text/css",t.href=a[e],t.onload=function(){-1===s.indexOf(t)&&(s.push(t),c===s.length&&p())},n.appendChild(t)}}if(l&&"string"==typeof l){const e=document.createElement("style");e.innerHTML=l,n.appendChild(e),d=!0}function h(e){e&&(this.ontransitionend=null),i.view.innerHTML="",t.classList.add("view-content"),t.style.opacity=0,t.style.visibility="hidden",t.style.transition="opacity 0.3s ease-out, visibility 0.3s ease-out",d?(i.view.appendChild(n),i.view.appendChild(t),0===c&&p()):(i.view.appendChild(t),p())}const u=i.view.querySelector(".view-content");u?(u.ontransitionend=h,u.style.opacity=0,u.style.visibility="hidden"):h()}_addWindowEventListeners(){const e=this;e._actualClickHandler=e._clickHandler.bind(this),e._actualPopstateHandler=e._popstateHandler.bind(this),document.body.addEventListener("click",e._actualClickHandler),window.addEventListener("popstate",e._actualPopstateHandler)}_clickHandler(e){const t=e.target.closest("a");if(!t)return;const i=this;let n=t.pathname;t.origin===window.location.origin&&(n!==window.location.pathname||""===t.hash&&"#"!==t.href.charAt(t.href.length-1))&&0===n.indexOf(i._resolvedBase)&&(t.search&&(n+=t.search),i.route(i._resolvePath(n),e,!1))}_resolvePath(e){return-1!==(e=e.split("/"))[e.length-1].indexOf(".")&&(e.pop(),e.push("")),e="/"+(e=e.join("/")).replace(this._resolvedBase,"")}_popstateHandler(e){null!==e.state&&this._routeToCurrentLocation(!1)}_routeToCurrentLocation(e){this.route(this._resolvePath(window.location.pathname+window.location.search),void 0,e)}_removeWindowEventListeners(){document.body.removeEventListener("click",this._actualClickHandler),window.removeEventListener("popstate",this._actualPopstateHandler)}}),Smart.Router=Smart.Utilities.Router;