Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 19 | import './header.scss'; |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 20 | import {IWSEvent} from '../../datasources/websocket/global'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 21 | import {IStoreService} from '../../datasources/stores/synchronizer.store'; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 22 | import {IXosAuthService} from '../../datasources/rest/auth.rest'; |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 23 | import {IXosNavigationService, IXosNavigationRoute} from '../services/navigation'; |
| 24 | import {IStateService} from 'angular-ui-router'; |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 25 | import * as $ from 'jquery'; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 26 | import {IXosStyleConfig} from '../../../index'; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 27 | import {IXosSearchService, IXosSearchResult} from '../../datasources/helpers/search.service'; |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 28 | import {IXosKeyboardShortcutService} from '../services/keyboard-shortcut'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 29 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 30 | export interface INotification extends IWSEvent { |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 31 | viewed?: boolean; |
| 32 | } |
| 33 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 34 | class HeaderController { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 35 | 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] | 36 | public notifications: INotification[] = []; |
| 37 | public newNotifications: INotification[] = []; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 38 | public version: string; |
| 39 | public userEmail: string; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 40 | public routeSelected: (route: IXosSearchResult) => void; |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 41 | public states: IXosNavigationRoute[]; |
| 42 | public query: string; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 43 | public search: (query: string) => any[]; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 44 | |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 45 | constructor( |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 46 | private $scope: angular.IScope, |
Matteo Scandolo | fc17099 | 2017-01-12 18:20:24 -0800 | [diff] [blame] | 47 | private $rootScope: ng.IScope, |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 48 | private $state: IStateService, |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 49 | private authService: IXosAuthService, |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 50 | private syncStore: IStoreService, |
| 51 | private toastr: ng.toastr.IToastrService, |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 52 | private toastrConfig: ng.toastr.IToastrConfig, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 53 | private NavigationService: IXosNavigationService, |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 54 | private StyleConfig: IXosStyleConfig, |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 55 | private SearchService: IXosSearchService, |
| 56 | private XosKeyboardShortcut: IXosKeyboardShortcutService |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 57 | ) { |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 58 | this.version = require('../../../../package.json').version; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 59 | angular.extend(this.toastrConfig, { |
| 60 | newestOnTop: false, |
| 61 | positionClass: 'toast-top-right', |
| 62 | preventDuplicates: false, |
| 63 | preventOpenDuplicates: false, |
| 64 | progressBar: true, |
| 65 | // autoDismiss: false, |
| 66 | // closeButton: false, |
| 67 | // timeOut: 0, |
| 68 | // tapToDismiss: false |
| 69 | }); |
| 70 | |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 71 | this.search = (query: string) => { |
| 72 | return this.SearchService.search(query); |
| 73 | }; |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 74 | |
| 75 | // listen for keypress |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 76 | this.XosKeyboardShortcut.registerKeyBinding({ |
Matteo Scandolo | c817849 | 2017-04-11 17:55:13 -0700 | [diff] [blame] | 77 | key: 'F', |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 78 | description: 'Select search box', |
| 79 | cb: () => { |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 80 | $('.navbar-form input').focus(); |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 81 | }, |
| 82 | }, 'global'); |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 83 | |
| 84 | // redirect to selected page |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 85 | this.routeSelected = (item: IXosSearchResult) => { |
| 86 | if (angular.isString(item.state)) { |
| 87 | this.$state.go(item.state); |
| 88 | } |
| 89 | else { |
| 90 | this.$state.go(item.state.name, item.state.params); |
| 91 | } |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 92 | this.query = null; |
| 93 | }; |
| 94 | |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 95 | this.userEmail = this.authService.getUser() ? this.authService.getUser().email : ''; |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 96 | |
| 97 | this.syncStore.query() |
| 98 | .subscribe( |
| 99 | (event: IWSEvent) => { |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 100 | $scope.$evalAsync(() => { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 101 | let toastrMsg: string; |
| 102 | let toastrLevel: string; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 103 | if (event.msg.object.backend_status.indexOf('0') > -1) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 104 | toastrMsg = 'Synchronization started for:'; |
| 105 | toastrLevel = 'info'; |
| 106 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 107 | else if (event.msg.object.backend_status.indexOf('1') > -1) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 108 | toastrMsg = 'Synchronization succedeed for:'; |
| 109 | toastrLevel = 'success'; |
| 110 | } |
| 111 | else if (event.msg.object.backend_status.indexOf('2') > -1) { |
| 112 | toastrMsg = 'Synchronization failed for:'; |
| 113 | toastrLevel = 'error'; |
| 114 | } |
| 115 | |
| 116 | if (toastrLevel && toastrMsg) { |
| 117 | this.toastr[toastrLevel](`${toastrMsg} ${event.msg.object.name}`, event.model); |
| 118 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 119 | // this.notifications.unshift(event); |
| 120 | // this.newNotifications = this.getNewNotifications(this.notifications); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 121 | }); |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 122 | } |
| 123 | ); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 124 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 125 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 126 | public getLogo(): string { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 127 | return require(`../../images/brand/${this.StyleConfig.logo}`); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 128 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 129 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 130 | // TODO display a list of notification in the template (if it make sense) |
| 131 | // public viewNotification = (notification: INotification) => { |
| 132 | // notification.viewed = true; |
| 133 | // this.newNotifications = this.getNewNotifications(this.notifications); |
| 134 | // }; |
| 135 | // |
| 136 | // private getNewNotifications = (notifications: INotification[]) => { |
| 137 | // return this.notifications.filter((n: INotification) => { |
| 138 | // return !n.viewed; |
| 139 | // }); |
| 140 | // }; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | export const xosHeader: angular.IComponentOptions = { |
| 144 | template: require('./header.html'), |
| 145 | controllerAs: 'vm', |
| 146 | controller: HeaderController |
| 147 | }; |