Add generic svg selection
[clamp.git] / ui-react / src / components / loop_viewer / svg / LoopSvg.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23 import React from 'react';
24 import styled from 'styled-components';
25 import LoopCache from '../../../api/LoopCache';
26 import { withRouter } from "react-router";
27 import LoopService from '../../../api/LoopService';
28 import LoopComponentConverter from './LoopComponentConverter';
29
30 const LoopViewSvgDivStyled = styled.div`
31         overflow: hidden;
32         background-color: ${props => (props.theme.loopViewerBackgroundColor)};
33         border: 1px solid;
34         border-color: ${props => (props.theme.loopViewerHeaderColor)};
35         height: 50%;
36 `
37
38 class LoopViewSvg extends React.Component {
39
40         static emptySvg = "<svg><text x=\"20\" y=\"40\">No LOOP (SVG)</text></svg>";
41
42         state = {
43                 svgContent: LoopViewSvg.emptySvg,
44                 loopCache: new LoopCache({}),
45                 componentModalMapping: new Map([]),
46         }
47
48         constructor(props) {
49                 super(props);
50                 this.handleSvgClick = this.handleSvgClick.bind(this);
51                 this.getSvg = this.getSvg.bind(this);
52                 this.state.loopCache = props.loopCache;
53                 this.state.componentModalMapping = LoopComponentConverter.buildMapOfComponents(props.loopCache);
54                 this.getSvg(props.loopCache.getLoopName());
55         }
56
57         shouldComponentUpdate(nextProps, nextState) {
58                 return this.state.svgContent !== nextState.svgContent;
59         }
60
61         componentWillReceiveProps(newProps) {
62                 this.setState({
63                         loopCache: newProps.loopCache,
64                         componentModalMapping: LoopComponentConverter.buildMapOfComponents(newProps.loopCache),
65
66                 });
67                 this.getSvg(newProps.loopCache.getLoopName());
68         }
69
70         getSvg(loopName) {
71                 if (typeof loopName !== "undefined") {
72                         LoopService.getSvg(loopName).then(svgXml => {
73                                 if (svgXml.length !== 0) {
74                                         this.setState({ svgContent: svgXml })
75                                 } else {
76                                         this.setState({ svgContent: LoopViewSvg.emptySvg })
77                                 }
78                         });
79                 }
80         }
81
82         handleSvgClick(event) {
83                 console.debug("svg click event received");
84                 var elementName = event.target.parentNode.parentNode.parentNode.getAttribute('data-element-id');
85                 console.info("SVG element clicked", elementName);
86                 this.props.history.push(this.state.componentModalMapping.get(elementName));
87         }
88
89         render() {
90                 return (
91                         <LoopViewSvgDivStyled id="loop_svg" dangerouslySetInnerHTML={{ __html: this.state.svgContent }} onClick={this.handleSvgClick}>
92
93                         </LoopViewSvgDivStyled>
94                 );
95         }
96 }
97
98 export default withRouter(LoopViewSvg);