Add ui tests
[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         margin-left: auto;
36         margin-right:auto;
37         text-align: center;
38
39 `
40
41 class LoopViewSvg extends React.Component {
42
43         static emptySvg = "<svg><text x=\"20\" y=\"40\">No LOOP (SVG)</text></svg>";
44
45         state = {
46                 svgContent: LoopViewSvg.emptySvg,
47                 loopCache: new LoopCache({}),
48                 componentModalMapping: new Map([])
49         }
50
51         constructor(props) {
52                 super(props);
53                 this.handleSvgClick = this.handleSvgClick.bind(this);
54                 this.getSvg = this.getSvg.bind(this);
55                 this.state.loopCache = props.loopCache;
56                 this.state.componentModalMapping = LoopComponentConverter.buildMapOfComponents(props.loopCache);
57                 this.getSvg(props.loopCache.getLoopName());
58                 console.log("!!!!!!!!!!!!!!!!!!!!!!!!:"+this.state.componentModalMapping);
59         }
60
61         shouldComponentUpdate(nextProps, nextState) {
62                 return this.state.svgContent !== nextState.svgContent;
63         }
64
65         componentWillReceiveProps(newProps) {   
66         console.log("!!!!!!!!!!!!!!!!!!!!!!!!");
67                 if (this.state.loopCache !== newProps.loopCache) {
68                 console.log("!!!!!!!!!!!!!!!!!!!!!!!!changed");
69                         this.setState({
70                                 loopCache: newProps.loopCache,
71                                 componentModalMapping: LoopComponentConverter.buildMapOfComponents(newProps.loopCache)
72                         });
73                         this.getSvg(newProps.loopCache.getLoopName());
74                 }
75         }
76
77         getSvg(loopName) {
78                 if (typeof loopName !== "undefined") {
79                         LoopService.getSvg(loopName).then(svgXml => {
80                                 if (svgXml.length !== 0) {
81                                         this.setState({ svgContent: svgXml })
82                                 } else {
83                                         this.setState({ svgContent: LoopViewSvg.emptySvg })
84                                 }
85                         });
86                 } else {
87                         this.setState({ svgContent: LoopViewSvg.emptySvg })
88                 }
89         }
90
91         handleSvgClick(event) {
92                 console.debug("svg click event received");
93                 var elementName = event.target.parentNode.parentNode.parentNode.getAttribute('data-element-id');
94                 console.info("SVG element clicked", elementName);
95                 this.props.history.push(this.state.componentModalMapping.get(elementName));
96         }
97
98         render() {
99                 return (
100                         <LoopViewSvgDivStyled dangerouslySetInnerHTML={{ __html: this.state.svgContent }} onClick={this.handleSvgClick}>
101
102                         </LoopViewSvgDivStyled>
103                 );
104         }
105 }
106
107 export default withRouter(LoopViewSvg);