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 | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 29 | import {Subscription} from 'rxjs'; |
| 30 | import {IXosConfigHelpersService} from '../services/helpers/config.helpers'; |
Matteo Scandolo | 71d74a4 | 2017-10-11 15:38:54 -0700 | [diff] [blame] | 31 | import {IXosDebugService} from '../debug/debug.service'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 32 | |
Matteo Scandolo | 52fa5cb | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 33 | export interface INotification extends IWSEvent { |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 34 | viewed?: boolean; |
| 35 | } |
| 36 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 37 | class HeaderController { |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 38 | static $inject = [ |
| 39 | '$log', |
| 40 | '$scope', |
| 41 | '$rootScope', |
| 42 | '$state', |
| 43 | 'AuthService', |
| 44 | 'SynchronizerStore', |
| 45 | 'toastr', |
| 46 | 'toastrConfig', |
| 47 | 'XosNavigationService', |
| 48 | 'StyleConfig', |
| 49 | 'SearchService', |
| 50 | 'XosKeyboardShortcut', |
Matteo Scandolo | 71d74a4 | 2017-10-11 15:38:54 -0700 | [diff] [blame] | 51 | 'ConfigHelpers', |
| 52 | 'XosDebug' |
Matteo Scandolo | e6f9c35 | 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 | e6f9c35 | 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 | e6f9c35 | 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 | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 77 | private XosKeyboardShortcut: IXosKeyboardShortcutService, |
Matteo Scandolo | 71d74a4 | 2017-10-11 15:38:54 -0700 | [diff] [blame] | 78 | private ConfigHelpers: IXosConfigHelpersService, |
| 79 | private XosDebugService: IXosDebugService |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 80 | ) { |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 81 | |
| 82 | } |
| 83 | |
| 84 | $onInit() { |
| 85 | this.$log.info('[XosHeader] Setup'); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 86 | this.version = require('../../../../package.json').version; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 87 | angular.extend(this.toastrConfig, { |
| 88 | newestOnTop: false, |
| 89 | positionClass: 'toast-top-right', |
| 90 | preventDuplicates: false, |
| 91 | preventOpenDuplicates: false, |
| 92 | progressBar: true, |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 93 | onTap: (toast) => { |
| 94 | this.$state.go(toast.scope.extraData.dest.name, toast.scope.extraData.dest.params); |
| 95 | } |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 96 | }); |
| 97 | |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 98 | this.search = (query: string) => { |
| 99 | return this.SearchService.search(query); |
| 100 | }; |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 101 | |
| 102 | // listen for keypress |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 103 | this.XosKeyboardShortcut.registerKeyBinding({ |
Matteo Scandolo | c817849 | 2017-04-11 17:55:13 -0700 | [diff] [blame] | 104 | key: 'F', |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 105 | description: 'Select search box', |
| 106 | cb: () => { |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 107 | $('.navbar-form input').focus(); |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 108 | }, |
| 109 | }, 'global'); |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 110 | |
| 111 | // redirect to selected page |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 112 | this.routeSelected = (item: IXosSearchResult) => { |
| 113 | if (angular.isString(item.state)) { |
| 114 | this.$state.go(item.state); |
| 115 | } |
| 116 | else { |
| 117 | this.$state.go(item.state.name, item.state.params); |
| 118 | } |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 119 | this.query = null; |
| 120 | }; |
| 121 | |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 122 | this.userEmail = this.authService.getUser() ? this.authService.getUser().email : ''; |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 123 | |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 124 | this.syncStoreSubscription = this.syncStore.query() |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 125 | .subscribe( |
| 126 | (event: IWSEvent) => { |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 127 | this.$scope.$evalAsync(() => { |
| 128 | |
Matteo Scandolo | 71d74a4 | 2017-10-11 15:38:54 -0700 | [diff] [blame] | 129 | if (!this.XosDebugService.status.notifications) { |
| 130 | // NOTE: notifications can be disabled |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 135 | if (event.model === 'Diag') { |
| 136 | // NOTE skip notifications for Diag model |
Matteo Scandolo | fe0d410 | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 137 | // this should not arrive, but a check won't harm |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
Matteo Scandolo | fe0d410 | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 141 | const isRemoval: boolean = event.deleted || false; |
| 142 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 143 | let toastrMsg: string; |
| 144 | let toastrLevel: string; |
Matteo Scandolo | fe0d410 | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 145 | if (!isRemoval) { |
| 146 | if (event.msg.object.backend_code === 0) { |
| 147 | toastrMsg = 'Synchronization in progress for:'; |
| 148 | toastrLevel = 'info'; |
| 149 | } |
| 150 | else if (event.msg.object.backend_code === 1) { |
| 151 | toastrMsg = 'Synchronization succedeed for:'; |
| 152 | toastrLevel = 'success'; |
| 153 | } |
| 154 | else if (event.msg.object.backend_code === 2) { |
| 155 | toastrMsg = 'Synchronization failed for:'; |
| 156 | toastrLevel = 'error'; |
| 157 | } |
| 158 | } |
| 159 | else { |
| 160 | toastrMsg = 'Deleted object:'; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 161 | toastrLevel = 'info'; |
| 162 | } |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 163 | |
| 164 | if (toastrLevel && toastrMsg) { |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 165 | let modelName = event.msg.object.name; |
| 166 | let modelClassName = event.model; |
| 167 | if (angular.isUndefined(event.msg.object.name) || event.msg.object.name === null) { |
Matteo Scandolo | fe0d410 | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 168 | modelName = `${modelClassName} [${event.msg.object.id}]`; |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | const dest = this.ConfigHelpers.stateWithParamsForJs(modelClassName, event.msg.object); |
| 172 | |
| 173 | if (!event.skip_notification) { |
| 174 | this.toastr[toastrLevel](`${toastrMsg} ${modelName}`, modelClassName, {extraData: {dest: dest}}); |
| 175 | } |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 176 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 177 | // this.notifications.unshift(event); |
| 178 | // this.newNotifications = this.getNewNotifications(this.notifications); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 179 | }); |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 180 | } |
| 181 | ); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 182 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 183 | |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 184 | $onDestroy() { |
| 185 | this.$log.info('[XosHeader] Teardown'); |
| 186 | this.syncStoreSubscription.unsubscribe(); |
| 187 | } |
| 188 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 189 | public getLogo(): string { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 190 | return require(`../../images/brand/${this.StyleConfig.logo}`); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 191 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 192 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 193 | // TODO display a list of notification in the template (if it make sense) |
| 194 | // public viewNotification = (notification: INotification) => { |
| 195 | // notification.viewed = true; |
| 196 | // this.newNotifications = this.getNewNotifications(this.notifications); |
| 197 | // }; |
| 198 | // |
| 199 | // private getNewNotifications = (notifications: INotification[]) => { |
| 200 | // return this.notifications.filter((n: INotification) => { |
| 201 | // return !n.viewed; |
| 202 | // }); |
| 203 | // }; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | export const xosHeader: angular.IComponentOptions = { |
| 207 | template: require('./header.html'), |
| 208 | controllerAs: 'vm', |
| 209 | controller: HeaderController |
| 210 | }; |