blob: 0a984942c5f54a09a95a34832a4f03bcef3afd1b [file] [log] [blame]
Matteo Scandolo63e43eb2016-12-14 14:18:53 -08001import './header.scss';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -08002import {IWSEvent} from '../../datasources/websocket/global';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08003import {IStoreService} from '../../datasources/stores/synchronizer.store';
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -08004import {IXosAuthService} from '../../datasources/rest/auth.rest';
Matteo Scandolo67c105f2017-01-09 09:30:52 -08005import {IXosNavigationService, IXosNavigationRoute} from '../services/navigation';
6import {IStateService} from 'angular-ui-router';
7import * as _ from 'lodash';
8import * as $ from 'jquery';
Matteo Scandolo828d1e82017-01-17 14:49:38 -08009import {IXosStyleConfig} from '../../../index';
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 Scandolo828d1e82017-01-17 14:49:38 -080016 static $inject = ['$scope', '$rootScope', '$state', 'AuthService', 'SynchronizerStore', 'toastr', 'toastrConfig', 'NavigationService', 'StyleConfig'];
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 Scandolofc170992017-01-12 18:20:24 -080027 private $rootScope: ng.IScope,
Matteo Scandolo67c105f2017-01-09 09:30:52 -080028 private $state: IStateService,
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080029 private authService: IXosAuthService,
Matteo Scandolo266907e2016-12-20 13:41:42 -080030 private syncStore: IStoreService,
31 private toastr: ng.toastr.IToastrService,
Matteo Scandolo67c105f2017-01-09 09:30:52 -080032 private toastrConfig: ng.toastr.IToastrConfig,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080033 private NavigationService: IXosNavigationService,
34 private StyleConfig: IXosStyleConfig
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080035 ) {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080036 this.version = require('../../../../package.json').version;
Matteo Scandolo266907e2016-12-20 13:41:42 -080037 angular.extend(this.toastrConfig, {
38 newestOnTop: false,
39 positionClass: 'toast-top-right',
40 preventDuplicates: false,
41 preventOpenDuplicates: false,
42 progressBar: true,
43 // autoDismiss: false,
44 // closeButton: false,
45 // timeOut: 0,
46 // tapToDismiss: false
47 });
48
Matteo Scandolofc170992017-01-12 18:20:24 -080049 this.$rootScope.$on('xos.core.modelSetup', () => {
Matteo Scandolo67c105f2017-01-09 09:30:52 -080050 this.states = this.NavigationService.query().reduce((list, state) => {
51 // if it does not have child (otherwise it is abstract)
52 if (!state.children || state.children.length === 0) {
53 list.push(state);
54 }
55 // else push child
56 if (state.children && state.children.length > 0) {
57 state.children.forEach(c => {
58 list.push(c);
59 });
60 }
61 return list;
62 }, []);
Matteo Scandolo67c105f2017-01-09 09:30:52 -080063 this.states = _.uniqBy(this.states, 'state');
Matteo Scandolofc170992017-01-12 18:20:24 -080064 });
Matteo Scandolo67c105f2017-01-09 09:30:52 -080065
66 // listen for keypress
67 $(document).on('keyup', (e) => {
68 if (e.key === 'f') {
69 $('.navbar-form input').focus();
70 }
71 });
72
73 // redirect to selected page
74 this.routeSelected = (item: IXosNavigationRoute) => {
75 this.$state.go(item.state);
76 this.query = null;
77 };
78
Matteo Scandolod62ea792016-12-22 14:02:28 -080079 this.userEmail = this.authService.getUser() ? this.authService.getUser().email : '';
Matteo Scandolo63e43eb2016-12-14 14:18:53 -080080
81 this.syncStore.query()
82 .subscribe(
83 (event: IWSEvent) => {
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080084 $scope.$evalAsync(() => {
Matteo Scandolo266907e2016-12-20 13:41:42 -080085 let toastrMsg: string;
86 let toastrLevel: string;
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080087 if (event.msg.object.backend_status.indexOf('0') > -1) {
Matteo Scandolo266907e2016-12-20 13:41:42 -080088 toastrMsg = 'Synchronization started for:';
89 toastrLevel = 'info';
90 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080091 else if (event.msg.object.backend_status.indexOf('1') > -1) {
Matteo Scandolo266907e2016-12-20 13:41:42 -080092 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 Scandoloa8a6fbb2016-12-21 16:59:08 -0800103 // this.notifications.unshift(event);
104 // this.newNotifications = this.getNewNotifications(this.notifications);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800105 });
Matteo Scandolo63e43eb2016-12-14 14:18:53 -0800106 }
107 );
Matteo Scandolof6acdbe2016-12-13 10:29:37 -0800108 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800109
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800110 public getLogo(): string {
Matteo Scandolo828d1e82017-01-17 14:49:38 -0800111 return require(`../../images/brand/${this.StyleConfig.logo}`);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800112 }
Matteo Scandoloc719e792016-12-14 15:48:31 -0800113
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800114 // 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 Scandolof6acdbe2016-12-13 10:29:37 -0800125}
126
127export const xosHeader: angular.IComponentOptions = {
128 template: require('./header.html'),
129 controllerAs: 'vm',
130 controller: HeaderController
131};