Initial coomit for AAI-UI(sparky-fe)
[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
45   buildNode(nodeType, nodeProps) {
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);
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 = true;
65             elementData = {
66               ...elementData,
67               isSelected: isButtonSelected
68             };
69           }
70         }
71         nodeVisualElements.push(
72           this.visualElementFactory.buildVisualElement(nodeProps.meta,
73             elementData.type, elementData, index));
74       });
75       //Draw overlay only if the node is validated
76       if (nodeProps.meta.nodeMeta.nodeValidated) {
77
78         if (nodeProps.meta.nodeMeta.nodeIssue) {
79           let warningOverlayProps = {
80             name: NodeVisualElementConstants.ICON_WARNING,
81           };
82           nodeVisualElements.push(
83             this.visualElementFactory.buildVisualElement(nodeProps,
84               NodeVisualElementConstants.ICON, warningOverlayProps,
85               nodeVisualElementsData.length + 1));
86         } else {
87           let tickOverlayProps = {
88             name: NodeVisualElementConstants.ICON_TICK,
89           };
90           nodeVisualElements.push(
91             this.visualElementFactory.buildVisualElement(nodeProps,
92               NodeVisualElementConstants.ICON, tickOverlayProps,
93               nodeVisualElementsData.length + 1));
94         }
95       }
96     }
97
98     if (nodeVisualElements) {
99       return React.createElement('g', finalProps, nodeVisualElements);
100     }
101
102     return React.createElement('g', finalProps);
103   }
104
105   extractVisualElementArrayFromMeta(nodeClassName) {
106     let nodeVisualElements = undefined;
107     if (this.graphMeta.aaiEntityNodeDescriptors) {
108       nodeVisualElements =
109         this.graphMeta.aaiEntityNodeDescriptors[nodeClassName].visualElements;
110     }
111     return nodeVisualElements;
112   }
113 }
114
115 export default NodeFactory;