-
Notifications
You must be signed in to change notification settings - Fork 24
/
build.js
336 lines (299 loc) · 7.86 KB
/
build.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
'use strict';
function attachEvents() {
node
.on('mouseover', function(d) {
if (!mousedown_node || d === mousedown_node) {
return;
}
// enlarge target node
d3.select(this).attr('transform', 'scale(1.1)');
})
.on('mouseout', function(d) {
if (!mousedown_node || d === mousedown_node) {
return;
}
// unenlarge target node
d3.select(this).attr('transform', '');
})
.on('mousedown', function(d) {
if (d3.event.ctrlKey) {
return;
}
// select node
mousedown_node = d;
if (mousedown_node === selected_node) {
selected_node = null;
}
else selected_node = mousedown_node;
selected_link = null;
// reposition drag line
drag_line
.classed('hidden', false)
.attr({
'x1': mousedown_node.x,
'y1': mousedown_node.y,
'x2': mousedown_node.x,
'y2': mousedown_node.y
});
restart();
attachEvents()
})
.on('mouseup', function(d) {
if (!mousedown_node) {
return;
}
// needed by FF
drag_line
.classed('hidden', true)
.style('marker-end', '');
// check for drag-to-self
mouseup_node = d;
if (mouseup_node === mousedown_node) {
editing = true;
editNode(d);
resetMouseVars();
return;
}
// unenlarge target node
d3.select(this).attr('transform', '');
// add link to graph (update if exists)
// NB: links are strictly source < target; arrows separately specified by booleans
var source, target,
source = mousedown_node;
target = mouseup_node;
var link = window.graph.links.filter(function(l) {
return (l.source === source && l.target === target) ||
(l.source === target && l.target === source);
})[0];
if (link) {
return;
} else {
link = {
source: source,
target: target,
strength: 10
};
window.graph.links.push(link);
}
// select new link
selected_link = link;
selected_node = null;
restart();
attachEvents();
});
path
.on('mousedown', function(d) {
if (d3.event.ctrlKey) {
return;
}
// select link
mousedown_link = d;
if (mousedown_link === selected_link) {
selected_link = null;
} else {
selected_link = mousedown_link;
}
selected_node = null;
editLink(d);
resetMouseVars();
restart();
});
}
function resetMouseVars() {
mousedown_node = null;
mouseup_node = null;
mousedown_link = null;
}
function resetMenus() {
d3.select('#node-menu').style('display', 'none');
d3.select('#link-menu').style('display', 'none');
}
function writeGraph() {
d3.select('#graph-field').html(JSON.stringify(window.graph));
}
function spliceLinksForNode(node) {
var toSplice = window.graph.links.filter(function(l) {
return (l.source === node || l.target === node);
});
toSplice.map(function(l) {
window.graph.links.splice(window.graph.links.indexOf(l), 1);
});
}
function mousedown() {
// prevent I-bar on drag
//d3.event.preventDefault();
// because :active only works in WebKit?
svg.classed('active', true);
if (d3.event.ctrlKey || mousedown_node || mousedown_link) {
return;
}
// insert new node at point
var point = d3.mouse(this),
node = {
id: ++window.graph.lastId,
name: 'New ' + window.graph.lastId,
x: point[0],
y: point[1],
r: 12
};
window.graph.nodes.push(node);
restart();
attachEvents();
}
function mousemove() {
if (!mousedown_node) {
return;
}
// update drag line
var point = d3.mouse(this);
drag_line
.attr({
'x1': mousedown_node.x,
'y1': mousedown_node.y,
'x2': point[0],
'y2': point[1]
});
attachEvents();
restart();
}
function mouseup() {
if (mousedown_node) {
// hide drag line
drag_line
.classed('hidden', true)
.style('marker-end', '');
}
if (editing) {
return;
}
// because :active only works in WebKit?
svg.classed('active', false);
// clear mouse event vars
resetMouseVars();
}
// only respond once per keydown
var lastKeyDown = -1;
function keydown() {
if (lastKeyDown !== -1) {
return;
}
lastKeyDown = d3.event.keyCode;
// ctrl
if(d3.event.keyCode === 17) {
node.call(force.drag);
svg.classed('ctrl', true);
}
if(!selected_node && !selected_link) {
return;
}
if (d3.event.keyCode === 8 || d3.event.keyCode === 46) {
d3.event.preventDefault();
if (selected_node) {
window.graph.nodes.splice(window.graph.nodes.indexOf(selected_node), 1);
spliceLinksForNode(selected_node);
} else if (selected_link) {
window.graph.links.splice(window.graph.links.indexOf(selected_link), 1);
}
selected_link = null;
selected_node = null;
restart();
attachEvents();
}
}
function keyup() {
lastKeyDown = -1;
// ctrl
if (d3.event.keyCode === 17) {
node
.on('mousedown.drag', null)
.on('touchstart.drag', null);
svg.classed('ctrl', false);
}
}
function editNode(d) {
d3.select('#link-menu').style('display', 'none');
var nodeMenu = d3.select('#node-menu');
nodeMenu.style('display', 'block');
nodeMenu.select('#edit-node-name')
.attr('value', d.name)
.on('keyup', function() {
window.graph.nodes.filter(function(node) {
return node === d;
})[0].name = this.value;
restart();
});
nodeMenu.select('#edit-node-r')
.attr('value', d.r)
.on('input', function() {
window.graph.nodes.filter(function(node) {
return node.id === d.id;
})[0].r = this.value;
restart();
});
nodeMenu.select('#edit-node-dashed')
.property('checked', d.dashed)
.on('change', function() {
window.graph.nodes.filter(function(link) {
return link === d;
})[0].dashed = d3.select(this).property('checked');
restart();
});
}
function editLink(d) {
d3.select('#node-menu').style('display', 'none');
var linkMenu = d3.select('#link-menu');
linkMenu.style('display', 'block');
linkMenu.select('#source-name').text(d.source.name);
linkMenu.select('#target-name').text(d.target.name);
linkMenu.select('#edit-center-text')
.attr('value', d.centerText ? d.centerText : '')
.on('keyup', function() {
window.graph.links.filter(function(link) {
return link === d;
})[0].centerText = this.value;
restart();
});
linkMenu.select('#edit-source-text')
.attr('value', d.sourceText ? d.sourceText : '')
.on('keyup', function() {
window.graph.links.filter(function(link) {
return link === d;
})[0].sourceText = this.value;
restart();
});
linkMenu.select('#edit-target-text')
.attr('value', d.targetText ? d.targetText : '')
.on('keyup', function() {
window.graph.links.filter(function(link) {
return link === d;
})[0].targetText = this.value;
restart();
});
linkMenu.select('#edit-strength')
.attr('value', d.strength)
.on('input', function() {
window.graph.links.filter(function(link) {
return link === d;
})[0].strength = this.value;
restart();
});
linkMenu.select('#edit-link-dashed')
.property('checked', d.dashed)
.on('change', function() {
window.graph.links.filter(function(link) {
return link === d;
})[0].dashed = d3.select(this).property('checked');
restart();
});
}
svg.on('mousedown', mousedown)
.on('mousemove', mousemove)
.on('mouseup', mouseup);
d3.select(window)
.on('keydown', keydown)
.on('keyup', keyup);
d3.select('.expand-help').on('click', function(e) {
d3.event.preventDefault();
var body = d3.select('.instructions .body');
body.classed('hidden', !body.classed('hidden'));
});