Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 19 | import './nav.scss'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 20 | import {IXosNavigationService, IXosNavigationRoute} from '../services/navigation'; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 21 | import {IXosAuthService} from '../../datasources/rest/auth.rest'; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 22 | import {IXosStyleConfig} from '../../../index'; |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 23 | import {IXosSidePanelService} from '../side-panel/side-panel.service'; |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 24 | import {IXosComponentInjectorService} from '../services/helpers/component-injector.helpers'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 25 | |
| 26 | class NavCtrl { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 27 | static $inject = ['$scope', '$state', 'XosNavigationService', 'AuthService', 'StyleConfig', 'XosSidePanel', 'XosComponentInjector']; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 28 | public routes: IXosNavigationRoute[]; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 29 | public navSelected: string; |
| 30 | public appName: string; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 31 | public payoff: string; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 32 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 33 | constructor( |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 34 | private $scope: ng.IScope, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 35 | private $state: angular.ui.IStateService, |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 36 | private navigationService: IXosNavigationService, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 37 | private authService: IXosAuthService, |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 38 | private StyleConfig: IXosStyleConfig, |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 39 | private XosSidePanel: IXosSidePanelService, |
| 40 | private XosComponentInjector: IXosComponentInjectorService |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 41 | ) { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 42 | this.routes = []; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 43 | this.$scope.$watch(() => this.navigationService.query(), (routes) => { |
| 44 | this.routes = routes; |
| 45 | }); |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 46 | this.appName = this.StyleConfig.projectName; |
| 47 | this.payoff = this.StyleConfig.payoff; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 48 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 49 | |
| 50 | activateRoute(route: IXosNavigationRoute) { |
Matteo Scandolo | 9ed0c90 | 2017-03-27 18:43:48 -0700 | [diff] [blame] | 51 | if (this.navSelected === route.state) { |
| 52 | delete this.navSelected; |
| 53 | return; |
| 54 | } |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 55 | this.navSelected = route.state; |
| 56 | } |
| 57 | |
| 58 | includes(state: string): boolean { |
| 59 | return this.$state.includes(state); |
| 60 | } |
| 61 | |
| 62 | isSelected(navId: string, navSelected: string) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 63 | const activeRoute = this.$state.current.name; |
| 64 | const separateRoutes = activeRoute.split('.'); |
| 65 | |
| 66 | if (!navSelected) { |
| 67 | navSelected = separateRoutes[1]; |
| 68 | } |
| 69 | |
| 70 | if (navId === navSelected) { |
| 71 | return false; |
| 72 | } |
| 73 | else if (this.$state.current.name.indexOf(navId) === -1 && navId === navSelected ) { |
| 74 | return false; |
| 75 | } |
| 76 | else { |
| 77 | return true; |
| 78 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 79 | } |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 80 | |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 81 | // NOTE remove me |
| 82 | togglePanel() { |
| 83 | this.XosSidePanel.injectComponent('xosAlert', {config: {type: 'danger'}, show: true}, 'Sample message'); |
| 84 | } |
Matteo Scandolo | 4222a43 | 2017-01-23 12:18:40 -0800 | [diff] [blame] | 85 | addToDashboard() { |
| 86 | this.XosComponentInjector.injectComponent('#dashboard-component-container', 'xosAlert', {config: {type: 'danger'}, show: true}, 'Sample message', false); |
| 87 | } |
Matteo Scandolo | 9d7940c | 2017-01-19 18:28:43 -0800 | [diff] [blame] | 88 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 89 | logout() { |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 90 | this.authService.logout() |
| 91 | .then(() => { |
| 92 | this.$state.go('login'); |
| 93 | }); |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 94 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | export const xosNav: angular.IComponentOptions = { |
| 98 | template: require('./nav.html'), |
| 99 | controllerAs: 'vm', |
| 100 | controller: NavCtrl |
| 101 | }; |