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) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 40 | const activeRoute = this.$state.current.name; |
| 41 | const separateRoutes = activeRoute.split('.'); |
| 42 | |
| 43 | if (!navSelected) { |
| 44 | navSelected = separateRoutes[1]; |
| 45 | } |
| 46 | |
| 47 | if (navId === navSelected) { |
| 48 | return false; |
| 49 | } |
| 50 | else if (this.$state.current.name.indexOf(navId) === -1 && navId === navSelected ) { |
| 51 | return false; |
| 52 | } |
| 53 | else { |
| 54 | return true; |
| 55 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 56 | } |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 57 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 58 | logout() { |
| 59 | this.authService.logout(); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 60 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | export const xosNav: angular.IComponentOptions = { |
| 64 | template: require('./nav.html'), |
| 65 | controllerAs: 'vm', |
| 66 | controller: NavCtrl |
| 67 | }; |