This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainContent.ts
120 lines (95 loc) 路 2.5 KB
/
MainContent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { logDebug } from '../../node_modules/asdom/assembly/utils'
import { customElements, HTMLElement } from '../../node_modules/asdom/assembly/index'
import { log } from '../../node_modules/asdom/assembly/imports'
import { AspkgElement } from './AspkgElement'
import { RouteHandler, Router } from '../UrlRouter'
import './pages/Home'
import './pages/FourOhFour'
import './pages/PackageDetails'
class Route {
path: string
element: string
}
const routes: Array<Route> = [
{ path: '/', element: 'home-page' },
{ path: '/404', element: 'four-oh-four' },
{ path: '/package', element: 'package-details' },
]
class CatchAllRoute extends RouteHandler {
constructor(public content: MainContent) {
super()
}
enter(): void {
log('enter any route')
}
leave(): void {
log('leave any route')
}
}
class HomeRoute extends RouteHandler {
constructor(public content: MainContent) {
super()
}
enter(): void {
log('enter / route')
this.content.__route = routes[0]
this.content.update()
}
leave(): void {
log('leave / route')
}
}
class PackageDetailsRoute extends RouteHandler {
constructor(public content: MainContent) {
super()
}
enter(): void {
log('enter /package route')
this.content.__route = routes[2]
this.content.update()
}
leave(): void {
log('leave /package route')
}
}
export const router: Router = new Router()
class MainContent extends AspkgElement {
static observedAttributes: string[] = []
constructor() {
super()
this.useShadow = false
}
private __pageContainer: HTMLElement | null = null
// TODO the initial value should be set based on the current route.
__route: Route = routes[0]
connectedCallback(): void {
super.connectedCallback()
this.__pageContainer = this.querySelector('.page-container') as HTMLElement
router.with('/', new HomeRoute(this))
router.with('/package', new PackageDetailsRoute(this))
router.with('*', new CatchAllRoute(this))
router.start()
this.update()
}
disconnectedCallback(): void {
router.stop()
}
update(): void {
const el = this.__route.element
logDebug('update: ' + el)
this.__pageContainer!.innerHTML = `<${el}></${el}>`
}
template(): string {
const hostSelector = this.shadowRoot != null ? ':host' : this.tagName.toLowerCase()
return /*html*/ `
<div class="page-container"></div>
<style>
${hostSelector} { display: contents }
.page-container {
display: contents;
}
</style>
`
}
}
customElements.define('main-content', () => new MainContent(), MainContent.observedAttributes)