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