Fixing AAI-1433
[aai/sparky-fe.git] / src / generic-components / graph / NodeFactory.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
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 import React from 'react';
22
23 import NodeVisualElementConstants from './NodeVisualElementConstants.js';
24 import NodeVisualElementFactory from './NodeVisualElementFactory.js';
25
26 class NodeFactory {
27
28   constructor() {
29     this.graphMeta = {};
30     this.visualElementFactory = new NodeVisualElementFactory();
31
32     this.setNodeMeta = this.setNodeMeta.bind(this);
33   }
34
35   setNodeMeta(metaObject) {
36     this.graphMeta = metaObject;
37     this.visualElementFactory.setVisualElementMeta(metaObject);
38   }
39  // hideButton a temporary solution to not display the button.
40   buildNode(nodeType, nodeProps, hideButton) {
41
42     let translate = `translate(
43                               ${nodeProps.renderProps.x}, 
44                               ${nodeProps.renderProps.y})`;
45     let finalProps = {
46       ...nodeProps.renderProps,
47       className: this.graphMeta.aaiEntityNodeDescriptors[nodeType].class,
48       transform: translate
49     };
50
51     let nodeVisualElementsData = this.extractVisualElementArrayFromMeta(
52       nodeType, hideButton);
53     let nodeVisualElements = undefined;
54     if (nodeVisualElementsData) {
55       nodeVisualElements = [];
56       nodeVisualElementsData.map((elementData, index) => {
57         if (elementData.type === NodeVisualElementConstants.BUTTON) {
58           if (nodeProps.buttons) {
59             let isButtonSelected = false;
60             if (index === 4) {
61               isButtonSelected = nodeProps.buttons[0];
62             }
63             if (index === 5) {
64               isButtonSelected = nodeProps.buttons[1];
65             }
66             elementData = {
67               ...elementData,
68               isSelected: isButtonSelected
69             };
70           }
71         }
72         nodeVisualElements.push(
73           this.visualElementFactory.buildVisualElement(nodeProps.meta,
74             elementData.type, elementData, index));
75       });
76       //Draw overlay only if the node is validated
77       if (nodeProps.meta.nodeMeta.nodeValidated) {
78
79         if (nodeProps.meta.nodeMeta.nodeIssue) {
80           let warningOverlayProps = {
81             name: NodeVisualElementConstants.ICON_WARNING,
82           };
83           nodeVisualElements.push(
84             this.visualElementFactory.buildVisualElement(nodeProps,
85               NodeVisualElementConstants.ICON, warningOverlayProps,
86               nodeVisualElementsData.length + 1));
87         } else {
88           let tickOverlayProps = {
89             name: NodeVisualElementConstants.ICON_TICK,
90           };
91           nodeVisualElements.push(
92             this.visualElementFactory.buildVisualElement(nodeProps,
93               NodeVisualElementConstants.ICON, tickOverlayProps,
94               nodeVisualElementsData.length + 1));
95         }
96       }
97     }
98
99     if (nodeVisualElements) {
100       return React.createElement('g', finalProps, nodeVisualElements);
101     }
102
103     return React.createElement('g', finalProps);
104   }
105
106   extractVisualElementArrayFromMeta(nodeClassName, hideButton) {
107     let nodeVisualElements = undefined;
108     if (this.graphMeta.aaiEntityNodeDescriptors) {
109       nodeVisualElements =
110         this.graphMeta.aaiEntityNodeDescriptors[nodeClassName].visualElements;
111       if(hideButton) {
112          // temp, until BE not sent the triangle button
113         for (var i = 0; i < nodeVisualElements.length; i++) {
114           if (nodeVisualElements[i].type === 'button' && nodeVisualElements[i].name === 'icon_triangle_warning') {
115             nodeVisualElements.splice(i, 1);
116             break;
117           }
118         }
119       }
120     }
121     return nodeVisualElements;
122   }
123 }
124
125 export default NodeFactory;