Revize b834be5c
Přidáno uživatelem Pavel Fidranský před téměř 6 roky(ů)
sources/src/main/webapp/js/services/graphLoader.js | ||
---|---|---|
1 |
/** |
|
2 |
* @constructor |
|
3 |
*/ |
|
4 |
function GraphLoader() { |
|
1 |
class GraphLoader { |
|
5 | 2 |
|
6 | 3 |
/** |
7 | 4 |
* Loads a new graph using graph data passed as parameters. |
8 | 5 |
* @param {object} data Data of the graph. |
9 | 6 |
* @throws {InvalidArgumentError} Thrown when either graph data are incomplete. |
10 | 7 |
*/ |
11 |
this.run = function(data) {
|
|
8 |
run(data) {
|
|
12 | 9 |
if (Utils.isUndefined(data.vertices) || Utils.isUndefined(data.edges)) { |
13 | 10 |
throw new InvalidArgumentError('Invalid data.'); |
14 | 11 |
} |
15 | 12 |
|
16 |
var canvasSize = ((data.vertices.length * 75) / Math.round(Math.sqrt(data.vertices.length))) + 1000;
|
|
13 |
const canvasSize = ((data.vertices.length * 75) / Math.round(Math.sqrt(data.vertices.length))) + 1000;
|
|
17 | 14 |
|
18 | 15 |
// store archetypes |
19 | 16 |
app.archetype.vertex = data.vertexArchetypes; |
... | ... | |
28 | 25 |
app.viewportComponent.addSvgDefinition('vertexArchetypeIcon-' + vertexArchetype.name, vertexArchetype.icon); |
29 | 26 |
}); |
30 | 27 |
|
31 |
var highlightedNodeId; |
|
32 |
var highlightedNodeType; |
|
28 |
// highlighted node |
|
29 |
let highlightedNodeId; |
|
30 |
let highlightedNodeType; |
|
33 | 31 |
if (Utils.isDefined(data.highlightedVertex) && data.highlightedVertex.length > 0) { |
34 |
var highlightedNodeAttr = data.highlightedVertex.split("-");
|
|
32 |
let highlightedNodeAttr = data.highlightedVertex.split('-');
|
|
35 | 33 |
if (highlightedNodeAttr.length === 2) { |
36 | 34 |
highlightedNodeType = highlightedNodeAttr[0]; |
37 | 35 |
highlightedNodeId = parseInt(highlightedNodeAttr[1], 10); |
38 | 36 |
} |
39 | 37 |
} |
38 |
|
|
39 |
// highlighted edge |
|
40 |
let highlightedEdgeId; |
|
40 | 41 |
if (Utils.isDefined(data.highlightedEdge) && data.highlightedEdge.length > 0) { |
41 |
var highlightedEdgeId = parseInt(data.highlightedEdge, 10);
|
|
42 |
highlightedEdgeId = parseInt(data.highlightedEdge, 10); |
|
42 | 43 |
} |
43 | 44 |
|
44 |
var highlightedNode = undefined;
|
|
45 |
var highlightedEdge = undefined;
|
|
45 |
let highlightedNode = undefined;
|
|
46 |
let highlightedEdge = undefined;
|
|
46 | 47 |
|
47 | 48 |
// construct vertices |
48 |
var vertexMap = {};
|
|
49 |
let vertexMap = new Map;
|
|
49 | 50 |
data.vertices.forEach(component => { |
50 |
var vertex = new Vertex(component);
|
|
51 |
let vertex = new Vertex(component);
|
|
51 | 52 |
|
52 | 53 |
if (highlightedNodeType === 'vertex' && highlightedNodeId === vertex.id ){ |
53 | 54 |
highlightedNode = vertex; |
54 | 55 |
} |
55 | 56 |
|
56 |
var position = component.position; |
|
57 |
|
|
58 |
if (position === null || Utils.isUndefined(position)) { |
|
57 |
if (component.position === null || Utils.isUndefined(component.position)) { |
|
59 | 58 |
// set random |
60 | 59 |
vertex.position = new Coordinates( |
61 | 60 |
Math.floor(Math.random() * canvasSize), |
62 | 61 |
Math.floor(Math.random() * canvasSize), |
63 | 62 |
); |
64 |
|
|
65 | 63 |
} else { |
66 |
vertex.position = new Coordinates(position.x, position.y);
|
|
64 |
vertex.position = new Coordinates(component.position.x, component.position.y);
|
|
67 | 65 |
} |
68 | 66 |
|
69 | 67 |
app.nodeList.push(vertex); |
70 | 68 |
app.vertexList.push(vertex); |
71 | 69 |
|
72 |
vertexMap[component.id] = vertex;
|
|
70 |
vertexMap.set(component.id, vertex);
|
|
73 | 71 |
}); |
74 | 72 |
|
75 | 73 |
// construct edges |
76 | 74 |
data.edges.forEach(component => { |
77 |
var edge = new Edge(component);
|
|
75 |
let edge = new Edge(component);
|
|
78 | 76 |
|
79 | 77 |
if (highlightedEdgeId === edge.id) { |
80 | 78 |
highlightedEdge = edge; |
81 | 79 |
} |
82 | 80 |
|
83 |
var fromNode = vertexMap[component.from];
|
|
81 |
let fromNode = vertexMap.get(component.from);
|
|
84 | 82 |
if (fromNode) { |
85 | 83 |
fromNode.addOutEdge(edge); |
86 | 84 |
} |
87 | 85 |
|
88 |
var toNode = vertexMap[component.to];
|
|
86 |
let toNode = vertexMap.get(component.to);
|
|
89 | 87 |
if (toNode) { |
90 | 88 |
toNode.addInEdge(edge); |
91 | 89 |
} |
... | ... | |
98 | 96 |
app.edgeList.push(edge); |
99 | 97 |
}); |
100 | 98 |
|
101 |
delete vertexMap; |
|
102 |
|
|
103 | 99 |
// render components |
104 | 100 |
app.vertexList.forEach(vertex => { |
105 | 101 |
app.viewportComponent.addNode(vertex); |
... | ... | |
119 | 115 |
|
120 | 116 |
// construct groups |
121 | 117 |
data.groups.forEach(component => { |
122 |
var group = new Group(component);
|
|
118 |
let group = new Group(component);
|
|
123 | 119 |
|
124 | 120 |
if (highlightedNodeType === 'group' && highlightedNodeId === group.id) { |
125 | 121 |
highlightedNode = group; |
126 | 122 |
} |
127 | 123 |
|
128 | 124 |
// position |
129 |
var position = component.position; |
|
130 |
if (position === null || Utils.isUndefined(position)) { |
|
125 |
if (component.position === null || Utils.isUndefined(component.position)) { |
|
131 | 126 |
// set random |
132 | 127 |
group.position = new Coordinates( |
133 | 128 |
Math.floor(Math.random() * canvasSize), |
134 | 129 |
Math.floor(Math.random() * canvasSize), |
135 | 130 |
); |
136 |
|
|
137 | 131 |
} else { |
138 |
group.position = new Coordinates(position.x, position.y);
|
|
132 |
group.position = new Coordinates(component.position.x, component.position.y);
|
|
139 | 133 |
} |
140 | 134 |
|
141 | 135 |
// vertices |
... | ... | |
193 | 187 |
if (Utils.isDefined(highlightedNode)) { |
194 | 188 |
highlightedNode.highlightWithNeighbours(true); |
195 | 189 |
} |
196 |
}; |
|
190 |
} |
|
191 |
} |
|
197 | 192 |
|
198 |
} |
Také k dispozici: Unified diff
reworked GraphLoader to ES6 class