1
|
/**
|
2
|
* @constructor
|
3
|
*/
|
4
|
function GraphLoader() {
|
5
|
|
6
|
/**
|
7
|
* Loads a new graph using graph data passed as parameters.
|
8
|
* @param {object} data Data of the graph.
|
9
|
* @param {object} graphExportData Export of a previously loaded diagram.
|
10
|
* @throws {InvalidArgumentException} Thrown when either graph data or export data are incomplete.
|
11
|
*/
|
12
|
this.run = function(data, graphExportData) {
|
13
|
if (app.utils.isUndefined(data.vertices) || app.utils.isUndefined(data.edges)) {
|
14
|
throw new InvalidArgumentException('Invalid data.');
|
15
|
}
|
16
|
|
17
|
if (graphExportData !== null && (app.utils.isUndefined(data.vertices) || app.utils.isUndefined(data.edges))) {
|
18
|
throw new InvalidArgumentException('Invalid graph export data.');
|
19
|
}
|
20
|
|
21
|
var canvasSize = ((data.vertices.length * 75) / Math.round(Math.sqrt(data.vertices.length))) + 1000;
|
22
|
|
23
|
// vertices
|
24
|
var vertexMap = {};
|
25
|
data.vertices.forEach(function(component) {
|
26
|
var vertex = new Vertex(component);
|
27
|
|
28
|
var isPositionSet = false;
|
29
|
if (graphExportData !== null) {
|
30
|
var exportedVertex = graphExportData.vertices.find(function(exportedVertex) {
|
31
|
return exportedVertex.name == this;
|
32
|
}, vertex.name);
|
33
|
|
34
|
// vertex is present in exported graph data
|
35
|
if (app.utils.isDefined(exportedVertex)) {
|
36
|
var coords = new Coordinates(exportedVertex.position.x, exportedVertex.position.y);
|
37
|
vertex.setPosition(coords);
|
38
|
|
39
|
isPositionSet = true;
|
40
|
}
|
41
|
}
|
42
|
|
43
|
if (isPositionSet === false) {
|
44
|
vertex.setPosition(new Coordinates(
|
45
|
Math.floor(Math.random() * canvasSize),
|
46
|
Math.floor(Math.random() * canvasSize),
|
47
|
));
|
48
|
}
|
49
|
|
50
|
app.nodeList.push(vertex);
|
51
|
app.vertexList.push(vertex);
|
52
|
|
53
|
vertexMap[component.symbolicName] = vertex;
|
54
|
|
55
|
app.viewportComponent.addVertex(vertex);
|
56
|
});
|
57
|
|
58
|
// edges
|
59
|
data.edges.forEach(function(component) {
|
60
|
var edge = new Edge(component);
|
61
|
|
62
|
var fromNode = vertexMap[component.from];
|
63
|
if (fromNode) {
|
64
|
fromNode.addOutEdge(edge);
|
65
|
}
|
66
|
|
67
|
var toNode = vertexMap[component.to];
|
68
|
if (toNode) {
|
69
|
toNode.addInEdge(edge);
|
70
|
}
|
71
|
|
72
|
app.edgeList.push(edge);
|
73
|
|
74
|
app.viewportComponent.addEdge(edge);
|
75
|
});
|
76
|
|
77
|
delete vertexMap;
|
78
|
|
79
|
// center viewport
|
80
|
app.viewportComponent.center();
|
81
|
|
82
|
// find unconnected vertices
|
83
|
app.vertexList.filter(function(vertex) {
|
84
|
return vertex.isUnconnected();
|
85
|
}).forEach(function(vertex) {
|
86
|
vertex.exclude();
|
87
|
app.sidebarComponent.unconnectedNodeListComponent.add(vertex);
|
88
|
});
|
89
|
|
90
|
// find missing components
|
91
|
app.edgeList.filter(function(edge) {
|
92
|
return edge.getFrom().name === app.constants.notFoundVertexName;
|
93
|
}).forEach(function(edge) {
|
94
|
var compatibilityInfoList = edge.getCompatibilityInfo();
|
95
|
compatibilityInfoList.forEach(function(compatibilityInfo) {
|
96
|
if (compatibilityInfo.incomps.length === 0) return;
|
97
|
|
98
|
compatibilityInfo.incomps.forEach(function(incompatibility) {
|
99
|
if (!incompatibility.desc.isIncompCause) return;
|
100
|
|
101
|
app.sidebarComponent.missingClassListComponent.add(incompatibility.desc.name);
|
102
|
});
|
103
|
});
|
104
|
});
|
105
|
|
106
|
// update status bar
|
107
|
app.sidebarComponent.statusBarComponent.setComponentCount(data.vertices.length);
|
108
|
app.sidebarComponent.statusBarComponent.setGraphVersion(app.cookies.get('graphVersion'));
|
109
|
};
|
110
|
|
111
|
}
|