blob: 97efee6fe5583e4abbe0a538f0ced1a83a2fd723 [file] [log] [blame]
Sean Condon28ecc5f2018-06-25 12:50:16 +01001/*
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 */
16import { FnService } from '../util/fn.service';
Sean Condon5ca00262018-09-06 17:55:25 +010017import { LogService } from '../log.service';
Sean Condon28ecc5f2018-06-25 12:50:16 +010018import { WebSocketService } from '../remote/websocket.service';
19
20import { PanelBaseImpl } from './panel.base';
Bhavesh72ead492018-07-19 16:29:18 +053021import { Output, EventEmitter, Input } from '@angular/core';
Sean Condon28ecc5f2018-06-25 12:50:16 +010022
23/**
24 * A generic model of the data returned from the *DetailsResponse
25 */
Sean Condon5ca00262018-09-06 17:55:25 +010026export interface DetailsResponse {
Sean Condon28ecc5f2018-06-25 12:50:16 +010027 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 */
38export abstract class DetailsPanelBaseImpl extends PanelBaseImpl {
39
Bhavesh72ead492018-07-19 16:29:18 +053040 @Input() id: string;
41 @Output() closeEvent = new EventEmitter<string>();
42
Sean Condon28ecc5f2018-06-25 12:50:16 +010043 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 Condon28ecc5f2018-06-25 12:50:16 +010052 protected log: LogService,
53 protected wss: WebSocketService,
54 protected tag: string,
55 ) {
Sean Condon95fb5742019-04-02 12:16:55 +010056 super(fs, log);
Sean Condon28ecc5f2018-06-25 12:50:16 +010057 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 /**
Bhavesh72ead492018-07-19 16:29:18 +053090 * Details Panel Data Request - should be called whenever row id changes
Sean Condon28ecc5f2018-06-25 12:50:16 +010091 */
Bhavesh72ead492018-07-19 16:29:18 +053092 requestDetailsPanelData(query: any) {
Sean Condon28ecc5f2018-06-25 12:50:16 +010093 this.closed = false;
Sean Condon28ecc5f2018-06-25 12:50:16 +010094 // 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;
Bhavesh72ead492018-07-19 16:29:18 +0530108 this.id = null;
109 this.closeEvent.emit(this.id);
Sean Condon28ecc5f2018-06-25 12:50:16 +0100110 }
Bhavesh72ead492018-07-19 16:29:18 +0530111
Sean Condon28ecc5f2018-06-25 12:50:16 +0100112}