Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 19 | /// <reference path="../../../../typings/index.d.ts" /> |
| 20 | |
| 21 | import * as _ from 'lodash'; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 22 | import {IXosStyleConfig} from '../../../index'; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 23 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 24 | export interface IXosNavigationRoute { |
| 25 | label: string; |
| 26 | state?: string; |
| 27 | url?: string; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 28 | parent?: string; |
| 29 | children?: [IXosNavigationRoute]; |
| 30 | opened?: boolean; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | export interface IXosNavigationService { |
| 34 | query(): IXosNavigationRoute[]; |
| 35 | add(route: IXosNavigationRoute): void; |
| 36 | } |
| 37 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 38 | // TODO support 3rd level to group service model under "Services" |
| 39 | |
| 40 | export class IXosNavigationService { |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 41 | static $inject = ['$log', 'StyleConfig']; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 42 | private routes: IXosNavigationRoute[]; |
| 43 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 44 | constructor( |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 45 | private $log: ng.ILogService, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 46 | private StyleConfig: IXosStyleConfig |
| 47 | ) { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 48 | const defaultRoutes = [ |
| 49 | { |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 50 | label: 'Home', |
| 51 | state: 'xos.dashboard' |
| 52 | }, |
| 53 | { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 54 | label: 'Core', |
| 55 | state: 'xos.core' |
| 56 | }, |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 57 | { |
| 58 | label: 'Service Graph', |
| 59 | state: 'xos.fine-grained-graph' |
| 60 | }, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 61 | ]; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 62 | // adding configuration defined routes |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 63 | // this.routes = StyleConfig.routes.concat(defaultRoutes).reverse(); |
| 64 | this.routes = defaultRoutes; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 65 | this.StyleConfig.routes.forEach(r => { |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 66 | this.add(r); |
| 67 | }); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | query() { |
| 71 | return this.routes; |
| 72 | } |
| 73 | |
Max Chu | 71b185a | 2017-09-07 17:50:58 -0700 | [diff] [blame] | 74 | add(route: IXosNavigationRoute, override: boolean = false) { |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 75 | if (angular.isDefined(route.state) && angular.isDefined(route.url)) { |
| 76 | throw new Error('[XosNavigation] You can\'t provide both state and url'); |
| 77 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 78 | |
Matteo Scandolo | 265c204 | 2017-03-20 10:15:40 -0700 | [diff] [blame] | 79 | // NOTE factor this out in a separate method an eventually use recursion since we can nest more routes |
Max Chu | 71b185a | 2017-09-07 17:50:58 -0700 | [diff] [blame] | 80 | let preExisting = null; |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 81 | const routeExist = _.findIndex(this.routes, (r: IXosNavigationRoute) => { |
Max Chu | 71b185a | 2017-09-07 17:50:58 -0700 | [diff] [blame] | 82 | if (r.label === route.label && (r.state === route.state || override) && r.parent === route.parent) { |
| 83 | preExisting = r; |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 84 | return true; |
| 85 | } |
Matteo Scandolo | 265c204 | 2017-03-20 10:15:40 -0700 | [diff] [blame] | 86 | else if (_.findIndex(r.children, route) > -1) { |
Max Chu | 71b185a | 2017-09-07 17:50:58 -0700 | [diff] [blame] | 87 | preExisting = r; |
Matteo Scandolo | 265c204 | 2017-03-20 10:15:40 -0700 | [diff] [blame] | 88 | return true; |
| 89 | } |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 90 | return false; |
| 91 | }) > -1; |
| 92 | |
Max Chu | 71b185a | 2017-09-07 17:50:58 -0700 | [diff] [blame] | 93 | if (routeExist && !override) { |
| 94 | this.$log.warn(`[XosNavigation] Route with label: ${route.label}, state: ${route.state} and parent: ${route.parent} already exists`); |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 98 | if (angular.isDefined(route.parent)) { |
| 99 | // route parent should be a state for now |
| 100 | const parentRoute = _.find(this.routes, {state: route.parent}); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 101 | if (angular.isDefined(parentRoute)) { |
| 102 | if (angular.isArray(parentRoute.children)) { |
Max Chu | 71b185a | 2017-09-07 17:50:58 -0700 | [diff] [blame] | 103 | if (override) { |
| 104 | _.remove(parentRoute.children, r => r === preExisting); |
| 105 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 106 | parentRoute.children.push(route); |
| 107 | } |
| 108 | else { |
| 109 | parentRoute.children = [route]; |
| 110 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 111 | } |
| 112 | else { |
Max Chu | 71b185a | 2017-09-07 17:50:58 -0700 | [diff] [blame] | 113 | this.$log.warn(`[XosNavigation] Parent State (${route.parent}) for state: ${route.state} does not exist`); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 114 | return; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | else { |
Max Chu | 71b185a | 2017-09-07 17:50:58 -0700 | [diff] [blame] | 118 | if (override) { |
| 119 | _.remove(this.routes, r => r === preExisting); |
| 120 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 121 | this.routes.push(route); |
| 122 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 123 | } |
| 124 | } |