Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 1 | /// <reference path="../../../../typings/index.d.ts" /> |
| 2 | |
| 3 | import * as _ from 'lodash'; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 4 | import {IXosStyleConfig} from '../../../index'; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 5 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 6 | export interface IXosNavigationRoute { |
| 7 | label: string; |
| 8 | state?: string; |
| 9 | url?: string; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 10 | parent?: string; |
| 11 | children?: [IXosNavigationRoute]; |
| 12 | opened?: boolean; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | export interface IXosNavigationService { |
| 16 | query(): IXosNavigationRoute[]; |
| 17 | add(route: IXosNavigationRoute): void; |
| 18 | } |
| 19 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 20 | // TODO support 3rd level to group service model under "Services" |
| 21 | |
| 22 | export class IXosNavigationService { |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 23 | static $inject = ['$log', 'StyleConfig']; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 24 | private routes: IXosNavigationRoute[]; |
| 25 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 26 | constructor( |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 27 | private $log: ng.ILogService, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 28 | private StyleConfig: IXosStyleConfig |
| 29 | ) { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 30 | const defaultRoutes = [ |
| 31 | { |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 32 | label: 'Home', |
| 33 | state: 'xos.dashboard' |
| 34 | }, |
| 35 | { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 36 | label: 'Core', |
| 37 | state: 'xos.core' |
| 38 | }, |
Matteo Scandolo | 7517178 | 2017-03-08 14:17:01 -0800 | [diff] [blame] | 39 | { |
| 40 | label: 'Service Graph', |
| 41 | state: 'xos.fine-grained-graph' |
| 42 | }, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 43 | ]; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 44 | // adding configuration defined routes |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 45 | // this.routes = StyleConfig.routes.concat(defaultRoutes).reverse(); |
| 46 | this.routes = defaultRoutes; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 47 | this.StyleConfig.routes.forEach(r => { |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 48 | this.add(r); |
| 49 | }); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | query() { |
| 53 | return this.routes; |
| 54 | } |
| 55 | |
| 56 | add(route: IXosNavigationRoute) { |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 57 | if (angular.isDefined(route.state) && angular.isDefined(route.url)) { |
| 58 | throw new Error('[XosNavigation] You can\'t provide both state and url'); |
| 59 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 60 | |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame] | 61 | 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 Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 73 | if (angular.isDefined(route.parent)) { |
| 74 | // route parent should be a state for now |
| 75 | const parentRoute = _.find(this.routes, {state: route.parent}); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 76 | if (angular.isDefined(parentRoute)) { |
| 77 | if (angular.isArray(parentRoute.children)) { |
| 78 | parentRoute.children.push(route); |
| 79 | } |
| 80 | else { |
| 81 | parentRoute.children = [route]; |
| 82 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 83 | } |
| 84 | else { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 85 | this.$log.warn(`[XosNavigation] Parent State (${route.parent}) for state: ${route.state} does not exists`); |
| 86 | return; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | else { |
| 90 | this.routes.push(route); |
| 91 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 92 | } |
| 93 | } |