blob: a89199ddf03216c2189aa091fd199f651491cabe [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 Scandolof6acdbe2016-12-13 10:29:37 -080029
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080030export interface INotification extends IWSEvent {
Matteo Scandoloc719e792016-12-14 15:48:31 -080031 viewed?: boolean;
32}
33
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080034class HeaderController {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080035 static $inject = ['$scope', '$rootScope', '$state', 'AuthService', 'SynchronizerStore', 'toastr', 'toastrConfig', 'XosNavigationService', 'StyleConfig', 'SearchService', 'XosKeyboardShortcut'];
Matteo Scandoloc719e792016-12-14 15:48:31 -080036 public notifications: INotification[] = [];
37 public newNotifications: INotification[] = [];
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080038 public version: string;
39 public userEmail: string;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080040 public routeSelected: (route: IXosSearchResult) => void;
Matteo Scandolo67c105f2017-01-09 09:30:52 -080041 public states: IXosNavigationRoute[];
42 public query: string;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080043 public search: (query: string) => any[];
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080044
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080045 constructor(
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080046 private $scope: angular.IScope,
Matteo Scandolofc170992017-01-12 18:20:24 -080047 private $rootScope: ng.IScope,
Matteo Scandolo67c105f2017-01-09 09:30:52 -080048 private $state: IStateService,
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080049 private authService: IXosAuthService,
Matteo Scandolo266907e2016-12-20 13:41:42 -080050 private syncStore: IStoreService,
51 private toastr: ng.toastr.IToastrService,
Matteo Scandolo67c105f2017-01-09 09:30:52 -080052 private toastrConfig: ng.toastr.IToastrConfig,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080053 private NavigationService: IXosNavigationService,
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080054 private StyleConfig: IXosStyleConfig,
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080055 private SearchService: IXosSearchService,
56 private XosKeyboardShortcut: IXosKeyboardShortcutService
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080057 ) {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080058 this.version = require('../../../../package.json').version;
Matteo Scandolo266907e2016-12-20 13:41:42 -080059 angular.extend(this.toastrConfig, {
60 newestOnTop: false,
61 positionClass: 'toast-top-right',
62 preventDuplicates: false,
63 preventOpenDuplicates: false,
64 progressBar: true,
65 // autoDismiss: false,
66 // closeButton: false,
67 // timeOut: 0,
68 // tapToDismiss: false
69 });
70
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080071 this.search = (query: string) => {
72 return this.SearchService.search(query);
73 };
Matteo Scandolo67c105f2017-01-09 09:30:52 -080074
75 // listen for keypress
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080076 this.XosKeyboardShortcut.registerKeyBinding({
Matteo Scandoloc8178492017-04-11 17:55:13 -070077 key: 'F',
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080078 description: 'Select search box',
79 cb: () => {
Matteo Scandolo67c105f2017-01-09 09:30:52 -080080 $('.navbar-form input').focus();
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080081 },
82 }, 'global');
Matteo Scandolo67c105f2017-01-09 09:30:52 -080083
84 // redirect to selected page
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080085 this.routeSelected = (item: IXosSearchResult) => {
86 if (angular.isString(item.state)) {
87 this.$state.go(item.state);
88 }
89 else {
90 this.$state.go(item.state.name, item.state.params);
91 }
Matteo Scandolo67c105f2017-01-09 09:30:52 -080092 this.query = null;
93 };
94
Matteo Scandolod62ea792016-12-22 14:02:28 -080095 this.userEmail = this.authService.getUser() ? this.authService.getUser().email : '';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080096
97 this.syncStore.query()
98 .subscribe(
99 (event: IWSEvent) => {
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800100 $scope.$evalAsync(() => {
Matteo Scandolo266907e2016-12-20 13:41:42 -0800101 let toastrMsg: string;
102 let toastrLevel: string;
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800103 if (event.msg.object.backend_status.indexOf('0') > -1) {
Matteo Scandolo266907e2016-12-20 13:41:42 -0800104 toastrMsg = 'Synchronization started for:';
105 toastrLevel = 'info';
106 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800107 else if (event.msg.object.backend_status.indexOf('1') > -1) {
Matteo Scandolo266907e2016-12-20 13:41:42 -0800108 toastrMsg = 'Synchronization succedeed for:';
109 toastrLevel = 'success';
110 }
111 else if (event.msg.object.backend_status.indexOf('2') > -1) {
112 toastrMsg = 'Synchronization failed for:';
113 toastrLevel = 'error';
114 }
115
116 if (toastrLevel && toastrMsg) {
117 this.toastr[toastrLevel](`${toastrMsg} ${event.msg.object.name}`, event.model);
118 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800119 // this.notifications.unshift(event);
120 // this.newNotifications = this.getNewNotifications(this.notifications);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800121 });
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800122 }
123 );
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800124 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800125
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800126 public getLogo(): string {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800127 return require(`../../images/brand/${this.StyleConfig.logo}`);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800128 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800129
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800130 // TODO display a list of notification in the template (if it make sense)
131 // public viewNotification = (notification: INotification) => {
132 // notification.viewed = true;
133 // this.newNotifications = this.getNewNotifications(this.notifications);
134 // };
135 //
136 // private getNewNotifications = (notifications: INotification[]) => {
137 // return this.notifications.filter((n: INotification) => {
138 // return !n.viewed;
139 // });
140 // };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800141}
142
143export const xosHeader: angular.IComponentOptions = {
144 template: require('./header.html'),
145 controllerAs: 'vm',
146 controller: HeaderController
147};