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 | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 3 | import {StyleConfig} from '../../config/style.config'; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 4 | import {IXosAuthService} from '../../datasources/rest/auth.rest'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 5 | |
| 6 | class NavCtrl { |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 7 | static $inject = ['$scope', '$state', 'NavigationService', 'AuthService']; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 8 | public routes: IXosNavigationRoute[]; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 9 | public navSelected: string; |
| 10 | public appName: string; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 11 | public payoff: string; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 12 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 13 | constructor( |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 14 | private $scope: ng.IScope, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 15 | private $state: angular.ui.IStateService, |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 16 | private navigationService: IXosNavigationService, |
| 17 | private authService: IXosAuthService |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 18 | ) { |
Matteo Scandolo | 3b3b3b4 | 2016-12-15 17:31:53 -0800 | [diff] [blame] | 19 | // NOTE we'll need to have: |
| 20 | // - Base routes (defined from configuration based on BRAND) |
| 21 | // - Autogenerated routes (nested somewhere) |
| 22 | // - Service Routes (dynamically added) |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 23 | this.routes = []; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 24 | this.$scope.$watch(() => this.navigationService.query(), (routes) => { |
| 25 | this.routes = routes; |
| 26 | }); |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 27 | this.appName = StyleConfig.projectName; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 28 | this.payoff = StyleConfig.payoff; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 29 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 30 | |
| 31 | activateRoute(route: IXosNavigationRoute) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 32 | this.navSelected = route.state; |
| 33 | } |
| 34 | |
| 35 | includes(state: string): boolean { |
| 36 | return this.$state.includes(state); |
| 37 | } |
| 38 | |
| 39 | isSelected(navId: string, navSelected: string) { |
| 40 | |
| 41 | // TODO activate only one state |
| 42 | |
| 43 | const activeRoute = this.$state.current.name; |
| 44 | const separateRoutes = activeRoute.split('.'); |
| 45 | |
| 46 | if (!navSelected) { |
| 47 | navSelected = separateRoutes[1]; |
| 48 | } |
| 49 | |
| 50 | if (navId === navSelected) { |
| 51 | return false; |
| 52 | } |
| 53 | else if (this.$state.current.name.indexOf(navId) === -1 && navId === navSelected ) { |
| 54 | return false; |
| 55 | } |
| 56 | else { |
| 57 | return true; |
| 58 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 59 | } |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 60 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 61 | logout() { |
| 62 | this.authService.logout(); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 63 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | export const xosNav: angular.IComponentOptions = { |
| 67 | template: require('./nav.html'), |
| 68 | controllerAs: 'vm', |
| 69 | controller: NavCtrl |
| 70 | }; |