-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathviewmodule.ts
More file actions
312 lines (269 loc) · 8.88 KB
/
Copy pathviewmodule.ts
File metadata and controls
312 lines (269 loc) · 8.88 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Viewmodules are a way to implement several pages within one HTML document.
* The HTML document resides at a base path, and each page is identified by
* a subpath. Multiple subpaths can be managed by a single viewmodule:
*
* Base path Subpath Viewmodule
* /dir/fruits /order order
* /dir/fruits /apples details
* /dir/fruits /bananas details
*
* When the HTML document is loaded, window.location.href is used to identify
* the base path and the current subpath.
*
* A global onclick listener is registered. Clicks leading a known subpath are
* cancelled, and a corresponding viewmodule is installed instead.
*
* Viewmodules replace the contents of the HTML element with ID
* 'viewmodule-root', also known as 'viewmodule root'.
*/
import {
debug,
error,
getElementByIdOrDie
} from 'util/common';
/**
* A viewmodule.
*
* Viewmodules need to implement method install.
*/
export interface Viewmodule {
/**
* Appends viewmodule contents to the empty element root.
*
* @param root the element to populate. Root is empty.
* @param subpath current subpath
*
* @returns An Object.
* Property title contains the new title for the page.
*/
install(root: HTMLElement, subpath: string): Promise<{title: string}>;
}
/**
* A registry of subpath-to-viewmodule pairs.
*/
export class Subpaths {
/**
* Backing container.
*/
private readonly map = new Map<string, Viewmodule>();
/**
* Register a new viewmodule for given subpath.
*
* @param subpath the subpath to assign the viewmodule to
* @param viewmodule the viewmodule to register
*
* @throws {Error} if subpath is invalid or if it is already registered.
*/
register(subpath: string, viewmodule: Viewmodule): void {
// Check for subpath validity
if (!/^\/[0-9a-zA-Z_/]*$/.test(subpath)) {
throw new Error(`Subpath '${subpath}' is invalid`)
}
// Check for duplicates
if (this.map.has(subpath)) {
throw new Error(`Subpath '${subpath}' is already registered`);
}
debug('ViewmoduleManager: registered', viewmodule, 'at', subpath);
this.map.set(subpath, viewmodule);
}
get size(): number {
return this.map.size;
}
[Symbol.iterator] = this.map[Symbol.iterator].bind(this.map);
get = this.map.get.bind(this.map)
}
/**
* Viewmodule manager class.
*/
export class ViewmoduleManager {
/**
* Base path.
*/
private readonly _base: string;
/**
* A function that outputs document title for a title suggested
* by submodule.
*/
private readonly titleFunction: (sub: string) => string;
/**
* Last installed subpath or null.
*/
private _installedSubpath: string | null = null;
/**
* Mapping from subpaths to viewmodules.
*/
private readonly registry: Subpaths;
/**
* Initialize viewmodules and install viewmodule based on current location.
*
* @param subpaths subpath mapping to use. A reference to this object is
* stored, so updates to subpaths are automatically visible.
*
* @throws {Error} if subpaths is empty, or if current location does not
* correspond to any path.
*/
constructor(subpaths: Subpaths) {
this.registry = subpaths;
if (this.registry.size == 0) {
throw new Error('No subpaths provided');
}
// Determine base
const path = getCurrentPath();
let base: string | null = null;
for (const [subpath, viewmodule] of this.registry) {
if (path.endsWith(subpath)) {
const b = path.substring(0, path.length - subpath.length);
// Select shortest base
if (base === null || base.length > b.length) {
base = b;
}
}
}
this._base = base ?? error(`Path '${path}' is unknown`);
// Determine title function
// (Cheat)
this.titleFunction = (s) => `${s} | KenDB`;
// Setup listeners
document.addEventListener('click',
this.handleClickEvent.bind(this));
window.addEventListener('popstate',
this.handlePopStateEvent.bind(this));
debug('ViewmoduleManager initialized with base', this.base,
'and root', this.root);
// Initialize root
this.install();
}
private handlePopStateEvent(event: PopStateEvent): void {
this.install();
}
private handleClickEvent(event: MouseEvent): void {
// Determine destination
function findDestination(): string | null {
let element: any = event.target;
while (true) {
if (!(element instanceof HTMLElement)) {
// Weird target or click not on anchor or button
return null;
} else if (element instanceof HTMLAnchorElement) {
return element.href;
}
element = element.parentElement;
}
}
const href = findDestination();
if (href === null) {
return;
}
// Interject
if (this.go(href)) {
event.preventDefault();
}
}
/**
* Return the HTML element that is filled by viewmodules.
*/
get root(): HTMLElement {
return getElementByIdOrDie('viewmodule-root');
}
/**
* Return the base path.
*/
get base(): string {
return this._base;
}
/**
* Return the current subpath according to window.location. It may not
* correctly correspond to the installed module or even be a registered
* subpath in case the location was modified in a way that was not handled
* by ViewmoduleManager.
*
* @throws {Error} in case the subpath could not be extracted.
*/
get currentSubpath(): string {
const path = getCurrentPath();
if (path.startsWith(this.base)) {
return path.substring(this.base.length);
} else {
throw new Error(`window.location (${path}) `
+ `does not start with base path (${this.base})`);
}
}
/**
* Return the subpath for which a viewmodule was last installed, or null.
*/
get installedSubpath(): string | null {
return this._installedSubpath;
}
/**
* Install the viewmodule appropriate for the current path.
*
* Behavior is undefined when current subpath is not registered.
*/
install(): void {
// Find appropriate viewmodule
const subpath = this.currentSubpath;
const viewmodule = this.registry.get(subpath);
if (viewmodule === undefined) {
throw Error(`No viewmodule registered at subpath '${subpath}'`);
}
// Replace root
const newRoot = this.root.cloneNode(false) as HTMLElement;
this.root.replaceWith(newRoot);
// Install viewmodule
debug('ViewmoduleManager: installing ', viewmodule, 'at', subpath);
this._installedSubpath = subpath;
viewmodule.install(newRoot, subpath)
.then((wishes) => {
document.title = this.titleFunction(wishes.title);
});
}
/**
* Install the viewmodule appropriate for the current path if subpath has
* changed since last installation.
*
* Behavior is undefined when current subpath is not registered.
*/
installIfNecessary(): void {
const subpath = this.currentSubpath;
if (subpath !== this.installedSubpath) {
this.install();
}
}
/**
* Go to given subpath and install appropriate viewmodule. A history state
* is pushed if the subpath is different from current. If href is not
* a path handled by this ViewmoduleManager, no action is taken.
*
* Behavior is undefined when new subpath is not registered.
*
* @param href the location to install.
*
* @returns true iff href is a path handled by this ViewmoduleManager.
*/
go(href: string): boolean {
// Determine if destination is relevant
const hrefUrl = new URL(href, window.location.href);
const path = hrefUrl.origin + hrefUrl.pathname;
if (!path.startsWith(this.base)) {
return false;
}
const subpath = path.substring(this.base.length);
const viewmodule = this.registry.get(subpath);
if (viewmodule === undefined) {
return false;
}
// Determine if pushState is necessary
if (this.installedSubpath !== subpath) {
window.history.pushState(null, '', href);
}
this.install();
return true;
}
}
/**
* Return the current path.
*/
function getCurrentPath(): string {
return window.location.origin + window.location.pathname;
}