blob: 1f01bf8f0d8dff0c170ab2665a2a190ce0ad8bed [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 Scandolof6acdbe2016-12-13 10:29:37 -080010
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080011export interface INotification extends IWSEvent {
Matteo Scandoloc719e792016-12-14 15:48:31 -080012 viewed?: boolean;
13}
14
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080015class HeaderController {
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080016 static $inject = ['$scope', '$rootScope', '$state', 'AuthService', 'SynchronizerStore', 'toastr', 'toastrConfig', 'NavigationService', 'StyleConfig', 'SearchService'];
Matteo Scandoloc719e792016-12-14 15:48:31 -080017 public notifications: INotification[] = [];
18 public newNotifications: INotification[] = [];
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080019 public version: string;
20 public userEmail: string;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080021 public routeSelected: (route: IXosSearchResult) => void;
Matteo Scandolo67c105f2017-01-09 09:30:52 -080022 public states: IXosNavigationRoute[];
23 public query: string;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080024 public search: (query: string) => any[];
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080025
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080026 constructor(
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080027 private $scope: angular.IScope,
Matteo Scandolofc170992017-01-12 18:20:24 -080028 private $rootScope: ng.IScope,
Matteo Scandolo67c105f2017-01-09 09:30:52 -080029 private $state: IStateService,
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080030 private authService: IXosAuthService,
Matteo Scandolo266907e2016-12-20 13:41:42 -080031 private syncStore: IStoreService,
32 private toastr: ng.toastr.IToastrService,
Matteo Scandolo67c105f2017-01-09 09:30:52 -080033 private toastrConfig: ng.toastr.IToastrConfig,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080034 private NavigationService: IXosNavigationService,
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080035 private StyleConfig: IXosStyleConfig,
36 private SearchService: IXosSearchService
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080037 ) {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080038 this.version = require('../../../../package.json').version;
Matteo Scandolo266907e2016-12-20 13:41:42 -080039 angular.extend(this.toastrConfig, {
40 newestOnTop: false,
41 positionClass: 'toast-top-right',
42 preventDuplicates: false,
43 preventOpenDuplicates: false,
44 progressBar: true,
45 // autoDismiss: false,
46 // closeButton: false,
47 // timeOut: 0,
48 // tapToDismiss: false
49 });
50
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080051 this.search = (query: string) => {
52 return this.SearchService.search(query);
53 };
Matteo Scandolo67c105f2017-01-09 09:30:52 -080054
55 // listen for keypress
56 $(document).on('keyup', (e) => {
57 if (e.key === 'f') {
58 $('.navbar-form input').focus();
59 }
60 });
61
62 // redirect to selected page
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080063 this.routeSelected = (item: IXosSearchResult) => {
64 if (angular.isString(item.state)) {
65 this.$state.go(item.state);
66 }
67 else {
68 this.$state.go(item.state.name, item.state.params);
69 }
Matteo Scandolo67c105f2017-01-09 09:30:52 -080070 this.query = null;
71 };
72
Matteo Scandolod62ea792016-12-22 14:02:28 -080073 this.userEmail = this.authService.getUser() ? this.authService.getUser().email : '';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080074
75 this.syncStore.query()
76 .subscribe(
77 (event: IWSEvent) => {
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080078 $scope.$evalAsync(() => {
Matteo Scandolo266907e2016-12-20 13:41:42 -080079 let toastrMsg: string;
80 let toastrLevel: string;
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080081 if (event.msg.object.backend_status.indexOf('0') > -1) {
Matteo Scandolo266907e2016-12-20 13:41:42 -080082 toastrMsg = 'Synchronization started for:';
83 toastrLevel = 'info';
84 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080085 else if (event.msg.object.backend_status.indexOf('1') > -1) {
Matteo Scandolo266907e2016-12-20 13:41:42 -080086 toastrMsg = 'Synchronization succedeed for:';
87 toastrLevel = 'success';
88 }
89 else if (event.msg.object.backend_status.indexOf('2') > -1) {
90 toastrMsg = 'Synchronization failed for:';
91 toastrLevel = 'error';
92 }
93
94 if (toastrLevel && toastrMsg) {
95 this.toastr[toastrLevel](`${toastrMsg} ${event.msg.object.name}`, event.model);
96 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080097 // this.notifications.unshift(event);
98 // this.newNotifications = this.getNewNotifications(this.notifications);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080099 });
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800100 }
101 );
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800102 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800103
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800104 public getLogo(): string {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800105 return require(`../../images/brand/${this.StyleConfig.logo}`);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800106 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800107
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800108 // TODO display a list of notification in the template (if it make sense)
109 // public viewNotification = (notification: INotification) => {
110 // notification.viewed = true;
111 // this.newNotifications = this.getNewNotifications(this.notifications);
112 // };
113 //
114 // private getNewNotifications = (notifications: INotification[]) => {
115 // return this.notifications.filter((n: INotification) => {
116 // return !n.viewed;
117 // });
118 // };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800119}
120
121export const xosHeader: angular.IComponentOptions = {
122 template: require('./header.html'),
123 controllerAs: 'vm',
124 controller: HeaderController
125};