it-tools/mixins/tool-routes.mixin.ts

38 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-02-13 19:55:45 +01:00
import {Component, Vue} from 'nuxt-property-decorator'
2021-03-14 20:11:39 +01:00
import {ToolRouteConfig} from '~/types/ToolConfig'
2021-02-13 19:55:45 +01:00
import {capitalise} from '~/utils/string'
@Component
2021-03-14 20:11:39 +01:00
export class ToolRoutesMixin extends Vue {
2021-02-13 19:55:45 +01:00
toolRoutesFlat : ToolRouteConfig[] = []
toolRoutesSections : {[key: string]: ToolRouteConfig[]} = {}
2021-03-14 20:11:39 +01:00
async created() {
const routes = this.$router.options.routes?.filter((r1, i, a) => r1.meta?.isTool && a.findIndex(r2 => r2.path.split('/').pop() === r1.path.split('/').pop()) === i) || []
2021-03-14 20:11:39 +01:00
const flat: ToolRouteConfig[] = []
const sections: { [key: string]: ToolRouteConfig[] } = {}
2021-02-13 19:55:45 +01:00
for (const route of routes) {
if ('component' in route) {
// @ts-ignore
const component = await route.component()
2021-02-13 19:55:45 +01:00
const routeConfig = {...route, config: component.options.methods.config()} as ToolRouteConfig
2021-03-14 20:11:39 +01:00
flat.push(routeConfig)
2021-02-13 19:55:45 +01:00
const sectionKey = capitalise(route.meta.section).replace(/_/g, ' ')
2021-03-14 20:11:39 +01:00
if (!(sectionKey in sections)) {
sections[sectionKey] = []
2021-02-13 19:55:45 +01:00
}
2021-03-14 20:11:39 +01:00
sections[sectionKey].push(routeConfig)
2021-02-13 19:55:45 +01:00
}
}
2021-03-14 20:11:39 +01:00
this.toolRoutesSections = sections
this.toolRoutesFlat = flat
2021-02-13 19:55:45 +01:00
}
}