DockerERTFF/public/dmxAppConnect/dmxRouting/dmxRouting.js

9 lines
9.2 KiB
JavaScript

/*!
App Connect Routing
Version: 3.0.7
(c) 2025 Wappler.io
@build 2025-02-14 11:20:03
*/
dmx.config.mapping.a="link",(()=>{const t=dmx.routing;dmx.routing={router:"hybrid",base:"",routes:[],getRoutes(){return this.routes.filter((function(t){return!t.app||t.app==dmx.app.name}))},getBase(){if(this.base)return this.base;var t=document.querySelector("base[href]");return t?t.getAttribute("href"):""},getUrlInfo(){var t="hash"==this.router?new URL(window.location.hash.slice(2),window.location.origin):window.location;return{path:t.pathname||"/",query:t.search.slice(1),hash:t.hash.slice(1)}},match(t,e,r){t=t||this.getUrlInfo().path,e=e||this.getRoutes();var i=dmx.routing.getBase();i&&(t=t.replace(i,"").replace(/^\/?/,"/"));for(var n=0;n<e.length;n++){e[n].routes&&(e[n].end=!1);var s=[],o=dmx.pathToRegexp(dmx.routing.join(r&&r.path?r.path:"/",e[n].path),s,e[n]).exec(decodeURI(t));if(o)return{path:o[0],params:s.reduce((function(t,e,r){return t[e.name]=o[r+1],t}),{}),url:e[n].url,routes:e[n].routes||[]}}return null},join:(t,e)=>t.replace(/\/$/,"")+"/"+e.replace(/^\//,""),evalScripts(t){window.grecaptcha&&t.querySelectorAll(".g-recaptcha").forEach((t=>{grecaptcha.render(t)}));try{t.querySelectorAll('script[type="text/javascript"],script:not([type])').forEach((t=>{try{const e=document.createElement("script");e.type="text/javascript",t.src&&(e.src=t.src),t.innerHTML&&(e.innerHTML=t.innerHTML),t.parentNode.replaceChild(e,t)}catch(e){console.error("Error executing script "+t.src,e)}}))}catch(t){console.error("An error occurred while trying to execute scripts",t)}}},t&&Object.assign(dmx.routing,t)})(),dmx.Component("route",{initialData:{isExact:!1,isMatch:!1,loading:!1,params:{},path:"",url:""},attributes:{path:{type:String,default:"*"},exact:{type:Boolean,default:!1},url:{type:String,default:""}},events:{show:Event,hide:Event,error:Event,unauthorized:Event,forbidden:Event,notfound:Event},render:!1,init(t){this._locationHandler=this._locationHandler.bind(this),this._error=this._error.bind(this),this._cache=new Map,this._content=t.innerHTML,this._keys=[],this._re=dmx.pathToRegexp(this.props.path,this._keys,{end:this.props.exact}),this._shown=!1,t.innerHTML="",window.addEventListener("popstate",this._locationHandler),window.addEventListener("pushstate",this._locationHandler),window.addEventListener("replacestate",this._locationHandler),window.addEventListener("hashchange",this._locationHandler),this._locationHandler()},destroy(){window.removeEventListener("popstate",this._locationHandler),window.removeEventListener("pushstate",this._locationHandler),window.removeEventListener("replacestate",this._locationHandler),window.removeEventListener("hashchange",this._locationHandler)},performUpdate(t){this.set({path:this.props.path,isExact:this.props.exact})},_load(){this._hide();const t=this.props.url;if(this._cache.has(t))return this._loaded=t,this._content=this._cache.get(t),void this._show();this.set("loading",!0),this._abortController=new AbortController,fetch(t,{credentials:"same-origin",signal:this._abortController.signal}).then((t=>t.text())).then((e=>{this.set("loading",!1),this._loaded=t,this._content=e,this._cache.set(t,e),this._show()})).catch(this._error)},_show(){this._shown||(this._abortController&&this._abortController.abort(),this.$node.innerHTML=this._content,this.$parse(),dmx.routing.evalScripts(this.$node),this._shown=!0,this.dispatchEvent("show"))},_hide(){this._shown&&(this._abortController&&this._abortController.abort(),this.effects&&(this.effects.forEach((t=>t())),this.effects=null),this.$destroyChildren(),this.$node.innerHTML="",this._shown=!1,this.dispatchEvent("hide"))},_error(t){this.set("loading",!1),this.dispatchEvent("error"),dmx.debug&&console.error(t)},_locationHandler(t){let e=dmx.routing.getUrlInfo().path;if("hybrid"==dmx.routing.router){let t=document.querySelector('meta[name="ac:base"]'),r=document.querySelector('meta[name="ac:route"]');t&&t.content&&(e=e.replace(t.content.replace(/\/$/,""),"")),r&&r.content&&(e=e.replace(dmx.pathToRegexp(r.content,[],{end:!1}),"").replace(/^(\/+)?/,"/"))}const r=this._re.exec(e);if(r){if(this.set({isMatch:!0,url:r[0],params:this._keys.reduce(((t,e,i)=>(t[e.name]=r[i+1],t)),{})}),this.data.loading&&this.props.url===this._loaded)return;this.props.url&&this.props.url!==this._loaded?this._load():this._show()}else this.set({isMatch:!1,url:"",params:{}}),this._hide()}}),dmx.Component("view",{initialData:{loading:!1,params:null},events:{load:Event,error:Event,unauthorized:Event,forbidden:Event,notfound:Event},methods:{load(t,e){this._load(t,e)},reload(){this._reload()}},init(t){window.__WAPPLER__||(this._locationHandler=this._locationHandler.bind(this),window.addEventListener("popstate",this._locationHandler),window.addEventListener("pushstate",this._locationHandler),window.addEventListener("replacestate",this._locationHandler),window.addEventListener("hashchange",this._locationHandler),"hybrid"==dmx.routing.router?this._url=location.pathname:this._locationHandler())},destroy(){window.removeEventListener("popstate",this._locationHandler),window.removeEventListener("pushstate",this._locationHandler),window.removeEventListener("replacestate",this._locationHandler),window.removeEventListener("hashchange",this._locationHandler)},_reload(){this._load(this._url,!0)},_load(t,e){this._url==t&&this.data.loading||(this._url!=t||e)&&(this.set("loading",!0),this._url=t,this._abortController&&this._abortController.abort(),this._abortController=new AbortController,fetch(t+(t.includes("?")?"&":"?")+"fragment=true",{credentials:"same-origin",headers:[["Accept","text/fragment+html"]],signal:this._abortController.signal}).then((t=>{if(this.set("loading",!1),200==t.status||0==t.status)t.text().then((t=>{this._show(t),this.dispatchEvent("load"),dmx.app._parseQuery()})).catch((t=>{console.error(t),this.dispatchEvent("error")}));else if(222==t.status)t.text().then((t=>{location.assign(t)})).catch((t=>{console.error(t),this.dispatchEvent("error")}));else{const e={401:"unauthorized",403:"forbidden",404:"notfound"};this.dispatchEvent(e[t.status]||"error")}})).catch((t=>{console.error(t),this.set("loading",!1),this.dispatchEvent("error")})))},_show(t){this.effects&&(this.effects.forEach((t=>t())),this.effects=null),this.$destroyChildren(),this.$node.innerHTML=t,this.$parse(),dmx.routing.evalScripts(this.$node)},_locationHandler(t){if("hybrid"==dmx.routing.router)this._load(location.pathname);else{let t=dmx.routing.getUrlInfo().path,e=dmx.routing.getRoutes(),r=this.parent;for(;r;){if(r.routes){e=r.routes;break}r=r.parent}const i=dmx.routing.match(t,e,r);i?(this.path=i.path,this.routes=i.routes,this.set("params",i.params),this._load(i.url)):console.warn("Route for "+t+" not found")}}}),dmx.Component("link",{attributes:{internal:{type:Boolean,default:!1},href:{type:String,default:null}},init(t){this._clickHandler=this._clickHandler.bind(this),this._stateHandler=this._stateHandler.bind(this),t.addEventListener("click",this._clickHandler),this.props.href&&(this.$node.setAttribute("href",this.props.href),this._fixUrl(t.getAttribute("href"))),t.classList.contains("nav-link")&&(window.addEventListener("popstate",this._stateHandler),window.addEventListener("pushstate",this._stateHandler),window.addEventListener("replacestate",this._stateHandler),window.addEventListener("hashchange",this._stateHandler),this._stateHandler())},performUpdate(t){t.has("href")&&(this.props.href?(this.$node.setAttribute("href",this.props.href),this._fixUrl(this.props.href)):this.$node.removeAttribute("href"))},destroy(){this.$node.removeEventListener("click",this._clickHandler),window.removeEventListener("popstate",this._stateHandler),window.removeEventListener("pushstate",this._stateHandler),window.removeEventListener("replacestate",this._stateHandler),window.removeEventListener("hashchange",this._stateHandler)},_fixUrl(t){if(t.startsWith("./")){let e=this.parent;for(;e;){if(e.routes&&e.path){t=dmx.routing.join("./",t.replace("./",e.path));break}e=e.parent}let r=document.querySelector('meta[name="ac:route"]');if(r&&r.content){let e=r.content,i=document.querySelector('meta[name="ac:base"]');i&&i.content&&(e=i.content.replace(/\/$/,"")+e);let n=dmx.pathToRegexp(e,[],{end:!1}).exec(location.pathname);n&&this.$node.setAttribute("href",t.replace("./",n[0].replace(/(\/+)?$/,"/")))}else this.$node.setAttribute("href",dmx.routing.join(dmx.routing.getBase(),t));this.props.internal=!0}},_navigate(t){if(t.startsWith("#"))return void(location.hash=t);"hash"===dmx.routing.router&&(t="#!"+t);const e=this.$node.title;history.pushState({title:e||document.title},"",t),e&&(document.title=e),window.dispatchEvent(new Event("pushstate"))},_clickHandler(t){const e=this.$node.getAttribute("href");e&&(this.props.internal||e.startsWith("#"))&&!t.ctrlKey&&0===t.button&&(t.preventDefault(),this._navigate(e))},_stateHandler(){const t=this.$node,e=t.getAttribute("href");if(e&&e.startsWith("#"))return;const r=t.href==window.location.href||t.href==window.location.href.split("?")[0].split("#")[0];if(t.classList.toggle("active",r),t.classList.contains("dropdown-item")){const e=t.parentNode.querySelectorAll(".dropdown-item");t.classList.remove("active");for(let r=0;r<e.length;r++){e[r].href==window.location.href||e[r].href==window.location.href.split("?")[0].split("#")[0]?(e[r].classList.add("active"),t.classList.add("active")):e[r].classList.remove("active")}}}});
//# sourceMappingURL=dmxRouting.js.map