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 | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 21 | static $inject = ['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( |
| 25 | private StyleConfig: IXosStyleConfig |
| 26 | ) { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 27 | const defaultRoutes = [ |
| 28 | { |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 29 | label: 'Home', |
| 30 | state: 'xos.dashboard' |
| 31 | }, |
| 32 | { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 33 | label: 'Core', |
| 34 | state: 'xos.core' |
| 35 | }, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 36 | ]; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 37 | // adding configuration defined routes |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 38 | // this.routes = StyleConfig.routes.concat(defaultRoutes).reverse(); |
| 39 | this.routes = defaultRoutes; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 40 | this.StyleConfig.routes.forEach(r => { |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 41 | this.add(r); |
| 42 | }); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | query() { |
| 46 | return this.routes; |
| 47 | } |
| 48 | |
| 49 | add(route: IXosNavigationRoute) { |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 50 | if (angular.isDefined(route.state) && angular.isDefined(route.url)) { |
| 51 | throw new Error('[XosNavigation] You can\'t provide both state and url'); |
| 52 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 53 | |
| 54 | |
| 55 | if (angular.isDefined(route.parent)) { |
| 56 | // route parent should be a state for now |
| 57 | const parentRoute = _.find(this.routes, {state: route.parent}); |
| 58 | |
| 59 | if (angular.isArray(parentRoute.children)) { |
| 60 | parentRoute.children.push(route); |
| 61 | } |
| 62 | else { |
| 63 | parentRoute.children = [route]; |
| 64 | } |
| 65 | } |
| 66 | else { |
| 67 | this.routes.push(route); |
| 68 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 69 | } |
| 70 | } |