| 215 | } |
| 216 | |
| 217 | class Navigation { |
| 218 | constructor( |
| 219 | public sections: () => NavigationSections, |
| 220 | public routes: RouteRecordRaw[] = [], |
| 221 | ) {} |
| 222 | |
| 223 | withSections(sections: NavigationSections): Navigation { |
| 224 | this.sections = () => sections; |
| 225 | return this; |
| 226 | } |
| 227 | |
| 228 | withRoutes(routes: RouteRecordRaw[]): Navigation { |
| 229 | this.routes = routes; |
| 230 | return this; |
| 231 | } |
| 232 | |
| 233 | install(app: App): void { |
| 234 | const { sections } = this; |
| 235 | |
| 236 | const navigation = ref<NavigationSections>(sections()); |
| 237 | const session = useSessionStore(); |
| 238 | const store = useStationStore(); |
| 239 | const userState = computed(() => ({ |
| 240 | user: store.$state.user, |
| 241 | privileges: store.$state.privileges, |
| 242 | selectedStation: session.data.selected, |
| 243 | })); |
| 244 | |
| 245 | watch( |
| 246 | () => userState.value, |
| 247 | () => { |
| 248 | const full = sections(); |
| 249 | navigation.value = { |
| 250 | main: this.retainAuthorizedNavigation(full.main), |
| 251 | }; |
| 252 | }, |
| 253 | { deep: true, immediate: true }, |
| 254 | ); |
| 255 | |
| 256 | app.config.globalProperties.$navigation = navigation; |
| 257 | } |
| 258 | |
| 259 | private retainAuthorizedNavigation = (items: NavigationItem[]): NavigationItem[] => { |
| 260 | return items.filter(item => { |
| 261 | item.items = this.retainAuthorizedNavigation(item.items || []); |
| 262 | |
| 263 | if (item.auth.type === NavigastionAuthType.Route) { |
| 264 | const route = this.findRouteByName(item.auth.route, this.routes); |
| 265 | |
| 266 | if (!route) { |
| 267 | logger.warn(`Route '${item.auth.route}' not found for navigation item`); |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | if (!route.meta) { |
| 272 | logger.warn(`Route '${item.auth.route}' has no meta with the auth configuration`); |
| 273 | return false; |
| 274 | } |
nothing calls this directly
no test coverage detected