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 | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 5 | import {IXosAuthService} from '../../datasources/rest/auth.rest'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 6 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 7 | export interface INotification extends IWSEvent { |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 8 | viewed?: boolean; |
| 9 | } |
| 10 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 11 | class HeaderController { |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 12 | static $inject = ['$scope', 'AuthService', 'SynchronizerStore', 'toastr', 'toastrConfig']; |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 13 | public notifications: INotification[] = []; |
| 14 | public newNotifications: INotification[] = []; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 15 | public version: string; |
| 16 | public userEmail: string; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 17 | |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 18 | constructor( |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 19 | private $scope: angular.IScope, |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 20 | private authService: IXosAuthService, |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 21 | private syncStore: IStoreService, |
| 22 | private toastr: ng.toastr.IToastrService, |
| 23 | private toastrConfig: ng.toastr.IToastrConfig |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 24 | ) { |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 25 | this.version = require('../../../../package.json').version; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 26 | 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 Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 38 | this.userEmail = this.authService.getUser() ? this.authService.getUser().email : ''; |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 39 | |
| 40 | this.syncStore.query() |
| 41 | .subscribe( |
| 42 | (event: IWSEvent) => { |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 43 | $scope.$evalAsync(() => { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 44 | let toastrMsg: string; |
| 45 | let toastrLevel: string; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 46 | if (event.msg.object.backend_status.indexOf('0') > -1) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 47 | toastrMsg = 'Synchronization started for:'; |
| 48 | toastrLevel = 'info'; |
| 49 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 50 | else if (event.msg.object.backend_status.indexOf('1') > -1) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 51 | 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 Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 62 | // this.notifications.unshift(event); |
| 63 | // this.newNotifications = this.getNewNotifications(this.notifications); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 64 | }); |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 65 | } |
| 66 | ); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 67 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 68 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 69 | public getLogo(): string { |
| 70 | return require(`../../images/brand/${StyleConfig.logo}`); |
| 71 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 72 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 73 | // 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 Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | export const xosHeader: angular.IComponentOptions = { |
| 87 | template: require('./header.html'), |
| 88 | controllerAs: 'vm', |
| 89 | controller: HeaderController |
| 90 | }; |