blob: 48aa2ede62d82a41da6a3e88b55775f999e986ab [file] [log] [blame]
Sean Condon55c30532018-10-29 12:26:57 +00001/*
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 {
17 FnService,
18 LoadingService,
19 LogService,
20 PanelBaseImpl
21} from 'gui2-fw-lib';
22
23/**
24 * Base model of panel view - implemented by Topology Panel components
25 */
26export abstract class TopoPanelBaseImpl extends PanelBaseImpl {
27
28 protected header: any;
29 protected body: any;
30 protected footer: any;
31
32 protected constructor(
33 protected fs: FnService,
34 protected ls: LoadingService,
35 protected log: LogService,
36 protected id: string
37 ) {
38 super(fs, ls, log);
39 }
40
41 protected init(el: any) {
42 this.header = el.append('div').classed('header', true);
43 this.body = el.append('div').classed('body', true);
44 this.footer = el.append('div').classed('footer', true);
45 }
46
47 /**
48 * Decode lists of props sent back through Web Socket
49 *
50 * Means that panels do not have to know property names in advance
51 * Driven by PropertyPanel on Server side
52 */
53 listProps(el, data) {
54 let sepLast: boolean = false;
55
56 // note: track whether we end with a separator or not...
57 data.propOrder.forEach((p) => {
58 if (p === '-') {
59 this.addSep(el);
60 sepLast = true;
61 } else {
62 this.addProp(el, data.propLabels[p], data.propValues[p]);
63 sepLast = false;
64 }
65 });
66 return sepLast;
67 }
68
69 addProp(el, label, value) {
70 const tr = el.append('tr');
71 let lab;
72
73 if (typeof label === 'string') {
74 lab = label.replace(/_/g, ' ');
75 } else {
76 lab = label;
77 }
78
79 function addCell(cls, txt) {
80 tr.append('td').attr('class', cls).text(txt);
81 }
82
83 addCell('label', lab + ' :');
84 addCell('value', value);
85 }
86
87 addSep(el) {
88 el.append('tr').append('td').attr('colspan', 2).append('hr');
89 }
90
91 appendToHeader(x) {
92 return this.header.append(x);
93 }
94
95 appendToBody(x) {
96 return this.body.append(x);
97 }
98
99 appendToFooter(x) {
100 return this.footer.append(x);
101 }
102
103 emptyRegions() {
104 this.header.selectAll('*').remove();
105 this.body.selectAll('*').remove();
106 this.footer.selectAll('*').remove();
107 }
108
109}