blob: 8f1f83ee321f6af33c14fc2d9ca4fa667fe6d9e5 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
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 Scandolof6acdbe2016-12-13 10:29:37 -080019import './nav.scss';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080020import {IXosNavigationService, IXosNavigationRoute} from '../services/navigation';
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080021import {IXosAuthService} from '../../datasources/rest/auth.rest';
Matteo Scandolo828d1e82017-01-17 14:49:38 -080022import {IXosStyleConfig} from '../../../index';
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080023import {IXosSidePanelService} from '../side-panel/side-panel.service';
Matteo Scandolo4222a432017-01-23 12:18:40 -080024import {IXosComponentInjectorService} from '../services/helpers/component-injector.helpers';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080025
26class NavCtrl {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080027 static $inject = ['$scope', '$state', 'XosNavigationService', 'AuthService', 'StyleConfig', 'XosSidePanel', 'XosComponentInjector'];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080028 public routes: IXosNavigationRoute[];
Matteo Scandolo266907e2016-12-20 13:41:42 -080029 public navSelected: string;
30 public appName: string;
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080031 public payoff: string;
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080032
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080033 constructor(
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080034 private $scope: ng.IScope,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080035 private $state: angular.ui.IStateService,
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080036 private navigationService: IXosNavigationService,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080037 private authService: IXosAuthService,
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080038 private StyleConfig: IXosStyleConfig,
Matteo Scandolo4222a432017-01-23 12:18:40 -080039 private XosSidePanel: IXosSidePanelService,
40 private XosComponentInjector: IXosComponentInjectorService
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080041 ) {
Matteo Scandoloee655a12016-12-19 15:38:43 -080042 this.routes = [];
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080043 this.$scope.$watch(() => this.navigationService.query(), (routes) => {
44 this.routes = routes;
45 });
Matteo Scandolo828d1e82017-01-17 14:49:38 -080046 this.appName = this.StyleConfig.projectName;
47 this.payoff = this.StyleConfig.payoff;
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080048 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080049
50 activateRoute(route: IXosNavigationRoute) {
Matteo Scandolo9ed0c902017-03-27 18:43:48 -070051 if (this.navSelected === route.state) {
52 delete this.navSelected;
53 return;
54 }
Matteo Scandolo266907e2016-12-20 13:41:42 -080055 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 Scandolo266907e2016-12-20 13:41:42 -080063 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 Scandoloa8a6fbb2016-12-21 16:59:08 -080079 }
Matteo Scandolo266907e2016-12-20 13:41:42 -080080
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080081 // NOTE remove me
82 togglePanel() {
83 this.XosSidePanel.injectComponent('xosAlert', {config: {type: 'danger'}, show: true}, 'Sample message');
84 }
Matteo Scandolo4222a432017-01-23 12:18:40 -080085 addToDashboard() {
86 this.XosComponentInjector.injectComponent('#dashboard-component-container', 'xosAlert', {config: {type: 'danger'}, show: true}, 'Sample message', false);
87 }
Matteo Scandolo9d7940c2017-01-19 18:28:43 -080088
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080089 logout() {
Matteo Scandolo04964232017-01-07 12:53:46 -080090 this.authService.logout()
91 .then(() => {
92 this.$state.go('login');
93 });
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080094 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080095}
96
97export const xosNav: angular.IComponentOptions = {
98 template: require('./nav.html'),
99 controllerAs: 'vm',
100 controller: NavCtrl
101};