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