blob: cad35dfe9d30223b7d4d0f36abc0ce116e993df5 [file] [log] [blame]
<!-- Copyright (C) 2014 Erik Ringsmuth - MIT license -->
<script type="text/javascript">
!function(t,e){function i(t,i,r){var n=e.createEvent("CustomEvent");return n.initCustomEvent(t,!1,!0,i),r.dispatchEvent(n)}e.registerElement("app-route",{prototype:Object.create(HTMLElement.prototype)}),e.registerElement("active-route",{prototype:Object.create(HTMLElement.prototype)});var r=Object.create(HTMLElement.prototype),n={},a="ActiveXObject"in t;r.attachedCallback=function(){"manual"!==this.getAttribute("init")&&this.init()},r.init=function(){this.isInitialized||(this.isInitialized=!0,this.activeRoute=e.createElement("app-route"),this.stateChangeHandler=this.go.bind(this),t.addEventListener("popstate",this.stateChangeHandler,!1),a&&t.addEventListener("hashchange",this.stateChangeHandler,!1),this.activeRouteContent=e.createElement("active-route"),this.appendChild(this.activeRouteContent),this.hasAttribute("shadow")&&(this.activeRouteContent=this.activeRouteContent.createShadowRoot()),this.go())},r.detachedCallback=function(){t.removeEventListener("popstate",this.stateChangeHandler,!1),a&&t.removeEventListener("hashchange",this.stateChangeHandler,!1)},r.go=function(){var e=this.parseUrlPath(t.location.href),r={path:e};if(i("state-change",r,this)){for(var n=this.querySelectorAll("app-route"),a=0;a<n.length;a++)if(this.testRoute(n[a].getAttribute("path"),e,this.getAttribute("trailingSlash"),n[a].hasAttribute("regex")))return void this.activateRoute(n[a],e);i("not-found",r,this)}},r.activateRoute=function(t,e){var r={path:e,route:t,oldRoute:this.activeRoute};if(i("activate-route-start",r,this)&&i("activate-route-start",r,t)){this.activeRoute.removeAttribute("active"),t.setAttribute("active","active"),this.activeRoute=t;var n=t.getAttribute("import"),a=t.getAttribute("path"),s=t.hasAttribute("regex"),o=t.getAttribute("element"),c=t.hasAttribute("template"),l=!c;l&&n?this.importAndActivateCustomElement(n,o,a,e,s,r):l&&!n&&o?this.activateCustomElement(o,a,e,s,r):c&&n?this.importAndActivateTemplate(n,t,r):c&&!n&&this.activateTemplate(t,r)}},r.importAndActivateCustomElement=function(t,i,r,a,s,o){if(!n.hasOwnProperty(t)){n[t]=!0;var c=e.createElement("link");c.setAttribute("rel","import"),c.setAttribute("href",t),e.head.appendChild(c)}this.activateCustomElement(i||t.split("/").slice(-1)[0].replace(".html",""),r,a,s,o)},r.activateCustomElement=function(i,r,n,a,s){var o=e.createElement(i),c=this.routeArguments(r,n,t.location.href,a);for(var l in c)c.hasOwnProperty(l)&&(o[l]=c[l]);this.activeElement(o,s)},r.importAndActivateTemplate=function(t,i,r){if(n.hasOwnProperty(t)){var a=e.querySelector('link[href="'+t+'"]');a.import?this.activeElement(e.importNode(a.import.querySelector("template").content,!0),r):a.onload=function(){i.hasAttribute("active")&&this.activeElement(e.importNode(a.import.querySelector("template").content,!0),r)}.bind(this)}else{n[t]=!0;var s=e.createElement("link");s.setAttribute("rel","import"),s.setAttribute("href",t),s.onload=function(){i.hasAttribute("active")&&this.activeElement(e.importNode(s.import.querySelector("template").content,!0),r)}.bind(this),e.head.appendChild(s)}},r.activateTemplate=function(t,i){var r=e.importNode(t.querySelector("template").content,!0);this.activeElement(r,i)},r.activeElement=function(t,e){for(;this.activeRouteContent.firstChild;)this.activeRouteContent.removeChild(this.activeRouteContent.firstChild);this.activeRouteContent.appendChild(t),i("activate-route-end",e,this),i("activate-route-end",e,e.route)},r.parseUrlPath=function(t){var e=t.split("/"),i="/"+e.splice(3,e.length-3).join("/"),r=i.split(/[\?#]/)[0],n=i.indexOf("#");if(-1!==n){var a=i.substring(n).split("?")[0];"#/"===a.substring(0,2)?r=a.substring(1):"#!/"===a.substring(0,3)&&(r=a.substring(2))}return r},r.testRoute=function(t,e,i,r){if("ignore"===i&&("/"===e.slice(-1)&&(e=e.slice(0,-1)),"/"!==t.slice(-1)||r||(t=t.slice(0,-1))),r){if("/"!==t.charAt(0))return!1;t=t.slice(1);var n="";if("/"===t.slice(-1))t=t.slice(0,-1);else{if("/i"!==t.slice(-2))return!1;t=t.slice(0,-2),n="i"}return new RegExp(t,n).test(e)}if(t===e||"*"===t)return!0;if(-1===t.indexOf("*")&&-1===t.indexOf(":"))return!1;var a=e.split("/"),s=t.split("/");if(a.length!==s.length)return!1;for(var o=0;o<s.length;o++){var c=s[o];if(c!==a[o]&&"*"!==c&&":"!==c.charAt(0))return!1}return!0},r.routeArguments=function(t,e,i,r){var n={},a=e.split("/");if(!r)for(var s=t.split("/"),o=0;o<s.length;o++){var c=s[o];":"===c.charAt(0)&&(n[c.substring(1)]=a[o])}var l=i.indexOf("?"),h="";if(-1!==l){h=i.substring(l);var u=h.indexOf("#");-1!==u&&(h=h.substring(0,u))}var v=i.indexOf("#/"),p=i.indexOf("#!/");if(-1!==v||-1!==p){var m="";m=i.substring(-1!==v?v:p),l=m.indexOf("?"),-1!==l&&(h=m.substring(l))}var d=h.substring(1).split("&");1===d.length&&""===d[0]&&(d=[]);for(var f=0;f<d.length;f++){var g=d[f],b=g.split("=");n[b[0]]=b.splice(1,b.length-1).join("=")}for(var A in n){var E=n[A];n[A]="true"===E?!0:"false"===E?!1:isNaN(E)||""===E?decodeURIComponent(E):+E}return n},e.registerElement("app-router",{prototype:r})}(window,document);
</script>