Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 1 | import './nav.scss'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 2 | import {IXosNavigationService, IXosNavigationRoute} from '../services/navigation'; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 3 | import {IXosAuthService} from '../../datasources/rest/auth.rest'; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 4 | import {IXosStyleConfig} from '../../../index'; |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 5 | import {IXosSidePanelService} from '../side-panel/side-panel.service'; |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 6 | import {IXosComponentInjectorService} from '../services/helpers/component-injector.helpers'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 7 | |
| 8 | class NavCtrl { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame^] | 9 | static $inject = ['$scope', '$state', 'XosNavigationService', 'AuthService', 'StyleConfig', 'XosSidePanel', 'XosComponentInjector']; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 10 | public routes: IXosNavigationRoute[]; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 11 | public navSelected: string; |
| 12 | public appName: string; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 13 | public payoff: string; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 14 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 15 | constructor( |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 16 | private $scope: ng.IScope, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 17 | private $state: angular.ui.IStateService, |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 18 | private navigationService: IXosNavigationService, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 19 | private authService: IXosAuthService, |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 20 | private StyleConfig: IXosStyleConfig, |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 21 | private XosSidePanel: IXosSidePanelService, |
| 22 | private XosComponentInjector: IXosComponentInjectorService |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 23 | ) { |
Matteo Scandolo | 3b3b3b4 | 2016-12-15 17:31:53 -0800 | [diff] [blame] | 24 | // 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 Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 28 | this.routes = []; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 29 | this.$scope.$watch(() => this.navigationService.query(), (routes) => { |
| 30 | this.routes = routes; |
| 31 | }); |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 32 | this.appName = this.StyleConfig.projectName; |
| 33 | this.payoff = this.StyleConfig.payoff; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 34 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 35 | |
| 36 | activateRoute(route: IXosNavigationRoute) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 37 | 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 Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 45 | 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 Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 61 | } |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 62 | |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 63 | // NOTE remove me |
| 64 | togglePanel() { |
| 65 | this.XosSidePanel.injectComponent('xosAlert', {config: {type: 'danger'}, show: true}, 'Sample message'); |
| 66 | } |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 67 | addToDashboard() { |
| 68 | this.XosComponentInjector.injectComponent('#dashboard-component-container', 'xosAlert', {config: {type: 'danger'}, show: true}, 'Sample message', false); |
| 69 | } |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 70 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 71 | logout() { |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 72 | this.authService.logout() |
| 73 | .then(() => { |
| 74 | this.$state.go('login'); |
| 75 | }); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 76 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | export const xosNav: angular.IComponentOptions = { |
| 80 | template: require('./nav.html'), |
| 81 | controllerAs: 'vm', |
| 82 | controller: NavCtrl |
| 83 | }; |