blob: 11dc8c94e334fa6a5cd7186438486d0e451a2861 [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';
17import { LoadingService } from '../layer/loading.service';
Sean Condon5ca00262018-09-06 17:55:25 +010018import { LogService } from '../log.service';
Sean Condon28ecc5f2018-06-25 12:50:16 +010019import { WebSocketService } from '../remote/websocket.service';
20
21import { PanelBaseImpl } from './panel.base';
Bhavesh72ead492018-07-19 16:29:18 +053022import { Output, EventEmitter, Input } from '@angular/core';
Sean Condon28ecc5f2018-06-25 12:50:16 +010023
24/**
25 * A generic model of the data returned from the *DetailsResponse
26 */
Sean Condon5ca00262018-09-06 17:55:25 +010027export interface DetailsResponse {
Sean Condon28ecc5f2018-06-25 12:50:16 +010028 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 */
39export abstract class DetailsPanelBaseImpl extends PanelBaseImpl {
40
Bhavesh72ead492018-07-19 16:29:18 +053041 @Input() id: string;
42 @Output() closeEvent = new EventEmitter<string>();
43
Sean Condon28ecc5f2018-06-25 12:50:16 +010044 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 ) {
58 super(fs, ls, log, wss, {});
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 /**
Bhavesh72ead492018-07-19 16:29:18 +053092 * Details Panel Data Request - should be called whenever row id changes
Sean Condon28ecc5f2018-06-25 12:50:16 +010093 */
Bhavesh72ead492018-07-19 16:29:18 +053094 requestDetailsPanelData(query: any) {
Sean Condon28ecc5f2018-06-25 12:50:16 +010095 this.closed = false;
Sean Condon28ecc5f2018-06-25 12:50:16 +010096 // 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;
Bhavesh72ead492018-07-19 16:29:18 +0530110 this.id = null;
111 this.closeEvent.emit(this.id);
Sean Condon28ecc5f2018-06-25 12:50:16 +0100112 }
Bhavesh72ead492018-07-19 16:29:18 +0530113
Sean Condon28ecc5f2018-06-25 12:50:16 +0100114}