blob: 31f2b4e8207c0e49044e71839bc7fe19eaf849ae [file] [log] [blame]
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -08001/// <reference path="../../../../typings/index.d.ts" />
2
3import * as _ from 'lodash';
Matteo Scandolo828d1e82017-01-17 14:49:38 -08004import {IXosStyleConfig} from '../../../index';
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -08005
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08006export interface IXosNavigationRoute {
7 label: string;
8 state?: string;
9 url?: string;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080010 parent?: string;
11 children?: [IXosNavigationRoute];
12 opened?: boolean;
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080013}
14
15export interface IXosNavigationService {
16 query(): IXosNavigationRoute[];
17 add(route: IXosNavigationRoute): void;
18}
19
20export class NavigationService {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080021 static $inject = ['StyleConfig'];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080022 private routes: IXosNavigationRoute[];
23
Matteo Scandolo828d1e82017-01-17 14:49:38 -080024 constructor(
25 private StyleConfig: IXosStyleConfig
26 ) {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080027 const defaultRoutes = [
28 {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080029 label: 'Home',
30 state: 'xos.dashboard'
31 },
32 {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080033 label: 'Core',
34 state: 'xos.core'
35 },
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080036 ];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080037 // adding configuration defined routes
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080038 // this.routes = StyleConfig.routes.concat(defaultRoutes).reverse();
39 this.routes = defaultRoutes;
Matteo Scandolo828d1e82017-01-17 14:49:38 -080040 this.StyleConfig.routes.forEach(r => {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080041 this.add(r);
42 });
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080043 }
44
45 query() {
46 return this.routes;
47 }
48
49 add(route: IXosNavigationRoute) {
Matteo Scandoloa4a47112016-12-16 10:06:13 -080050 if (angular.isDefined(route.state) && angular.isDefined(route.url)) {
51 throw new Error('[XosNavigation] You can\'t provide both state and url');
52 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080053
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 Scandolof2c3ed62016-12-15 14:32:50 -080069 }
70}