blob: 6fb688392ab939facbb5c2c2c022f3aaa985d9be [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 Scandolo265c2042017-03-20 10:15:40 -070061 // NOTE factor this out in a separate method an eventually use recursion since we can nest more routes
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080062 const routeExist = _.findIndex(this.routes, (r: IXosNavigationRoute) => {
63 if (r.label === route.label && r.state === route.state && r.parent === route.parent) {
64 return true;
65 }
Matteo Scandolo265c2042017-03-20 10:15:40 -070066 else if (_.findIndex(r.children, route) > -1) {
67 return true;
68 }
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080069 return false;
70 }) > -1;
71
72 if (routeExist) {
73 this.$log.warn(`[XosNavigation] Route with label: ${route.label}, state: ${route.state} and parent: ${route.parent} already exist`);
74 return;
75 }
76
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080077 if (angular.isDefined(route.parent)) {
78 // route parent should be a state for now
79 const parentRoute = _.find(this.routes, {state: route.parent});
Matteo Scandolo1aee1982017-02-17 08:33:23 -080080 if (angular.isDefined(parentRoute)) {
81 if (angular.isArray(parentRoute.children)) {
82 parentRoute.children.push(route);
83 }
84 else {
85 parentRoute.children = [route];
86 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080087 }
88 else {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080089 this.$log.warn(`[XosNavigation] Parent State (${route.parent}) for state: ${route.state} does not exists`);
90 return;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080091 }
92 }
93 else {
94 this.routes.push(route);
95 }
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080096 }
97}