forked from erikringsmuth/app-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-router.html
4 lines (4 loc) · 5.84 KB
/
app-router.html
1
2
3
4
<!-- Copyright (C) 2014 Erik Ringsmuth - MIT license -->
<script type="text/javascript">
!function(t,e){function a(t,a,r){var i=e.createEvent("CustomEvent");return i.initCustomEvent(t,!1,!0,a),r.dispatchEvent(i)}function r(e){var r=p.parseUrl(t.location.href,e.getAttribute("mode")),n={path:r.path};if(a("state-change",n,e)){for(var s=e.firstElementChild;s;){if("APP-ROUTE"===s.tagName&&p.testRoute(s.getAttribute("path"),r.path,e.getAttribute("trailingSlash"),s.hasAttribute("regex")))return i(e,s,r),void 0;s=s.nextSibling}a("not-found",n,e)}}function i(t,e,r){if(e.hasAttribute("redirect"))return t.go(e.getAttribute("redirect"),{replace:!0}),void 0;var i={path:r.path,route:e,oldRoute:t.activeRoute};a("activate-route-start",i,t)&&a("activate-route-start",i,e)&&(t.previousRoute&&t.previousRoute.transitionAnimationInProgress&&c(t.previousRoute),t.activeRoute&&t.activeRoute.removeAttribute("active"),t.previousRoute=t.activeRoute,t.activeRoute=e,t.activeRoute.setAttribute("active","active"),e.hasAttribute("import")?s(t,e.getAttribute("import"),e,r,i):e.hasAttribute("element")?u(t,e.getAttribute("element"),e,r,i):e.firstElementChild&&"TEMPLATE"===e.firstElementChild.tagName&&h(t,n(e.firstElementChild,e,r),i))}function n(t,a,r){if("createInstance"in t){var i=p.routeArguments(a.getAttribute("path"),r.path,r.search,a.hasAttribute("regex"));for(var n in i)i.hasOwnProperty(n)&&(t.templateInstance.model[n]=i[n]);return t.createInstance(t.templateInstance.model,t.bindingDelegate)}return e.importNode(t.content,!0)}function s(t,a,r,i,n){function s(){o(t,u,a,r,i,n)}var u;g.hasOwnProperty(a)?(u=e.querySelector('link[href="'+a+'"]'),u.import?s():u.addEventListener("load",s)):(g[a]=!0,u=e.createElement("link"),u.setAttribute("rel","import"),u.setAttribute("href",a),u.addEventListener("load",s),e.head.appendChild(u))}function o(t,e,a,r,i,s){r.hasAttribute("active")&&(r.hasAttribute("template")?h(t,n(e.import.querySelector("template"),r,i),s):u(t,r.getAttribute("element")||a.split("/").slice(-1)[0].replace(".html",""),r,i,s))}function u(t,a,r,i,n){var s=e.createElement(a),o=p.routeArguments(r.getAttribute("path"),i.path,i.search,r.hasAttribute("regex"));for(var u in o)o.hasOwnProperty(u)&&(s[u]=o[u]);h(t,s,n)}function h(t,e,r){t.hasAttribute("core-animated-pages")||l(t.previousRoute),t.activeRoute.appendChild(e),t.hasAttribute("core-animated-pages")&&(t.coreAnimatedPages.selected=t.activeRoute.getAttribute("path"),t.previousRoute&&(t.previousRoute.transitionAnimationInProgress=!0)),a("activate-route-end",r,t),a("activate-route-end",r,r.route)}function c(t){t&&(t.transitionAnimationInProgress=!1,l(t))}function l(t){if(t)for(var e=t.firstChild;e;){var a=e;e=e.nextSibling,"TEMPLATE"!==a.tagName&&t.removeChild(a)}}var p={},g={},d="ActiveXObject"in t,v=Object.create(HTMLElement.prototype);v.util=p,e.registerElement("app-route",{prototype:Object.create(HTMLElement.prototype)}),v.attachedCallback=function(){"manual"!==this.getAttribute("init")&&this.init()},v.init=function(){var a=this;a.isInitialized||(a.isInitialized=!0,a.hasAttribute("trailingSlash")||a.setAttribute("trailingSlash","strict"),a.hasAttribute("mode")||a.setAttribute("mode","auto"),a.hasAttribute("core-animated-pages")&&(a.createShadowRoot(),a.coreAnimatedPages=e.createElement("core-animated-pages"),a.coreAnimatedPages.appendChild(e.createElement("content")),a.coreAnimatedPages.style.position="static",a.coreAnimatedPages.setAttribute("valueattr","path"),a.coreAnimatedPages.setAttribute("transitions",a.getAttribute("transitions")),a.shadowRoot.appendChild(a.coreAnimatedPages),a.coreAnimatedPages.addEventListener("core-animated-pages-transition-end",function(){c(a.previousRoute)})),a.stateChangeHandler=r.bind(null,a),t.addEventListener("popstate",a.stateChangeHandler,!1),d&&t.addEventListener("hashchange",a.stateChangeHandler,!1),r(a))},v.detachedCallback=function(){t.removeEventListener("popstate",this.stateChangeHandler,!1),d&&t.removeEventListener("hashchange",this.stateChangeHandler,!1)},v.go=function(e,a){"pushstate"!==this.getAttribute("mode")&&(e="#"+e),a&&a.replace!==!0?t.history.pushState(null,null,e):t.history.replaceState(null,null,e),r(this)},p.parseUrl=function(t,a){var r={isHashPath:"hash"===a};if("function"==typeof URL){var i=new URL(t);r.path=i.pathname,r.hash=i.hash,r.search=i.search}else{var n=e.createElement("a");n.href=t,r.path=n.pathname,"/"!==r.path.charAt(0)&&(r.path="/"+r.path),r.hash=n.hash,r.search=n.search}if("pushstate"!==a&&("#/"===r.hash.substring(0,2)?(r.isHashPath=!0,r.path=r.hash.substring(1)):"#!/"===r.hash.substring(0,3)?(r.isHashPath=!0,r.path=r.hash.substring(2)):r.isHashPath&&(r.path=0===r.hash.length?"/":r.hash.substring(1)),r.isHashPath)){var s=r.path.indexOf("?");-1!==s&&(r.search=r.path.substring(s),r.path=r.path.substring(0,s))}return r},p.testRoute=function(t,e,a,r){if("ignore"===a&&("/"===e.slice(-1)&&(e=e.slice(0,-1)),"/"!==t.slice(-1)||r||(t=t.slice(0,-1))),r)return p.testRegExString(t,e);if(t===e||"*"===t)return!0;if(-1===t.indexOf("*")&&-1===t.indexOf(":"))return!1;var i=e.split("/"),n=t.split("/");if(i.length!==n.length)return!1;for(var s=0;s<n.length;s++){var o=n[s];if(o!==i[s]&&"*"!==o&&":"!==o.charAt(0))return!1}return!0},p.routeArguments=function(t,e,a,r){var i={};if(!r)for(var n=e.split("/"),s=t.split("/"),o=0;o<s.length;o++){var u=s[o];":"===u.charAt(0)&&(i[u.substring(1)]=n[o])}var h=a.substring(1).split("&");1===h.length&&""===h[0]&&(h=[]);for(var c=0;c<h.length;c++){var l=h[c],g=l.split("=");i[g[0]]=g.splice(1,g.length-1).join("=")}for(var d in i)i[d]=p.typecast(i[d]);return i},p.typecast=function(t){return"true"===t?!0:"false"===t?!1:isNaN(t)||""===t||"0"===t.charAt(0)?decodeURIComponent(t):+t},p.testRegExString=function(t,e){if("/"!==t.charAt(0))return!1;t=t.slice(1);var a="";if("/"===t.slice(-1))t=t.slice(0,-1);else{if("/i"!==t.slice(-2))return!1;t=t.slice(0,-2),a="i"}return new RegExp(t,a).test(e)},e.registerElement("app-router",{prototype:v})}(window,document);
</script>