blob: e41333c80085ef9d8e164c2dfdc1375c4e008b61 [file] [log] [blame]
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -08001/// <reference path="../../../../typings/index.d.ts" />
2
3import * as _ from 'lodash';
Matteo Scandolo828d1e82017-01-17 14:49:38 -08004import {IXosStyleConfig} from '../../../index';
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -08005
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
Matteo Scandolo1aee1982017-02-17 08:33:23 -080020// TODO support 3rd level to group service model under "Services"
21
22export class IXosNavigationService {
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080023 static $inject = ['$log', 'StyleConfig'];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080024 private routes: IXosNavigationRoute[];
25
Matteo Scandolo828d1e82017-01-17 14:49:38 -080026 constructor(
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080027 private $log: ng.ILogService,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080028 private StyleConfig: IXosStyleConfig
29 ) {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080030 const defaultRoutes = [
31 {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080032 label: 'Home',
33 state: 'xos.dashboard'
34 },
35 {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080036 label: 'Core',
37 state: 'xos.core'
38 },
Matteo Scandolo75171782017-03-08 14:17:01 -080039 {
40 label: 'Service Graph',
41 state: 'xos.fine-grained-graph'
42 },
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080043 ];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080044 // adding configuration defined routes
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080045 // this.routes = StyleConfig.routes.concat(defaultRoutes).reverse();
46 this.routes = defaultRoutes;
Matteo Scandolo828d1e82017-01-17 14:49:38 -080047 this.StyleConfig.routes.forEach(r => {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080048 this.add(r);
49 });
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080050 }
51
52 query() {
53 return this.routes;
54 }
55
56 add(route: IXosNavigationRoute) {
Matteo Scandoloa4a47112016-12-16 10:06:13 -080057 if (angular.isDefined(route.state) && angular.isDefined(route.url)) {
58 throw new Error('[XosNavigation] You can\'t provide both state and url');
59 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080060
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080061 const routeExist = _.findIndex(this.routes, (r: IXosNavigationRoute) => {
62 if (r.label === route.label && r.state === route.state && r.parent === route.parent) {
63 return true;
64 }
65 return false;
66 }) > -1;
67
68 if (routeExist) {
69 this.$log.warn(`[XosNavigation] Route with label: ${route.label}, state: ${route.state} and parent: ${route.parent} already exist`);
70 return;
71 }
72
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080073 if (angular.isDefined(route.parent)) {
74 // route parent should be a state for now
75 const parentRoute = _.find(this.routes, {state: route.parent});
Matteo Scandolo1aee1982017-02-17 08:33:23 -080076 if (angular.isDefined(parentRoute)) {
77 if (angular.isArray(parentRoute.children)) {
78 parentRoute.children.push(route);
79 }
80 else {
81 parentRoute.children = [route];
82 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080083 }
84 else {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080085 this.$log.warn(`[XosNavigation] Parent State (${route.parent}) for state: ${route.state} does not exists`);
86 return;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080087 }
88 }
89 else {
90 this.routes.push(route);
91 }
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080092 }
93}