Adding filter bar
[aai/sparky-fe.git] / src / generic-components / graph / NodeFactory.js
1 /*
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 import React from 'react';
27
28 import NodeVisualElementConstants from './NodeVisualElementConstants.js';
29 import NodeVisualElementFactory from './NodeVisualElementFactory.js';
30
31 class NodeFactory {
32
33   constructor() {
34     this.graphMeta = {};
35     this.visualElementFactory = new NodeVisualElementFactory();
36
37     this.setNodeMeta = this.setNodeMeta.bind(this);
38   }
39
40   setNodeMeta(metaObject) {
41     this.graphMeta = metaObject;
42     this.visualElementFactory.setVisualElementMeta(metaObject);
43   }
44  // hideButton a temporary solution to not display the button.
45   buildNode(nodeType, nodeProps, hideButton) {
46
47     let translate = `translate(
48                               ${nodeProps.renderProps.x}, 
49                               ${nodeProps.renderProps.y})`;
50     let finalProps = {
51       ...nodeProps.renderProps,
52       className: this.graphMeta.aaiEntityNodeDescriptors[nodeType].class,
53       transform: translate
54     };
55
56     let nodeVisualElementsData = this.extractVisualElementArrayFromMeta(
57       nodeType, hideButton);
58     let nodeVisualElements = undefined;
59     if (nodeVisualElementsData) {
60       nodeVisualElements = [];
61       nodeVisualElementsData.map((elementData, index) => {
62         if (elementData.type === NodeVisualElementConstants.BUTTON) {
63           if (nodeProps.buttons) {
64             let isButtonSelected = false;
65             if (index === 4) {
66               isButtonSelected = nodeProps.buttons[0];
67             }
68             if (index === 5) {
69               isButtonSelected = nodeProps.buttons[1];
70             }
71             elementData = {
72               ...elementData,
73               isSelected: isButtonSelected
74             };
75           }
76         }
77         nodeVisualElements.push(
78           this.visualElementFactory.buildVisualElement(nodeProps.meta,
79             elementData.type, elementData, index));
80       });
81       //Draw overlay only if the node is validated
82       if (nodeProps.meta.nodeMeta.nodeValidated) {
83
84         if (nodeProps.meta.nodeMeta.nodeIssue) {
85           let warningOverlayProps = {
86             name: NodeVisualElementConstants.ICON_WARNING,
87           };
88           nodeVisualElements.push(
89             this.visualElementFactory.buildVisualElement(nodeProps,
90               NodeVisualElementConstants.ICON, warningOverlayProps,
91               nodeVisualElementsData.length + 1));
92         } else {
93           let tickOverlayProps = {
94             name: NodeVisualElementConstants.ICON_TICK,
95           };
96           nodeVisualElements.push(
97             this.visualElementFactory.buildVisualElement(nodeProps,
98               NodeVisualElementConstants.ICON, tickOverlayProps,
99               nodeVisualElementsData.length + 1));
100         }
101       }
102     }
103
104     if (nodeVisualElements) {
105       return React.createElement('g', finalProps, nodeVisualElements);
106     }
107
108     return React.createElement('g', finalProps);
109   }
110
111   extractVisualElementArrayFromMeta(nodeClassName, hideButton) {
112     let nodeVisualElements = undefined;
113     if (this.graphMeta.aaiEntityNodeDescriptors) {
114       nodeVisualElements =
115         this.graphMeta.aaiEntityNodeDescriptors[nodeClassName].visualElements;
116       if(hideButton) {
117          // temp, until BE not sent the triangle button
118         for (var i = 0; i < nodeVisualElements.length; i++) {
119           if (nodeVisualElements[i].type === 'button' && nodeVisualElements[i].name === 'icon_triangle_warning') {
120             nodeVisualElements.splice(i, 1);
121             return;
122           }
123         }
124       }
125     }
126     return nodeVisualElements;
127   }
128 }
129
130 export default NodeFactory;