Revize 43950ee6
Přidáno uživatelem Patrik Harag před téměř 6 roky(ů)
sources/imiger-core/src/main/webapp/js/graphLoader.js | ||
---|---|---|
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 |
* @throws {InvalidArgumentError} Thrown when either graph data are incomplete. |
|
10 |
*/ |
|
11 |
this.run = function(data) { |
|
12 |
if (app.utils.isUndefined(data.vertices) || app.utils.isUndefined(data.edges)) { |
|
13 |
throw new InvalidArgumentError('Invalid data.'); |
|
14 |
} |
|
15 |
|
|
16 |
var canvasSize = ((data.vertices.length * 75) / Math.round(Math.sqrt(data.vertices.length))) + 1000; |
|
17 |
|
|
18 |
// store archetypes |
|
19 |
app.archetype.vertex = data.vertexArchetypes; |
|
20 |
app.archetype.edge = data.edgeArchetypes; |
|
21 |
|
|
22 |
app.attributeTypeList = data.attributeTypes; |
|
23 |
app.possibleEnumValues = data.possibleEnumValues; |
|
24 |
|
|
25 |
app.archetype.vertex.forEach(function (vertexArchetype) { |
|
26 |
|
|
27 |
var icon = ""; |
|
28 |
if(app.utils.isDefined(vertexArchetype.icon) && vertexArchetype.icon !== "") { |
|
29 |
icon = vertexArchetype.icon; |
|
30 |
} else { |
|
31 |
// TODO: solve duplicate icons when name starts with the same letter |
|
32 |
icon = "<text fill='black' x='0' y='7'>" + vertexArchetype.name.substring(0,1) + "</text>" |
|
33 |
} |
|
34 |
app.viewportComponent.addSvgDefinition('vertexArchetypeIcon-' + vertexArchetype.name, icon); |
|
35 |
}); |
|
36 |
|
|
37 |
var highlightedNodeId; |
|
38 |
var highlightedNodeType; |
|
39 |
if (app.utils.isDefined(data.highlightedVertex) && data.highlightedVertex.length > 0) { |
|
40 |
var highlightedNodeAttr = data.highlightedVertex.split("-"); |
|
41 |
if (highlightedNodeAttr.length === 2) { |
|
42 |
highlightedNodeType = highlightedNodeAttr[0]; |
|
43 |
highlightedNodeId = parseInt(highlightedNodeAttr[1], 10); |
|
44 |
} |
|
45 |
} |
|
46 |
if (app.utils.isDefined(data.highlightedEdge) && data.highlightedEdge.length > 0) { |
|
47 |
var highlightedEdgeId = parseInt(data.highlightedEdge, 10); |
|
48 |
} |
|
49 |
|
|
50 |
var highlightedNode = undefined; |
|
51 |
var highlightedEdge = undefined; |
|
52 |
|
|
53 |
// construct vertices |
|
54 |
var vertexMap = {}; |
|
55 |
data.vertices.forEach(function(component) { |
|
56 |
var vertex = new Vertex(component); |
|
57 |
|
|
58 |
if (highlightedNodeType === 'vertex' && highlightedNodeId === vertex.id ){ |
|
59 |
highlightedNode = vertex; |
|
60 |
} |
|
61 |
|
|
62 |
var position = component.position; |
|
63 |
|
|
64 |
if (position === null || app.utils.isUndefined(position)) { |
|
65 |
// set random |
|
66 |
vertex.setPosition(new Coordinates( |
|
67 |
Math.floor(Math.random() * canvasSize), |
|
68 |
Math.floor(Math.random() * canvasSize), |
|
69 |
)); |
|
70 |
|
|
71 |
} else { |
|
72 |
vertex.setPosition(new Coordinates(position.x, position.y)); |
|
73 |
} |
|
74 |
|
|
75 |
app.nodeList.push(vertex); |
|
76 |
app.vertexList.push(vertex); |
|
77 |
|
|
78 |
vertexMap[component.id] = vertex; |
|
79 |
}); |
|
80 |
|
|
81 |
// construct edges |
|
82 |
data.edges.forEach(function(component) { |
|
83 |
var edge = new Edge(component); |
|
84 |
|
|
85 |
if (highlightedEdgeId === edge.id ){ |
|
86 |
highlightedEdge = edge; |
|
87 |
} |
|
88 |
|
|
89 |
var fromNode = vertexMap[component.from]; |
|
90 |
if (fromNode) { |
|
91 |
fromNode.addOutEdge(edge); |
|
92 |
} |
|
93 |
|
|
94 |
var toNode = vertexMap[component.to]; |
|
95 |
if (toNode) { |
|
96 |
toNode.addInEdge(edge); |
|
97 |
} |
|
98 |
|
|
99 |
if (fromNode && toNode) { |
|
100 |
fromNode.incrementRelatedArchetype(toNode.archetype); |
|
101 |
toNode.incrementRelatedArchetype(fromNode.archetype); |
|
102 |
} |
|
103 |
|
|
104 |
app.edgeList.push(edge); |
|
105 |
}); |
|
106 |
|
|
107 |
delete vertexMap; |
|
108 |
|
|
109 |
// render components |
|
110 |
app.vertexList.forEach(function(vertex) { |
|
111 |
app.viewportComponent.addVertex(vertex); |
|
112 |
}); |
|
113 |
|
|
114 |
app.edgeList.forEach(function(edge) { |
|
115 |
app.viewportComponent.addEdge(edge); |
|
116 |
}); |
|
117 |
|
|
118 |
// find unconnected vertices |
|
119 |
app.vertexList.filter(function(vertex) { |
|
120 |
return vertex.isUnconnected(); |
|
121 |
}).forEach(function(vertex) { |
|
122 |
vertex.exclude(); |
|
123 |
app.sidebarComponent.unconnectedNodeListComponent.add(vertex); |
|
124 |
}); |
|
125 |
|
|
126 |
// construct groups |
|
127 |
data.groups.forEach(function(component) { |
|
128 |
var group = new Group(component); |
|
129 |
|
|
130 |
if (highlightedNodeType === 'group' && highlightedNodeId === group.id ){ |
|
131 |
highlightedNode = group; |
|
132 |
} |
|
133 |
|
|
134 |
// position |
|
135 |
var position = component.position; |
|
136 |
if (position === null || app.utils.isUndefined(position)) { |
|
137 |
// set random |
|
138 |
group.setPosition(new Coordinates( |
|
139 |
Math.floor(Math.random() * canvasSize), |
|
140 |
Math.floor(Math.random() * canvasSize), |
|
141 |
)); |
|
142 |
|
|
143 |
} else { |
|
144 |
group.setPosition(new Coordinates(position.x, position.y)); |
|
145 |
} |
|
146 |
|
|
147 |
// vertices |
|
148 |
app.vertexList.filter(function(vertex) { |
|
149 |
return component.verticesId.indexOf(vertex.id) > -1; |
|
150 |
}).forEach(function(vertex) { |
|
151 |
group.addVertex(vertex); |
|
152 |
}); |
|
153 |
|
|
154 |
app.nodeList.push(group); |
|
155 |
app.groupList.push(group); |
|
156 |
|
|
157 |
app.viewportComponent.addGroup(group); |
|
158 |
}); |
|
159 |
|
|
160 |
// exclude nodes |
|
161 |
data.sideBar.forEach(function(excludedNode) { |
|
162 |
if(typeof excludedNode.id !== 'string' && !(excludedNode.id instanceof String)) { |
|
163 |
return; |
|
164 |
} |
|
165 |
var idArr = excludedNode.id.split("-"); |
|
166 |
if(idArr.length !== 2){ |
|
167 |
return; |
|
168 |
} |
|
169 |
idArr[1] = parseInt(idArr[1], 10); |
|
170 |
|
|
171 |
var node = app.nodeList.find(function(node) { |
|
172 |
var prefix = ''; |
|
173 |
if (node instanceof Vertex) { |
|
174 |
prefix = 'vertex'; |
|
175 |
} else if (node instanceof Group) { |
|
176 |
prefix = 'group'; |
|
177 |
} |
|
178 |
return idArr[0] === prefix && node.id === idArr[1]; |
|
179 |
}); |
|
180 |
|
|
181 |
if (app.utils.isDefined(node)) { |
|
182 |
app.sidebarComponent.excludedNodeListComponent.add(node); |
|
183 |
} |
|
184 |
}); |
|
185 |
|
|
186 |
// center viewport |
|
187 |
app.viewportComponent.center(); |
|
188 |
|
|
189 |
// update status bar |
|
190 |
app.sidebarComponent.statusBarComponent.setComponentCount(data.vertices.length); |
|
191 |
|
|
192 |
if (app.utils.isDefined(highlightedEdge)) { |
|
193 |
highlightedEdge.setHighlighted(true); |
|
194 |
highlightedEdge.getFrom().setHighlighted(true); |
|
195 |
highlightedEdge.getTo().setHighlighted(true); |
|
196 |
} |
|
197 |
if (app.utils.isDefined(highlightedNode)) highlightedNode.setHighlightedWithNeighbours(true); |
|
198 |
}; |
|
199 |
|
|
200 |
} |
Také k dispozici: Unified diff
Remove duplicate file (refs #7468)