-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathRouter.js
144 lines (135 loc) · 4.2 KB
/
Router.js
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
define(
[
'pages',
'pages/vocabulary/index', // for not found route
'querystring',
'services/AuthAPI',
'atlas-state',
'knockout',
'const',
'services/EventBus',
'datatables.net',
'utils/DatatablePaginationUtils',
'director',
],
(
pages,
vocabularyPage,
querystring,
authApi,
sharedState,
ko,
constants,
EventBus,
dataTables,
DatatablePaginationUtils
) => {
class AtlasRouter {
constructor() {
this.activeRoute = ko.observable({});
this.currentView = ko.observable('loading');
this.onLoginSubscription;
this.pages = Object.values(pages);
this.routerParams = ko.observable();
this.currentViewAccessible = ko.pureComputed(() => {
return this.currentView && (
sharedState.appInitializationStatus() !== constants.applicationStatuses.failed
&& (sharedState.appInitializationStatus() !== constants.applicationStatuses.noSourcesAvailable
|| ['ohdsi-configuration', 'source-manager'].includes(this.currentView())
));
});
this.currentView.subscribe(() => {
EventBus.errorMsg(undefined);
});
}
run() {
const routerOptions = {
notfound: () => this.handleNotFound(),
};
this.router = new Router(this.aggregateRoutes());
this.router.qs = this.qs;
this.router.configure(routerOptions);
this.router.init('/');
}
qs() {
return querystring.parse(window.location.href.split('?')[1]);
}
handleNotFound() {
this.router.setRoute(vocabularyPage.baseUrl);
}
aggregateRoutes() {
const routes = this.pages.reduce((routes, page) => {
const pageRoutes = page.buildRoutes(this);
for (let key in pageRoutes) {
pageRoutes[key].title = page.title;
}
return {
...routes,
...pageRoutes,
};
}, {});
const routesWithRefreshedToken = Object.keys(routes).reduce((accumulator, key) => {
accumulator[key] = (...args) => {
sharedState.loading(true);
if (this.onLoginSubscription) {
this.onLoginSubscription.dispose();
}
const handler = routes[key].handler.bind(null, ...args);
const title = routes[key].title;
routes[key].checkPermission()
.then(() => handler())
.catch((ex) => {
console.error(ex !== undefined ? ex : 'Permission error');
// protected route didn't pass the token check -> show white page
this.setCurrentView('white-page');
// wait until user authenticates
this.schedulePageUpdateOnLogin(handler);
})
.finally(() => {
sharedState.loading(false);
});
this.activeRoute({
handler,
isSecured: routes[key].isSecured,
title: routes[key].title,
});
};
return accumulator;
}, {});
// anyway, we should track the moment when the user exits and check permissions once again
authApi.isAuthenticated.subscribe((isAuthenticated) => {
const { isSecured, handler } = this.activeRoute();
if (!isAuthenticated && isSecured) {
this.setCurrentView('white-page');
this.schedulePageUpdateOnLogin(handler);
}
});
return routesWithRefreshedToken;
}
schedulePageUpdateOnLogin(routeHandler) {
this.onLoginSubscription = authApi.isAuthenticated.subscribe((isAuthenticated) => {
if (isAuthenticated) {
routeHandler();
this.onLoginSubscription.dispose();
}
});
}
setCurrentView(view, routerParams = false) {
if (view !== this.currentView()) {
this.currentView('loading');
}
if (routerParams !== false) {
this.routerParams(routerParams);
}
this.currentView(view);
setTimeout(() => {
$.fn.dataTable.tables().forEach(dt => {
DatatablePaginationUtils.refreshTable($(dt).dataTable().DataTable());
});
$('faceted-datatable').trigger('refresh.faceted-dt');
});
}
}
return new AtlasRouter();
}
);