blob: 2aef872ab6df8110e6eefef22b912766eaf3edfa [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 Scandoloa8a6fbb2016-12-21 16:59:08 -08005import {IXosAuthService} from '../../datasources/rest/auth.rest';
Matteo Scandolo67c105f2017-01-09 09:30:52 -08006import {IXosNavigationService, IXosNavigationRoute} from '../services/navigation';
7import {IStateService} from 'angular-ui-router';
8import * as _ from 'lodash';
9import * as $ from 'jquery';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080010
Matteo Scandolo52fa5cb2016-12-16 10:06:13 -080011export interface INotification extends IWSEvent {
Matteo Scandoloc719e792016-12-14 15:48:31 -080012 viewed?: boolean;
13}
14
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080015class HeaderController {
Matteo Scandolo67c105f2017-01-09 09:30:52 -080016 static $inject = ['$scope', '$state', 'AuthService', 'SynchronizerStore', 'toastr', 'toastrConfig', 'NavigationService'];
Matteo Scandoloc719e792016-12-14 15:48:31 -080017 public notifications: INotification[] = [];
18 public newNotifications: INotification[] = [];
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080019 public version: string;
20 public userEmail: string;
Matteo Scandolo67c105f2017-01-09 09:30:52 -080021 public routeSelected: (route: IXosNavigationRoute) => void;
22 public states: IXosNavigationRoute[];
23 public query: string;
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080024
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080025 constructor(
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080026 private $scope: angular.IScope,
Matteo Scandolo67c105f2017-01-09 09:30:52 -080027 private $state: IStateService,
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080028 private authService: IXosAuthService,
Matteo Scandolo266907e2016-12-20 13:41:42 -080029 private syncStore: IStoreService,
30 private toastr: ng.toastr.IToastrService,
Matteo Scandolo67c105f2017-01-09 09:30:52 -080031 private toastrConfig: ng.toastr.IToastrConfig,
32 private NavigationService: IXosNavigationService
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080033 ) {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080034 this.version = require('../../../../package.json').version;
Matteo Scandolo266907e2016-12-20 13:41:42 -080035 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 Scandolo67c105f2017-01-09 09:30:52 -080047 // 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 }, []);
62 console.log(this.states.length);
63 this.states = _.uniqBy(this.states, 'state');
64 console.log(this.states.length);
65 }, 500);
66
67 // listen for keypress
68 $(document).on('keyup', (e) => {
69 if (e.key === 'f') {
70 $('.navbar-form input').focus();
71 }
72 });
73
74 // redirect to selected page
75 this.routeSelected = (item: IXosNavigationRoute) => {
76 this.$state.go(item.state);
77 this.query = null;
78 };
79
Matteo Scandolod62ea792016-12-22 14:02:28 -080080 this.userEmail = this.authService.getUser() ? this.authService.getUser().email : '';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080081
82 this.syncStore.query()
83 .subscribe(
84 (event: IWSEvent) => {
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080085 $scope.$evalAsync(() => {
Matteo Scandolo266907e2016-12-20 13:41:42 -080086 let toastrMsg: string;
87 let toastrLevel: string;
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080088 if (event.msg.object.backend_status.indexOf('0') > -1) {
Matteo Scandolo266907e2016-12-20 13:41:42 -080089 toastrMsg = 'Synchronization started for:';
90 toastrLevel = 'info';
91 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080092 else if (event.msg.object.backend_status.indexOf('1') > -1) {
Matteo Scandolo266907e2016-12-20 13:41:42 -080093 toastrMsg = 'Synchronization succedeed for:';
94 toastrLevel = 'success';
95 }
96 else if (event.msg.object.backend_status.indexOf('2') > -1) {
97 toastrMsg = 'Synchronization failed for:';
98 toastrLevel = 'error';
99 }
100
101 if (toastrLevel && toastrMsg) {
102 this.toastr[toastrLevel](`${toastrMsg} ${event.msg.object.name}`, event.model);
103 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800104 // this.notifications.unshift(event);
105 // this.newNotifications = this.getNewNotifications(this.notifications);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800106 });
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800107 }
108 );
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800109 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800110
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800111 public getLogo(): string {
112 return require(`../../images/brand/${StyleConfig.logo}`);
113 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800114
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800115 // TODO display a list of notification in the template (if it make sense)
116 // public viewNotification = (notification: INotification) => {
117 // notification.viewed = true;
118 // this.newNotifications = this.getNewNotifications(this.notifications);
119 // };
120 //
121 // private getNewNotifications = (notifications: INotification[]) => {
122 // return this.notifications.filter((n: INotification) => {
123 // return !n.viewed;
124 // });
125 // };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800126}
127
128export const xosHeader: angular.IComponentOptions = {
129 template: require('./header.html'),
130 controllerAs: 'vm',
131 controller: HeaderController
132};