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