blob: 5ef9459b7d1b45aff4f20afb30a449eba75cb096 [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 Scandolof6acdbe2016-12-13 10:29:37 -080030
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080031export interface INotification extends IWSEvent {
Matteo Scandoloc719e792016-12-14 15:48:31 -080032 viewed?: boolean;
33}
34
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080035class HeaderController {
Matteo Scandolo31daa802017-09-01 12:19:56 -070036 static $inject = [
37 '$log',
38 '$scope',
39 '$rootScope',
40 '$state',
41 'AuthService',
42 'SynchronizerStore',
43 'toastr',
44 'toastrConfig',
45 'XosNavigationService',
46 'StyleConfig',
47 'SearchService',
48 'XosKeyboardShortcut',
Matteo Scandolof3717252017-10-11 15:38:54 -070049 'ConfigHelpers',
50 'XosDebug'
Matteo Scandolo31daa802017-09-01 12:19:56 -070051 ];
Matteo Scandoloc719e792016-12-14 15:48:31 -080052 public notifications: INotification[] = [];
53 public newNotifications: INotification[] = [];
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080054 public version: string;
55 public userEmail: string;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080056 public routeSelected: (route: IXosSearchResult) => void;
Matteo Scandolo67c105f2017-01-09 09:30:52 -080057 public states: IXosNavigationRoute[];
58 public query: string;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080059 public search: (query: string) => any[];
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080060
Matteo Scandolo31daa802017-09-01 12:19:56 -070061 private syncStoreSubscription: Subscription;
62
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080063 constructor(
Matteo Scandolo31daa802017-09-01 12:19:56 -070064 private $log: ng.ILogService,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080065 private $scope: angular.IScope,
Matteo Scandolofc170992017-01-12 18:20:24 -080066 private $rootScope: ng.IScope,
Matteo Scandolo67c105f2017-01-09 09:30:52 -080067 private $state: IStateService,
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080068 private authService: IXosAuthService,
Matteo Scandolo266907e2016-12-20 13:41:42 -080069 private syncStore: IStoreService,
70 private toastr: ng.toastr.IToastrService,
Matteo Scandolo67c105f2017-01-09 09:30:52 -080071 private toastrConfig: ng.toastr.IToastrConfig,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080072 private NavigationService: IXosNavigationService,
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080073 private StyleConfig: IXosStyleConfig,
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080074 private SearchService: IXosSearchService,
Matteo Scandolo31daa802017-09-01 12:19:56 -070075 private XosKeyboardShortcut: IXosKeyboardShortcutService,
Matteo Scandolof3717252017-10-11 15:38:54 -070076 private ConfigHelpers: IXosConfigHelpersService,
77 private XosDebugService: IXosDebugService
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080078 ) {
Matteo Scandolo31daa802017-09-01 12:19:56 -070079
80 }
81
82 $onInit() {
83 this.$log.info('[XosHeader] Setup');
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080084 this.version = require('../../../../package.json').version;
Matteo Scandolo266907e2016-12-20 13:41:42 -080085 angular.extend(this.toastrConfig, {
86 newestOnTop: false,
87 positionClass: 'toast-top-right',
88 preventDuplicates: false,
89 preventOpenDuplicates: false,
90 progressBar: true,
Matteo Scandolo31daa802017-09-01 12:19:56 -070091 onTap: (toast) => {
92 this.$state.go(toast.scope.extraData.dest.name, toast.scope.extraData.dest.params);
93 }
Matteo Scandolo266907e2016-12-20 13:41:42 -080094 });
95
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080096 this.search = (query: string) => {
97 return this.SearchService.search(query);
98 };
Matteo Scandolo67c105f2017-01-09 09:30:52 -080099
100 // listen for keypress
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800101 this.XosKeyboardShortcut.registerKeyBinding({
Matteo Scandoloc8178492017-04-11 17:55:13 -0700102 key: 'F',
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800103 description: 'Select search box',
104 cb: () => {
Matteo Scandolo67c105f2017-01-09 09:30:52 -0800105 $('.navbar-form input').focus();
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800106 },
107 }, 'global');
Matteo Scandolo67c105f2017-01-09 09:30:52 -0800108
109 // redirect to selected page
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800110 this.routeSelected = (item: IXosSearchResult) => {
111 if (angular.isString(item.state)) {
112 this.$state.go(item.state);
113 }
114 else {
115 this.$state.go(item.state.name, item.state.params);
116 }
Matteo Scandolo67c105f2017-01-09 09:30:52 -0800117 this.query = null;
118 };
119
Matteo Scandolod62ea792016-12-22 14:02:28 -0800120 this.userEmail = this.authService.getUser() ? this.authService.getUser().email : '';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800121
Matteo Scandolo31daa802017-09-01 12:19:56 -0700122 this.syncStoreSubscription = this.syncStore.query()
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800123 .subscribe(
124 (event: IWSEvent) => {
Matteo Scandolo31daa802017-09-01 12:19:56 -0700125 this.$scope.$evalAsync(() => {
126
Matteo Scandolof3717252017-10-11 15:38:54 -0700127 if (!this.XosDebugService.status.notifications) {
128 // NOTE: notifications can be disabled
129 return;
130 }
131
132
Matteo Scandolo31daa802017-09-01 12:19:56 -0700133 if (event.model === 'Diag') {
134 // NOTE skip notifications for Diag model
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800135 // this should not arrive, but a check won't harm
Matteo Scandolo31daa802017-09-01 12:19:56 -0700136 return;
137 }
138
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800139 const isRemoval: boolean = event.deleted || false;
140
Matteo Scandolo266907e2016-12-20 13:41:42 -0800141 let toastrMsg: string;
142 let toastrLevel: string;
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800143 if (!isRemoval) {
144 if (event.msg.object.backend_code === 0) {
145 toastrMsg = 'Synchronization in progress for:';
146 toastrLevel = 'info';
147 }
148 else if (event.msg.object.backend_code === 1) {
149 toastrMsg = 'Synchronization succedeed for:';
150 toastrLevel = 'success';
151 }
152 else if (event.msg.object.backend_code === 2) {
153 toastrMsg = 'Synchronization failed for:';
154 toastrLevel = 'error';
155 }
156 }
157 else {
158 toastrMsg = 'Deleted object:';
Matteo Scandolo266907e2016-12-20 13:41:42 -0800159 toastrLevel = 'info';
160 }
Matteo Scandolo266907e2016-12-20 13:41:42 -0800161
162 if (toastrLevel && toastrMsg) {
Matteo Scandolo31daa802017-09-01 12:19:56 -0700163 let modelName = event.msg.object.name;
164 let modelClassName = event.model;
165 if (angular.isUndefined(event.msg.object.name) || event.msg.object.name === null) {
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800166 modelName = `${modelClassName} [${event.msg.object.id}]`;
Matteo Scandolo31daa802017-09-01 12:19:56 -0700167 }
168
Zack Williams2d0e29e2018-03-27 10:51:07 -0700169 const dest = this.ConfigHelpers.stateWithParamsForJs(modelClassName, event.msg.object.id);
Matteo Scandolo31daa802017-09-01 12:19:56 -0700170
171 if (!event.skip_notification) {
172 this.toastr[toastrLevel](`${toastrMsg} ${modelName}`, modelClassName, {extraData: {dest: dest}});
173 }
Matteo Scandolo266907e2016-12-20 13:41:42 -0800174 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800175 // this.notifications.unshift(event);
176 // this.newNotifications = this.getNewNotifications(this.notifications);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800177 });
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800178 }
179 );
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800180 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800181
Matteo Scandolo31daa802017-09-01 12:19:56 -0700182 $onDestroy() {
183 this.$log.info('[XosHeader] Teardown');
184 this.syncStoreSubscription.unsubscribe();
185 }
186
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800187 public getLogo(): string {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800188 return require(`../../images/brand/${this.StyleConfig.logo}`);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800189 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800190
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800191 // TODO display a list of notification in the template (if it make sense)
192 // public viewNotification = (notification: INotification) => {
193 // notification.viewed = true;
194 // this.newNotifications = this.getNewNotifications(this.notifications);
195 // };
196 //
197 // private getNewNotifications = (notifications: INotification[]) => {
198 // return this.notifications.filter((n: INotification) => {
199 // return !n.viewed;
200 // });
201 // };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800202}
203
204export const xosHeader: angular.IComponentOptions = {
205 template: require('./header.html'),
206 controllerAs: 'vm',
207 controller: HeaderController
208};