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 | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 29 | import {Subscription} from 'rxjs'; |
| 30 | import {IXosConfigHelpersService} from '../services/helpers/config.helpers'; |
Matteo Scandolo | f371725 | 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 | 31daa80 | 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 | f371725 | 2017-10-11 15:38:54 -0700 | [diff] [blame] | 51 | 'ConfigHelpers', |
| 52 | 'XosDebug' |
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, |
| 79 | private XosDebugService: IXosDebugService |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 80 | ) { |
Matteo Scandolo | 31daa80 | 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 | 31daa80 | 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 | 31daa80 | 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 | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 127 | this.$scope.$evalAsync(() => { |
| 128 | |
Matteo Scandolo | f371725 | 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 | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 135 | if (event.model === 'Diag') { |
| 136 | // NOTE skip notifications for Diag model |
| 137 | return; |
| 138 | } |
| 139 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 140 | let toastrMsg: string; |
| 141 | let toastrLevel: string; |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 142 | if (event.msg.object.backend_code === 0) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 143 | toastrMsg = 'Synchronization started for:'; |
| 144 | toastrLevel = 'info'; |
| 145 | } |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 146 | else if (event.msg.object.backend_code === 1) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 147 | toastrMsg = 'Synchronization succedeed for:'; |
| 148 | toastrLevel = 'success'; |
| 149 | } |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 150 | else if (event.msg.object.backend_code === 2) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 151 | toastrMsg = 'Synchronization failed for:'; |
| 152 | toastrLevel = 'error'; |
| 153 | } |
| 154 | |
| 155 | if (toastrLevel && toastrMsg) { |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 156 | let modelName = event.msg.object.name; |
| 157 | let modelClassName = event.model; |
| 158 | if (angular.isUndefined(event.msg.object.name) || event.msg.object.name === null) { |
| 159 | modelName = `${event.msg.object.leaf_model_name} [${event.msg.object.id}]`; |
| 160 | } |
| 161 | |
| 162 | const dest = this.ConfigHelpers.stateWithParamsForJs(modelClassName, event.msg.object); |
| 163 | |
| 164 | if (!event.skip_notification) { |
| 165 | this.toastr[toastrLevel](`${toastrMsg} ${modelName}`, modelClassName, {extraData: {dest: dest}}); |
| 166 | } |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 167 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 168 | // this.notifications.unshift(event); |
| 169 | // this.newNotifications = this.getNewNotifications(this.notifications); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 170 | }); |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 171 | } |
| 172 | ); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 173 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 174 | |
Matteo Scandolo | 31daa80 | 2017-09-01 12:19:56 -0700 | [diff] [blame] | 175 | $onDestroy() { |
| 176 | this.$log.info('[XosHeader] Teardown'); |
| 177 | this.syncStoreSubscription.unsubscribe(); |
| 178 | } |
| 179 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 180 | public getLogo(): string { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 181 | return require(`../../images/brand/${this.StyleConfig.logo}`); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 182 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 183 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 184 | // TODO display a list of notification in the template (if it make sense) |
| 185 | // public viewNotification = (notification: INotification) => { |
| 186 | // notification.viewed = true; |
| 187 | // this.newNotifications = this.getNewNotifications(this.notifications); |
| 188 | // }; |
| 189 | // |
| 190 | // private getNewNotifications = (notifications: INotification[]) => { |
| 191 | // return this.notifications.filter((n: INotification) => { |
| 192 | // return !n.viewed; |
| 193 | // }); |
| 194 | // }; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | export const xosHeader: angular.IComponentOptions = { |
| 198 | template: require('./header.html'), |
| 199 | controllerAs: 'vm', |
| 200 | controller: HeaderController |
| 201 | }; |