blob: 0e55dee3067fbb5e02b9f5367a4f521474ee0727 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001/*
2 * Copyright 2017-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080017import './header.scss';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080018import {IWSEvent} from '../../datasources/websocket/global';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080019import {IStoreService} from '../../datasources/stores/synchronizer.store';
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080020import {IXosAuthService} from '../../datasources/rest/auth.rest';
Matteo Scandolo67c105f2017-01-09 09:30:52 -080021import {IXosNavigationService, IXosNavigationRoute} from '../services/navigation';
22import {IStateService} from 'angular-ui-router';
Matteo Scandolo67c105f2017-01-09 09:30:52 -080023import * as $ from 'jquery';
Matteo Scandolo828d1e82017-01-17 14:49:38 -080024import {IXosStyleConfig} from '../../../index';
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080025import {IXosSearchService, IXosSearchResult} from '../../datasources/helpers/search.service';
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080026import {IXosKeyboardShortcutService} from '../services/keyboard-shortcut';
Matteo Scandolo31daa802017-09-01 12:19:56 -070027import {Subscription} from 'rxjs';
28import {IXosConfigHelpersService} from '../services/helpers/config.helpers';
Matteo Scandolof3717252017-10-11 15:38:54 -070029import {IXosDebugService} from '../debug/debug.service';
Matteo Scandolo57aee472018-06-28 15:24:42 -070030import {IXosVersionReaderService} from '../services/version-reader';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080031
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080032export interface INotification extends IWSEvent {
Matteo Scandoloc719e792016-12-14 15:48:31 -080033 viewed?: boolean;
34}
35
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080036class HeaderController {
Matteo Scandolo31daa802017-09-01 12:19:56 -070037 static $inject = [
38 '$log',
39 '$scope',
40 '$rootScope',
41 '$state',
42 'AuthService',
43 'SynchronizerStore',
44 'toastr',
45 'toastrConfig',
46 'XosNavigationService',
47 'StyleConfig',
48 'SearchService',
49 'XosKeyboardShortcut',
Matteo Scandolof3717252017-10-11 15:38:54 -070050 'ConfigHelpers',
Matteo Scandolo57aee472018-06-28 15:24:42 -070051 'XosDebug',
52 'XosVersionReaderService'
Matteo Scandolo31daa802017-09-01 12:19:56 -070053 ];
Matteo Scandoloc719e792016-12-14 15:48:31 -080054 public notifications: INotification[] = [];
55 public newNotifications: INotification[] = [];
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080056 public version: string;
57 public userEmail: string;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080058 public routeSelected: (route: IXosSearchResult) => void;
Matteo Scandolo67c105f2017-01-09 09:30:52 -080059 public states: IXosNavigationRoute[];
60 public query: string;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080061 public search: (query: string) => any[];
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080062
Matteo Scandolo31daa802017-09-01 12:19:56 -070063 private syncStoreSubscription: Subscription;
64
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080065 constructor(
Matteo Scandolo31daa802017-09-01 12:19:56 -070066 private $log: ng.ILogService,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080067 private $scope: angular.IScope,
Matteo Scandolofc170992017-01-12 18:20:24 -080068 private $rootScope: ng.IScope,
Matteo Scandolo67c105f2017-01-09 09:30:52 -080069 private $state: IStateService,
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080070 private authService: IXosAuthService,
Matteo Scandolo266907e2016-12-20 13:41:42 -080071 private syncStore: IStoreService,
72 private toastr: ng.toastr.IToastrService,
Matteo Scandolo67c105f2017-01-09 09:30:52 -080073 private toastrConfig: ng.toastr.IToastrConfig,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080074 private NavigationService: IXosNavigationService,
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080075 private StyleConfig: IXosStyleConfig,
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080076 private SearchService: IXosSearchService,
Matteo Scandolo31daa802017-09-01 12:19:56 -070077 private XosKeyboardShortcut: IXosKeyboardShortcutService,
Matteo Scandolof3717252017-10-11 15:38:54 -070078 private ConfigHelpers: IXosConfigHelpersService,
Matteo Scandolo57aee472018-06-28 15:24:42 -070079 private XosDebugService: IXosDebugService,
80 private XosVersionReaderService: IXosVersionReaderService
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080081 ) {
Matteo Scandolo31daa802017-09-01 12:19:56 -070082
83 }
84
85 $onInit() {
86 this.$log.info('[XosHeader] Setup');
Matteo Scandolo57aee472018-06-28 15:24:42 -070087
88 this.XosVersionReaderService.getVersion()
89 .then((version: string) => {
90 this.version = version;
91 });
92
Matteo Scandolo266907e2016-12-20 13:41:42 -080093 angular.extend(this.toastrConfig, {
94 newestOnTop: false,
95 positionClass: 'toast-top-right',
96 preventDuplicates: false,
97 preventOpenDuplicates: false,
98 progressBar: true,
Matteo Scandolo31daa802017-09-01 12:19:56 -070099 onTap: (toast) => {
100 this.$state.go(toast.scope.extraData.dest.name, toast.scope.extraData.dest.params);
101 }
Matteo Scandolo266907e2016-12-20 13:41:42 -0800102 });
103
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800104 this.search = (query: string) => {
105 return this.SearchService.search(query);
106 };
Matteo Scandolo67c105f2017-01-09 09:30:52 -0800107
108 // listen for keypress
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800109 this.XosKeyboardShortcut.registerKeyBinding({
Matteo Scandoloc8178492017-04-11 17:55:13 -0700110 key: 'F',
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800111 description: 'Select search box',
112 cb: () => {
Matteo Scandolo67c105f2017-01-09 09:30:52 -0800113 $('.navbar-form input').focus();
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800114 },
115 }, 'global');
Matteo Scandolo67c105f2017-01-09 09:30:52 -0800116
117 // redirect to selected page
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800118 this.routeSelected = (item: IXosSearchResult) => {
119 if (angular.isString(item.state)) {
120 this.$state.go(item.state);
121 }
122 else {
123 this.$state.go(item.state.name, item.state.params);
124 }
Matteo Scandolo67c105f2017-01-09 09:30:52 -0800125 this.query = null;
126 };
127
Matteo Scandolod62ea792016-12-22 14:02:28 -0800128 this.userEmail = this.authService.getUser() ? this.authService.getUser().email : '';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800129
Matteo Scandolo31daa802017-09-01 12:19:56 -0700130 this.syncStoreSubscription = this.syncStore.query()
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800131 .subscribe(
132 (event: IWSEvent) => {
Matteo Scandolo31daa802017-09-01 12:19:56 -0700133 this.$scope.$evalAsync(() => {
134
Matteo Scandolof3717252017-10-11 15:38:54 -0700135 if (!this.XosDebugService.status.notifications) {
136 // NOTE: notifications can be disabled
137 return;
138 }
139
140
Matteo Scandolo31daa802017-09-01 12:19:56 -0700141 if (event.model === 'Diag') {
142 // NOTE skip notifications for Diag model
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800143 // this should not arrive, but a check won't harm
Matteo Scandolo31daa802017-09-01 12:19:56 -0700144 return;
145 }
146
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800147 const isRemoval: boolean = event.deleted || false;
148
Matteo Scandolo266907e2016-12-20 13:41:42 -0800149 let toastrMsg: string;
150 let toastrLevel: string;
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800151 if (!isRemoval) {
152 if (event.msg.object.backend_code === 0) {
153 toastrMsg = 'Synchronization in progress for:';
154 toastrLevel = 'info';
155 }
156 else if (event.msg.object.backend_code === 1) {
157 toastrMsg = 'Synchronization succedeed for:';
158 toastrLevel = 'success';
159 }
160 else if (event.msg.object.backend_code === 2) {
161 toastrMsg = 'Synchronization failed for:';
162 toastrLevel = 'error';
163 }
164 }
165 else {
166 toastrMsg = 'Deleted object:';
Matteo Scandolo266907e2016-12-20 13:41:42 -0800167 toastrLevel = 'info';
168 }
Matteo Scandolo266907e2016-12-20 13:41:42 -0800169
170 if (toastrLevel && toastrMsg) {
Matteo Scandolo31daa802017-09-01 12:19:56 -0700171 let modelName = event.msg.object.name;
172 let modelClassName = event.model;
173 if (angular.isUndefined(event.msg.object.name) || event.msg.object.name === null) {
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800174 modelName = `${modelClassName} [${event.msg.object.id}]`;
Matteo Scandolo31daa802017-09-01 12:19:56 -0700175 }
176
Zack Williams2d0e29e2018-03-27 10:51:07 -0700177 const dest = this.ConfigHelpers.stateWithParamsForJs(modelClassName, event.msg.object.id);
Matteo Scandolo31daa802017-09-01 12:19:56 -0700178
179 if (!event.skip_notification) {
180 this.toastr[toastrLevel](`${toastrMsg} ${modelName}`, modelClassName, {extraData: {dest: dest}});
181 }
Matteo Scandolo266907e2016-12-20 13:41:42 -0800182 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800183 // this.notifications.unshift(event);
184 // this.newNotifications = this.getNewNotifications(this.notifications);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800185 });
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800186 }
187 );
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800188 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800189
Matteo Scandolo31daa802017-09-01 12:19:56 -0700190 $onDestroy() {
191 this.$log.info('[XosHeader] Teardown');
192 this.syncStoreSubscription.unsubscribe();
193 }
194
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800195 public getLogo(): string {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800196 return require(`../../images/brand/${this.StyleConfig.logo}`);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800197 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800198
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800199 // TODO display a list of notification in the template (if it make sense)
200 // public viewNotification = (notification: INotification) => {
201 // notification.viewed = true;
202 // this.newNotifications = this.getNewNotifications(this.notifications);
203 // };
204 //
205 // private getNewNotifications = (notifications: INotification[]) => {
206 // return this.notifications.filter((n: INotification) => {
207 // return !n.viewed;
208 // });
209 // };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800210}
211
212export const xosHeader: angular.IComponentOptions = {
213 template: require('./header.html'),
214 controllerAs: 'vm',
215 controller: HeaderController
216};