-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.js
More file actions
94 lines (75 loc) · 2.65 KB
/
router.js
File metadata and controls
94 lines (75 loc) · 2.65 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
const getRouterLevel = (router) => {
let level = 0;
let parent = router.cparent;
while (parent.cparent) {
if (parent.type === Router) {
level += 1;
}
parent = parent.cparent;
}
return level;
}
export class Router extends lng.Component {
static navigate(path, data={}) {
this.data = data;
window.location.hash = '#' + path;
}
static back(data={}) {
this.data = data;
window.history.back();
}
getRoutes() {
return this.children.map(ch => ch.ref)
}
_init() {
/* This will clear hash when reloading page */
history.replaceState(null, null, ' ');
this.patch(this.routes);
this.children.forEach((ch) => {
ch.patch({alpha: 0});
});
this.activeRoute = '';
window.addEventListener('hashchange', this._onPageLoad.bind(this));
}
_getFocused() {
return this.activeRoute ? this.tag(this.activeRoute) : this;
}
_onPageLoad(event) {
const route = window.location.hash.substr(1).split('/');
const oldRoute = event.oldURL.indexOf('#') !== -1 ? event.oldURL.substring(event.oldURL.indexOf('#') + 1).split('/') : [];
const routerIndex = getRouterLevel(this);
const routeChanged = route[routerIndex] != oldRoute[routerIndex];
if (!routeChanged) {
return
}
console.log('Route changed:', oldRoute.join('/'), '->', route.join('/'), 'data:', Router.data);
const inactive = this.children.find((ch) => ch.ref === oldRoute[routerIndex]);
const active = this.children.find((ch) => ch.ref === route[routerIndex]);
const deactivateOldRoute = () => {
if (inactive && inactive._onRouteDeactivated) {
return inactive._onRouteDeactivated(Router.data);
}
return Promise.resolve();
};
const activateNewRoute = () => {
if (active) {
Router.data = {};
this.activeRoute = route[routerIndex];
/* Call new route hook if any */
if (active._onRouteActivated) {
return active._onRouteActivated(Router.data);
}
}
return Promise.resolve();
};
const updateRouterState = () => {
this._refocus();
return Promise.resolve();
};
deactivateOldRoute()
.then(() => {if (inactive) { inactive.setSmooth('alpha', 0)}})
.then(() => {if (active) {active.setSmooth('alpha', 1)}})
.then(() => activateNewRoute())
.then(() => updateRouterState())
}
}