blob: 660c14429b723834f1b887cddc9b5eb02d6c52ac [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080019/// <reference path="../../../../typings/index.d.ts" />
20
21import * as _ from 'lodash';
Matteo Scandolo828d1e82017-01-17 14:49:38 -080022import {IXosStyleConfig} from '../../../index';
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080023
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080024export interface IXosNavigationRoute {
25 label: string;
26 state?: string;
27 url?: string;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080028 parent?: string;
29 children?: [IXosNavigationRoute];
30 opened?: boolean;
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080031}
32
33export interface IXosNavigationService {
34 query(): IXosNavigationRoute[];
35 add(route: IXosNavigationRoute): void;
36}
37
Matteo Scandolo1aee1982017-02-17 08:33:23 -080038// TODO support 3rd level to group service model under "Services"
39
40export class IXosNavigationService {
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080041 static $inject = ['$log', 'StyleConfig'];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080042 private routes: IXosNavigationRoute[];
43
Matteo Scandolo828d1e82017-01-17 14:49:38 -080044 constructor(
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080045 private $log: ng.ILogService,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080046 private StyleConfig: IXosStyleConfig
47 ) {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080048 const defaultRoutes = [
49 {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080050 label: 'Home',
51 state: 'xos.dashboard'
52 },
53 {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080054 label: 'Core',
55 state: 'xos.core'
56 },
Matteo Scandolo75171782017-03-08 14:17:01 -080057 {
58 label: 'Service Graph',
59 state: 'xos.fine-grained-graph'
60 },
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080061 ];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080062 // adding configuration defined routes
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080063 // this.routes = StyleConfig.routes.concat(defaultRoutes).reverse();
64 this.routes = defaultRoutes;
Matteo Scandolo828d1e82017-01-17 14:49:38 -080065 this.StyleConfig.routes.forEach(r => {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080066 this.add(r);
67 });
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080068 }
69
70 query() {
71 return this.routes;
72 }
73
Max Chu6c64fdc2017-09-07 17:50:58 -070074 add(route: IXosNavigationRoute, override: boolean = false) {
Matteo Scandoloa4a47112016-12-16 10:06:13 -080075 if (angular.isDefined(route.state) && angular.isDefined(route.url)) {
76 throw new Error('[XosNavigation] You can\'t provide both state and url');
77 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080078
Matteo Scandolo265c2042017-03-20 10:15:40 -070079 // NOTE factor this out in a separate method an eventually use recursion since we can nest more routes
Max Chu6c64fdc2017-09-07 17:50:58 -070080 let preExisting = null;
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080081 const routeExist = _.findIndex(this.routes, (r: IXosNavigationRoute) => {
Max Chu6c64fdc2017-09-07 17:50:58 -070082 if (r.label === route.label && (r.state === route.state || override) && r.parent === route.parent) {
83 preExisting = r;
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080084 return true;
85 }
Matteo Scandolo265c2042017-03-20 10:15:40 -070086 else if (_.findIndex(r.children, route) > -1) {
Max Chu6c64fdc2017-09-07 17:50:58 -070087 preExisting = r;
Matteo Scandolo265c2042017-03-20 10:15:40 -070088 return true;
89 }
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080090 return false;
91 }) > -1;
92
Max Chu6c64fdc2017-09-07 17:50:58 -070093 if (routeExist && !override) {
94 this.$log.warn(`[XosNavigation] Route with label: ${route.label}, state: ${route.state} and parent: ${route.parent} already exists`);
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080095 return;
96 }
97
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080098 if (angular.isDefined(route.parent)) {
99 // route parent should be a state for now
100 const parentRoute = _.find(this.routes, {state: route.parent});
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800101 if (angular.isDefined(parentRoute)) {
102 if (angular.isArray(parentRoute.children)) {
Max Chu6c64fdc2017-09-07 17:50:58 -0700103 if (override) {
104 _.remove(parentRoute.children, r => r === preExisting);
105 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800106 parentRoute.children.push(route);
107 }
108 else {
109 parentRoute.children = [route];
110 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800111 }
112 else {
Max Chu6c64fdc2017-09-07 17:50:58 -0700113 this.$log.warn(`[XosNavigation] Parent State (${route.parent}) for state: ${route.state} does not exist`);
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800114 return;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800115 }
116 }
117 else {
Max Chu6c64fdc2017-09-07 17:50:58 -0700118 if (override) {
119 _.remove(this.routes, r => r === preExisting);
120 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800121 this.routes.push(route);
122 }
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800123 }
124}