-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathBase.mjs
217 lines (187 loc) · 6.63 KB
/
Base.mjs
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
import Base from '../core/Base.mjs';
import HashHistory from '../util/HashHistory.mjs';
const
amountSlashesRegex = /\//g,
routeParamRegex = /{[^\s/]+}/g
/**
* @class Neo.controller.Base
* @extends Neo.core.Base
*/
class Controller extends Base {
static config = {
/**
* @member {String} className='Neo.controller.Base'
* @protected
*/
className: 'Neo.controller.Base',
/**
* @member {String} ntype='controller'
* @protected
*/
ntype: 'controller',
/**
* If the URL does not contain a hash value when creating this controller instance,
* neo will set this hash value for us.
* @member {String|null} defaultHash=null
*/
defaultHash: null,
/**
* @member {String|null} defaultRoute=null
*/
defaultRoute: null,
/**
* @member {Object} handleRoutes={}
*/
handleRoutes: {},
/**
* @example
* routes: {
* '/home' : 'handleHomeRoute',
* '/users/{userId}' : {handler: 'handleUserRoute', preHandler: 'preHandleUserRoute'},
* '/users/{userId}/posts/{postId}': 'handlePostRoute',
* 'default' : 'handleOtherRoutes'
* }
* @member {Object} routes_={}
*/
routes_: {}
}
/**
* @param {Object} config
*/
construct(config) {
super.construct(config);
HashHistory.on('change', this.onHashChange, this)
}
/**
* Triggered after the routes config got changed
* @param {Object} value
* @param {Object} oldValue
* @protected
*/
afterSetRoutes(value, oldValue){
let me = this,
routeKeys = Object.keys(value);
me.routes = routeKeys.sort(me.#sortRoutes).reduce((obj, key) => {
obj[key] = value[key];
return obj
}, {});
me.handleRoutes = {};
routeKeys.forEach(key => {
if (key.toLowerCase() === 'default'){
me.defaultRoute = value[key]
} else {
me.handleRoutes[key] = new RegExp(key.replace(routeParamRegex, '([\\w-.]+)')+'$')
}
})
}
/**
* @param args
*/
destroy(...args) {
HashHistory.un('change', this.onHashChange, this);
super.destroy(...args)
}
/**
*
*/
async onConstructed() {
let me = this,
{defaultHash, windowId} = me,
currentHash = HashHistory.first(windowId);
// get outside the construction chain => a related cmp & vm has to be constructed too
await me.timeout(1);
if (currentHash) {
if (currentHash.windowId === windowId) {
await me.onHashChange(currentHash, null)
}
} else {
/*
* worker.App: onLoadApplication() will push config.hash into the HashHistory with a 5ms delay.
* We only want to set a default route, in case the HashHistory is empty and there is no initial
* value that will get consumed.
*/
!Neo.config.hash && defaultHash && Neo.Main.setRoute({value: defaultHash, windowId})
}
}
/**
* Placeholder method which gets triggered when the hash inside the browser url changes
* @param {Object} value
* @param {Object} oldValue
*/
async onHashChange(value, oldValue) {
// We only want to trigger hash changes for the same browser window (SharedWorker context)
if (value.windowId !== this.windowId) {
return
}
let me = this,
counter = 0,
hasRouteBeenFound = false,
{handleRoutes, routes} = me,
routeKeys = Object.keys(handleRoutes),
routeKeysLength = routeKeys.length,
arrayParamIds, arrayParamValues, handler, key, paramObject, preHandler, responsePreHandler, result, route;
while (routeKeysLength > 0 && counter < routeKeysLength && !hasRouteBeenFound) {
key = routeKeys[counter];
handler = null;
preHandler = null;
responsePreHandler = null;
paramObject = {};
result = value.hashString.match(handleRoutes[key]);
if (result) {
arrayParamIds = key.match(routeParamRegex);
arrayParamValues = result.splice(1, result.length - 1);
if (arrayParamIds && arrayParamIds.length !== arrayParamValues.length) {
throw 'Number of IDs and number of Values do not match'
}
for (let i = 0; arrayParamIds && i < arrayParamIds.length; i++) {
paramObject[arrayParamIds[i].substring(1, arrayParamIds[i].length - 1)] = arrayParamValues[i]
}
route = routes[key];
if (Neo.isString(route)) {
handler = route;
responsePreHandler = true
} else if (Neo.isObject(route)) {
handler = route.handler;
preHandler = route.preHandler
}
hasRouteBeenFound = true
}
counter++
}
// execute
if (hasRouteBeenFound) {
if (preHandler) {
responsePreHandler = await me[preHandler]?.call(me, paramObject, value, oldValue)
} else {
responsePreHandler = true
}
if (responsePreHandler) {
await me[handler]?.call(me, paramObject, value, oldValue)
}
}
if (routeKeys.length > 0 && !hasRouteBeenFound) {
if (me.defaultRoute) {
me[me.defaultRoute]?.(value, oldValue)
} else {
me.onNoRouteFound(value, oldValue)
}
}
}
/**
* Placeholder method which gets triggered when an invalid route is called
* @param {Object} value
* @param {Object} oldValue
*/
onNoRouteFound(value, oldValue) {
}
/**
* Internal helper method to sort routes by their amount of slashes
* @param {String} route1
* @param {String} route2
* @returns {Number}
*/
#sortRoutes(route1, route2) {
return (route1.match(amountSlashesRegex) || []).length - (route2.match(amountSlashesRegex)|| []).length
}
}
export default Neo.setupClass(Controller);