-
Notifications
You must be signed in to change notification settings - Fork 2
/
Routes.js
executable file
·287 lines (262 loc) · 6.97 KB
/
Routes.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
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
import { createLoggedinTrigger, createLoginTrigger, createNotFoundTrigger } from './triggers'
import { translateRoute } from './translateRoute'
import { createRedirect } from './createRedirect'
import { reactiveTranslate } from '../i18n/reactiveTranslate'
import { arrayToQueryParams } from '../../ui/utils/arrayToQueryParams'
/**
* Routes are static definitions of pages that the {Router} uses to navigate.
*
* A route defines the following properties:
*
* {path}
* Unique (relative) url that separates this route from other route.
* Must be a function, that resolves to a valid (relative) url string.
*
*
* {label}
* The translation id for the human readable name of this route.
* Must be a string.
*
* {triggersEnter}
* A list of functions that run on the router's on-enter trigger.
* Can be undefined or a function that resolves to an array of functions.
*
* {load}
* An async function that loads the corresponding template of the given route.
* Must be an async function.
*
* {template}
* The name of the template as defined in the Template file.
* Must be a string and exactly match the Template's name.
*
* {target}
* Optional. Defines a render target in case a route is defined for a specific area to be drawn
* by the rendering engine.
*
* {data}
* Optional. An object with arbitrary properties that can be passed to the Template and
* will occur in the Template's {instance.data}. Use this to define routing callbacks
* to keep the {Routes} definitions out of the Template files.
*
*
* @class Singleton class-like structure
*/
export const Routes = {}
// triggers for triggersEnter
// use lazy initialization within
// the triggersEnter methods
let rootLoginTrigger
let myClassesTrigger
let notFoundTrigger
// redirects for data callbacks
// use lazy initialization within
// the data callback methods
let toMyClasses
/**
* This route is triggered when the user enters the url without any further suffix.
* Based on the current logged-in state (logged-in, logged-out) the respective trigger
* will invoke a redirect to either the login page or the dashboard page.
*
* Example: http://localhost:3000
* Example: https://mysite.com
*/
Routes.root = {
path: () => '/',
label: reactiveTranslate('routes.redirecting'),
triggersEnter: () => {
if (!rootLoginTrigger) {
rootLoginTrigger = createLoginTrigger(Routes.login)
}
if (!myClassesTrigger) {
myClassesTrigger = createLoggedinTrigger(Routes.myClasses)
}
return [rootLoginTrigger, myClassesTrigger]
},
async load () {
return import('../../ui/components/loading/loading')
},
target: null,
template: 'loading',
data: null
}
/**
* Renders a default template for all pages that have not been found.
*/
Routes.notFound = {
path: () => {
const notFound = translateRoute(Routes.notFound)
return `/${notFound}`
},
label: reactiveTranslate('pages.notFound.title'),
triggersEnter: () => [],
async load () {
return import('../../ui/pages/notfound/notFound')
},
target: 'logged-out-render-target',
template: 'notFound',
data: {
next () {
return Routes.overview
}
}
}
/**
* Reroute to notFound route in case an unknown / non-maching url has been detected.
*/
Routes.fallback = {
path: () => '*',
label: reactiveTranslate('routes.redirecting'),
triggersEnter: () => {
if (!notFoundTrigger) notFoundTrigger = createNotFoundTrigger(Routes.notFound)
return [notFoundTrigger]
},
async load () {
return import('../../ui/components/loading/loading')
},
target: null,
template: 'loading',
data: null
}
/**
* The login page for authentication.
*/
Routes.login = {
path: () => {
const path = translateRoute(Routes.login)
return `/${path}`
},
label: reactiveTranslate('pages.login.title'),
triggersEnter: () => {
if (!myClassesTrigger) myClassesTrigger = createLoggedinTrigger(Routes.myClasses)
return [myClassesTrigger]
},
async load () {
return import('../../ui/pages/login/login')
},
target: 'logged-out-render-target',
template: 'login',
data: {
onSuccess: () => {
if (!toMyClasses) {
toMyClasses = createRedirect(Routes.myClasses)
}
toMyClasses()
}
}
}
/**
* The logout page for explicit unloading of data und unregistering publications.
*/
Routes.logout = {
path: () => {
const path = translateRoute(Routes.logout)
return `/${path}`
},
label: reactiveTranslate('pages.logout.title'),
triggersEnter: () => [],
async load () {
return import('../../ui/pages/logout/logout')
},
target: 'logged-out-render-target',
template: 'logout',
data: {
onSuccess: () => createRedirect(Routes.login).call()
}
}
/**
* The main overview page where all classes are listed.
*/
Routes.myClasses = {
path: () => {
const path = translateRoute(Routes.myClasses)
return `/${path}`
},
label: reactiveTranslate('pages.myClasses.title'),
triggersEnter: () => {
if (!rootLoginTrigger) rootLoginTrigger = createLoginTrigger(Routes.login)
return [rootLoginTrigger]
},
async load () {
return import('../../ui/pages/myclasses/myClasses')
},
target: null,
template: 'myClasses',
data: {
getEntryRoute () {
return Routes.class
},
getUserRoute () {
return Routes.user
}
}
}
/**
* Summary page for a single class.
*/
Routes.class = {
path: (classId = ':classId') => {
const path = translateRoute(Routes.class)
return `/${path}/${classId}`
},
label: reactiveTranslate('pages.class.title'),
triggersEnter: () => {
if (!rootLoginTrigger) rootLoginTrigger = createLoginTrigger(Routes.login)
return [rootLoginTrigger]
},
async load () {
return import('../../ui/pages/class/class')
},
parent: Routes.myClasses,
target: null,
template: 'class',
data: null
}
/**
* Summary page for a single user
*/
Routes.user = {
path: (userId = ':userId', ...queryParams) => {
const userPath = translateRoute(Routes.user)
if (!queryParams.length) {
return `/${userPath}/${userId}`
}
const queryParamsStr = arrayToQueryParams(queryParams)
return `/${userPath}/${userId}?${queryParamsStr}`
},
label: reactiveTranslate('pages.user.title'),
triggersEnter: () => {
if (!rootLoginTrigger) rootLoginTrigger = createLoginTrigger(Routes.login)
return [rootLoginTrigger]
},
async load () {
return import('../../ui/pages/user/user')
},
parent: Routes.class,
target: null,
template: 'user',
data: null
}
/**
* Legal routes need to be present for all possible sub-types:
* - imprint
* - terms
* - privacy
* - contact
*/
Routes.legal = {
path: (type = ':type') => {
const legalPath = translateRoute(Routes.legal)
const translatedType = type === ':type'
? type
: translateRoute({ template: type })
return `/${legalPath}/${translatedType}`
},
label: reactiveTranslate('legal.title'),
triggersEnter: () => [], // public
async load () {
return import('../../ui/pages/legal/legal')
},
target: null,
template: 'legal',
data: null
}