blob: efaa3853026b4ff50e1919a511a90030778d7d6c [file] [log] [blame]
Matteo Scandolo63e43eb2016-12-14 14:18:53 -08001import './header.scss';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08002import {StyleConfig} from '../../config/style.config';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -08003import {IWSEvent} from '../../datasources/websocket/global';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08004import {IStoreService} from '../../datasources/stores/synchronizer.store';
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -08005import {IXosAuthService} from '../../datasources/rest/auth.rest';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08006
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -08007export interface INotification extends IWSEvent {
Matteo Scandoloc719e792016-12-14 15:48:31 -08008 viewed?: boolean;
9}
10
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080011class HeaderController {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080012 static $inject = ['$scope', 'AuthService', 'SynchronizerStore', 'toastr', 'toastrConfig'];
Matteo Scandoloc719e792016-12-14 15:48:31 -080013 public notifications: INotification[] = [];
14 public newNotifications: INotification[] = [];
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080015 public version: string;
16 public userEmail: string;
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080017
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080018 constructor(
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080019 private $scope: angular.IScope,
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080020 private authService: IXosAuthService,
Matteo Scandolo266907e2016-12-20 13:41:42 -080021 private syncStore: IStoreService,
22 private toastr: ng.toastr.IToastrService,
23 private toastrConfig: ng.toastr.IToastrConfig
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080024 ) {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080025 this.version = require('../../../../package.json').version;
Matteo Scandolo266907e2016-12-20 13:41:42 -080026 angular.extend(this.toastrConfig, {
27 newestOnTop: false,
28 positionClass: 'toast-top-right',
29 preventDuplicates: false,
30 preventOpenDuplicates: false,
31 progressBar: true,
32 // autoDismiss: false,
33 // closeButton: false,
34 // timeOut: 0,
35 // tapToDismiss: false
36 });
37
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080038 this.userEmail = this.authService.getUser().email;
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080039
40 this.syncStore.query()
41 .subscribe(
42 (event: IWSEvent) => {
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080043 $scope.$evalAsync(() => {
Matteo Scandolo266907e2016-12-20 13:41:42 -080044 let toastrMsg: string;
45 let toastrLevel: string;
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080046 if (event.msg.object.backend_status.indexOf('0') > -1) {
Matteo Scandolo266907e2016-12-20 13:41:42 -080047 toastrMsg = 'Synchronization started for:';
48 toastrLevel = 'info';
49 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080050 else if (event.msg.object.backend_status.indexOf('1') > -1) {
Matteo Scandolo266907e2016-12-20 13:41:42 -080051 toastrMsg = 'Synchronization succedeed for:';
52 toastrLevel = 'success';
53 }
54 else if (event.msg.object.backend_status.indexOf('2') > -1) {
55 toastrMsg = 'Synchronization failed for:';
56 toastrLevel = 'error';
57 }
58
59 if (toastrLevel && toastrMsg) {
60 this.toastr[toastrLevel](`${toastrMsg} ${event.msg.object.name}`, event.model);
61 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080062 // this.notifications.unshift(event);
63 // this.newNotifications = this.getNewNotifications(this.notifications);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080064 });
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080065 }
66 );
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080067 }
Matteo Scandoloc719e792016-12-14 15:48:31 -080068
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080069 public getLogo(): string {
70 return require(`../../images/brand/${StyleConfig.logo}`);
71 }
Matteo Scandoloc719e792016-12-14 15:48:31 -080072
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080073 // TODO display a list of notification in the template (if it make sense)
74 // public viewNotification = (notification: INotification) => {
75 // notification.viewed = true;
76 // this.newNotifications = this.getNewNotifications(this.notifications);
77 // };
78 //
79 // private getNewNotifications = (notifications: INotification[]) => {
80 // return this.notifications.filter((n: INotification) => {
81 // return !n.viewed;
82 // });
83 // };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080084}
85
86export const xosHeader: angular.IComponentOptions = {
87 template: require('./header.html'),
88 controllerAs: 'vm',
89 controller: HeaderController
90};