blob: 26f6fd04f69879d8cb27129653ae0213174fbf78 [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 Scandolof3717252017-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 Scandolo31daa802017-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 Scandolof3717252017-10-11 15:38:54 -070051 'ConfigHelpers',
52 'XosDebug'
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,
79 private XosDebugService: IXosDebugService
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080080 ) {
Matteo Scandolo31daa802017-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 Scandolo31daa802017-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 Scandolo31daa802017-09-01 12:19:56 -0700124 this.syncStoreSubscription = this.syncStore.query()
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800125 .subscribe(
126 (event: IWSEvent) => {
Matteo Scandolo31daa802017-09-01 12:19:56 -0700127 this.$scope.$evalAsync(() => {
128
Matteo Scandolof3717252017-10-11 15:38:54 -0700129 if (!this.XosDebugService.status.notifications) {
130 // NOTE: notifications can be disabled
131 return;
132 }
133
134
Matteo Scandolo31daa802017-09-01 12:19:56 -0700135 if (event.model === 'Diag') {
136 // NOTE skip notifications for Diag model
137 return;
138 }
139
Matteo Scandolo266907e2016-12-20 13:41:42 -0800140 let toastrMsg: string;
141 let toastrLevel: string;
Matteo Scandolo31daa802017-09-01 12:19:56 -0700142 if (event.msg.object.backend_code === 0) {
Matteo Scandolo266907e2016-12-20 13:41:42 -0800143 toastrMsg = 'Synchronization started for:';
144 toastrLevel = 'info';
145 }
Matteo Scandolo31daa802017-09-01 12:19:56 -0700146 else if (event.msg.object.backend_code === 1) {
Matteo Scandolo266907e2016-12-20 13:41:42 -0800147 toastrMsg = 'Synchronization succedeed for:';
148 toastrLevel = 'success';
149 }
Matteo Scandolo31daa802017-09-01 12:19:56 -0700150 else if (event.msg.object.backend_code === 2) {
Matteo Scandolo266907e2016-12-20 13:41:42 -0800151 toastrMsg = 'Synchronization failed for:';
152 toastrLevel = 'error';
153 }
154
155 if (toastrLevel && toastrMsg) {
Matteo Scandolo31daa802017-09-01 12:19:56 -0700156 let modelName = event.msg.object.name;
157 let modelClassName = event.model;
158 if (angular.isUndefined(event.msg.object.name) || event.msg.object.name === null) {
159 modelName = `${event.msg.object.leaf_model_name} [${event.msg.object.id}]`;
160 }
161
162 const dest = this.ConfigHelpers.stateWithParamsForJs(modelClassName, event.msg.object);
163
164 if (!event.skip_notification) {
165 this.toastr[toastrLevel](`${toastrMsg} ${modelName}`, modelClassName, {extraData: {dest: dest}});
166 }
Matteo Scandolo266907e2016-12-20 13:41:42 -0800167 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800168 // this.notifications.unshift(event);
169 // this.newNotifications = this.getNewNotifications(this.notifications);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800170 });
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800171 }
172 );
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800173 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800174
Matteo Scandolo31daa802017-09-01 12:19:56 -0700175 $onDestroy() {
176 this.$log.info('[XosHeader] Teardown');
177 this.syncStoreSubscription.unsubscribe();
178 }
179
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800180 public getLogo(): string {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800181 return require(`../../images/brand/${this.StyleConfig.logo}`);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800182 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800183
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800184 // TODO display a list of notification in the template (if it make sense)
185 // public viewNotification = (notification: INotification) => {
186 // notification.viewed = true;
187 // this.newNotifications = this.getNewNotifications(this.notifications);
188 // };
189 //
190 // private getNewNotifications = (notifications: INotification[]) => {
191 // return this.notifications.filter((n: INotification) => {
192 // return !n.viewed;
193 // });
194 // };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800195}
196
197export const xosHeader: angular.IComponentOptions = {
198 template: require('./header.html'),
199 controllerAs: 'vm',
200 controller: HeaderController
201};