blob: 8bb60a41a442d1a037ead13d5dabd1e4503fe277 [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 Scandolo63e43eb2016-12-14 14:18:53 -080019import './header.scss';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080020import {IWSEvent} from '../../datasources/websocket/global';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080021import {IStoreService} from '../../datasources/stores/synchronizer.store';
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080022import {IXosAuthService} from '../../datasources/rest/auth.rest';
Matteo Scandolo67c105f2017-01-09 09:30:52 -080023import {IXosNavigationService, IXosNavigationRoute} from '../services/navigation';
24import {IStateService} from 'angular-ui-router';
Matteo Scandolo67c105f2017-01-09 09:30:52 -080025import * as $ from 'jquery';
Matteo Scandolo828d1e82017-01-17 14:49:38 -080026import {IXosStyleConfig} from '../../../index';
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080027import {IXosSearchService, IXosSearchResult} from '../../datasources/helpers/search.service';
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080028import {IXosKeyboardShortcutService} from '../services/keyboard-shortcut';
Matteo Scandoloe6f9c352017-09-01 12:19:56 -070029import {Subscription} from 'rxjs';
30import {IXosConfigHelpersService} from '../services/helpers/config.helpers';
Matteo Scandolo71d74a42017-10-11 15:38:54 -070031import {IXosDebugService} from '../debug/debug.service';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080032
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080033export interface INotification extends IWSEvent {
Matteo Scandoloc719e792016-12-14 15:48:31 -080034 viewed?: boolean;
35}
36
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080037class HeaderController {
Matteo Scandoloe6f9c352017-09-01 12:19:56 -070038 static $inject = [
39 '$log',
40 '$scope',
41 '$rootScope',
42 '$state',
43 'AuthService',
44 'SynchronizerStore',
45 'toastr',
46 'toastrConfig',
47 'XosNavigationService',
48 'StyleConfig',
49 'SearchService',
50 'XosKeyboardShortcut',
Matteo Scandolo71d74a42017-10-11 15:38:54 -070051 'ConfigHelpers',
52 'XosDebug'
Matteo Scandoloe6f9c352017-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 Scandoloe6f9c352017-09-01 12:19:56 -070063 private syncStoreSubscription: Subscription;
64
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080065 constructor(
Matteo Scandoloe6f9c352017-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 Scandoloe6f9c352017-09-01 12:19:56 -070077 private XosKeyboardShortcut: IXosKeyboardShortcutService,
Matteo Scandolo71d74a42017-10-11 15:38:54 -070078 private ConfigHelpers: IXosConfigHelpersService,
79 private XosDebugService: IXosDebugService
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080080 ) {
Matteo Scandoloe6f9c352017-09-01 12:19:56 -070081
82 }
83
84 $onInit() {
85 this.$log.info('[XosHeader] Setup');
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080086 this.version = require('../../../../package.json').version;
Matteo Scandolo266907e2016-12-20 13:41:42 -080087 angular.extend(this.toastrConfig, {
88 newestOnTop: false,
89 positionClass: 'toast-top-right',
90 preventDuplicates: false,
91 preventOpenDuplicates: false,
92 progressBar: true,
Matteo Scandoloe6f9c352017-09-01 12:19:56 -070093 onTap: (toast) => {
94 this.$state.go(toast.scope.extraData.dest.name, toast.scope.extraData.dest.params);
95 }
Matteo Scandolo266907e2016-12-20 13:41:42 -080096 });
97
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080098 this.search = (query: string) => {
99 return this.SearchService.search(query);
100 };
Matteo Scandolo67c105f2017-01-09 09:30:52 -0800101
102 // listen for keypress
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800103 this.XosKeyboardShortcut.registerKeyBinding({
Matteo Scandoloc8178492017-04-11 17:55:13 -0700104 key: 'F',
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800105 description: 'Select search box',
106 cb: () => {
Matteo Scandolo67c105f2017-01-09 09:30:52 -0800107 $('.navbar-form input').focus();
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800108 },
109 }, 'global');
Matteo Scandolo67c105f2017-01-09 09:30:52 -0800110
111 // redirect to selected page
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800112 this.routeSelected = (item: IXosSearchResult) => {
113 if (angular.isString(item.state)) {
114 this.$state.go(item.state);
115 }
116 else {
117 this.$state.go(item.state.name, item.state.params);
118 }
Matteo Scandolo67c105f2017-01-09 09:30:52 -0800119 this.query = null;
120 };
121
Matteo Scandolod62ea792016-12-22 14:02:28 -0800122 this.userEmail = this.authService.getUser() ? this.authService.getUser().email : '';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800123
Matteo Scandoloe6f9c352017-09-01 12:19:56 -0700124 this.syncStoreSubscription = this.syncStore.query()
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800125 .subscribe(
126 (event: IWSEvent) => {
Matteo Scandoloe6f9c352017-09-01 12:19:56 -0700127 this.$scope.$evalAsync(() => {
128
Matteo Scandolo71d74a42017-10-11 15:38:54 -0700129 if (!this.XosDebugService.status.notifications) {
130 // NOTE: notifications can be disabled
131 return;
132 }
133
134
Matteo Scandoloe6f9c352017-09-01 12:19:56 -0700135 if (event.model === 'Diag') {
136 // NOTE skip notifications for Diag model
Matteo Scandolofe0d4102017-11-21 10:41:28 -0800137 // this should not arrive, but a check won't harm
Matteo Scandoloe6f9c352017-09-01 12:19:56 -0700138 return;
139 }
140
Matteo Scandolofe0d4102017-11-21 10:41:28 -0800141 const isRemoval: boolean = event.deleted || false;
142
Matteo Scandolo266907e2016-12-20 13:41:42 -0800143 let toastrMsg: string;
144 let toastrLevel: string;
Matteo Scandolofe0d4102017-11-21 10:41:28 -0800145 if (!isRemoval) {
146 if (event.msg.object.backend_code === 0) {
147 toastrMsg = 'Synchronization in progress for:';
148 toastrLevel = 'info';
149 }
150 else if (event.msg.object.backend_code === 1) {
151 toastrMsg = 'Synchronization succedeed for:';
152 toastrLevel = 'success';
153 }
154 else if (event.msg.object.backend_code === 2) {
155 toastrMsg = 'Synchronization failed for:';
156 toastrLevel = 'error';
157 }
158 }
159 else {
160 toastrMsg = 'Deleted object:';
Matteo Scandolo266907e2016-12-20 13:41:42 -0800161 toastrLevel = 'info';
162 }
Matteo Scandolo266907e2016-12-20 13:41:42 -0800163
164 if (toastrLevel && toastrMsg) {
Matteo Scandoloe6f9c352017-09-01 12:19:56 -0700165 let modelName = event.msg.object.name;
166 let modelClassName = event.model;
167 if (angular.isUndefined(event.msg.object.name) || event.msg.object.name === null) {
Matteo Scandolofe0d4102017-11-21 10:41:28 -0800168 modelName = `${modelClassName} [${event.msg.object.id}]`;
Matteo Scandoloe6f9c352017-09-01 12:19:56 -0700169 }
170
171 const dest = this.ConfigHelpers.stateWithParamsForJs(modelClassName, event.msg.object);
172
173 if (!event.skip_notification) {
174 this.toastr[toastrLevel](`${toastrMsg} ${modelName}`, modelClassName, {extraData: {dest: dest}});
175 }
Matteo Scandolo266907e2016-12-20 13:41:42 -0800176 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800177 // this.notifications.unshift(event);
178 // this.newNotifications = this.getNewNotifications(this.notifications);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800179 });
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800180 }
181 );
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800182 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800183
Matteo Scandoloe6f9c352017-09-01 12:19:56 -0700184 $onDestroy() {
185 this.$log.info('[XosHeader] Teardown');
186 this.syncStoreSubscription.unsubscribe();
187 }
188
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800189 public getLogo(): string {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800190 return require(`../../images/brand/${this.StyleConfig.logo}`);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800191 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800192
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800193 // TODO display a list of notification in the template (if it make sense)
194 // public viewNotification = (notification: INotification) => {
195 // notification.viewed = true;
196 // this.newNotifications = this.getNewNotifications(this.notifications);
197 // };
198 //
199 // private getNewNotifications = (notifications: INotification[]) => {
200 // return this.notifications.filter((n: INotification) => {
201 // return !n.viewed;
202 // });
203 // };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800204}
205
206export const xosHeader: angular.IComponentOptions = {
207 template: require('./header.html'),
208 controllerAs: 'vm',
209 controller: HeaderController
210};