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