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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
"use strict"
define(['jquery', 'd3', 'consts', 'rz_bus', 'util', 'model/graph', 'model/core', 'view/helpers', 'view/view', 'rz_observer', 'view/selection', 'rz_config', 'rz_mesh', 'model/diff', "view/graph_view"],
function($, d3, consts, rz_bus, util, model_graph, model_core, view_helpers, view, rz_observer, selection, rz_config, rz_mesh, model_diff, graph_view) {
var addednodes = [],
vis,
graphinterval = 0,
timeline_timer = 0,
deliverables = [],
circle, // <-- should not be module globals.
scrollValue = 0,
main_graph,
edit_graph,
main_graph_view,
edit_graph_view;
// "CSS" for SVG elements. Reused for editing elements.
var node_text_dx = 15,
node_text_dy = '.30em',
svg_input_fo_node_x = node_text_dx,
svg_input_fo_node_y = '-.70em',
svg_input_fo_height = '30px';
/**
* svgInput - creates an embedded input element under a given
*
* edit_node(@sibling, @node)
* edit_link(@sibling, @link)
*/
var svgInput = (function() {
var measure_node = $('#measure-node')[0],
measure_link = $('#measure-link')[0],
original_element,
is_link;
function appendForeignElementInputWithID(base, elemid, width, height)
{
var input = document.createElement('input'),
body = document.createElement('body'),
fo = document.createElementNS('http://www.w3.org/2000/svg', 'foreignObject');
body.appendChild(input);
fo.setAttribute('height', height || svg_input_fo_height);
fo.style.pointerEvents = 'none';
input.style.pointerEvents = 'all';
fo.appendChild(body);
base.appendChild(fo);
input.setAttribute('id', elemid);
return input;
}
function measure(text)
{
var span;
span = is_link ? measure_link : measure_node;
span.innerHTML = text;
return span.getBoundingClientRect().width; // $().width() works too
}
function onkeydown(e) {
var ret = undefined,
jelement = createOrGetSvgInput(),
element = jelement[0],
newname = jelement.val(),
fo = createOrGetSvgInputFO(),
d;
if (element != this) {
console.log('unexpected editname_on_keypress this should be the svg-input element');
}
if (e.which == 13 || e.which == 27) {
ret = false;
d = jelement.data().d;
if (e.which == 13 && newname != d.name) {
if (d.hasOwnProperty('__src')) {
main_graph.update_link(d, {name: newname});
} else {
// TODO - display hourglass
// TODO - use promises to make A follows B readable.
main_graph.update_node(d, {name: newname});
}
}
hide();
}
rz_bus.ui_key.push({where: consts.KEYSTROKE_WHERE_EDIT_NODE, keys: [e.which]});
return ret;
};
function resize_measure(e) {
resize(measure($(e.target).val()) + 30);
}
function resize(new_width) {
var svg_input = createOrGetSvgInput(),
fo = createOrGetSvgInputFO();
svg_input.css('width', new_width);
fo.attr('width', new_width);
}
// FIXME: element being deleted. Some delete is legit - removal of related element. Some isn't (a click).
// Instead of investigating (time constraint) reparenting as sibling, and introducing
// this function. Cost of creation of element is negligble, it's just ugly..
function createOrGetSvgInput()
{
var svg_input_name = 'svg-input',
svg_input_selector = '#' + svg_input_name,
svg_input = $(svg_input_selector);
if (svg_input.length == 0) {
console.log('creating new svg-input');
svg_input = $(appendForeignElementInputWithID(vis[0][0], svg_input_name));
svg_input.on('keydown', onkeydown);
svg_input.bind('change keypress', resize_measure);
}
return svg_input;
}
function createOrGetSvgInputFO()
{
return createOrGetSvgInput().parent().parent();
}
/*
* @param e visual node element
* @param n node model object
*/
function enable(e, n) {
var oldname = n.name,
svg_input = createOrGetSvgInput(),
fo = createOrGetSvgInputFO();
is_link = n.hasOwnProperty('__src');
e.parentNode.appendChild(fo[0]); // This will unparent from the old parent
if (is_link) {
fo.attr('transform', e.getAttribute('transform'));
// XXX links set the text-anchor middle attribute. no idea how to do that
fo.attr('x', -$(e).width() / 2);
fo.attr('y', -$(e).height() / 2 - 3); // XXX This minus 3 is only kinda ok.
fo.attr('class', 'svg-input-fo-link');
} else {
fo.attr('x', svg_input_fo_node_x);
fo.attr('y', svg_input_fo_node_y);
fo.attr('transform', null);
fo.attr('class', 'svg-input-fo-node');
}
// Set width correctly
resize(measure(oldname) + 30);
fo.show();
svg_input.val(oldname);
svg_input.data().d = n;
svg_input.focus();
if (original_element) {
original_element.show();
}
original_element = $(e);
original_element.hide();
// TODO: set cursor to correct location in text
}
function hide() {
createOrGetSvgInputFO().hide();
if (original_element && original_element.show) {
original_element.show();
}
}
return {
enable: enable,
hide: hide,
};
}());
var zoomProgress = false;
var zoomBus = new Bacon.Bus();
var zoom_property = zoomBus.toProperty();
zoomBus.push(zoomProgress); // set initial value
function svg_click_handler(e) {
if (zoomProgress) {
zoomProgress = false;
return;
}
if (e.originalEvent.target.nodeName != 'svg') {
return;
}
svgInput.hide();
selection.clear();
view.hide();
update_view__graph(false);
}
function recenterZoom() {
vis.attr("transform", "translate(0,0)scale(1)");
}
main_graph = new model_graph.Graph({temporary: false, base: null});
edit_graph = new model_graph.Graph({temporary: true, base: main_graph});
var initDrawingArea = function () {
function zoom() {
zoomProgress = true;
vis.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
d3.event.sourceEvent.stopPropagation();
}
// TODO: we are listening both on graph.diffBus and selection.selectionChangedBus,
// but the relation is actually:
//
// diffBus -> SelectedNodesBus -> us
// diffBus -> us
//
// we need to deduplicate this event
// but there is no coordination, resulting in double updates.
selection.selectionChangedBus.onValue(
function() {
main_graph.setRegularState();
update_view__graph(false);
}
);
var user_id = $('#user_id'),
user = user_id.text();
if (user_id.length > 0) {
console.log('found user ID: \'' + user + '\'');
main_graph.set_user(user);
edit_graph.set_user(user);
}
var el = document.body;
vis = d3.select(el).append("svg:svg")
.attr('id', 'canvas_d3')
.attr("width", '100%')
.attr("height", '100%')
.attr("pointer-events", "all")
.append("g")
.attr("class", "zoom");
d3.select(el).select("svg").append("svg:defs")
.data(["end"]) // Different link/path types can be defined here
.append("svg:marker") // This section adds in the arrows
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 22)
.attr("refY", -1.5)
.attr("markerWidth", 4)
.attr("markerHeight", 4)
.attr("orient", "auto")
.append("svg:path")
.attr("d", "M0,-5L10,0L0,5");
/*
* init zoom behavior
*/
var zoom_obj = d3.behavior.zoom().scaleExtent([0.1, 3]).on("zoom", zoom);
zoom_obj(d3.select('#canvas_d3'));
d3.select("svg").on("dblclick.zoom", null); // disable zoom on double click
$('svg').click(svg_click_handler);
main_graph_view = graph_view.GraphView({
parent_element: vis,
graph_name: "main",
graph: main_graph,
zoom_property: zoom_property,
temporary: false,
node_text_dx: node_text_dx,
node_text_dy: node_text_dy,
svgInput: svgInput,
// FIXME: good place to animate bubble radius
bubble_property: edit_graph.diffBus.map(function () {
if (edit_graph.nodes().length == 0) {
return 0;
} else {
return 180;
}
}),
});
edit_graph_view = graph_view.GraphView({
parent_element: vis,
graph_name: "edit",
graph: edit_graph,
zoom_property: zoom_property,
temporary: true,
node_text_dx: node_text_dx,
node_text_dy: node_text_dy,
});
// $('#canvas_d3').dblclick(canvas_handler_dblclick); - see #138
if (rz_config.backend_enabled) {
main_graph.load_from_backend();
}
}
/**
* Initialize backend websocket connection - this will have no effect if
* rz_config.backend__maintain_ws_connection is set to 'false'
*/
function init_ws_connection(){
if (true == rz_config.backend__maintain_ws_connection){
rz_mesh.init({graph: main_graph});
}
}
initDrawingArea();
init_ws_connection();
/**
* find the visual element counterpart of a given model object. This relies on
* the visual element having an id attribute equal to the object's id.
*
* @return null if visual element is not found
*/
function locate_visual_element(model_obj){
var id_sel = $('#' + model_obj.id);
if (0 == id_sel.length){
console.warn('unable to find visual element for model object: object id: ' + model_obj.id.toString())
return null;
}
return id_sel[0];
}
/**
* add node on canvas double click
*/
function canvas_handler_dblclick(){
var n = model_core.create_node__set_random_id();
n.name = ''; // will be set by user
main_graph.commit_and_tx_diff__topo(model_diff.new_topo({
node_set_add : [n].map(model_util.adapt_format_write_node),
}));
var n_ve = locate_visual_element(n); // locate visual element
var on_slowdown_cb = function(){
svgInput.enable($(n_ve).find('.nodetext'), n);
observer.disconnect();
}
var mutation_handler = rz_observer.new_Mutation_Handler__on_dxy_slowdown(on_slowdown_cb);
var observer = rz_observer.new_MutationObserver(mutation_handler);
mutation_handler.on_slowdown_threshold_reached;
observer.observe(n_ve, {
subtree: false,
childList : false,
attributes: true,
attributeOldValue : true,
});
}
function update_view__graph(relayout)
{
main_graph_view.update_view(relayout);
edit_graph_view.update_view(relayout);
}
return {
main_graph: main_graph,
edit_graph: edit_graph,
main_graph_view: main_graph_view,
edit_graph_view: edit_graph_view,
load_from_json: function(result) {
main_graph.load_from_json(result);
recenterZoom();
update_view__graph(true);
},
update_view__graph : update_view__graph,
}
}); /* close define call */
|