Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

9 righe
9.5 KiB

  1. /*!
  2. DMXzone App Connect Routing
  3. Version: 2.0.3
  4. (c) 2021 DMXzone.com
  5. @build 2021-09-06 11:50:18
  6. */
  7. dmx.config.mapping.a="link",dmx.routing={router:"hybrid",base:"",routes:[],getRoutes:function(){return this.routes.filter(function(t){return!t.app||t.app==dmx.app.name})},getBase:function(){if(this.base)return this.base;var t=document.querySelector("base[href]");return t?t.getAttribute("href"):""},getUrlInfo:function(){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:function(t,e,i){t=t||this.getUrlInfo().path,e=e||this.getRoutes();var r=dmx.routing.getBase();r&&(t=t.replace(r,"").replace(/^\/?/,"/"));for(var n=0;n<e.length;n++){e[n].routes&&(e[n].end=!1);var s=[],o=dmx.pathToRegexp(dmx.routing.join(i&&i.path?i.path:"/",e[n].path),s,e[n]).exec(decodeURI(t));if(o)return{path:o[0],params:s.reduce(function(t,e,i){return t[e.name]=o[i+1],t},{}),url:e[n].url,routes:e[n].routes||[]}}return null},join:function(t,e){return t.replace(/\/$/,"")+"/"+e.replace(/^\//,"")}},dmx.Component("route",{initialData:{params:{},isExact:!1,isMatch:!1,loading:!1,path:"",url:""},tag:"div",attributes:{path:{type:String,default:"*"},exact:{type:Boolean,default:!1},url:{type:String,default:null}},events:{show:Event,hide:Event,error:Event,unauthorized:Event,forbidden:Event,notfound:Event},render:function(t){this.detached=[],this.template=document.createElement("div"),this.template.innerHTML=this.$node.innerHTML,this.$node.innerHTML="",this.$parse(this.template),this.keys=[],this.re=dmx.pathToRegexp(this.props.path,this.keys,{end:this.props.exact}),this.xhr=new XMLHttpRequest,this.xhr.addEventListener("load",this.onload.bind(this)),this.xhr.addEventListener("abort",this.onabort.bind(this)),this.xhr.addEventListener("error",this.onerror.bind(this)),this.xhr.addEventListener("timeout",this.ontimeout.bind(this)),this.update()},update:function(){var t=window.location.pathname;if("hybrid"!=dmx.routing.router)t=dmx.routing.getUrlInfo().path;else{var e=document.querySelector('meta[name="ac:base"]'),i=document.querySelector('meta[name="ac:route"]');e&&e.content&&(t=t.replace(e.content.replace(/\/$/,""),"")),i&&i.content&&(t=t.replace(dmx.pathToRegexp(i.content,[],{end:!1}),"").replace(/^(\/+)?/,"/"))}var r=this.re.exec(t);if(this.set("path",this.props.path),this.set("isExact",!!this.props.exact),this.set("isMatch",!!r),r){if(this.set("url",r[0]),this.set("params",this.keys.reduce(function(t,e,i){return t[e.name]=r[i+1],t},{})),this.props.url&&this.props.url==this.loaded&&this.data.loading)return;this.props.url&&this.props.url!=this.loaded?(this.$node.innerHTML="",this.children.splice(0).forEach(function(t){t.$destroy()}),this.set("loading",!0),this.loaded=this.props.url,this.xhr.abort(),this.xhr.open("GET",this.props.url),this.xhr.send()):this.template.parentNode||(this.$node.appendChild(this.template),this.evalScripts(this.$node),this.dispatchEvent("show"))}else this.xhr.abort(),this.set("url",""),this.set("params",{}),this.template.parentNode&&(this.$node.removeChild(this.template),this.dispatchEvent("hide"))},evalScripts:function(t){var e=t.querySelectorAll('script[type="text/javascript"],script:not([type])');dmx.array(e).forEach(function(t){var 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)})},onload:function(t){this.set("loading",!1),200==this.xhr.status||0==this.xhr.status?(this.template.innerHTML=this.xhr.responseText,this.bindings=[],this.$parse(this.template),this.$node.appendChild(this.template),this.evalScripts(this.$node),window.grecaptcha&&dmx.array(this.$node.querySelectorAll(".g-recaptcha")).forEach(function(t){grecaptcha.render(t)}),this.dispatchEvent("show")):401==this.xhr.status?this.dispatchEvent("unauthorized"):403==this.xhr.status?this.dispatchEvent("forbidden"):404==this.xhr.status?this.dispatchEvent("notfound"):this.dispatchEvent("error")},onabort:function(t){this.set("loading",!1)},onerror:function(t){this.set("loading",!1),this.dispatchEvent("error")},ontimeout:function(t){this.set("loading",!1),this.dispatchEvent("error")}}),dmx.Component("view",{initialData:{loading:!1,params:null},tag:"div",events:{load:Event,error:Event,unauthorized:Event,forbidden:Event,notfound:Event},render:function(t){this.xhr=new XMLHttpRequest,this.xhr.addEventListener("load",this.onload.bind(this)),this.xhr.addEventListener("abort",this.onabort.bind(this)),this.xhr.addEventListener("error",this.onerror.bind(this)),this.xhr.addEventListener("timeout",this.ontimeout.bind(this)),"hybrid"==dmx.routing.router&&(this.url=location.pathname),this.$parse()},update:function(t){if(!window.__WAPPLER__)if("hybrid"==dmx.routing.router){var e=location.pathname;if(this.url==e&&this.data.loading)return;this.url!=e&&(this.set("loading",!0),e+=((this.url=e).includes("?")?"&":"?")+"fragment=true",this.xhr.abort(),this.xhr.open("GET",e),this.xhr.setRequestHeader("accept","text/fragment+html"),this.xhr.send())}else{for(var i=dmx.routing.getUrlInfo().path,r=dmx.routing.getRoutes(),n=this.parent;n;){if(n.routes){r=n.routes;break}n=n.parent}var s=dmx.routing.match(i,r,n);s?(this.path=s.path,this.routes=s.routes,this.set("params",s.params),this.url!=s.url&&(this.children.splice(0).forEach(function(t){t.$destroy()}),this.bindings=[],this.$node.innerHTML="",this.set("loading",!0),this.url=s.url,this.xhr.abort(),this.xhr.open("GET",s.url),this.xhr.setRequestHeader("accept","text/fragment+html"),this.xhr.send())):console.warn("Route for "+i+" not found")}},evalScripts:function(t){try{var e=t.querySelectorAll('script[type="text/javascript"],script:not([type])');dmx.array(e).forEach(function(e){try{var t=document.createElement("script");t.type="text/javascript",e.src&&(t.src=e.src),e.innerHTML&&(t.innerHTML=e.innerHTML),e.onerror=function(){console.error("ERROR",arguments)},e.parentNode.replaceChild(t,e)}catch(t){console.error("Error executing script "+e.src,t)}})}catch(t){console.error("An error occurred while trying to execute scripts",t)}},onload:function(t){this.set("loading",!1),200==this.xhr.status||0==this.xhr.status?(this.children.splice(0).forEach(function(t){t.$destroy()}),this.bindings=[],this.$node.innerHTML=this.xhr.responseText,this.$parse(this.$node),this.evalScripts(this.$node),window.grecaptcha&&dmx.array(this.$node.querySelectorAll(".g-recaptcha")).forEach(function(t){grecaptcha.render(t)}),this.dispatchEvent("load")):222==this.xhr.status?location.assign(this.xhr.responseText):401==this.xhr.status?this.dispatchEvent("unauthorized"):403==this.xhr.status?this.dispatchEvent("forbidden"):404==this.xhr.status?this.dispatchEvent("notfound"):this.dispatchEvent("error")},onabort:function(t){this.set("loading",!1)},onerror:function(t){this.set("loading",!1),this.dispatchEvent("error")},ontimeout:function(t){this.set("loading",!1),this.dispatchEvent("error")}}),dmx.Component("link",{initialData:{active:!1},tag:"a",attributes:{href:{type:String,default:""},internal:{type:Boolean,default:!1}},render:function(t){dmx.BaseComponent.prototype.render.call(this,t),this.$node.addEventListener("click",this.navigate.bind(this)),this.routeLink=!1,this.update({})},update:function(t){if(t.href!=this.props.href){var e=this.props.href;if("hybrid"!=dmx.routing.router){if((e.startsWith("/")&&!e.startsWith("//")||e.startsWith("#"))&&(this.routeLink=!0),e.startsWith("./")){for(var i=this.parent;i;){if(i.routes&&i.path){e=e.replace("./",i.path);break}i=i.parent}e=e.replace("./","/"),this.routeLink=!0}e.startsWith("~/")&&(e=e.replace("~/","/"),this.routeLink=!0),"path"!=dmx.routing.router||e.startsWith("#")||(e=dmx.routing.join(dmx.routing.getBase(),e))}else{if("."==e[0]&&"/"==e[1]){for(i=this.parent;i;){if(i.routes&&i.path){e=dmx.routing.join("./",e.replace("./",i.path));break}i=i.parent}if(!(n=document.querySelector('meta[name="ac:route"]'))||!n.content)return this.$node.setAttribute("href",dmx.routing.join(dmx.routing.getBase(),e)),void(this.routeLink=!0);var r=n.content;if((s=document.querySelector('meta[name="ac:base"]'))&&s.content&&(r=s.content.replace(/\/$/,"")+r),o=dmx.pathToRegexp(r,[],{end:!1}).exec(location.pathname))return this.$node.setAttribute("href",e.replace("./",o[0].replace(/(\/+)?$/,"/"))),void(this.routeLink=!0)}if("~"==e[0]&&"/"==e[1]){var n;if(!(n=document.querySelector('meta[name="ac:route"]'))||!n.content)return this.$node.setAttribute("href",dmx.routing.join(dmx.routing.getBase(),e.slice(1))),void(this.routeLink=!0);var s,o;r=n.content;if((s=document.querySelector('meta[name="ac:base"]'))&&s.content&&(r=s.content.replace(/\/$/,"")+r),o=dmx.pathToRegexp(r,[],{end:!1}).exec(location.pathname))return this.$node.setAttribute("href",e.replace("~/",o[0].replace(/(\/+)?$/,"/"))),void(this.routeLink=!0)}"#"==e[0]?this.routeLink=!0:this.routeLink=!1}this.$node.setAttribute("href",e)}this.set("active",this.isActive())},isActive:function(){try{var t=dmx.routing.getUrlInfo();return new URL(this.$node.href).pathname==t.path}catch(t){return!1}},navigate:function(t){if((this.routeLink||this.props.internal)&&!t.ctrlKey&&0==t.button){t.preventDefault();var e=this.$node.getAttribute("href");if("hybrid"!=dmx.routing.router)window.history.pushState({title:this.$node.title||document.title},"","hash"==dmx.routing.router?"#!"+e:e);else{if("#"==e[0])return void(window.location.hash=e);window.history.pushState({title:this.$node.title||document.title},"",e),this.$node.title&&(document.title=this.$node.title)}dmx.requestUpdate();var i=document.createEvent("Event");i.initEvent("pushstate",!1,!1),window.dispatchEvent(i)}}});
  8. //# sourceMappingURL=../maps/dmxRouting.js.map