Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | import { FnService } from '../util/fn.service'; |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 17 | import { LogService } from '../log.service'; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 18 | import { WebSocketService } from '../remote/websocket.service'; |
| 19 | |
| 20 | import { PanelBaseImpl } from './panel.base'; |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 21 | import { Output, EventEmitter, Input } from '@angular/core'; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * A generic model of the data returned from the *DetailsResponse |
| 25 | */ |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 26 | export interface DetailsResponse { |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 27 | details: any; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Extends the PanelBase abstract class specifically for showing details |
| 32 | * |
| 33 | * This makes another call through WSS to the server for specific |
| 34 | * details to fill the panel with |
| 35 | * |
| 36 | * This replaces the detailspanel service in the old gui |
| 37 | */ |
| 38 | export abstract class DetailsPanelBaseImpl extends PanelBaseImpl { |
| 39 | |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 40 | @Input() id: string; |
| 41 | @Output() closeEvent = new EventEmitter<string>(); |
| 42 | |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 43 | private root: string; |
| 44 | private req: string; |
| 45 | private resp: string; |
| 46 | private handlers: string[] = []; |
| 47 | public detailsData: any = {}; |
| 48 | public closed: boolean = false; |
| 49 | |
| 50 | constructor( |
| 51 | protected fs: FnService, |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 52 | protected log: LogService, |
| 53 | protected wss: WebSocketService, |
| 54 | protected tag: string, |
| 55 | ) { |
Sean Condon | 95fb574 | 2019-04-02 12:16:55 +0100 | [diff] [blame] | 56 | super(fs, log); |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 57 | this.root = tag + 's'; |
| 58 | this.req = tag + 'DetailsRequest'; |
| 59 | this.resp = tag + 'DetailsResponse'; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * When the details panel is created set up a listener on |
| 64 | * Web Socket for details responses |
| 65 | */ |
| 66 | init() { |
| 67 | this.wss.bindHandlers(new Map<string, (data) => void>([ |
| 68 | [this.resp, (data) => this.detailsPanelResponseCb(data)] |
| 69 | ])); |
| 70 | this.handlers.push(this.resp); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * When the details panel is destroyed this should be called to |
| 75 | * de-register from the WebSocket |
| 76 | */ |
| 77 | destroy() { |
| 78 | this.wss.unbindHandlers(this.handlers); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * A callback that executes when the details data that was requested |
| 83 | * on WebSocketService arrives. |
| 84 | */ |
| 85 | detailsPanelResponseCb(data: DetailsResponse) { |
| 86 | this.detailsData = data['details']; |
| 87 | } |
| 88 | |
| 89 | /** |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 90 | * Details Panel Data Request - should be called whenever row id changes |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 91 | */ |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 92 | requestDetailsPanelData(query: any) { |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 93 | this.closed = false; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 94 | // Do not send if the Web Socket hasn't opened |
| 95 | if (this.wss.isConnected()) { |
| 96 | if (this.fs.debugOn('panel')) { |
| 97 | this.log.debug('Details panel data REQUEST:', this.req, query); |
| 98 | } |
| 99 | this.wss.sendEvent(this.req, query); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * this should be called when the details panel close button is clicked |
| 105 | */ |
| 106 | close(): void { |
| 107 | this.closed = true; |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 108 | this.id = null; |
| 109 | this.closeEvent.emit(this.id); |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 110 | } |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 111 | |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 112 | } |