blob: 6e057bc1afb532150fb796d23220b9631e4e1392 [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 Scandolof6acdbe2016-12-13 10:29:37 -08005
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -08006export interface INotification extends IWSEvent {
Matteo Scandoloc719e792016-12-14 15:48:31 -08007 viewed?: boolean;
8}
9
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080010class HeaderController {
Matteo Scandolo266907e2016-12-20 13:41:42 -080011 static $inject = ['$scope', 'SynchronizerStore', 'toastr', 'toastrConfig'];
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080012 public title: string;
Matteo Scandoloc719e792016-12-14 15:48:31 -080013 public notifications: INotification[] = [];
14 public newNotifications: INotification[] = [];
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080015
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080016 constructor(
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080017 private $scope: angular.IScope,
Matteo Scandolo266907e2016-12-20 13:41:42 -080018 private syncStore: IStoreService,
19 private toastr: ng.toastr.IToastrService,
20 private toastrConfig: ng.toastr.IToastrConfig
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080021 ) {
Matteo Scandolo266907e2016-12-20 13:41:42 -080022
23 angular.extend(this.toastrConfig, {
24 newestOnTop: false,
25 positionClass: 'toast-top-right',
26 preventDuplicates: false,
27 preventOpenDuplicates: false,
28 progressBar: true,
29 // autoDismiss: false,
30 // closeButton: false,
31 // timeOut: 0,
32 // tapToDismiss: false
33 });
34
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080035 this.title = StyleConfig.projectName;
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080036
37 this.syncStore.query()
38 .subscribe(
39 (event: IWSEvent) => {
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080040 $scope.$evalAsync(() => {
Matteo Scandolo266907e2016-12-20 13:41:42 -080041 let toastrMsg: string;
42 let toastrLevel: string;
43 if (event.msg.object.backend_status.indexOf('1') > -1) {
44 toastrMsg = 'Synchronization started for:';
45 toastrLevel = 'info';
46 }
47 else if (event.msg.object.backend_status.indexOf('0') > -1) {
48 toastrMsg = 'Synchronization succedeed for:';
49 toastrLevel = 'success';
50 }
51 else if (event.msg.object.backend_status.indexOf('2') > -1) {
52 toastrMsg = 'Synchronization failed for:';
53 toastrLevel = 'error';
54 }
55
56 if (toastrLevel && toastrMsg) {
57 this.toastr[toastrLevel](`${toastrMsg} ${event.msg.object.name}`, event.model);
58 }
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080059 this.notifications.unshift(event);
60 this.newNotifications = this.getNewNotifications(this.notifications);
61 });
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080062 }
63 );
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080064 }
Matteo Scandoloc719e792016-12-14 15:48:31 -080065
Matteo Scandolo266907e2016-12-20 13:41:42 -080066 // TODO display a list of notification in the template
Matteo Scandoloc719e792016-12-14 15:48:31 -080067 public viewNotification = (notification: INotification) => {
68 notification.viewed = true;
69 this.newNotifications = this.getNewNotifications(this.notifications);
70 };
71
72 private getNewNotifications = (notifications: INotification[]) => {
73 return this.notifications.filter((n: INotification) => {
74 return !n.viewed;
75 });
76 };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080077}
78
79export const xosHeader: angular.IComponentOptions = {
80 template: require('./header.html'),
81 controllerAs: 'vm',
82 controller: HeaderController
83};