blob: 08ad48a9ce710490302e6b39ca11be50e70d6c04 [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 Scandolo4222a432017-01-23 12:18:40 -08009 static $inject = ['$scope', '$state', 'NavigationService', '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 Scandolo3b3b3b42016-12-15 17:31:53 -080024 // NOTE we'll need to have:
25 // - Base routes (defined from configuration based on BRAND)
26 // - Autogenerated routes (nested somewhere)
27 // - Service Routes (dynamically added)
Matteo Scandoloee655a12016-12-19 15:38:43 -080028 this.routes = [];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080029 this.$scope.$watch(() => this.navigationService.query(), (routes) => {
30 this.routes = routes;
31 });
Matteo Scandolo828d1e82017-01-17 14:49:38 -080032 this.appName = this.StyleConfig.projectName;
33 this.payoff = this.StyleConfig.payoff;
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080034 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080035
36 activateRoute(route: IXosNavigationRoute) {
Matteo Scandolo266907e2016-12-20 13:41:42 -080037 this.navSelected = route.state;
38 }
39
40 includes(state: string): boolean {
41 return this.$state.includes(state);
42 }
43
44 isSelected(navId: string, navSelected: string) {
Matteo Scandolo266907e2016-12-20 13:41:42 -080045 const activeRoute = this.$state.current.name;
46 const separateRoutes = activeRoute.split('.');
47
48 if (!navSelected) {
49 navSelected = separateRoutes[1];
50 }
51
52 if (navId === navSelected) {
53 return false;
54 }
55 else if (this.$state.current.name.indexOf(navId) === -1 && navId === navSelected ) {
56 return false;
57 }
58 else {
59 return true;
60 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080061 }
Matteo Scandolo266907e2016-12-20 13:41:42 -080062
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080063 // NOTE remove me
64 togglePanel() {
65 this.XosSidePanel.injectComponent('xosAlert', {config: {type: 'danger'}, show: true}, 'Sample message');
66 }
Matteo Scandolo4222a432017-01-23 12:18:40 -080067 addToDashboard() {
68 this.XosComponentInjector.injectComponent('#dashboard-component-container', 'xosAlert', {config: {type: 'danger'}, show: true}, 'Sample message', false);
69 }
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080070
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080071 logout() {
Matteo Scandolo04964232017-01-07 12:53:46 -080072 this.authService.logout()
73 .then(() => {
74 this.$state.go('login');
75 });
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080076 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080077}
78
79export const xosNav: angular.IComponentOptions = {
80 template: require('./nav.html'),
81 controllerAs: 'vm',
82 controller: NavCtrl
83};