blob: d184fc21e83e29dbba5d9b71ec63cc52b7aa0a8d [file] [log] [blame]
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -08001/// <reference path="../../../../typings/index.d.ts" />
2
3import * as _ from 'lodash';
4import {StyleConfig} from '../../config/style.config';
5
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 {
21 private routes: IXosNavigationRoute[];
22
23 constructor() {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080024 const defaultRoutes = [
25 {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080026 label: 'Home',
27 state: 'xos.dashboard'
28 },
29 {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080030 label: 'Core',
31 state: 'xos.core'
32 },
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080033 ];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080034 // adding configuration defined routes
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080035 // this.routes = StyleConfig.routes.concat(defaultRoutes).reverse();
36 this.routes = defaultRoutes;
37 StyleConfig.routes.forEach(r => {
38 this.add(r);
39 });
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080040 }
41
42 query() {
43 return this.routes;
44 }
45
46 add(route: IXosNavigationRoute) {
Matteo Scandoloa4a47112016-12-16 10:06:13 -080047 if (angular.isDefined(route.state) && angular.isDefined(route.url)) {
48 throw new Error('[XosNavigation] You can\'t provide both state and url');
49 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080050
51
52 if (angular.isDefined(route.parent)) {
53 // route parent should be a state for now
54 const parentRoute = _.find(this.routes, {state: route.parent});
55
56 if (angular.isArray(parentRoute.children)) {
57 parentRoute.children.push(route);
58 }
59 else {
60 parentRoute.children = [route];
61 }
62 }
63 else {
64 this.routes.push(route);
65 }
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080066 }
67}