forked from KathiraveluLab/DHGWorkflow
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathGraphArea.jsx
More file actions
68 lines (63 loc) · 2.56 KB
/
GraphArea.jsx
File metadata and controls
68 lines (63 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React, { useEffect, useRef, useState } from 'react';
import { edgeValidator, nodeValidator } from './config/defaultValidators';
import MyGraph from './graph-builder';
import { actionType as T } from './reducer';
function Graph({
el, superState, dispatcher, graphID, serverID, graphML, projectName, graphContainerRef, active, authorName,
}) {
const [instance, setInstance] = useState(null);
const ref = useRef();
const setContainerDim = (element) => {
if (element) {
const elToAss = element;
elToAss.style.width = `${graphContainerRef.current.offsetWidth - 2}px`;
elToAss.style.height = `${graphContainerRef.current.offsetHeight - 2}px`;
}
};
const initialiseNewGraph = () => {
const myGraph = new MyGraph(
graphID, ref.current, dispatcher, superState, projectName, nodeValidator, edgeValidator, authorName,
);
if (graphID) myGraph.loadGraphFromLocalStorage();
if (serverID) {
myGraph.set({ serverID });
myGraph.forcePullFromServer();
}
if (graphML) myGraph.setGraphML(graphML);
myGraph.setCurStatus();
myGraph.cy.on('zoom', () => {
dispatcher({ type: T.SET_ZOOM_LEVEL, payload: (myGraph.cy.zoom() * 100).toFixed(0) });
});
return myGraph;
};
// Remote server implementation - Not being used.
// useEffect(() => {
// if (active && loaded && serverID) {
// window.history.pushState(null, null, path.join(process.env.PUBLIC_URL, 's', serverID));
// } else if (active && loaded && graphID) {
// window.history.pushState(null, null, path.join(process.env.PUBLIC_URL, 'l', graphID));
// }
// }, [active, serverID, loaded, graphID]);
useEffect(() => instance && instance.set({ superState }), [instance, superState]);
useEffect(() => active && instance && instance.setCurStatus(), [active && instance]);
useEffect(() => {
if (active && instance) dispatcher({ type: T.SET_CUR_INSTANCE, payload: instance });
}, [active && instance]);
useEffect(() => {
if (ref.current) {
setContainerDim(ref.current);
window.addEventListener('resize', () => setContainerDim(ref.current));
setInstance(initialiseNewGraph());
}
}, [ref]);
const { id } = el;
return (
<div
style={{ zIndex: 1, display: active ? 'block' : 'none' }}
key={id}
className="graph-element"
ref={ref}
/>
);
}
export default Graph;