blob: 2082efb76a9d80e7e69d78cb1e0cf90ce950100d [file] [log] [blame]
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08001import './nav.scss';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08002import {IXosNavigationService, IXosNavigationRoute} from '../services/navigation';
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -08003import {IXosAuthService} from '../../datasources/rest/auth.rest';
Matteo Scandolo828d1e82017-01-17 14:49:38 -08004import {IXosStyleConfig} from '../../../index';
Matteo Scandolo9d7940c2017-01-19 18:28:43 -08005import {IXosSidePanelService} from '../side-panel/side-panel.service';
Matteo Scandolo4222a432017-01-23 12:18:40 -08006import {IXosComponentInjectorService} from '../services/helpers/component-injector.helpers';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08007
8class NavCtrl {
Matteo Scandolo1aee1982017-02-17 08:33:23 -08009 static $inject = ['$scope', '$state', 'XosNavigationService', 'AuthService', 'StyleConfig', 'XosSidePanel', 'XosComponentInjector'];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080010 public routes: IXosNavigationRoute[];
Matteo Scandolo266907e2016-12-20 13:41:42 -080011 public navSelected: string;
12 public appName: string;
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080013 public payoff: string;
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080014
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080015 constructor(
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080016 private $scope: ng.IScope,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080017 private $state: angular.ui.IStateService,
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080018 private navigationService: IXosNavigationService,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080019 private authService: IXosAuthService,
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080020 private StyleConfig: IXosStyleConfig,
Matteo Scandolo4222a432017-01-23 12:18:40 -080021 private XosSidePanel: IXosSidePanelService,
22 private XosComponentInjector: IXosComponentInjectorService
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080023 ) {
Matteo Scandoloee655a12016-12-19 15:38:43 -080024 this.routes = [];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080025 this.$scope.$watch(() => this.navigationService.query(), (routes) => {
26 this.routes = routes;
27 });
Matteo Scandolo828d1e82017-01-17 14:49:38 -080028 this.appName = this.StyleConfig.projectName;
29 this.payoff = this.StyleConfig.payoff;
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080030 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080031
32 activateRoute(route: IXosNavigationRoute) {
Matteo Scandolo266907e2016-12-20 13:41:42 -080033 this.navSelected = route.state;
34 }
35
36 includes(state: string): boolean {
37 return this.$state.includes(state);
38 }
39
40 isSelected(navId: string, navSelected: string) {
Matteo Scandolo266907e2016-12-20 13:41:42 -080041 const activeRoute = this.$state.current.name;
42 const separateRoutes = activeRoute.split('.');
43
44 if (!navSelected) {
45 navSelected = separateRoutes[1];
46 }
47
48 if (navId === navSelected) {
49 return false;
50 }
51 else if (this.$state.current.name.indexOf(navId) === -1 && navId === navSelected ) {
52 return false;
53 }
54 else {
55 return true;
56 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080057 }
Matteo Scandolo266907e2016-12-20 13:41:42 -080058
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080059 // NOTE remove me
60 togglePanel() {
61 this.XosSidePanel.injectComponent('xosAlert', {config: {type: 'danger'}, show: true}, 'Sample message');
62 }
Matteo Scandolo4222a432017-01-23 12:18:40 -080063 addToDashboard() {
64 this.XosComponentInjector.injectComponent('#dashboard-component-container', 'xosAlert', {config: {type: 'danger'}, show: true}, 'Sample message', false);
65 }
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080066
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080067 logout() {
Matteo Scandolo04964232017-01-07 12:53:46 -080068 this.authService.logout()
69 .then(() => {
70 this.$state.go('login');
71 });
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080072 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080073}
74
75export const xosNav: angular.IComponentOptions = {
76 template: require('./nav.html'),
77 controllerAs: 'vm',
78 controller: NavCtrl
79};