blob: a465f8ff5a5bf3dce91412ce5e838df7ecd2da6a [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 Scandolo31daa802017-09-01 12:19:56 -070029import {Subscription} from 'rxjs';
30import {IXosConfigHelpersService} from '../services/helpers/config.helpers';
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',
50 'ConfigHelpers'
51 ];
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,
76 private ConfigHelpers: IXosConfigHelpersService
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080077 ) {
Matteo Scandolo31daa802017-09-01 12:19:56 -070078
79 }
80
81 $onInit() {
82 this.$log.info('[XosHeader] Setup');
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080083 this.version = require('../../../../package.json').version;
Matteo Scandolo266907e2016-12-20 13:41:42 -080084 angular.extend(this.toastrConfig, {
85 newestOnTop: false,
86 positionClass: 'toast-top-right',
87 preventDuplicates: false,
88 preventOpenDuplicates: false,
89 progressBar: true,
Matteo Scandolo31daa802017-09-01 12:19:56 -070090 onTap: (toast) => {
91 this.$state.go(toast.scope.extraData.dest.name, toast.scope.extraData.dest.params);
92 }
Matteo Scandolo266907e2016-12-20 13:41:42 -080093 });
94
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080095 this.search = (query: string) => {
96 return this.SearchService.search(query);
97 };
Matteo Scandolo67c105f2017-01-09 09:30:52 -080098
99 // listen for keypress
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800100 this.XosKeyboardShortcut.registerKeyBinding({
Matteo Scandoloc8178492017-04-11 17:55:13 -0700101 key: 'F',
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800102 description: 'Select search box',
103 cb: () => {
Matteo Scandolo67c105f2017-01-09 09:30:52 -0800104 $('.navbar-form input').focus();
Matteo Scandolo5053cbe2017-01-31 17:37:56 -0800105 },
106 }, 'global');
Matteo Scandolo67c105f2017-01-09 09:30:52 -0800107
108 // redirect to selected page
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800109 this.routeSelected = (item: IXosSearchResult) => {
110 if (angular.isString(item.state)) {
111 this.$state.go(item.state);
112 }
113 else {
114 this.$state.go(item.state.name, item.state.params);
115 }
Matteo Scandolo67c105f2017-01-09 09:30:52 -0800116 this.query = null;
117 };
118
Matteo Scandolod62ea792016-12-22 14:02:28 -0800119 this.userEmail = this.authService.getUser() ? this.authService.getUser().email : '';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800120
Matteo Scandolo31daa802017-09-01 12:19:56 -0700121 this.syncStoreSubscription = this.syncStore.query()
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800122 .subscribe(
123 (event: IWSEvent) => {
Matteo Scandolo31daa802017-09-01 12:19:56 -0700124 this.$scope.$evalAsync(() => {
125
126 if (event.model === 'Diag') {
127 // NOTE skip notifications for Diag model
128 return;
129 }
130
Matteo Scandolo266907e2016-12-20 13:41:42 -0800131 let toastrMsg: string;
132 let toastrLevel: string;
Matteo Scandolo31daa802017-09-01 12:19:56 -0700133 if (event.msg.object.backend_code === 0) {
Matteo Scandolo266907e2016-12-20 13:41:42 -0800134 toastrMsg = 'Synchronization started for:';
135 toastrLevel = 'info';
136 }
Matteo Scandolo31daa802017-09-01 12:19:56 -0700137 else if (event.msg.object.backend_code === 1) {
Matteo Scandolo266907e2016-12-20 13:41:42 -0800138 toastrMsg = 'Synchronization succedeed for:';
139 toastrLevel = 'success';
140 }
Matteo Scandolo31daa802017-09-01 12:19:56 -0700141 else if (event.msg.object.backend_code === 2) {
Matteo Scandolo266907e2016-12-20 13:41:42 -0800142 toastrMsg = 'Synchronization failed for:';
143 toastrLevel = 'error';
144 }
145
146 if (toastrLevel && toastrMsg) {
Matteo Scandolo31daa802017-09-01 12:19:56 -0700147 let modelName = event.msg.object.name;
148 let modelClassName = event.model;
149 if (angular.isUndefined(event.msg.object.name) || event.msg.object.name === null) {
150 modelName = `${event.msg.object.leaf_model_name} [${event.msg.object.id}]`;
151 }
152
153 const dest = this.ConfigHelpers.stateWithParamsForJs(modelClassName, event.msg.object);
154
155 if (!event.skip_notification) {
156 this.toastr[toastrLevel](`${toastrMsg} ${modelName}`, modelClassName, {extraData: {dest: dest}});
157 }
Matteo Scandolo266907e2016-12-20 13:41:42 -0800158 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800159 // this.notifications.unshift(event);
160 // this.newNotifications = this.getNewNotifications(this.notifications);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800161 });
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800162 }
163 );
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800164 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800165
Matteo Scandolo31daa802017-09-01 12:19:56 -0700166 $onDestroy() {
167 this.$log.info('[XosHeader] Teardown');
168 this.syncStoreSubscription.unsubscribe();
169 }
170
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800171 public getLogo(): string {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800172 return require(`../../images/brand/${this.StyleConfig.logo}`);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800173 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800174
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800175 // TODO display a list of notification in the template (if it make sense)
176 // public viewNotification = (notification: INotification) => {
177 // notification.viewed = true;
178 // this.newNotifications = this.getNewNotifications(this.notifications);
179 // };
180 //
181 // private getNewNotifications = (notifications: INotification[]) => {
182 // return this.notifications.filter((n: INotification) => {
183 // return !n.viewed;
184 // });
185 // };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800186}
187
188export const xosHeader: angular.IComponentOptions = {
189 template: require('./header.html'),
190 controllerAs: 'vm',
191 controller: HeaderController
192};