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 | |
| 20 | export class NavigationService { |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame^] | 21 | static $inject = ['$log', 'StyleConfig']; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 22 | private routes: IXosNavigationRoute[]; |
| 23 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 24 | constructor( |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame^] | 25 | private $log: ng.ILogService, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 26 | private StyleConfig: IXosStyleConfig |
| 27 | ) { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 28 | const defaultRoutes = [ |
| 29 | { |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 30 | label: 'Home', |
| 31 | state: 'xos.dashboard' |
| 32 | }, |
| 33 | { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 34 | label: 'Core', |
| 35 | state: 'xos.core' |
| 36 | }, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 37 | ]; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 38 | // adding configuration defined routes |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 39 | // this.routes = StyleConfig.routes.concat(defaultRoutes).reverse(); |
| 40 | this.routes = defaultRoutes; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 41 | this.StyleConfig.routes.forEach(r => { |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 42 | this.add(r); |
| 43 | }); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | query() { |
| 47 | return this.routes; |
| 48 | } |
| 49 | |
| 50 | add(route: IXosNavigationRoute) { |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 51 | if (angular.isDefined(route.state) && angular.isDefined(route.url)) { |
| 52 | throw new Error('[XosNavigation] You can\'t provide both state and url'); |
| 53 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 54 | |
Matteo Scandolo | a6487ce | 2017-02-06 16:42:21 -0800 | [diff] [blame^] | 55 | const routeExist = _.findIndex(this.routes, (r: IXosNavigationRoute) => { |
| 56 | if (r.label === route.label && r.state === route.state && r.parent === route.parent) { |
| 57 | return true; |
| 58 | } |
| 59 | return false; |
| 60 | }) > -1; |
| 61 | |
| 62 | if (routeExist) { |
| 63 | this.$log.warn(`[XosNavigation] Route with label: ${route.label}, state: ${route.state} and parent: ${route.parent} already exist`); |
| 64 | return; |
| 65 | } |
| 66 | |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 67 | |
| 68 | if (angular.isDefined(route.parent)) { |
| 69 | // route parent should be a state for now |
| 70 | const parentRoute = _.find(this.routes, {state: route.parent}); |
| 71 | |
| 72 | if (angular.isArray(parentRoute.children)) { |
| 73 | parentRoute.children.push(route); |
| 74 | } |
| 75 | else { |
| 76 | parentRoute.children = [route]; |
| 77 | } |
| 78 | } |
| 79 | else { |
| 80 | this.routes.push(route); |
| 81 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 82 | } |
| 83 | } |