Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 1 | import './header.scss'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 2 | import {StyleConfig} from '../../config/style.config'; |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 3 | import {IWSEvent} from '../../datasources/websocket/global'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 4 | import {IStoreService} from '../../datasources/stores/synchronizer.store'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 5 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 6 | export interface INotification extends IWSEvent { |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 7 | viewed?: boolean; |
| 8 | } |
| 9 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 10 | class HeaderController { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 11 | static $inject = ['$scope', 'SynchronizerStore', 'toastr', 'toastrConfig']; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 12 | public title: string; |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 13 | public notifications: INotification[] = []; |
| 14 | public newNotifications: INotification[] = []; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 15 | |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 16 | constructor( |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 17 | private $scope: angular.IScope, |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 18 | private syncStore: IStoreService, |
| 19 | private toastr: ng.toastr.IToastrService, |
| 20 | private toastrConfig: ng.toastr.IToastrConfig |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 21 | ) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 22 | |
| 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 Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 35 | this.title = StyleConfig.projectName; |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 36 | |
| 37 | this.syncStore.query() |
| 38 | .subscribe( |
| 39 | (event: IWSEvent) => { |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 40 | $scope.$evalAsync(() => { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 41 | 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 Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 59 | this.notifications.unshift(event); |
| 60 | this.newNotifications = this.getNewNotifications(this.notifications); |
| 61 | }); |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 62 | } |
| 63 | ); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 64 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 65 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 66 | // TODO display a list of notification in the template |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 67 | 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 Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | export const xosHeader: angular.IComponentOptions = { |
| 80 | template: require('./header.html'), |
| 81 | controllerAs: 'vm', |
| 82 | controller: HeaderController |
| 83 | }; |