blob: e61b1f27e5cd779a80178b56c74cc8276f9931c2 [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';
Sean Condon436c60a2021-01-01 14:23:29 +000021import { InjectionToken, Inject, Component, 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
Sean Condon436c60a2021-01-01 14:23:29 +000030export const TAG = new InjectionToken<string>('tag');
31
Sean Condon28ecc5f2018-06-25 12:50:16 +010032/**
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 Condon436c60a2021-01-01 14:23:29 +000040@Component({
41 template: ''
42})
Sean Condon28ecc5f2018-06-25 12:50:16 +010043export abstract class DetailsPanelBaseImpl extends PanelBaseImpl {
44
Bhavesh72ead492018-07-19 16:29:18 +053045 @Input() id: string;
46 @Output() closeEvent = new EventEmitter<string>();
47
Sean Condon28ecc5f2018-06-25 12:50:16 +010048 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 Condon28ecc5f2018-06-25 12:50:16 +010057 protected log: LogService,
58 protected wss: WebSocketService,
Sean Condon436c60a2021-01-01 14:23:29 +000059 @Inject(TAG) protected tag: string,
Sean Condon28ecc5f2018-06-25 12:50:16 +010060 ) {
Sean Condon95fb5742019-04-02 12:16:55 +010061 super(fs, log);
Sean Condon28ecc5f2018-06-25 12:50:16 +010062 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 /**
Bhavesh72ead492018-07-19 16:29:18 +053095 * Details Panel Data Request - should be called whenever row id changes
Sean Condon28ecc5f2018-06-25 12:50:16 +010096 */
Bhavesh72ead492018-07-19 16:29:18 +053097 requestDetailsPanelData(query: any) {
Sean Condon28ecc5f2018-06-25 12:50:16 +010098 this.closed = false;
Sean Condon28ecc5f2018-06-25 12:50:16 +010099 // 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;
Bhavesh72ead492018-07-19 16:29:18 +0530113 this.id = null;
114 this.closeEvent.emit(this.id);
Sean Condon28ecc5f2018-06-25 12:50:16 +0100115 }
Bhavesh72ead492018-07-19 16:29:18 +0530116
Sean Condon28ecc5f2018-06-25 12:50:16 +0100117}