Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 17 | import './header.scss'; |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 18 | import {IWSEvent} from '../../datasources/websocket/global'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 19 | import {IStoreService} from '../../datasources/stores/synchronizer.store'; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 20 | import {IXosAuthService} from '../../datasources/rest/auth.rest'; |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 21 | import {IXosNavigationService, IXosNavigationRoute} from '../services/navigation'; |
| 22 | import {IStateService} from 'angular-ui-router'; |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 23 | import * as $ from 'jquery'; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 24 | import {IXosStyleConfig} from '../../../index'; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 25 | import {IXosSearchService, IXosSearchResult} from '../../datasources/helpers/search.service'; |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 26 | import {IXosKeyboardShortcutService} from '../services/keyboard-shortcut'; |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 27 | import {Subscription} from 'rxjs'; |
| 28 | import {IXosConfigHelpersService} from '../services/helpers/config.helpers'; |
Matteo Scandolo | f371725 | 2017-10-11 15:38:54 -0700 | [diff] [blame] | 29 | import {IXosDebugService} from '../debug/debug.service'; |
Matteo Scandolo | 57aee47 | 2018-06-28 15:24:42 -0700 | [diff] [blame] | 30 | import {IXosVersionReaderService} from '../services/version-reader'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 31 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 32 | export interface INotification extends IWSEvent { |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 33 | viewed?: boolean; |
| 34 | } |
| 35 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 36 | class HeaderController { |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 37 | static $inject = [ |
| 38 | '$log', |
| 39 | '$scope', |
| 40 | '$rootScope', |
| 41 | '$state', |
| 42 | 'AuthService', |
| 43 | 'SynchronizerStore', |
| 44 | 'toastr', |
| 45 | 'toastrConfig', |
| 46 | 'XosNavigationService', |
| 47 | 'StyleConfig', |
| 48 | 'SearchService', |
| 49 | 'XosKeyboardShortcut', |
Matteo Scandolo | f371725 | 2017-10-11 15:38:54 -0700 | [diff] [blame] | 50 | 'ConfigHelpers', |
Matteo Scandolo | 57aee47 | 2018-06-28 15:24:42 -0700 | [diff] [blame] | 51 | 'XosDebug', |
| 52 | 'XosVersionReaderService' |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 53 | ]; |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 54 | public notifications: INotification[] = []; |
| 55 | public newNotifications: INotification[] = []; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 56 | public version: string; |
| 57 | public userEmail: string; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 58 | public routeSelected: (route: IXosSearchResult) => void; |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 59 | public states: IXosNavigationRoute[]; |
| 60 | public query: string; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 61 | public search: (query: string) => any[]; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 62 | |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 63 | private syncStoreSubscription: Subscription; |
| 64 | |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 65 | constructor( |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 66 | private $log: ng.ILogService, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 67 | private $scope: angular.IScope, |
Matteo Scandolo | fc17099 | 2017-01-12 18:20:24 -0800 | [diff] [blame] | 68 | private $rootScope: ng.IScope, |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 69 | private $state: IStateService, |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 70 | private authService: IXosAuthService, |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 71 | private syncStore: IStoreService, |
| 72 | private toastr: ng.toastr.IToastrService, |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 73 | private toastrConfig: ng.toastr.IToastrConfig, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 74 | private NavigationService: IXosNavigationService, |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 75 | private StyleConfig: IXosStyleConfig, |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 76 | private SearchService: IXosSearchService, |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 77 | private XosKeyboardShortcut: IXosKeyboardShortcutService, |
Matteo Scandolo | f371725 | 2017-10-11 15:38:54 -0700 | [diff] [blame] | 78 | private ConfigHelpers: IXosConfigHelpersService, |
Matteo Scandolo | 57aee47 | 2018-06-28 15:24:42 -0700 | [diff] [blame] | 79 | private XosDebugService: IXosDebugService, |
| 80 | private XosVersionReaderService: IXosVersionReaderService |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 81 | ) { |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 82 | |
| 83 | } |
| 84 | |
| 85 | $onInit() { |
| 86 | this.$log.info('[XosHeader] Setup'); |
Matteo Scandolo | 57aee47 | 2018-06-28 15:24:42 -0700 | [diff] [blame] | 87 | |
| 88 | this.XosVersionReaderService.getVersion() |
| 89 | .then((version: string) => { |
| 90 | this.version = version; |
| 91 | }); |
| 92 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 93 | angular.extend(this.toastrConfig, { |
| 94 | newestOnTop: false, |
| 95 | positionClass: 'toast-top-right', |
| 96 | preventDuplicates: false, |
| 97 | preventOpenDuplicates: false, |
| 98 | progressBar: true, |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 99 | onTap: (toast) => { |
| 100 | this.$state.go(toast.scope.extraData.dest.name, toast.scope.extraData.dest.params); |
| 101 | } |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 102 | }); |
| 103 | |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 104 | this.search = (query: string) => { |
| 105 | return this.SearchService.search(query); |
| 106 | }; |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 107 | |
| 108 | // listen for keypress |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 109 | this.XosKeyboardShortcut.registerKeyBinding({ |
Matteo Scandolo | c817849 | 2017-04-11 17:55:13 -0700 | [diff] [blame] | 110 | key: 'F', |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 111 | description: 'Select search box', |
| 112 | cb: () => { |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 113 | $('.navbar-form input').focus(); |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 114 | }, |
| 115 | }, 'global'); |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 116 | |
| 117 | // redirect to selected page |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 118 | this.routeSelected = (item: IXosSearchResult) => { |
| 119 | if (angular.isString(item.state)) { |
| 120 | this.$state.go(item.state); |
| 121 | } |
| 122 | else { |
| 123 | this.$state.go(item.state.name, item.state.params); |
| 124 | } |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 125 | this.query = null; |
| 126 | }; |
| 127 | |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 128 | this.userEmail = this.authService.getUser() ? this.authService.getUser().email : ''; |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 129 | |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 130 | this.syncStoreSubscription = this.syncStore.query() |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 131 | .subscribe( |
| 132 | (event: IWSEvent) => { |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 133 | this.$scope.$evalAsync(() => { |
| 134 | |
Matteo Scandolo | f371725 | 2017-10-11 15:38:54 -0700 | [diff] [blame] | 135 | if (!this.XosDebugService.status.notifications) { |
| 136 | // NOTE: notifications can be disabled |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 141 | if (event.model === 'Diag') { |
| 142 | // NOTE skip notifications for Diag model |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 143 | // this should not arrive, but a check won't harm |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 144 | return; |
| 145 | } |
| 146 | |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 147 | const isRemoval: boolean = event.deleted || false; |
| 148 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 149 | let toastrMsg: string; |
| 150 | let toastrLevel: string; |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 151 | if (!isRemoval) { |
| 152 | if (event.msg.object.backend_code === 0) { |
| 153 | toastrMsg = 'Synchronization in progress for:'; |
| 154 | toastrLevel = 'info'; |
| 155 | } |
| 156 | else if (event.msg.object.backend_code === 1) { |
| 157 | toastrMsg = 'Synchronization succedeed for:'; |
| 158 | toastrLevel = 'success'; |
| 159 | } |
| 160 | else if (event.msg.object.backend_code === 2) { |
| 161 | toastrMsg = 'Synchronization failed for:'; |
| 162 | toastrLevel = 'error'; |
| 163 | } |
| 164 | } |
| 165 | else { |
| 166 | toastrMsg = 'Deleted object:'; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 167 | toastrLevel = 'info'; |
| 168 | } |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 169 | |
| 170 | if (toastrLevel && toastrMsg) { |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 171 | let modelName = event.msg.object.name; |
| 172 | let modelClassName = event.model; |
| 173 | if (angular.isUndefined(event.msg.object.name) || event.msg.object.name === null) { |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 174 | modelName = `${modelClassName} [${event.msg.object.id}]`; |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Zack Williams | 2d0e29e | 2018-03-27 10:51:07 -0700 | [diff] [blame] | 177 | const dest = this.ConfigHelpers.stateWithParamsForJs(modelClassName, event.msg.object.id); |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 178 | |
| 179 | if (!event.skip_notification) { |
| 180 | this.toastr[toastrLevel](`${toastrMsg} ${modelName}`, modelClassName, {extraData: {dest: dest}}); |
| 181 | } |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 182 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 183 | // this.notifications.unshift(event); |
| 184 | // this.newNotifications = this.getNewNotifications(this.notifications); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 185 | }); |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 186 | } |
| 187 | ); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 188 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 189 | |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 190 | $onDestroy() { |
| 191 | this.$log.info('[XosHeader] Teardown'); |
| 192 | this.syncStoreSubscription.unsubscribe(); |
| 193 | } |
| 194 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 195 | public getLogo(): string { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 196 | return require(`../../images/brand/${this.StyleConfig.logo}`); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 197 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 198 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 199 | // TODO display a list of notification in the template (if it make sense) |
| 200 | // public viewNotification = (notification: INotification) => { |
| 201 | // notification.viewed = true; |
| 202 | // this.newNotifications = this.getNewNotifications(this.notifications); |
| 203 | // }; |
| 204 | // |
| 205 | // private getNewNotifications = (notifications: INotification[]) => { |
| 206 | // return this.notifications.filter((n: INotification) => { |
| 207 | // return !n.viewed; |
| 208 | // }); |
| 209 | // }; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | export const xosHeader: angular.IComponentOptions = { |
| 213 | template: require('./header.html'), |
| 214 | controllerAs: 'vm', |
| 215 | controller: HeaderController |
| 216 | }; |