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 | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 6 | interface INotification extends IWSEvent { |
| 7 | viewed?: boolean; |
| 8 | } |
| 9 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 10 | class HeaderController { |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 11 | static $inject = ['$scope', 'SynchronizerStore']; |
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 | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 18 | private syncStore: IStoreService |
| 19 | ) { |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 20 | this.title = StyleConfig.projectName; |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 21 | |
| 22 | this.syncStore.query() |
| 23 | .subscribe( |
| 24 | (event: IWSEvent) => { |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 25 | $scope.$evalAsync(() => { |
| 26 | this.notifications.unshift(event); |
| 27 | this.newNotifications = this.getNewNotifications(this.notifications); |
| 28 | }); |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 29 | } |
| 30 | ); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 31 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 32 | |
| 33 | public viewNotification = (notification: INotification) => { |
| 34 | notification.viewed = true; |
| 35 | this.newNotifications = this.getNewNotifications(this.notifications); |
| 36 | }; |
| 37 | |
| 38 | private getNewNotifications = (notifications: INotification[]) => { |
| 39 | return this.notifications.filter((n: INotification) => { |
| 40 | return !n.viewed; |
| 41 | }); |
| 42 | }; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | export const xosHeader: angular.IComponentOptions = { |
| 46 | template: require('./header.html'), |
| 47 | controllerAs: 'vm', |
| 48 | controller: HeaderController |
| 49 | }; |