blob: 8926080fcfda9be094c9b534a671ff055ed3c42a [file] [log] [blame]
Matteo Scandolo63e43eb2016-12-14 14:18:53 -08001import './header.scss';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -08002import {IWSEvent} from '../../datasources/websocket/global';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08003import {IStoreService} from '../../datasources/stores/synchronizer.store';
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -08004import {IXosAuthService} from '../../datasources/rest/auth.rest';
Matteo Scandolo67c105f2017-01-09 09:30:52 -08005import {IXosNavigationService, IXosNavigationRoute} from '../services/navigation';
6import {IStateService} from 'angular-ui-router';
Matteo Scandolo67c105f2017-01-09 09:30:52 -08007import * as $ from 'jquery';
Matteo Scandolo828d1e82017-01-17 14:49:38 -08008import {IXosStyleConfig} from '../../../index';
Matteo Scandolo86bc26a2017-01-18 11:06:47 -08009import {IXosSearchService, IXosSearchResult} from '../../datasources/helpers/search.service';
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080010import {IXosKeyboardShortcutService} from '../services/keyboard-shortcut';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080011
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080012export interface INotification extends IWSEvent {
Matteo Scandoloc719e792016-12-14 15:48:31 -080013 viewed?: boolean;
14}
15
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080016class HeaderController {
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080017 static $inject = ['$scope', '$rootScope', '$state', 'AuthService', 'SynchronizerStore', 'toastr', 'toastrConfig', 'NavigationService', 'StyleConfig', 'SearchService', 'XosKeyboardShortcut'];
Matteo Scandoloc719e792016-12-14 15:48:31 -080018 public notifications: INotification[] = [];
19 public newNotifications: INotification[] = [];
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080020 public version: string;
21 public userEmail: string;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080022 public routeSelected: (route: IXosSearchResult) => void;
Matteo Scandolo67c105f2017-01-09 09:30:52 -080023 public states: IXosNavigationRoute[];
24 public query: string;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080025 public search: (query: string) => any[];
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080026
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080027 constructor(
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080028 private $scope: angular.IScope,
Matteo Scandolofc170992017-01-12 18:20:24 -080029 private $rootScope: ng.IScope,
Matteo Scandolo67c105f2017-01-09 09:30:52 -080030 private $state: IStateService,
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080031 private authService: IXosAuthService,
Matteo Scandolo266907e2016-12-20 13:41:42 -080032 private syncStore: IStoreService,
33 private toastr: ng.toastr.IToastrService,
Matteo Scandolo67c105f2017-01-09 09:30:52 -080034 private toastrConfig: ng.toastr.IToastrConfig,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080035 private NavigationService: IXosNavigationService,
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080036 private StyleConfig: IXosStyleConfig,
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080037 private SearchService: IXosSearchService,
38 private XosKeyboardShortcut: IXosKeyboardShortcutService
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080039 ) {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080040 this.version = require('../../../../package.json').version;
Matteo Scandolo266907e2016-12-20 13:41:42 -080041 angular.extend(this.toastrConfig, {
42 newestOnTop: false,
43 positionClass: 'toast-top-right',
44 preventDuplicates: false,
45 preventOpenDuplicates: false,
46 progressBar: true,
47 // autoDismiss: false,
48 // closeButton: false,
49 // timeOut: 0,
50 // tapToDismiss: false
51 });
52
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080053 this.search = (query: string) => {
54 return this.SearchService.search(query);
55 };
Matteo Scandolo67c105f2017-01-09 09:30:52 -080056
57 // listen for keypress
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080058 this.XosKeyboardShortcut.registerKeyBinding({
59 key: 'f',
60 description: 'Select search box',
61 cb: () => {
Matteo Scandolo67c105f2017-01-09 09:30:52 -080062 $('.navbar-form input').focus();
Matteo Scandolo5053cbe2017-01-31 17:37:56 -080063 },
64 }, 'global');
Matteo Scandolo67c105f2017-01-09 09:30:52 -080065
66 // redirect to selected page
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080067 this.routeSelected = (item: IXosSearchResult) => {
68 if (angular.isString(item.state)) {
69 this.$state.go(item.state);
70 }
71 else {
72 this.$state.go(item.state.name, item.state.params);
73 }
Matteo Scandolo67c105f2017-01-09 09:30:52 -080074 this.query = null;
75 };
76
Matteo Scandolod62ea792016-12-22 14:02:28 -080077 this.userEmail = this.authService.getUser() ? this.authService.getUser().email : '';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080078
79 this.syncStore.query()
80 .subscribe(
81 (event: IWSEvent) => {
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080082 $scope.$evalAsync(() => {
Matteo Scandolo266907e2016-12-20 13:41:42 -080083 let toastrMsg: string;
84 let toastrLevel: string;
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080085 if (event.msg.object.backend_status.indexOf('0') > -1) {
Matteo Scandolo266907e2016-12-20 13:41:42 -080086 toastrMsg = 'Synchronization started for:';
87 toastrLevel = 'info';
88 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080089 else if (event.msg.object.backend_status.indexOf('1') > -1) {
Matteo Scandolo266907e2016-12-20 13:41:42 -080090 toastrMsg = 'Synchronization succedeed for:';
91 toastrLevel = 'success';
92 }
93 else if (event.msg.object.backend_status.indexOf('2') > -1) {
94 toastrMsg = 'Synchronization failed for:';
95 toastrLevel = 'error';
96 }
97
98 if (toastrLevel && toastrMsg) {
99 this.toastr[toastrLevel](`${toastrMsg} ${event.msg.object.name}`, event.model);
100 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800101 // this.notifications.unshift(event);
102 // this.newNotifications = this.getNewNotifications(this.notifications);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800103 });
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800104 }
105 );
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800106 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800107
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800108 public getLogo(): string {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800109 return require(`../../images/brand/${this.StyleConfig.logo}`);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800110 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800111
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800112 // TODO display a list of notification in the template (if it make sense)
113 // public viewNotification = (notification: INotification) => {
114 // notification.viewed = true;
115 // this.newNotifications = this.getNewNotifications(this.notifications);
116 // };
117 //
118 // private getNewNotifications = (notifications: INotification[]) => {
119 // return this.notifications.filter((n: INotification) => {
120 // return !n.viewed;
121 // });
122 // };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800123}
124
125export const xosHeader: angular.IComponentOptions = {
126 template: require('./header.html'),
127 controllerAs: 'vm',
128 controller: HeaderController
129};