-
Notifications
You must be signed in to change notification settings - Fork 83
/
app-router.js
2 lines (2 loc) · 7.81 KB
/
app-router.js
1
2
// @license Copyright (C) 2015 Erik Ringsmuth - MIT license
!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=m.parseUrl(t.location.href,e.getAttribute("mode"));if(r.hash!==E.hash&&r.path===E.path&&r.search===E.search&&r.isHashPath===E.isHashPath)return"disabled"!==e.getAttribute("scroll-to-hash")&&g(r.hash),void(E=r);E=r;var n={path:r.path,state:t.history.state};if(a("state-change",n,e)){for(var s=e.firstElementChild;s;){if("APP-ROUTE"===s.tagName&&m.testRoute(s.getAttribute("path"),r.path,e.getAttribute("trailingSlash"),s.hasAttribute("regex")))return void i(e,s,r);s=s.nextSibling}a("not-found",n,e)}}function i(e,r,i){if(r.hasAttribute("redirect"))return void e.go(r.getAttribute("redirect"),{replace:!0});if(r!==e.activeRoute||"noop"!==r.getAttribute("onUrlChange")){var o={path:i.path,route:r,oldRoute:e.activeRoute,state:t.history.state};a("activate-route-start",o,e)&&a("activate-route-start",o,r)&&(e.loadingRoute=r,r===e.activeRoute&&"updateModel"===r.getAttribute("onUrlChange")?n(e,r,i,o):r.hasAttribute("import")?s(e,r.getAttribute("import"),r,i,o):r.hasAttribute("element")?h(e,r.getAttribute("element"),r,i,o):r.firstElementChild&&"TEMPLATE"===r.firstElementChild.tagName&&(r.isInlineTemplate=!0,u(e,r.firstElementChild,r,i,o)))}}function n(t,e,r,i){var n=l(t,e,r,i);e.hasAttribute("template")||e.isInlineTemplate?c(e.lastElementChild.templateInstance.model,n):c(e.firstElementChild,n),a("activate-route-end",i,t),a("activate-route-end",i,i.route)}function s(t,r,i,n,s){function h(){l.loaded=!0,o(t,l,r,i,n,s)}function u(e){var n={errorEvent:e,importUri:r,routeDetail:s};a("import-error",n,t),a("import-error",n,i)}var l;b.hasOwnProperty(r)?(l=b[r],l.loaded?o(t,l,r,i,n,s):(l.addEventListener("load",h),l.addEventListener("error",u))):(l=e.createElement("link"),l.setAttribute("rel","import"),l.setAttribute("href",r),l.setAttribute("async","async"),l.addEventListener("load",h),l.addEventListener("error",u),l.loaded=!1,e.head.appendChild(l),b[r]=l)}function o(t,e,a,r,i,n){if(r.importLink=e,r===t.loadingRoute)if(r.hasAttribute("template")){var s,o=r.getAttribute("template");s=o?e.import.getElementById(o):e.import.querySelector("template"),u(t,s,r,i,n)}else h(t,r.getAttribute("element")||a.split("/").slice(-1)[0].replace(".html",""),r,i,n)}function h(t,a,r,i,n){var s=e.createElement(a),o=l(t,r,i,n);c(s,o),p(t,s,i,n)}function u(t,a,r,i,n){var s;if("createInstance"in a){var o=l(t,r,i,n);s=a.createInstance(o)}else s=e.importNode(a.content,!0);p(t,s,i,n)}function l(t,e,r,i){var n=m.routeArguments(e.getAttribute("path"),r.path,r.search,e.hasAttribute("regex"),"auto"===t.getAttribute("typecast"));return(e.hasAttribute("bindRouter")||t.hasAttribute("bindRouter"))&&(n.router=t),i.model=n,a("before-data-binding",i,t),a("before-data-binding",i,i.route),i.model}function c(t,e){for(var a in e)e.hasOwnProperty(a)&&(t[a]=e[a])}function p(t,e,r,i){d(t.previousRoute),t.previousRoute=t.activeRoute,t.activeRoute=t.loadingRoute,t.loadingRoute=null,t.previousRoute&&t.previousRoute.removeAttribute("active"),t.activeRoute.setAttribute("active","active"),t.hasAttribute("core-animated-pages")&&i.route!==i.oldRoute||d(t.previousRoute),t.activeRoute.appendChild(e),t.hasAttribute("core-animated-pages")&&(t.coreAnimatedPages.selected=t.activeRoute.getAttribute("path")),r.hash&&!t.hasAttribute("core-animated-pages")&&"disabled"!==t.getAttribute("scroll-to-hash")&&g(r.hash),a("activate-route-end",i,t),a("activate-route-end",i,i.route)}function d(t){if(t){var e=t.firstChild;for(t.isInlineTemplate&&(e=t.querySelector("template").nextSibling);e;){var a=e;e=e.nextSibling,t.removeChild(a)}}}function g(t){t&&setTimeout(function(){var a;try{a=e.querySelector("html /deep/ "+t)||e.querySelector('html /deep/ [name="'+t.substring(1)+'"]')}catch(r){a=e.querySelector(t)||e.querySelector('[name="'+t.substring(1)+'"]')}a&&a.scrollIntoView&&a.scrollIntoView(!0)},0)}function v(t,e,a,r,i){var n=t[e],s=a[r];if("**"===n&&e===t.length-1)return!0;if("undefined"==typeof n||"undefined"==typeof s)return n===s;if(n===s||"*"===n||":"===n.charAt(0))return":"===n.charAt(0)&&"undefined"!=typeof i&&(i[n.substring(1)]=a[r]),v(t,e+1,a,r+1,i);if("**"===n)for(var o=r;o<a.length;o++)if(v(t,e+1,a,o,i))return!0;return!1}var m={},b={},f="ActiveXObject"in t,A=!!t.navigator.userAgent.match(/Edge/),E={},R=Object.create(HTMLElement.prototype);R.util=m,e.registerElement("app-route",{prototype:Object.create(HTMLElement.prototype)}),R.attachedCallback=function(){"manual"!==this.getAttribute("init")&&this.init()},R.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("typecast")||a.setAttribute("typecast","auto"),a.hasAttribute("scroll-to-hash")||a.setAttribute("scroll-to-hash","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(){a.previousRoute&&!a.previousRoute.hasAttribute("active")&&d(a.previousRoute)})),a.stateChangeHandler=r.bind(null,a),t.addEventListener("popstate",a.stateChangeHandler,!1),(f||A)&&t.addEventListener("hashchange",a.stateChangeHandler,!1),r(a))},R.detachedCallback=function(){t.removeEventListener("popstate",this.stateChangeHandler,!1),(f||A)&&t.removeEventListener("hashchange",this.stateChangeHandler,!1)},R.go=function(a,r){"pushstate"!==this.getAttribute("mode")&&(a="hashbang"===this.getAttribute("mode")?"#!"+a:"#"+a);var i=t.history.state;r&&r.replace===!0?t.history.replaceState(i,null,a):t.history.pushState(i,null,a);try{var n=new PopStateEvent("popstate",{bubbles:!1,cancelable:!1,state:i});"dispatchEvent_"in t?t.dispatchEvent_(n):t.dispatchEvent(n)}catch(a){var s=e.createEvent("CustomEvent");s.initCustomEvent("popstate",!1,!1,{state:i}),t.dispatchEvent(s)}},m.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&&(0===r.hash.length?r.path="/":r.path=r.hash.substring(1)),r.isHashPath)){r.hash="";var s=r.path.indexOf("#");s!==-1&&(r.hash=r.path.substring(s),r.path=r.path.substring(0,s));var o=r.path.indexOf("?");o!==-1&&(r.search=r.path.substring(o),r.path=r.path.substring(0,o))}return r},m.testRoute=function(t,e,a,r){return"ignore"===a&&("/"===e.slice(-1)&&(e=e.slice(0,-1)),"/"!==t.slice(-1)||r||(t=t.slice(0,-1))),r?m.testRegExString(t,e):t===e||"*"===t||("/"!==t.charAt(0)&&(t="/**/"+t),v(t.split("/"),1,e.split("/"),1))},m.routeArguments=function(t,e,a,r,i){var n={};r||("/"!==t.charAt(0)&&(t="/**/"+t),v(t.split("/"),1,e.split("/"),1,n));var s=a.substring(1).split("&");1===s.length&&""===s[0]&&(s=[]);for(var o=0;o<s.length;o++){var h=s[o],u=h.split("=");n[u[0]]=u.splice(1,u.length-1).join("=")}if(i)for(var l in n)n[l]=m.typecast(n[l]);return n},m.typecast=function(t){return"true"===t||"false"!==t&&(isNaN(t)||""===t||"0"===t.charAt(0)?decodeURIComponent(t):+t)},m.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:R})}(window,document);