blob: c8408787e045cbf54165b93ce1b6d505632517fe [file] [log] [blame]
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -08001/// <reference path="../../../../typings/index.d.ts" />
2
3import * as _ from 'lodash';
4import {StyleConfig} from '../../config/style.config';
5
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08006export interface IXosNavigationRoute {
7 label: string;
8 state?: string;
9 url?: string;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080010 parent?: string;
11 children?: [IXosNavigationRoute];
12 opened?: boolean;
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080013}
14
15export interface IXosNavigationService {
16 query(): IXosNavigationRoute[];
17 add(route: IXosNavigationRoute): void;
18}
19
20export class NavigationService {
21 private routes: IXosNavigationRoute[];
22
23 constructor() {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080024 const defaultRoutes = [
25 {
26 label: 'Core',
27 state: 'xos.core'
28 },
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080029 {
30 label: 'Home',
31 state: 'xos.dashboard'
32 }
33 ];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080034 // adding configuration defined routes
35 this.routes = StyleConfig.routes.concat(defaultRoutes).reverse();
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080036 }
37
38 query() {
39 return this.routes;
40 }
41
42 add(route: IXosNavigationRoute) {
Matteo Scandoloa4a47112016-12-16 10:06:13 -080043 if (angular.isDefined(route.state) && angular.isDefined(route.url)) {
44 throw new Error('[XosNavigation] You can\'t provide both state and url');
45 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080046
47
48 if (angular.isDefined(route.parent)) {
49 // route parent should be a state for now
50 const parentRoute = _.find(this.routes, {state: route.parent});
51
52 if (angular.isArray(parentRoute.children)) {
53 parentRoute.children.push(route);
54 }
55 else {
56 parentRoute.children = [route];
57 }
58 }
59 else {
60 this.routes.push(route);
61 }
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080062 }
63}