Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 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 | */ |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 16 | import { Component, Input, OnInit, OnDestroy, Inject, NgZone } from '@angular/core'; |
| 17 | import { Router } from '@angular/router'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 18 | import { LionService } from '../../util/lion.service'; |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 19 | import { LogService } from '../../log.service'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 20 | import { NavService } from '../../nav/nav.service'; |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 21 | import { WebSocketService } from '../../remote/websocket.service'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * ONOS GUI -- Masthead Component |
| 25 | */ |
| 26 | @Component({ |
| 27 | selector: 'onos-mast', |
| 28 | templateUrl: './mast.component.html', |
| 29 | styleUrls: ['./mast.component.css', './mast.theme.css'] |
| 30 | }) |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 31 | export class MastComponent implements OnInit, OnDestroy { |
| 32 | @Input() username: string; |
| 33 | |
| 34 | lionFn; // Function |
| 35 | viewMap = new Map<string, string>([]); // A map of app names |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 36 | confirmMessage: string = ''; |
| 37 | strongWarning: string = ''; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 38 | |
| 39 | constructor( |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 40 | private lion: LionService, |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 41 | private log: LogService, |
| 42 | public ns: NavService, |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 43 | private wss: WebSocketService, |
| 44 | private router: Router, |
| 45 | private zone: NgZone, |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 46 | @Inject('Window') private window: any, |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 47 | ) { |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 48 | this.viewMap.set('apps', 'https://wiki.onosproject.org/display/ONOS/GUI+Application+View'); |
| 49 | this.viewMap.set('device', 'https://wiki.onosproject.org/display/ONOS/GUI+Device+View'); |
| 50 | this.viewMap.set('', 'https://wiki.onosproject.org/display/ONOS/The+ONOS+Web+GUI'); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | ngOnInit() { |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 54 | if (this.lion.ubercache.length === 0) { |
| 55 | this.lionFn = this.dummyLion; |
| 56 | this.lion.loadCbs.set('mast', () => this.doLion()); |
| 57 | this.log.debug('LION not available when MastComponent initialized'); |
| 58 | } else { |
| 59 | this.doLion(); |
| 60 | } |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 61 | |
| 62 | this.wss.bindHandlers(new Map<string, (data) => void>([ |
| 63 | ['guiRemoved', (data) => this.triggerRefresh(data, false) ], |
| 64 | ['guiAdded', (data) => this.triggerRefresh(data, true) ] |
| 65 | ])); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 66 | this.log.debug('MastComponent initialized'); |
| 67 | } |
| 68 | |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 69 | /** |
| 70 | * Nav component should never be closed, but in case it does, it's |
| 71 | * safer to tidy up after itself |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 72 | */ |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 73 | ngOnDestroy() { |
| 74 | this.lion.loadCbs.delete('mast'); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 77 | /** |
| 78 | * Read the LION bundle for App and set up the lionFn |
| 79 | */ |
| 80 | doLion() { |
| 81 | this.lionFn = this.lion.bundle('core.fw.Mast'); |
| 82 | if (this.username === undefined) { |
| 83 | this.username = this.lionFn('unknown_user'); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * A dummy implementation of the lionFn until the response is received and the LION |
| 89 | * bundle is received from the WebSocket |
| 90 | */ |
| 91 | dummyLion(key: string): string { |
| 92 | return '%' + key + '%'; |
| 93 | } |
| 94 | |
| 95 | directTo() { |
| 96 | const curId = this.window.location.pathname.replace('/', ''); |
| 97 | let helpUrl: string = this.viewMap.get(curId); |
| 98 | if (helpUrl === undefined) { |
| 99 | helpUrl = this.viewMap.get(''); |
| 100 | this.log.warn('No help file linked for view:', curId); |
| 101 | } |
| 102 | this.window.open(helpUrl); |
| 103 | } |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 104 | |
| 105 | triggerRefresh(data: any, added: boolean): void { |
| 106 | this.confirmMessage = this.lionFn(added ? 'uicomp_added' : 'uicomp_removed'); |
| 107 | this.log.debug('Refresh has been triggered - item', added ? 'added' : 'removed', ' - ', data); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Callback when the Confirm dialog is shown and a choice is made |
| 112 | */ |
| 113 | dOk(choice: boolean) { |
| 114 | if (choice) { |
| 115 | this.ns.getUiViews(); |
| 116 | this.router.navigate(['/']); |
| 117 | this.zone.runOutsideAngular(() => { |
| 118 | location.reload(); |
| 119 | }); |
| 120 | this.log.debug('Refresh confirmed'); // Will not be printed if page reloads |
| 121 | |
| 122 | } else { |
| 123 | this.log.debug('Refresh cancelled'); |
| 124 | } |
| 125 | this.confirmMessage = ''; |
| 126 | this.strongWarning = ''; |
| 127 | } |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 128 | } |