it-tools/mixins/tool-routes.mixin.ts
2021-03-15 12:49:38 +01:00

37 lines
1.2 KiB
TypeScript

import {Component, Vue} from 'nuxt-property-decorator'
import {ToolRouteConfig} from '~/types/ToolConfig'
import {capitalise} from '~/utils/string'
@Component
export class ToolRoutesMixin extends Vue {
toolRoutesFlat : ToolRouteConfig[] = []
toolRoutesSections : {[key: string]: ToolRouteConfig[]} = {}
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) || []
const flat: ToolRouteConfig[] = []
const sections: { [key: string]: ToolRouteConfig[] } = {}
for (const route of routes) {
if ('component' in route) {
// @ts-ignore
const component = await route.component()
const routeConfig = {...route, config: component.options.methods.config()} as ToolRouteConfig
flat.push(routeConfig)
const sectionKey = capitalise(route.meta.section).replace(/_/g, ' ')
if (!(sectionKey in sections)) {
sections[sectionKey] = []
}
sections[sectionKey].push(routeConfig)
}
}
this.toolRoutesSections = sections
this.toolRoutesFlat = flat
}
}