2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 import {drag} from 'd3-drag';
27 import {forceSimulation, forceLink, forceManyBody, forceCenter} from 'd3-force';
28 import {interpolateNumber} from 'd3-interpolate';
29 import {select, event as currentEvent} from 'd3-selection';
30 import React, {Component, PropTypes} from 'react';
31 import {interval, now} from 'd3-timer';
32 import {zoom, zoomIdentity} from 'd3-zoom';
33 import NodeConstants from './NodeVisualElementConstants.js';
35 import {simulationKeys} from './ForceDefinitions.js';
36 import NodeFactory from './NodeFactory.js';
37 import NodeVisualElementFactory from './NodeVisualElementFactory.js';
39 class ForceDirectedGraph extends Component {
41 viewWidth: PropTypes.number,
42 viewHeight: PropTypes.number,
43 graphData: PropTypes.object,
44 nodeIdKey: PropTypes.string,
45 linkIdKey: PropTypes.string,
46 nodeSelectedCallback: PropTypes.func,
47 nodeButtonSelectedCallback: PropTypes.func,
48 currentlySelectedNodeView: PropTypes.string
51 static defaultProps = {
55 graphCounter: -1, nodeDataArray: [], linkDataArray: [], graphMeta: {}
59 nodeSelectedCallback: undefined,
60 nodeButtonSelectedCallback: undefined,
61 currentlySelectedNodeView: ''
68 nodes: [], links: [], mainGroupTransform: zoomIdentity
71 this.updateSimulationForce = this.updateSimulationForce.bind(this);
72 this.resetTransform = this.resetTransform.bind(this);
73 this.applyBufferDataToState = this.applyBufferDataToState.bind(this);
74 this.createNodePropForState = this.createNodePropForState.bind(this);
75 this.createLinkPropForState = this.createLinkPropForState.bind(this);
76 this.startSimulation = this.startSimulation.bind(this);
77 this.simulationComplete = this.simulationComplete.bind(this);
78 this.simulationTick = this.simulationTick.bind(this);
79 this.nodeSelected = this.nodeSelected.bind(this);
80 this.onZoom = this.onZoom.bind(this);
81 this.onGraphDrag = this.onGraphDrag.bind(this);
82 this.onNodeDrag = this.onNodeDrag.bind(this);
83 this.addNodeInterpolator = this.addNodeInterpolator.bind(this);
84 this.runInterpolators = this.runInterpolators.bind(this);
89 this.nodeButtonDatum = [];
90 this.nodeFactory = new NodeFactory();
91 this.visualElementFactory = new NodeVisualElementFactory();
93 this.isGraphMounted = false;
95 this.listenerGraphCounter = -1;
96 this.nodeIndexTracker = new Map();
97 this.interpolators = new Map();
98 this.areInterpolationsRunning = false;
100 this.newNodeSelected = true;
101 this.currentlySelectedNodeButton = undefined;
103 this.intervalTimer = interval(this.applyBufferDataToState, simulationKeys.DATA_COPY_INTERVAL);
104 this.intervalTimer.stop();
106 this.interpolationTimer = interval(this.runInterpolators, simulationKeys.DATA_COPY_INTERVAL);
107 this.interpolationTimer.stop();
109 this.simulation = forceSimulation();
110 this.simulation.on('end', this.simulationComplete);
111 this.simulation.stop();
114 zoom().scaleExtent([NodeConstants.SCALE_EXTENT_MIN, NodeConstants.SACEL_EXTENT_MAX]);
115 this.svgZoom.clickDistance(2);
116 this.nodeDrag = drag().clickDistance(2);
118 this.updateSimulationForce();
120 if (props.graphData) {
121 if (props.graphData.graphCounter !== -1) {
122 this.startSimulation(props.graphData);
127 componentDidMount() {
128 this.isGraphMounted = true;
131 componentWillReceiveProps(nextProps) {
132 if (nextProps.graphData.graphCounter !== this.props.graphData.graphCounter) {
133 this.listenerGraphCounter = this.props.graphData.graphCounter;
134 this.newNodeSelected = true;
135 this.resetTransform();
136 this.startSimulation(nextProps.graphData);
140 componentDidUpdate(prevProps) {
141 let hasNewGraphDataRendered = (prevProps.graphData.graphCounter ===
142 this.props.graphData.graphCounter);
143 let shouldAttachListeners = (this.listenerGraphCounter !== this.props.graphData.graphCounter);
144 let nodeCount = this.state.nodes.length;
147 if (hasNewGraphDataRendered && shouldAttachListeners) {
149 let nodes = select('.fdgMainSvg').select('.fdgMainG')
150 .selectAll('.aai-entity-node')
151 .data(this.nodeDatum);
153 nodes.on('click', (d) => {
154 this.nodeSelected(d);
157 nodes.call(this.nodeDrag.on('drag', (d) => {
158 let xAndY = [currentEvent.x, currentEvent.y];
159 this.onNodeDrag(d, xAndY);
162 let mainSVG = select('.fdgMainSvg');
163 let mainView = mainSVG.select('.fdgMainView');
164 this.svgZoom.transform(mainSVG, zoomIdentity);
165 this.svgZoom.transform(mainView, zoomIdentity);
167 mainSVG.call(this.svgZoom.on('zoom', () => { // D3 Zoom also handles panning
168 this.onZoom(currentEvent.transform);
169 })).on('dblclick.zoom', null); // Ignore the double-click zoom event
171 this.listenerGraphCounter = this.props.graphData.graphCounter;
176 componentWillUnmount() {
177 this.isGraphMounted = false;
179 let nodes = select('.fdgMainSvg').select('.fdgMainG')
180 .selectAll('.aai-entity-node');
181 let nodeButtons = nodes.selectAll('.node-button');
183 nodes.on('click', null);
184 nodeButtons.on('click', null);
186 let mainSVG = select('.fdgMainSvg');
188 mainSVG.call(this.svgZoom.on('zoom', null)).on('dblclick.zoom', null);
189 mainSVG.call(drag().on('drag', null));
192 updateSimulationForce() {
193 this.simulation.force('link', forceLink());
194 this.simulation.force('link').id((d) => {
197 this.simulation.force('link').strength(0.3);
198 this.simulation.force('link').distance(100);
200 this.simulation.force('charge', forceManyBody());
201 this.simulation.force('charge').strength(-1250);
202 this.simulation.alpha(1);
204 this.simulation.force('center',
205 forceCenter(this.props.viewWidth / 2, this.props.viewHeight / 2));
209 if (this.isGraphMounted) {
210 this.setState(() => {
212 mainGroupTransform: zoomIdentity
218 applyBufferDataToState() {
219 this.nodeIndexTracker.clear();
222 this.nodeBuffer.map((node, i) => {
223 let nodeProps = this.createNodePropForState(node);
225 if (nodeProps.meta.nodeMeta.className === NodeConstants.SELECTED_NODE_CLASS_NAME ||
226 nodeProps.meta.nodeMeta.className === NodeConstants.SELECTED_SEARCHED_NODE_CLASS_NAME) {
228 this.nodeButtonDatum[0].data = nodeProps.meta;
232 buttons: [this.nodeButtonDatum[0].isSelected]
236 newNodes.push(this.nodeFactory.buildNode(nodeProps.meta.nodeMeta.className, nodeProps));
238 this.nodeIndexTracker.set(node.id, i);
242 this.linkBuffer.map((link) => {
244 let linkProps = this.createLinkPropForState(link);
245 newLinks.push(this.visualElementFactory.createSvgLine(linkProps, key));
248 if (this.isGraphMounted) {
249 this.setState(() => {
251 nodes: newNodes, links: newLinks
257 createNodePropForState(nodeData) {
260 key: nodeData.id, x: nodeData.x, y: nodeData.y
267 createLinkPropForState(linkData) {
269 className: 'aai-entity-link',
270 x1: linkData.source.x,
271 y1: linkData.source.y,
272 x2: linkData.target.x,
273 y2: linkData.target.y
277 startSimulation(graphData) {
278 this.nodeFactory.setNodeMeta(graphData.graphMeta);
280 // Experiment with removing length = 0... might not be needed as new array
281 // assignment will likely destroy old reference
282 this.nodeBuffer.length = 0;
283 this.nodeBuffer = Array.from(graphData.nodeDataArray);
284 this.linkBuffer.length = 0;
285 this.linkBuffer = Array.from(graphData.linkDataArray);
286 this.nodeDatum.length = 0;
287 this.nodeDatum = Array.from(graphData.nodeDataArray);
289 this.nodeButtonDatum.length = 0;
291 let isNodeDetailsSelected = true;
292 this.nodeButtonDatum.push({
293 name: NodeConstants.ICON_ELLIPSES, isSelected: isNodeDetailsSelected
296 if (isNodeDetailsSelected) {
297 this.currentlySelectedNodeButton = NodeConstants.ICON_ELLIPSES;
300 this.updateSimulationForce();
302 this.simulation.nodes(this.nodeBuffer);
303 this.simulation.force('link').links(this.linkBuffer);
304 this.simulation.on('tick', this.simulationTick);
305 this.simulation.restart();
308 simulationComplete() {
309 this.intervalTimer.stop();
310 this.applyBufferDataToState();
314 this.intervalTimer.restart(this.applyBufferDataToState, simulationKeys.DATA_COPY_INTERVAL);
315 this.simulation.on('tick', null);
318 nodeSelected(datum) {
319 if (this.props.nodeSelectedCallback) {
320 this.props.nodeSelectedCallback(datum);
323 let didUpdateNew = false;
324 let didUpdatePrevious = false;
325 let isSameNodeSelected = true;
327 // Check to see if a default node was previously selected
328 let selectedDefaultNode = select('.fdgMainSvg').select('.fdgMainG')
329 .selectAll('.aai-entity-node')
330 .filter('.selected-node');
331 if (!selectedDefaultNode.empty()) {
332 if (selectedDefaultNode.datum().id !== datum.id) {
333 this.nodeBuffer[selectedDefaultNode.datum().index].nodeMeta.className =
334 NodeConstants.GENERAL_NODE_CLASS_NAME;
335 didUpdatePrevious = true;
336 isSameNodeSelected = false;
340 // Check to see if a searched node was previously selected
341 let selectedSearchedNode = select('.fdgMainSvg').select('.fdgMainG')
342 .selectAll('.aai-entity-node')
343 .filter('.selected-search-node');
344 if (!selectedSearchedNode.empty()) {
345 if (selectedSearchedNode.datum().id !== datum.id) {
346 this.nodeBuffer[selectedSearchedNode.datum().index].nodeMeta.className =
347 NodeConstants.SEARCHED_NODE_CLASS_NAME;
348 didUpdatePrevious = true;
349 isSameNodeSelected = false;
353 if (!isSameNodeSelected) {
354 let newlySelectedNode = select('.fdgMainSvg').select('.fdgMainG')
355 .selectAll('.aai-entity-node')
357 return (datum.id === d.id);
359 if (!newlySelectedNode.empty()) {
361 if (newlySelectedNode.datum().nodeMeta.searchTarget) {
362 this.nodeBuffer[newlySelectedNode.datum().index].nodeMeta.className =
363 NodeConstants.SELECTED_SEARCHED_NODE_CLASS_NAME;
365 this.nodeBuffer[newlySelectedNode.datum().index].nodeMeta.className =
366 NodeConstants.SELECTED_NODE_CLASS_NAME;
372 if (didUpdatePrevious && didUpdateNew) {
373 this.newNodeSelected = true;
374 this.applyBufferDataToState();
378 onZoom(eventTransform) {
379 if (this.isGraphMounted) {
380 this.setState(() => {
382 mainGroupTransform: eventTransform
388 onGraphDrag(xAndYCoords) {
389 let translate = `translate(${xAndYCoords.x}, ${xAndYCoords.y})`;
390 let oldTransform = this.state.mainGroupTransform;
391 if (this.isGraphMounted) {
392 this.setState(() => {
394 ...oldTransform, translate
400 onNodeDrag(datum, xAndYCoords) {
401 let nodeIndex = this.nodeIndexTracker.get(datum.id);
402 if (this.nodeBuffer[nodeIndex]) {
403 this.nodeBuffer[nodeIndex].x = xAndYCoords[0];
404 this.nodeBuffer[nodeIndex].y = xAndYCoords[1];
405 this.applyBufferDataToState();
409 addNodeInterpolator(nodeId, key, startingValue, endingValue, duration) {
410 let numberInterpolator = interpolateNumber(startingValue, endingValue);
412 let interpolationObject = {
413 nodeId: nodeId, key: key, duration: duration, timeCreated: timeNow, method: numberInterpolator
415 this.interpolators.set(nodeId, interpolationObject);
417 if (!this.areInterpolationsRunning) {
418 this.interpolationTimer.restart(this.runInterpolators, simulationKeys.DATA_COPY_INTERVAL);
419 this.areInterpolationsRunning = true;
424 // If we have no more interpolators to run then shut'r down!
425 if (this.interpolators.size === 0) {
426 this.interpolationTimer.stop();
427 this.areInterpolationsRunning = false;
430 let iterpolatorsComplete = [];
431 // Apply interpolation values
432 this.interpolators.forEach((interpolator) => {
433 let nodeIndex = this.nodeIndexTracker.get(interpolator.nodeId);
435 let elapsedTime = now() - interpolator.timeCreated;
436 // Normalize t as D3's interpolateNumber needs a value between 0 and 1
437 let t = elapsedTime / interpolator.duration;
440 iterpolatorsComplete.push(interpolator.nodeId);
442 this.nodeBuffer[nodeIndex][interpolator.key] = interpolator.method(t);
446 // Remove any interpolators that are complete
447 if (iterpolatorsComplete.length > 0) {
448 for (let i = 0; i < iterpolatorsComplete.length; i++) {
449 this.interpolators.delete(iterpolatorsComplete[i]);
453 this.applyBufferDataToState();
457 // We will be using these values veru shortly, commenting out for eslint
458 // reasons so we can build for PV let {viewWidth, viewHeight} = this.props;
459 let {nodes, links, mainGroupTransform} = this.state;
462 <div className='ts-force-selected-graph'>
463 <svg className={'fdgMainSvg'} width='100%' height='100%'>
464 <rect className={'fdgMainView'} x='0.5' y='0.5' width='99%'
465 height='99%' fill='none'/>
466 <filter id='selected-node-drop-shadow'>
467 <feGaussianBlur in='SourceAlpha' stdDeviation='2.2'/>
468 <feOffset dx='-1' dy='1' result='offsetblur'/>
469 <feFlood floodColor='rgba(0,0,0,0.5)'/>
470 <feComposite in2='offsetblur' operator='in'/>
473 <feMergeNode in='SourceGraphic'/>
476 <g className={'fdgMainG'} transform={mainGroupTransform}>
485 static graphCounter = 0;
487 static generateNewProps(nodeArray, linkArray, metaData) {
488 ForceDirectedGraph.graphCounter += 1;
490 graphCounter: ForceDirectedGraph.graphCounter,
491 nodeDataArray: nodeArray,
492 linkDataArray: linkArray,
498 export default ForceDirectedGraph;