make relationship app use SVG IDs; make (unused) form for reading info
[scpubgit/stemmatology.git] / stemmaweb / root / js / relationship.js
CommitLineData
aff524fc 1var MARGIN=30;
2var svg_root = null;
3var svg_root_element = null;
4var start_element_height = 0;
5var reltypes = {};
6
7fd1d97a 7function getTextPath() {
aff524fc 8 var currpath = window.location.pathname;
93daee83 9 // Get rid of trailing slash
7fd1d97a 10 if( currpath.lastIndexOf('/') == currpath.length - 1 ) {
11 currpath = currpath.slice( 0, currpath.length - 1)
12 };
93daee83 13 // Get rid of query parameters
14 if( currpath.lastIndexOf('?') != -1 ) {
15 currpath = currpath.slice( 0, currpath.lastIndexOf('?') );
16 };
7fd1d97a 17 var path_elements = currpath.split('/');
18 var textid = path_elements.pop();
19 var basepath = path_elements.join( '/' );
20 var path_parts = [ basepath, textid ];
21 return path_parts;
581aee24 22}
23
72874569 24function getRelativePath() {
25 var path_parts = getTextPath();
26 return path_parts[0];
27}
28
29function getRelationshipURL() {
30 var path_parts = getTextPath();
31 return path_parts[0] + '/' + path_parts[1] + '/relationships';
581aee24 32}
33
28f2ef34 34// Make an XML ID into a valid selector
35function jq(myid) {
36 return '#' + myid.replace(/(:|\.)/g,'\\$1');
37}
38
581aee24 39function svgEnlargementLoaded() {
63213207 40 //Give some visual evidence that we are working
41 $('#loading_overlay').show();
42 lo_height = $("#enlargement_container").outerHeight();
43 lo_width = $("#enlargement_container").outerWidth();
44 $("#loading_overlay").height( lo_height );
45 $("#loading_overlay").width( lo_width );
46 $("#loading_overlay").offset( $("#enlargement_container").offset() );
47 $("#loading_message").offset(
48 { 'top': lo_height / 2 - $("#loading_message").height() / 2,
49 'left': lo_width / 2 - $("#loading_message").width() / 2 });
72874569 50 //Set viewbox widht and height to widht and height of $('#svgenlargement svg').
aff524fc 51 $('#update_workspace_button').data('locked', false);
52 $('#update_workspace_button').css('background-position', '0px 44px');
72874569 53 //This is essential to make sure zooming and panning works properly.
54 $('#svgenlargement ellipse').attr( {stroke:'green', fill:'#b3f36d'} );
55 var graph_svg = $('#svgenlargement svg');
56 var svg_g = $('#svgenlargement svg g')[0];
aff524fc 57 if (!svg_g) return;
72874569 58 svg_root = graph_svg.svg().svg('get').root();
aff524fc 59
60 // Find the real root and ignore any text nodes
61 for (i = 0; i < svg_root.childNodes.length; ++i) {
62 if (svg_root.childNodes[i].nodeName != '#text') {
63 svg_root_element = svg_root.childNodes[i];
64 break;
65 }
66 }
67
68 // This might be a little application specific and should be pushed to the backend
69 // but... This collapes corrector hands when they agree with the original
70 // e.g., P46-*,P46-C becomes simply P46
71 $(svg_root).find('text').each(function () {
72 var t = $(this).text();
73 var ws = t.split(',');
74 for (i = 0; i < ws.length; ++i) {
75 if (ws[i].indexOf('-*')> -1) {
76 var collapse = false;
77 var main = $.trim(ws[i].substring(0,ws[i].indexOf('-')));
78 for (j = 0; j < ws.length; ++j) {
79 if (i != j && ws[j].indexOf('-')> -1) {
80 var pair = $.trim(ws[j].substring(0,ws[j].indexOf('-')));
81 if (main == pair) {
82 collapse = true;
83 ws[j] = '';
84 }
85 }
86 }
87 if (collapse) ws[i] = main;
88 }
89 }
90 t = '';
91 for (i = 0; i < ws.length; ++i) {
92 if (ws[i].length) {
93 if (t.length>0) t+=', ';
94 t+=ws[i];
95 }
96 }
97 $(this).text(t);
98 });
99
72874569 100 svg_root.viewBox.baseVal.width = graph_svg.attr( 'width' );
101 svg_root.viewBox.baseVal.height = graph_svg.attr( 'height' );
102 //Now set scale and translate so svg height is about 150px and vertically centered in viewbox.
103 //This is just to create a nice starting enlargement.
104 var initial_svg_height = 250;
105 var scale = initial_svg_height/graph_svg.attr( 'height' );
106 var additional_translate = (graph_svg.attr( 'height' ) - initial_svg_height)/(2*scale);
107 var transform = svg_g.getAttribute('transform');
108 var translate = parseFloat( transform.match( /translate\([^\)]*\)/ )[0].split('(')[1].split(' ')[1].split(')')[0] );
109 translate += additional_translate;
110 var transform = 'rotate(0) scale(' + scale + ') translate(4 ' + translate + ')';
111 svg_g.setAttribute('transform', transform);
112 //used to calculate min and max zoom level:
28f2ef34 113 start_element_height = $('#__START__ ellipse')[0].getBBox().height;
63213207 114 add_relations( function() { $('#loading_overlay').hide(); });
7fd1d97a 115}
116
63213207 117function add_relations( callback_fn ) {
72874569 118 var basepath = getRelativePath();
119 var textrelpath = getRelationshipURL();
120 $.getJSON( basepath + '/definitions', function(data) {
7fd1d97a 121 var rel_types = data.types.sort();
72874569 122 $.getJSON( textrelpath,
7fd1d97a 123 function(data) {
124 $.each(data, function( index, rel_info ) {
125 var type_index = $.inArray(rel_info.type, rel_types);
93daee83 126 var source_found = get_ellipse( rel_info.source );
127 var target_found = get_ellipse( rel_info.target );
128 if( type_index != -1 && source_found.size() && target_found.size() ) {
72874569 129 var relation = relation_manager.create( rel_info.source, rel_info.target, type_index );
130 relation.data( 'type', rel_info.type );
131 relation.data( 'scope', rel_info.scope );
31aaf446 132 relation.data( 'note', rel_info.note );
72874569 133 var node_obj = get_node_obj(rel_info.source);
134 node_obj.set_draggable( false );
135 node_obj.ellipse.data( 'node_obj', null );
136 node_obj = get_node_obj(rel_info.target);
137 node_obj.set_draggable( false );
138 node_obj.ellipse.data( 'node_obj', null );
7fd1d97a 139 }
63213207 140 });
141 callback_fn.call();
72874569 142 });
7fd1d97a 143 });
581aee24 144}
145
146function get_ellipse( node_id ) {
28f2ef34 147 return $( jq( node_id ) + ' ellipse');
581aee24 148}
149
150function get_node_obj( node_id ) {
72874569 151 var node_ellipse = get_ellipse( node_id );
152 if( node_ellipse.data( 'node_obj' ) == null ) {
153 node_ellipse.data( 'node_obj', new node_obj(node_ellipse) );
154 };
155 return node_ellipse.data( 'node_obj' );
581aee24 156}
157
581aee24 158function node_obj(ellipse) {
159 this.ellipse = ellipse;
160 var self = this;
161
162 this.x = 0;
163 this.y = 0;
164 this.dx = 0;
165 this.dy = 0;
166 this.node_elements = node_elements_for(self.ellipse);
167
168 this.get_id = function() {
28f2ef34 169 return $(self.ellipse).parent().attr('id')
581aee24 170 }
171
172 this.set_draggable = function( draggable ) {
173 if( draggable ) {
0fa07e25 174 $(self.ellipse).attr( {stroke:'black', fill:'#fff'} );
175 $(self.ellipse).parent().mousedown( this.mousedown_listener );
176 $(self.ellipse).parent().hover( this.enter_node, this.leave_node );
177 self.ellipse.siblings('text').attr('class', 'noselect draggable');
581aee24 178 } else {
0fa07e25 179 self.ellipse.siblings('text').attr('class', '');
180 $(self.ellipse).parent().unbind('mouseenter').unbind('mouseleave').unbind('mousedown');
181 $(self.ellipse).attr( {stroke:'green', fill:'#b3f36d'} );
581aee24 182 }
183 }
184
185 this.mousedown_listener = function(evt) {
186 evt.stopPropagation();
187 self.x = evt.clientX;
188 self.y = evt.clientY;
189 $('body').mousemove( self.mousemove_listener );
190 $('body').mouseup( self.mouseup_listener );
0fa07e25 191 $(self.ellipse).parent().unbind('mouseenter').unbind('mouseleave')
581aee24 192 self.ellipse.attr( 'fill', '#ff66ff' );
193 first_node_g_element = $("#svgenlargement g .node" ).filter( ":first" );
194 if( first_node_g_element.attr('id') !== self.get_g().attr('id') ) { self.get_g().insertBefore( first_node_g_element ) };
195 }
196
197 this.mousemove_listener = function(evt) {
72874569 198 self.dx = (evt.clientX - self.x) / mouse_scale;
199 self.dy = (evt.clientY - self.y) / mouse_scale;
581aee24 200 self.move_elements();
0fa07e25 201 evt.returnValue = false;
202 evt.preventDefault();
203 return false;
581aee24 204 }
205
206 this.mouseup_listener = function(evt) {
207 if( $('ellipse[fill="#ffccff"]').size() > 0 ) {
28f2ef34 208 var source_node_id = $(self.ellipse).parent().attr('id');
0fa07e25 209 var source_node_text = self.ellipse.siblings('text').text();
28f2ef34 210 var target_node_id = $('ellipse[fill="#ffccff"]').parent().attr('id');
0fa07e25 211 var target_node_text = $('ellipse[fill="#ffccff"]').siblings("text").text();
581aee24 212 $('#source_node_id').val( source_node_id );
0fa07e25 213 $('#source_node_text').val( source_node_text );
581aee24 214 $('#target_node_id').val( target_node_id );
0fa07e25 215 $('#target_node_text').val( target_node_text );
581aee24 216 $('#dialog-form').dialog( 'open' );
217 };
218 $('body').unbind('mousemove');
219 $('body').unbind('mouseup');
220 self.ellipse.attr( 'fill', '#fff' );
0fa07e25 221 $(self.ellipse).parent().hover( self.enter_node, self.leave_node );
72874569 222 self.reset_elements();
581aee24 223 }
224
225 this.cpos = function() {
226 return { x: self.ellipse.attr('cx'), y: self.ellipse.attr('cy') };
227 }
228
229 this.get_g = function() {
230 return self.ellipse.parent('g');
231 }
232
233 this.enter_node = function(evt) {
234 self.ellipse.attr( 'fill', '#ffccff' );
235 }
236
237 this.leave_node = function(evt) {
238 self.ellipse.attr( 'fill', '#fff' );
239 }
240
241 this.greyout_edges = function() {
242 $.each( self.node_elements, function(index, value) {
243 value.grey_out('.edge');
244 });
245 }
246
247 this.ungreyout_edges = function() {
248 $.each( self.node_elements, function(index, value) {
249 value.un_grey_out('.edge');
250 });
251 }
252
253 this.move_elements = function() {
254 $.each( self.node_elements, function(index, value) {
255 value.move(self.dx,self.dy);
256 });
257 }
258
259 this.reset_elements = function() {
260 $.each( self.node_elements, function(index, value) {
261 value.reset();
262 });
263 }
264
265 this.update_elements = function() {
266 self.node_elements = node_elements_for(self.ellipse);
267 }
268
269 self.set_draggable( true );
270}
271
272function svgshape( shape_element ) {
273 this.shape = shape_element;
274 this.move = function(dx,dy) {
275 this.shape.attr( "transform", "translate(" + dx + " " + dy + ")" );
276 }
277 this.reset = function() {
278 this.shape.attr( "transform", "translate( 0, 0 )" );
279 }
280 this.grey_out = function(filter) {
281 if( this.shape.parent(filter).size() != 0 ) {
282 this.shape.attr({'stroke':'#e5e5e5', 'fill':'#e5e5e5'});
283 }
284 }
285 this.un_grey_out = function(filter) {
286 if( this.shape.parent(filter).size() != 0 ) {
287 this.shape.attr({'stroke':'#000000', 'fill':'#000000'});
288 }
289 }
290}
291
292function svgpath( path_element, svg_element ) {
293 this.svg_element = svg_element;
294 this.path = path_element;
295 this.x = this.path.x;
296 this.y = this.path.y;
297 this.move = function(dx,dy) {
298 this.path.x = this.x + dx;
299 this.path.y = this.y + dy;
300 }
301 this.reset = function() {
302 this.path.x = this.x;
303 this.path.y = this.y;
304 }
305 this.grey_out = function(filter) {
306 if( this.svg_element.parent(filter).size() != 0 ) {
307 this.svg_element.attr('stroke', '#e5e5e5');
308 this.svg_element.siblings('text').attr('fill', '#e5e5e5');
0fa07e25 309 this.svg_element.siblings('text').attr('class', 'noselect');
581aee24 310 }
311 }
312 this.un_grey_out = function(filter) {
313 if( this.svg_element.parent(filter).size() != 0 ) {
314 this.svg_element.attr('stroke', '#000000');
315 this.svg_element.siblings('text').attr('fill', '#000000');
0fa07e25 316 this.svg_element.siblings('text').attr('class', '');
581aee24 317 }
318 }
319}
320
321function node_elements_for( ellipse ) {
322 node_elements = get_edge_elements_for( ellipse );
323 node_elements.push( new svgshape( ellipse.siblings('text') ) );
324 node_elements.push( new svgshape( ellipse ) );
325 return node_elements;
326}
327
328function get_edge_elements_for( ellipse ) {
329 edge_elements = new Array();
28f2ef34 330 node_id = ellipse.parent().attr('id');
581aee24 331 edge_in_pattern = new RegExp( node_id + '$' );
332 edge_out_pattern = new RegExp( '^' + node_id );
333 $.each( $('#svgenlargement .edge,#svgenlargement .relation').children('title'), function(index) {
334 title = $(this).text();
335 if( edge_in_pattern.test(title) ) {
336 polygon = $(this).siblings('polygon');
337 if( polygon.size() > 0 ) {
338 edge_elements.push( new svgshape( polygon ) );
339 }
340 path_segments = $(this).siblings('path')[0].pathSegList;
341 edge_elements.push( new svgpath( path_segments.getItem(path_segments.numberOfItems - 1), $(this).siblings('path') ) );
342 }
343 if( edge_out_pattern.test(title) ) {
344 path_segments = $(this).siblings('path')[0].pathSegList;
345 edge_elements.push( new svgpath( path_segments.getItem(0), $(this).siblings('path') ) );
346 }
347 });
348 return edge_elements;
349}
350
351function relation_factory() {
352 var self = this;
353 this.color_memo = null;
354 //TODO: colors hard coded for now
355 this.temp_color = '#FFA14F';
356 this.relation_colors = [ "#5CCCCC", "#67E667", "#F9FE72", "#6B90D4", "#FF7673", "#E467B3", "#AA67D5", "#8370D8", "#FFC173" ];
357
358 this.create_temporary = function( source_node_id, target_node_id ) {
28f2ef34 359 var relation_id = get_relation_id( source_node_id, target_node_id );
360 var relation = $( jq( relation_id ) );
72874569 361 if( relation.size() == 0 ) {
581aee24 362 draw_relation( source_node_id, target_node_id, self.temp_color );
363 } else {
364 self.color_memo = relation.children('path').attr( 'stroke' );
365 relation.children('path').attr( 'stroke', self.temp_color );
366 }
367 }
368 this.remove_temporary = function() {
369 var path_element = $('#svgenlargement .relation').children('path[stroke="' + self.temp_color + '"]');
370 if( self.color_memo != null ) {
371 path_element.attr( 'stroke', self.color_memo );
372 self.color_memo = null;
373 } else {
72874569 374 var temporary = path_element.parent('g').remove();
375 temporary.empty();
376 temporary = null;
581aee24 377 }
378 }
379 this.create = function( source_node_id, target_node_id, color_index ) {
380 //TODO: Protect from (color_)index out of bound..
381 var relation_color = self.relation_colors[ color_index ];
72874569 382 var relation = draw_relation( source_node_id, target_node_id, relation_color );
383 get_node_obj( source_node_id ).update_elements();
384 get_node_obj( target_node_id ).update_elements();
385 return relation;
581aee24 386 }
72874569 387 this.toggle_active = function( relation_id ) {
28f2ef34 388 var relation = $( jq( relation_id ) );
72874569 389 var relation_path = relation.children('path');
390 if( !relation.data( 'active' ) ) {
391 relation_path.css( {'cursor':'pointer'} );
392 relation_path.mouseenter( function(event) {
393 outerTimer = setTimeout( function() {
394 timer = setTimeout( function() {
28f2ef34 395 var related_nodes = get_related_nodes( relation_id );
396 var source_node_id = related_nodes[0];
397 var target_node_id = related_nodes[1];
72874569 398 $('#delete_source_node_id').val( source_node_id );
399 $('#delete_target_node_id').val( target_node_id );
400 self.showinfo(relation);
401 }, 500 )
402 }, 1000 );
403 });
404 relation_path.mouseleave( function(event) {
405 clearTimeout(outerTimer);
406 if( timer != null ) { clearTimeout(timer); }
407 });
408 relation.data( 'active', true );
409 } else {
410 relation_path.unbind( 'mouseenter' );
411 relation_path.unbind( 'mouseleave' );
412 relation_path.css( {'cursor':'inherit'} );
413 relation.data( 'active', false );
414 }
415 }
416 this.showinfo = function(relation) {
31aaf446 417 var htmlstr = 'type: ' + relation.data( 'type' ) + '<br/>scope: ' + relation.data( 'scope' );
418 if( relation.data( 'note' ) ) {
419 htmlstr = htmlstr + '<br/>note: ' + relation.data( 'note' );
420 }
421 $('#delete-form-text').html( htmlstr );
72874569 422 var points = relation.children('path').attr('d').slice(1).replace('C',' ').split(' ');
423 var xs = parseFloat( points[0].split(',')[0] );
424 var xe = parseFloat( points[1].split(',')[0] );
425 var ys = parseFloat( points[0].split(',')[1] );
426 var ye = parseFloat( points[3].split(',')[1] );
427 var p = svg_root.createSVGPoint();
428 p.x = xs + ((xe-xs)*1.1);
429 p.y = ye - ((ye-ys)/2);
aff524fc 430 var ctm = svg_root_element.getScreenCTM();
72874569 431 var nx = p.matrixTransform(ctm).x;
432 var ny = p.matrixTransform(ctm).y;
433 var dialog_aria = $ ("div[aria-labelledby='ui-dialog-title-delete-form']");
434 $('#delete-form').dialog( 'open' );
435 dialog_aria.offset({ left: nx, top: ny });
436 }
437 this.remove = function( relation_id ) {
28f2ef34 438 var relation = $( jq( relation_id ) );
72874569 439 relation.remove();
581aee24 440 }
441}
442
28f2ef34 443// Utility function to create/return the ID of a relation link between
444// a source and target.
445function get_relation_id( source_id, target_id ) {
446 var idlist = [ source_id, target_id ];
447 idlist.sort();
448 return 'relation-' + idlist[0] + '-...-' + idlist[1];
449}
450
451function get_related_nodes( relation_id ) {
452 var srctotarg = relation_id.substr( 9 );
453 return srctotarg.split('-...-');
454}
455
581aee24 456function draw_relation( source_id, target_id, relation_color ) {
72874569 457 var source_ellipse = get_ellipse( source_id );
458 var target_ellipse = get_ellipse( target_id );
28f2ef34 459 var relation_id = get_relation_id( source_id, target_id );
72874569 460 var svg = $('#svgenlargement').children('svg').svg().svg('get');
461 var path = svg.createPath();
462 var sx = parseInt( source_ellipse.attr('cx') );
463 var rx = parseInt( source_ellipse.attr('rx') );
464 var sy = parseInt( source_ellipse.attr('cy') );
465 var ex = parseInt( target_ellipse.attr('cx') );
466 var ey = parseInt( target_ellipse.attr('cy') );
28f2ef34 467 var relation = svg.group( $("#svgenlargement svg g"),
468 { 'class':'relation', 'id':relation_id } );
72874569 469 svg.title( relation, source_id + '->' + target_id );
470 svg.path( relation, path.move( sx, sy ).curveC( sx + (2*rx), sy, ex + (2*rx), ey, ex, ey ), {fill: 'none', stroke: relation_color, strokeWidth: 4});
581aee24 471 var relation_element = $('#svgenlargement .relation').filter( ':last' );
472 relation_element.insertBefore( $('#svgenlargement g g').filter(':first') );
72874569 473 return relation_element;
581aee24 474}
475
72874569 476
581aee24 477$(document).ready(function () {
72874569 478
479 timer = null;
581aee24 480 relation_manager = new relation_factory();
72874569 481 $('#update_workspace_button').data('locked', false);
581aee24 482
72874569 483 $('#enlargement').mousedown(function (event) {
581aee24 484 $(this)
72874569 485 .data('down', true)
486 .data('x', event.clientX)
487 .data('y', event.clientY)
488 .data('scrollLeft', this.scrollLeft)
aff524fc 489 stateTf = svg_root_element.getCTM().inverse();
72874569 490 var p = svg_root.createSVGPoint();
491 p.x = event.clientX;
492 p.y = event.clientY;
493 stateOrigin = p.matrixTransform(stateTf);
aff524fc 494 event.returnValue = false;
495 event.preventDefault();
72874569 496 return false;
581aee24 497 }).mouseup(function (event) {
72874569 498 $(this).data('down', false);
581aee24 499 }).mousemove(function (event) {
72874569 500 if( timer != null ) { clearTimeout(timer); }
501 if ( ($(this).data('down') == true) && ($('#update_workspace_button').data('locked') == false) ) {
502 var p = svg_root.createSVGPoint();
503 p.x = event.clientX;
504 p.y = event.clientY;
505 p = p.matrixTransform(stateTf);
506 var matrix = stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y);
507 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
aff524fc 508 svg_root_element.setAttribute("transform", s);
581aee24 509 }
aff524fc 510 event.returnValue = false;
511 event.preventDefault();
581aee24 512 }).mousewheel(function (event, delta) {
72874569 513 event.returnValue = false;
514 event.preventDefault();
515 if ( $('#update_workspace_button').data('locked') == false ) {
aff524fc 516 if (!delta || delta == null || delta == 0) delta = event.originalEvent.wheelDelta;
517 if (!delta || delta == null || delta == 0) delta = -1 * event.originalEvent.detail;
72874569 518 if( delta < -9 ) { delta = -9 };
519 var z = 1 + delta/10;
aff524fc 520 z = delta > 0 ? 1 : -1;
521 var g = svg_root_element;
522 if (g && ((z<1 && (g.getScreenCTM().a * start_element_height) > 4.0) || (z>=1 && (g.getScreenCTM().a * start_element_height) < 100))) {
72874569 523 var root = svg_root;
524 var p = root.createSVGPoint();
aff524fc 525 p.x = event.originalEvent.clientX;
526 p.y = event.originalEvent.clientY;
72874569 527 p = p.matrixTransform(g.getCTM().inverse());
aff524fc 528 var scaleLevel = 1+(z/20);
529 var k = root.createSVGMatrix().translate(p.x, p.y).scale(scaleLevel).translate(-p.x, -p.y);
72874569 530 var matrix = g.getCTM().multiply(k);
531 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
532 g.setAttribute("transform", s);
533 }
534 }
581aee24 535 }).css({
536 'overflow' : 'hidden',
537 'cursor' : '-moz-grab'
538 });
539
540
541 $( "#dialog-form" ).dialog({
542 autoOpen: false,
543 height: 270,
544 width: 290,
545 modal: true,
546 buttons: {
547 "Ok": function() {
548 $('#status').empty();
aff524fc 549 form_values = $('#collapse_node_form').serialize();
72874569 550 ncpath = getRelationshipURL();
551 $(':button :contains("Ok")').attr("disabled", true);
581aee24 552 var jqjson = $.post( ncpath, form_values, function(data) {
553 $.each( data, function(item, source_target) {
988c7619 554 var source_found = get_ellipse( source_target[0] );
555 var target_found = get_ellipse( source_target[1] );
556 if( source_found.size() && target_found.size() ) {
28f2ef34 557 var relation = relation_manager.create( source_target[0], source_target[1], $('#rel_type')[0].selectedIndex-1 );
988c7619 558 relation.data( 'type', $('#rel_type :selected').text() );
559 relation.data( 'scope', $('#scope :selected').text() );
560 relation.data( 'note', $('#note').val() );
28f2ef34 561 relation_manager.toggle_active( relation.attr('id') );
988c7619 562 }
581aee24 563 });
581aee24 564 $( "#dialog-form" ).dialog( "close" );
72874569 565 }, 'json' );
581aee24 566 },
567 Cancel: function() {
581aee24 568 $( this ).dialog( "close" );
569 }
570 },
571 create: function(event, ui) {
572 $(this).data( 'relation_drawn', false );
573 //TODO? Err handling?
72874569 574 var basepath = getRelativePath();
575 var jqjson = $.getJSON( basepath + '/definitions', function(data) {
581aee24 576 var types = data.types.sort();
577 $.each( types, function(index, value) {
aff524fc 578 $('#rel_type').append( $('<option />').attr( "value", value ).text(value) );
581aee24 579 $('#keymaplist').append( $('<li>').css( "border-color", relation_manager.relation_colors[index] ).text(value) );
580 });
581 var scopes = data.scopes;
582 $.each( scopes, function(index, value) {
aff524fc 583 $('#scope').append( $('<option />').attr( "value", value ).text(value) );
581aee24 584 });
585 });
586 },
587 open: function() {
588 relation_manager.create_temporary( $('#source_node_id').val(), $('#target_node_id').val() );
72874569 589 $(".ui-widget-overlay").css("background", "none");
590 $("#dialog_overlay").show();
591 $("#dialog_overlay").height( $("#enlargement_container").height() );
63213207 592 $("#dialog_overlay").width( $("#enlargement_container").innerWidth() );
72874569 593 $("#dialog_overlay").offset( $("#enlargement_container").offset() );
581aee24 594 },
595 close: function() {
72874569 596 relation_manager.remove_temporary();
581aee24 597 $( '#status' ).empty();
598 $("#dialog_overlay").hide();
599 }
600 }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
72874569 601 if( ( ajaxSettings.type == 'POST' ) && jqXHR.status == 403 ) {
602 var errobj = jQuery.parseJSON( jqXHR.responseText );
603 $('#status').append( '<p class="error">Error: ' + errobj.error + '</br>The relationship cannot be made.</p>' );
581aee24 604 }
605 } );
606
72874569 607 $( "#delete-form" ).dialog({
608 autoOpen: false,
31aaf446 609 height: 135,
72874569 610 width: 160,
611 modal: false,
612 buttons: {
613 Cancel: function() {
614 $( this ).dialog( "close" );
615 },
616 Delete: function() {
617 form_values = $('#delete_relation_form').serialize()
618 ncpath = getRelationshipURL()
619 var jqjson = $.ajax({ url: ncpath, data: form_values, success: function(data) {
620 $.each( data, function(item, source_target) {
28f2ef34 621 relation_manager.remove( get_relation_id( source_target[0], source_target[1] ) );
72874569 622 });
623 $( "#delete-form" ).dialog( "close" );
624 }, dataType: 'json', type: 'DELETE' });
625 }
626 },
627 create: function(event, ui) {
628 var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ).css( 'width', '100%' );
629 buttonset.find( "button:contains('Cancel')" ).css( 'float', 'right' );
630 var dialog_aria = $("div[aria-labelledby='ui-dialog-title-delete-form']");
631 dialog_aria.mouseenter( function() {
632 if( mouseWait != null ) { clearTimeout(mouseWait) };
633 })
634 dialog_aria.mouseleave( function() {
635 mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
636 })
637 },
638 open: function() {
639 mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
640 },
641 close: function() {
642 }
643 });
644
aff524fc 645 // function for reading form dialog should go here; for now hide the element
28f2ef34 646 $('#reading-form').dialog({
647 autoOpen: false,
648 height: 400,
649 width: 200,
650 modal: true,
651 buttons: {
652 Cancel: function() {
653 $( this ).dialog( "close" );
654 },
655 Update: function() {
656 form_values = $('#reading_data_form').serialize();
657 ncpath = getReadingURL();
658 var reading_element = get_node_obj( $('#reading_data_id').val() );
659 $(':button :contains("Update")').attr("disabled", true);
660 var jqjson = $.post( ncpath, form_values, function(data) {
661 $.each( data, function(key, value) {
662 reading_element.data( key, value );
663 });
664 $( "#reading-form" ).dialog( "close" );
665 });
666 }
667 },
668 create: function() {},
669 open: function() {},
670 close: function() {
671 $("#dialog_overlay").hide();
672 }
673 });
aff524fc 674
581aee24 675 $('#update_workspace_button').click( function() {
676 var svg_enlargement = $('#svgenlargement').svg().svg('get').root();
aff524fc 677 mouse_scale = svg_root_element.getScreenCTM().a;
72874569 678 if( $(this).data('locked') == true ) {
679 $('#svgenlargement ellipse' ).each( function( index ) {
680 if( $(this).data( 'node_obj' ) != null ) {
681 $(this).data( 'node_obj' ).ungreyout_edges();
682 $(this).data( 'node_obj' ).set_draggable( false );
683 var node_id = $(this).data( 'node_obj' ).get_id();
684 toggle_relation_active( node_id );
685 $(this).data( 'node_obj', null );
686 }
581aee24 687 })
581aee24 688 $(this).data('locked', false);
72874569 689 $(this).css('background-position', '0px 44px');
581aee24 690 } else {
72874569 691 var left = $('#enlargement').offset().left;
692 var right = left + $('#enlargement').width();
aff524fc 693 var tf = svg_root_element.getScreenCTM().inverse();
72874569 694 var p = svg_root.createSVGPoint();
695 p.x=left;
696 p.y=100;
697 var cx_min = p.matrixTransform(tf).x;
698 p.x=right;
699 var cx_max = p.matrixTransform(tf).x;
700 $('#svgenlargement ellipse').each( function( index ) {
701 var cx = parseInt( $(this).attr('cx') );
702 if( cx > cx_min && cx < cx_max) {
703 if( $(this).data( 'node_obj' ) == null ) {
704 $(this).data( 'node_obj', new node_obj( $(this) ) );
705 } else {
706 $(this).data( 'node_obj' ).set_draggable( true );
707 }
708 $(this).data( 'node_obj' ).greyout_edges();
709 var node_id = $(this).data( 'node_obj' ).get_id();
710 toggle_relation_active( node_id );
581aee24 711 }
72874569 712 });
713 $(this).css('background-position', '0px 0px');
581aee24 714 $(this).data('locked', true );
581aee24 715 }
716 });
717
9ca77245 718 $('.helptag').popupWindow({
719 height:500,
720 width:800,
721 top:50,
722 left:50,
723 scrollbars:1
724 });
725
726
72874569 727 function toggle_relation_active( node_id ) {
728 $('#svgenlargement .relation').find( "title:contains('" + node_id + "')" ).each( function(index) {
729 matchid = new RegExp( "^" + node_id );
730 if( $(this).text().match( matchid ) != null ) {
28f2ef34 731 var relation_id = $(this).parent().attr('id');
732 relation_manager.toggle_active( relation_id );
72874569 733 };
734 });
581aee24 735 }
581aee24 736
aff524fc 737 expandFillPageClients();
738 $(window).resize(function() {
739 expandFillPageClients();
740 });
741
72874569 742});
581aee24 743
744
aff524fc 745function expandFillPageClients() {
746 $('.fillPage').each(function () {
747 $(this).height($(window).height() - $(this).offset().top - MARGIN);
748 });
749}
750
751function loadSVG(svgData) {
752 var svgElement = $('#svgenlargement');
753
754 $(svgElement).svg('destroy');
755
756 $(svgElement).svg({
757 loadURL: svgData,
758 onLoad : svgEnlargementLoaded
759 });
760}
761
762
763/* OS Gadget stuff
764
765function svg_select_callback(topic, data, subscriberData) {
766 svgData = data;
767 loadSVG(svgData);
768}
769
770function loaded() {
771 var prefs = new gadgets.Prefs();
772 var preferredHeight = parseInt(prefs.getString('height'));
773 if (gadgets.util.hasFeature('dynamic-height')) gadgets.window.adjustHeight(preferredHeight);
774 expandFillPageClients();
775}
776
777if (gadgets.util.hasFeature('pubsub-2')) {
778 gadgets.HubSettings.onConnect = function(hum, suc, err) {
779 subId = gadgets.Hub.subscribe("interedition.svg.selected", svg_select_callback);
780 loaded();
781 };
782}
783else gadgets.util.registerOnLoadHandler(loaded);
784*/