Merge "Fix warnings"
[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 { withRouter } from "react-router";
26 import LoopService from '../../../api/LoopService'
27
28 const LoopViewSvgDivStyled = styled.div`
29         overflow: hidden;
30         background-color: ${props => (props.theme.loopViewerBackgroundColor)};
31         border: 1px solid;
32         border-color: ${props => (props.theme.loopViewerHeaderColor)};
33         height: 50%;
34 `
35
36 class LoopViewSvg extends React.Component {
37
38         static emptySvg = "<svg><text x=\"20\" y=\"40\">No LOOP (SVG)</text></svg>";
39         static operationalPolicyDataElementId = "OperationalPolicy";
40
41
42         state = {
43                 svgContent: LoopViewSvg.emptySvg,
44                 loopCache: this.props.loopCache,
45         }
46
47         constructor(props) {
48                 super(props);
49                 this.state.loopCache = props.loopCache;
50                 this.handleSvgClick = this.handleSvgClick.bind(this);
51                 this.getSvg = this.getSvg.bind(this);
52         }
53
54         shouldComponentUpdate(nextProps, nextState) {
55                 return this.state.svgContent !== nextState.svgContent;
56         }
57
58         componentWillReceiveProps(newProps) {
59                 this.setState({ loopCache: newProps.loopCache });
60                 this.getSvg();
61         }
62
63         getSvg() {
64                 LoopService.getSvg(this.state.loopCache.getLoopName()).then(svgXml => {
65                         if (svgXml.length !== 0) {
66                                 this.setState({ svgContent: svgXml })
67                         } else {
68                                 this.setState({ svgContent: LoopViewSvg.emptySvg })
69                         }
70                 });
71         }
72
73         handleSvgClick(event) {
74                 console.debug("svg click event received");
75                 var elementName = event.target.parentNode.parentNode.parentNode.getAttribute('data-element-id');
76                 console.info("SVG element clicked", elementName);
77                 if (typeof elementName === "undefined" || elementName === "VES") {
78                         return;
79                 } else if (elementName === LoopViewSvg.operationalPolicyDataElementId) {
80                         this.props.history.push('/operationalPolicyModal');
81                 } else {
82                         this.props.history.push('/configurationPolicyModal');
83                 }
84         }
85
86         render() {
87                 return (
88                         <LoopViewSvgDivStyled id="loop_svg" dangerouslySetInnerHTML={{ __html: this.state.svgContent }} onClick={this.handleSvgClick}>
89
90                         </LoopViewSvgDivStyled>
91                 );
92         }
93 }
94
95 export default withRouter(LoopViewSvg);