-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLayout.js
More file actions
191 lines (153 loc) · 5.02 KB
/
Layout.js
File metadata and controls
191 lines (153 loc) · 5.02 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
module.exports = constant;
var merge = require('ngraph.merge');
var random = require('ngraph.random').random;
var centrality = require('ngraph.centrality');
var AtlasSupervisor = require('./supervisor');
/**
* Does not really perform any layouting algorithm but is compliant
* with renderer interface. Allowing clients to provide specific positioning
* callback and get static layout of the graph
*
* @param {Viva.Graph.graph} graph to layout
* @param config
* @param {Object} userSettings
*/
function constant(graph, config, userSettings) {
userSettings = merge(userSettings, {
maxX: 1024,
maxY: 1024,
seed: 'Deterministic randomness made me do this'
});
var rand = random(userSettings.seed),
layoutLinks = {},
generateRandomPosition = function () {
return {
x: rand.next(userSettings.maxX/2) - rand.next(userSettings.maxX/2),
y: rand.next(userSettings.maxY/2) - rand.next(userSettings.maxY/2),
isPinned: false,
changed: false
};
},
layoutNodes = typeof Object.create === 'function' ? Object.create(null) : {},
layoutNodesArray = [],
layoutLinksArray = [],
initNode = function (node) {
var nodeBody = generateRandomPosition();
nodeBody.id = node.id;
layoutNodesArray.push(nodeBody);
layoutNodes[node.id] = nodeBody;
},
initLink = function (link) {
layoutLinks[link.id] = link;
layoutLinksArray.push(link);
},
onGraphChanged = function (changes) {
console.warn('Not implemented');
/*for (var i = 0; i < changes.length; ++i) {
var change = changes[i];
if (change.node) {
if (change.changeType === 'add') {
initNode(change.node);
} else {
delete layoutNodes[change.node.id];
}
}
if (change.link) {
if (change.changeType === 'add') {
initLink(change.link);
} else {
delete layoutLinks[change.link.id];
}
}
}*/
};
graph.forEachNode(initNode);
graph.forEachLink(initLink);
var degreeCentrality = centrality.degree(graph);
graph.on('changed', onGraphChanged);
var supervisor = new AtlasSupervisor({
nodes: layoutNodesArray,
edges: layoutLinksArray,
degree: degreeCentrality
}, config);
return {
config: function (config) {
supervisor.configure(config)
},
/**
* Attempts to layout graph within given number of iterations.
*
* @param {integer} [iterationsCount] number of algorithm's iterations.
* The constant layout ignores this parameter.
*/
run: function (iterationsCount) {
throw new Error('not implemented');
},
/**
* One step of layout algorithm
*/
step: function () {
if (supervisor.isPending()) return;
supervisor.step();
return false;
},
/**
* Returns rectangle structure {x1, y1, x2, y2}, which represents
* current space occupied by graph.
*/
getGraphRect: function () {
return supervisor.getGraphRect();
},
/**
* Request to release all resources
*/
dispose: function () {
graph.off('change', onGraphChanged);
supervisor.kill();
},
isNodePinned: function (node) {
return layoutNodes[node.id].isPinned;
},
/**
* Requests layout algorithm to pin/unpin node to its current position
* Pinned nodes should not be affected by layout algorithm and always
* remain at their position
*/
pinNode: function (node, isPinned) {
var body = layoutNodes[node.id];
if (body.isPinned !== isPinned) {
body.isPinned = isPinned;
body.changed = true;
}
},
/**
* Gets position of a node by its id. If node was not seen by this
* layout algorithm undefined value is returned;
*/
getNodePosition: getNodePosition,
/**
* Returns {from, to} position of a link.
*/
getLinkPosition: function (linkId) {
var link = layoutLinks[linkId];
return {
from: getNodePosition(link.fromId),
to: getNodePosition(link.toId)
};
},
/**
* Sets position of a node to a given coordinates
*/
setNodePosition: function (nodeId, x, y) {
var body = layoutNodes[nodeId];
if (body) {
body.x = x;
body.y = y;
}
body.changed = true;
}
};
function getNodePosition(nodeId) {
return layoutNodes[nodeId];
}
}