blob: e35c9f8fa372548b9189a11658dad5f4d72d0369 [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 Scandoloa6487ce2017-02-06 16:42:21 -080021 static $inject = ['$log', 'StyleConfig'];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080022 private routes: IXosNavigationRoute[];
23
Matteo Scandolo828d1e82017-01-17 14:49:38 -080024 constructor(
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080025 private $log: ng.ILogService,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080026 private StyleConfig: IXosStyleConfig
27 ) {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080028 const defaultRoutes = [
29 {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080030 label: 'Home',
31 state: 'xos.dashboard'
32 },
33 {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080034 label: 'Core',
35 state: 'xos.core'
36 },
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080037 ];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080038 // adding configuration defined routes
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080039 // this.routes = StyleConfig.routes.concat(defaultRoutes).reverse();
40 this.routes = defaultRoutes;
Matteo Scandolo828d1e82017-01-17 14:49:38 -080041 this.StyleConfig.routes.forEach(r => {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080042 this.add(r);
43 });
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080044 }
45
46 query() {
47 return this.routes;
48 }
49
50 add(route: IXosNavigationRoute) {
Matteo Scandoloa4a47112016-12-16 10:06:13 -080051 if (angular.isDefined(route.state) && angular.isDefined(route.url)) {
52 throw new Error('[XosNavigation] You can\'t provide both state and url');
53 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080054
Matteo Scandoloa6487ce2017-02-06 16:42:21 -080055 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 Scandoloe0d71ea2016-12-19 11:56:12 -080067
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 Scandolof2c3ed62016-12-15 14:32:50 -080082 }
83}