update TS module route

This commit is contained in:
Jérémy Munsch 2022-06-29 17:33:00 +02:00
parent 75396b99c6
commit a0b7f41bf5
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC

View file

@ -1,21 +1,22 @@
export default {
const route = {
methods: {
route(
routeName: string,
parameters: Record<string, number | string> = {}
): string | null {
let route =
Object.keys(window.__SYSTEM._routes).indexOf(routeName) !== -1
? window.__SYSTEM._routes[routeName]
const routes = route.flattenProperties(window.__SYSTEM._routes);
const routeUrl =
Object.keys(routes).indexOf(routeName) !== -1
? routes[routeName]
: null;
if (!route) {
return route;
if (!routeUrl) {
return routeUrl;
}
// copy parameters
const iParameters = Object.assign({}, parameters),
origin = window.location.origin,
hash = window.location.hash;
let pathname = route.replace(origin, ""),
let pathname = routeUrl.replace(origin, ""),
search = window.location.search;
// Replace in path name.
@ -55,4 +56,21 @@ export default {
return `${origin}${pathname}${search}${hash}`;
},
},
flattenProperties(
obj: Record<PropertyKey, any>,
parent: string | null = null,
res: Record<PropertyKey, any> = {}
): Record<PropertyKey, any> {
for (const key of Object.keys(obj)) {
const propName = parent ? parent + "." + key : key;
if (typeof obj[key] === "object") {
route.flattenProperties(obj[key], propName, res);
} else {
res[propName] = obj[key];
}
}
return res;
},
};
export default route;