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 | 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 | e6f9c35 | 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', |
| 50 | 'ConfigHelpers' |
| 51 | ]; |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 52 | public notifications: INotification[] = []; |
| 53 | public newNotifications: INotification[] = []; |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 54 | public version: string; |
| 55 | public userEmail: string; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 56 | public routeSelected: (route: IXosSearchResult) => void; |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 57 | public states: IXosNavigationRoute[]; |
| 58 | public query: string; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 59 | public search: (query: string) => any[]; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 60 | |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame^] | 61 | private syncStoreSubscription: Subscription; |
| 62 | |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 63 | constructor( |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame^] | 64 | private $log: ng.ILogService, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 65 | private $scope: angular.IScope, |
Matteo Scandolo | fc17099 | 2017-01-12 18:20:24 -0800 | [diff] [blame] | 66 | private $rootScope: ng.IScope, |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 67 | private $state: IStateService, |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 68 | private authService: IXosAuthService, |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 69 | private syncStore: IStoreService, |
| 70 | private toastr: ng.toastr.IToastrService, |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 71 | private toastrConfig: ng.toastr.IToastrConfig, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 72 | private NavigationService: IXosNavigationService, |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 73 | private StyleConfig: IXosStyleConfig, |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 74 | private SearchService: IXosSearchService, |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame^] | 75 | private XosKeyboardShortcut: IXosKeyboardShortcutService, |
| 76 | private ConfigHelpers: IXosConfigHelpersService |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 77 | ) { |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame^] | 78 | |
| 79 | } |
| 80 | |
| 81 | $onInit() { |
| 82 | this.$log.info('[XosHeader] Setup'); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 83 | this.version = require('../../../../package.json').version; |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 84 | angular.extend(this.toastrConfig, { |
| 85 | newestOnTop: false, |
| 86 | positionClass: 'toast-top-right', |
| 87 | preventDuplicates: false, |
| 88 | preventOpenDuplicates: false, |
| 89 | progressBar: true, |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame^] | 90 | onTap: (toast) => { |
| 91 | this.$state.go(toast.scope.extraData.dest.name, toast.scope.extraData.dest.params); |
| 92 | } |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 93 | }); |
| 94 | |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 95 | this.search = (query: string) => { |
| 96 | return this.SearchService.search(query); |
| 97 | }; |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 98 | |
| 99 | // listen for keypress |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 100 | this.XosKeyboardShortcut.registerKeyBinding({ |
Matteo Scandolo | c817849 | 2017-04-11 17:55:13 -0700 | [diff] [blame] | 101 | key: 'F', |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 102 | description: 'Select search box', |
| 103 | cb: () => { |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 104 | $('.navbar-form input').focus(); |
Matteo Scandolo | 5053cbe | 2017-01-31 17:37:56 -0800 | [diff] [blame] | 105 | }, |
| 106 | }, 'global'); |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 107 | |
| 108 | // redirect to selected page |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 109 | this.routeSelected = (item: IXosSearchResult) => { |
| 110 | if (angular.isString(item.state)) { |
| 111 | this.$state.go(item.state); |
| 112 | } |
| 113 | else { |
| 114 | this.$state.go(item.state.name, item.state.params); |
| 115 | } |
Matteo Scandolo | 67c105f | 2017-01-09 09:30:52 -0800 | [diff] [blame] | 116 | this.query = null; |
| 117 | }; |
| 118 | |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 119 | this.userEmail = this.authService.getUser() ? this.authService.getUser().email : ''; |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 120 | |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame^] | 121 | this.syncStoreSubscription = this.syncStore.query() |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 122 | .subscribe( |
| 123 | (event: IWSEvent) => { |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame^] | 124 | this.$scope.$evalAsync(() => { |
| 125 | |
| 126 | if (event.model === 'Diag') { |
| 127 | // NOTE skip notifications for Diag model |
| 128 | return; |
| 129 | } |
| 130 | |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 131 | let toastrMsg: string; |
| 132 | let toastrLevel: string; |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame^] | 133 | if (event.msg.object.backend_code === 0) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 134 | toastrMsg = 'Synchronization started for:'; |
| 135 | toastrLevel = 'info'; |
| 136 | } |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame^] | 137 | else if (event.msg.object.backend_code === 1) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 138 | toastrMsg = 'Synchronization succedeed for:'; |
| 139 | toastrLevel = 'success'; |
| 140 | } |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame^] | 141 | else if (event.msg.object.backend_code === 2) { |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 142 | toastrMsg = 'Synchronization failed for:'; |
| 143 | toastrLevel = 'error'; |
| 144 | } |
| 145 | |
| 146 | if (toastrLevel && toastrMsg) { |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame^] | 147 | let modelName = event.msg.object.name; |
| 148 | let modelClassName = event.model; |
| 149 | if (angular.isUndefined(event.msg.object.name) || event.msg.object.name === null) { |
| 150 | modelName = `${event.msg.object.leaf_model_name} [${event.msg.object.id}]`; |
| 151 | } |
| 152 | |
| 153 | const dest = this.ConfigHelpers.stateWithParamsForJs(modelClassName, event.msg.object); |
| 154 | |
| 155 | if (!event.skip_notification) { |
| 156 | this.toastr[toastrLevel](`${toastrMsg} ${modelName}`, modelClassName, {extraData: {dest: dest}}); |
| 157 | } |
Matteo Scandolo | 266907e | 2016-12-20 13:41:42 -0800 | [diff] [blame] | 158 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 159 | // this.notifications.unshift(event); |
| 160 | // this.newNotifications = this.getNewNotifications(this.notifications); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 161 | }); |
Matteo Scandolo | 63e43eb | 2016-12-14 14:18:53 -0800 | [diff] [blame] | 162 | } |
| 163 | ); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 164 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 165 | |
Matteo Scandolo | e6f9c35 | 2017-09-01 12:19:56 -0700 | [diff] [blame^] | 166 | $onDestroy() { |
| 167 | this.$log.info('[XosHeader] Teardown'); |
| 168 | this.syncStoreSubscription.unsubscribe(); |
| 169 | } |
| 170 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 171 | public getLogo(): string { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 172 | return require(`../../images/brand/${this.StyleConfig.logo}`); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 173 | } |
Matteo Scandolo | c719e79 | 2016-12-14 15:48:31 -0800 | [diff] [blame] | 174 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 175 | // TODO display a list of notification in the template (if it make sense) |
| 176 | // public viewNotification = (notification: INotification) => { |
| 177 | // notification.viewed = true; |
| 178 | // this.newNotifications = this.getNewNotifications(this.notifications); |
| 179 | // }; |
| 180 | // |
| 181 | // private getNewNotifications = (notifications: INotification[]) => { |
| 182 | // return this.notifications.filter((n: INotification) => { |
| 183 | // return !n.viewed; |
| 184 | // }); |
| 185 | // }; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | export const xosHeader: angular.IComponentOptions = { |
| 189 | template: require('./header.html'), |
| 190 | controllerAs: 'vm', |
| 191 | controller: HeaderController |
| 192 | }; |