|
| 1 | +import { LitElement, TemplateResult } from 'lit'; |
| 2 | +import { customElement } from 'lit/decorators.js'; |
| 3 | +import { html, unsafeStatic } from 'lit/static-html.js'; |
| 4 | + |
| 5 | +import { GetServiceResolverEvent } from '../BrowserEvent/index.js'; |
| 6 | +import { RouteResolver } from '../Service/index.js'; |
| 7 | +import { RouteConfiguration } from '../Type/Definition/RouteConfiguration.js'; |
| 8 | + |
| 9 | +@customElement('ember-nexus-app-core-router') |
| 10 | +class Router extends LitElement { |
| 11 | + protected _routeConfiguration: RouteConfiguration | null = null; |
| 12 | + |
| 13 | + protected _routeResolver: RouteResolver | null = null; |
| 14 | + |
| 15 | + handleNewRoute(route: string): void { |
| 16 | + // eslint-disable-next-line no-console |
| 17 | + console.log(`handle new route: ${route}`); |
| 18 | + this._routeResolver |
| 19 | + ?.findRouteConfiguration(route) |
| 20 | + .then((routeConfiguration) => { |
| 21 | + if (routeConfiguration === null) { |
| 22 | + // eslint-disable-next-line no-console |
| 23 | + console.log(`unable to resolve route: ${route}`); |
| 24 | + return; |
| 25 | + } |
| 26 | + this._routeConfiguration = routeConfiguration; |
| 27 | + return; |
| 28 | + }) |
| 29 | + .catch((e) => { |
| 30 | + // eslint-disable-next-line no-console |
| 31 | + console.log('error during resolving route'); |
| 32 | + // eslint-disable-next-line no-console |
| 33 | + console.log(e); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + handlePopStateEvent(): void { |
| 38 | + // eslint-disable-next-line no-console |
| 39 | + console.log('(popstate) Location changed to: ', window.location.pathname); |
| 40 | + this.handleNewRoute(window.location.pathname); |
| 41 | + } |
| 42 | + |
| 43 | + handleLinkClickEvent(event: PointerEvent): void { |
| 44 | + const target = event.target; |
| 45 | + if (target === null) { |
| 46 | + return; |
| 47 | + } |
| 48 | + if (!(target instanceof HTMLElement)) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const newRawUrl = target.attributes.getNamedItem('href')?.value; |
| 53 | + if (newRawUrl === null || newRawUrl === undefined) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const currentUrl = window.location.origin; |
| 58 | + |
| 59 | + // Create a new URL object to resolve the relative path to an absolute URL |
| 60 | + const newAbsoluteUrl = new URL(newRawUrl as string, currentUrl); |
| 61 | + |
| 62 | + if (newAbsoluteUrl.host !== window.location.host) { |
| 63 | + // clicked link to different domain, ignoring it |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // eslint-disable-next-line no-console |
| 68 | + console.log(`new absolute url: ${newAbsoluteUrl}`); |
| 69 | + history.pushState({}, '', newAbsoluteUrl); |
| 70 | + event.preventDefault(); |
| 71 | + |
| 72 | + this.handleNewRoute(newAbsoluteUrl.pathname); |
| 73 | + } |
| 74 | + |
| 75 | + connectedCallback(): void { |
| 76 | + super.connectedCallback(); |
| 77 | + window.addEventListener('popstate', this.handlePopStateEvent.bind(this)); |
| 78 | + document.addEventListener('click', this.handleLinkClickEvent.bind(this)); |
| 79 | + |
| 80 | + const getServiceResolverEvent = new GetServiceResolverEvent(); |
| 81 | + this.dispatchEvent(getServiceResolverEvent); |
| 82 | + const serviceResolver = getServiceResolverEvent.getServiceResolver(); |
| 83 | + if (serviceResolver !== null) { |
| 84 | + const routeResolver = serviceResolver.getService<RouteResolver>(RouteResolver.identifier); |
| 85 | + if (routeResolver !== null) { |
| 86 | + // eslint-disable-next-line no-console |
| 87 | + console.log('router init complete'); |
| 88 | + this._routeResolver = routeResolver; |
| 89 | + this.handleNewRoute(window.location.pathname); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + disconnectedCallback(): void { |
| 95 | + window.removeEventListener('popstate', this.handlePopStateEvent); |
| 96 | + document.removeEventListener('click', this.handleLinkClickEvent); |
| 97 | + super.disconnectedCallback(); |
| 98 | + } |
| 99 | + |
| 100 | + protected getRouteWebComponentTag(): string | null { |
| 101 | + if (this._routeConfiguration === null) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + if (typeof this._routeConfiguration.webComponent === 'string') { |
| 105 | + return typeof this._routeConfiguration.webComponent; |
| 106 | + } |
| 107 | + // todo: support function based routes |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + render(): TemplateResult { |
| 112 | + const routeWebComponentTag = this.getRouteWebComponentTag() ?? 'ember-nexus-app-core-page-error-404'; |
| 113 | + const routeWebComponent = unsafeStatic(routeWebComponentTag); |
| 114 | + return html`<${routeWebComponent}></${routeWebComponent}>`; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +export { Router }; |
0 commit comments