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 | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 6 | import {IXosNavigationService, IXosNavigationRoute} from '../services/navigation'; |
| 7 | import {IStateService} from 'angular-ui-router'; |
| 8 | import * as _ from 'lodash'; |
| 9 | import * as $ from 'jquery'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 10 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 11 | export interface INotification extends IWSEvent { |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 12 | viewed?: boolean; |
| 13 | } |
| 14 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 15 | class HeaderController { |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 16 | static $inject = ['$scope', '$state', 'AuthService', 'SynchronizerStore', 'toastr', 'toastrConfig', 'NavigationService']; |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 17 | public notifications: INotification[] = []; |
| 18 | public newNotifications: INotification[] = []; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 19 | public version: string; |
| 20 | public userEmail: string; |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 21 | public routeSelected: (route: IXosNavigationRoute) => void; |
| 22 | public states: IXosNavigationRoute[]; |
| 23 | public query: string; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 24 | |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 25 | constructor( |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 26 | private $scope: angular.IScope, |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 27 | private $state: IStateService, |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 28 | private authService: IXosAuthService, |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 29 | private syncStore: IStoreService, |
| 30 | private toastr: ng.toastr.IToastrService, |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 31 | private toastrConfig: ng.toastr.IToastrConfig, |
| 32 | private NavigationService: IXosNavigationService |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 33 | ) { |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 34 | this.version = require('../../../../package.json').version; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 35 | angular.extend(this.toastrConfig, { |
| 36 | newestOnTop: false, |
| 37 | positionClass: 'toast-top-right', |
| 38 | preventDuplicates: false, |
| 39 | preventOpenDuplicates: false, |
| 40 | progressBar: true, |
| 41 | // autoDismiss: false, |
| 42 | // closeButton: false, |
| 43 | // timeOut: 0, |
| 44 | // tapToDismiss: false |
| 45 | }); |
| 46 | |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 47 | // TODO set a global event after routes have been loaded |
| 48 | window.setTimeout(() => { |
| 49 | this.states = this.NavigationService.query().reduce((list, state) => { |
| 50 | // if it does not have child (otherwise it is abstract) |
| 51 | if (!state.children || state.children.length === 0) { |
| 52 | list.push(state); |
| 53 | } |
| 54 | // else push child |
| 55 | if (state.children && state.children.length > 0) { |
| 56 | state.children.forEach(c => { |
| 57 | list.push(c); |
| 58 | }); |
| 59 | } |
| 60 | return list; |
| 61 | }, []); |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 62 | this.states = _.uniqBy(this.states, 'state'); |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 63 | }, 500); |
| 64 | |
| 65 | // listen for keypress |
| 66 | $(document).on('keyup', (e) => { |
| 67 | if (e.key === 'f') { |
| 68 | $('.navbar-form input').focus(); |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | // redirect to selected page |
| 73 | this.routeSelected = (item: IXosNavigationRoute) => { |
Matteo Scandolo | ac8c8c2 | 2017-01-09 15:04:32 -0800 | [diff] [blame] | 74 | console.log(`go to: ${item.state}`); |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 75 | this.$state.go(item.state); |
| 76 | this.query = null; |
| 77 | }; |
| 78 | |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 79 | this.userEmail = this.authService.getUser() ? this.authService.getUser().email : ''; |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 80 | |
| 81 | this.syncStore.query() |
| 82 | .subscribe( |
| 83 | (event: IWSEvent) => { |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 84 | $scope.$evalAsync(() => { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 85 | let toastrMsg: string; |
| 86 | let toastrLevel: string; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 87 | if (event.msg.object.backend_status.indexOf('0') > -1) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 88 | toastrMsg = 'Synchronization started for:'; |
| 89 | toastrLevel = 'info'; |
| 90 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 91 | else if (event.msg.object.backend_status.indexOf('1') > -1) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 92 | toastrMsg = 'Synchronization succedeed for:'; |
| 93 | toastrLevel = 'success'; |
| 94 | } |
| 95 | else if (event.msg.object.backend_status.indexOf('2') > -1) { |
| 96 | toastrMsg = 'Synchronization failed for:'; |
| 97 | toastrLevel = 'error'; |
| 98 | } |
| 99 | |
| 100 | if (toastrLevel && toastrMsg) { |
| 101 | this.toastr[toastrLevel](`${toastrMsg} ${event.msg.object.name}`, event.model); |
| 102 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 103 | // this.notifications.unshift(event); |
| 104 | // this.newNotifications = this.getNewNotifications(this.notifications); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 105 | }); |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 106 | } |
| 107 | ); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 108 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 109 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 110 | public getLogo(): string { |
| 111 | return require(`../../images/brand/${StyleConfig.logo}`); |
| 112 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 113 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 114 | // TODO display a list of notification in the template (if it make sense) |
| 115 | // public viewNotification = (notification: INotification) => { |
| 116 | // notification.viewed = true; |
| 117 | // this.newNotifications = this.getNewNotifications(this.notifications); |
| 118 | // }; |
| 119 | // |
| 120 | // private getNewNotifications = (notifications: INotification[]) => { |
| 121 | // return this.notifications.filter((n: INotification) => { |
| 122 | // return !n.viewed; |
| 123 | // }); |
| 124 | // }; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | export const xosHeader: angular.IComponentOptions = { |
| 128 | template: require('./header.html'), |
| 129 | controllerAs: 'vm', |
| 130 | controller: HeaderController |
| 131 | }; |