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