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'; |
| 18 | import { LogService } from '../../log.service'; |
| 19 | import { WebSocketService } from '../remote/websocket.service'; |
| 20 | |
| 21 | import { PanelBaseImpl } from './panel.base'; |
| 22 | |
| 23 | /** |
| 24 | * A generic model of the data returned from the *DetailsResponse |
| 25 | */ |
| 26 | interface DetailsResponse { |
| 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 | |
| 40 | private root: string; |
| 41 | private req: string; |
| 42 | private resp: string; |
| 43 | private handlers: string[] = []; |
| 44 | public detailsData: any = {}; |
| 45 | public closed: boolean = false; |
| 46 | |
| 47 | constructor( |
| 48 | protected fs: FnService, |
| 49 | protected ls: LoadingService, |
| 50 | protected log: LogService, |
| 51 | protected wss: WebSocketService, |
| 52 | protected tag: string, |
| 53 | ) { |
| 54 | super(fs, ls, log, wss, {}); |
| 55 | this.root = tag + 's'; |
| 56 | this.req = tag + 'DetailsRequest'; |
| 57 | this.resp = tag + 'DetailsResponse'; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * When the details panel is created set up a listener on |
| 62 | * Web Socket for details responses |
| 63 | */ |
| 64 | init() { |
| 65 | this.wss.bindHandlers(new Map<string, (data) => void>([ |
| 66 | [this.resp, (data) => this.detailsPanelResponseCb(data)] |
| 67 | ])); |
| 68 | this.handlers.push(this.resp); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * When the details panel is destroyed this should be called to |
| 73 | * de-register from the WebSocket |
| 74 | */ |
| 75 | destroy() { |
| 76 | this.wss.unbindHandlers(this.handlers); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * A callback that executes when the details data that was requested |
| 81 | * on WebSocketService arrives. |
| 82 | */ |
| 83 | detailsPanelResponseCb(data: DetailsResponse) { |
| 84 | this.detailsData = data['details']; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Details Panel Data Request - should be called whenever id changes |
| 89 | * If id is empty, no request is made |
| 90 | */ |
| 91 | requestDetailsPanelData(id: string) { |
| 92 | if (id === '') { |
| 93 | return; |
| 94 | } |
| 95 | this.closed = false; |
| 96 | const query = {'id': id}; |
| 97 | |
| 98 | // Do not send if the Web Socket hasn't opened |
| 99 | if (this.wss.isConnected()) { |
| 100 | if (this.fs.debugOn('panel')) { |
| 101 | this.log.debug('Details panel data REQUEST:', this.req, query); |
| 102 | } |
| 103 | this.wss.sendEvent(this.req, query); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * this should be called when the details panel close button is clicked |
| 109 | */ |
| 110 | close(): void { |
| 111 | this.closed = true; |
| 112 | } |
| 113 | } |