1 function getTextPath() {
2 var currpath = window.location.pathname
3 if( currpath.lastIndexOf('/') == currpath.length - 1 ) {
4 currpath = currpath.slice( 0, currpath.length - 1)
6 var path_elements = currpath.split('/');
7 var textid = path_elements.pop();
8 var basepath = path_elements.join( '/' );
9 var path_parts = [ basepath, textid ];
13 function getRelativePath() {
14 var path_parts = getTextPath();
18 function getRelationshipURL() {
19 var path_parts = getTextPath();
20 return path_parts[0] + '/' + path_parts[1] + '/relationships';
23 function svgEnlargementLoaded() {
24 //Give some visual evidence that we are working
25 $('#loading_overlay').show();
26 lo_height = $("#enlargement_container").outerHeight();
27 lo_width = $("#enlargement_container").outerWidth();
28 $("#loading_overlay").height( lo_height );
29 $("#loading_overlay").width( lo_width );
30 $("#loading_overlay").offset( $("#enlargement_container").offset() );
31 $("#loading_message").offset(
32 { 'top': lo_height / 2 - $("#loading_message").height() / 2,
33 'left': lo_width / 2 - $("#loading_message").width() / 2 });
34 //Set viewbox widht and height to widht and height of $('#svgenlargement svg').
35 //This is essential to make sure zooming and panning works properly.
36 $('#svgenlargement ellipse').attr( {stroke:'green', fill:'#b3f36d'} );
37 var graph_svg = $('#svgenlargement svg');
38 var svg_g = $('#svgenlargement svg g')[0];
39 svg_root = graph_svg.svg().svg('get').root();
40 svg_root.viewBox.baseVal.width = graph_svg.attr( 'width' );
41 svg_root.viewBox.baseVal.height = graph_svg.attr( 'height' );
42 //Now set scale and translate so svg height is about 150px and vertically centered in viewbox.
43 //This is just to create a nice starting enlargement.
44 var initial_svg_height = 250;
45 var scale = initial_svg_height/graph_svg.attr( 'height' );
46 var additional_translate = (graph_svg.attr( 'height' ) - initial_svg_height)/(2*scale);
47 var transform = svg_g.getAttribute('transform');
48 var translate = parseFloat( transform.match( /translate\([^\)]*\)/ )[0].split('(')[1].split(' ')[1].split(')')[0] );
49 translate += additional_translate;
50 var transform = 'rotate(0) scale(' + scale + ') translate(4 ' + translate + ')';
51 svg_g.setAttribute('transform', transform);
52 //used to calculate min and max zoom level:
53 start_element_height = $("#svgenlargement .node title:contains('#START#')").siblings('ellipse')[0].getBBox().height;
54 add_relations( function() { $('#loading_overlay').hide(); });
57 function add_relations( callback_fn ) {
58 var basepath = getRelativePath();
59 var textrelpath = getRelationshipURL();
60 $.getJSON( basepath + '/definitions', function(data) {
61 var rel_types = data.types.sort();
62 $.getJSON( textrelpath,
64 $.each(data, function( index, rel_info ) {
65 var type_index = $.inArray(rel_info.type, rel_types);
66 if( type_index != -1 ) {
67 var relation = relation_manager.create( rel_info.source, rel_info.target, type_index );
68 relation.data( 'type', rel_info.type );
69 relation.data( 'scope', rel_info.scope );
70 relation.data( 'note', rel_info.note );
71 var node_obj = get_node_obj(rel_info.source);
72 node_obj.set_draggable( false );
73 node_obj.ellipse.data( 'node_obj', null );
74 node_obj = get_node_obj(rel_info.target);
75 node_obj.set_draggable( false );
76 node_obj.ellipse.data( 'node_obj', null );
84 function get_ellipse( node_id ) {
85 return $('#svgenlargement .node').children('title').filter( function(index) {
86 return $(this).text() == node_id;
87 }).siblings('ellipse');
90 function get_node_obj( node_id ) {
91 var node_ellipse = get_ellipse( node_id );
92 if( node_ellipse.data( 'node_obj' ) == null ) {
93 node_ellipse.data( 'node_obj', new node_obj(node_ellipse) );
95 return node_ellipse.data( 'node_obj' );
98 function get_edge( edge_id ) {
99 return $('#svgenlargement .edge').filter( function(index) {
100 return $(this).children( 'title' ).text() == $('<div/>').html(edge_id).text() ;
104 function node_obj(ellipse) {
105 this.ellipse = ellipse;
112 this.node_elements = node_elements_for(self.ellipse);
114 this.get_id = function() {
115 return self.ellipse.siblings('title').text()
118 this.set_draggable = function( draggable ) {
120 self.ellipse.attr( {stroke:'black', fill:'#fff'} );
121 self.ellipse.mousedown( this.mousedown_listener );
122 self.ellipse.hover( this.enter_node, this.leave_node );
124 self.ellipse.unbind('mouseenter').unbind('mouseleave').unbind('mousedown');
125 self.ellipse.attr( {stroke:'green', fill:'#b3f36d'} );
129 this.mousedown_listener = function(evt) {
130 evt.stopPropagation();
131 self.x = evt.clientX;
132 self.y = evt.clientY;
133 $('body').mousemove( self.mousemove_listener );
134 $('body').mouseup( self.mouseup_listener );
135 self.ellipse.unbind('mouseenter').unbind('mouseleave')
136 self.ellipse.attr( 'fill', '#ff66ff' );
137 first_node_g_element = $("#svgenlargement g .node" ).filter( ":first" );
138 if( first_node_g_element.attr('id') !== self.get_g().attr('id') ) { self.get_g().insertBefore( first_node_g_element ) };
141 this.mousemove_listener = function(evt) {
142 self.dx = (evt.clientX - self.x) / mouse_scale;
143 self.dy = (evt.clientY - self.y) / mouse_scale;
144 self.move_elements();
147 this.mouseup_listener = function(evt) {
148 if( $('ellipse[fill="#ffccff"]').size() > 0 ) {
149 var source_node_id = self.ellipse.siblings('title').text();
150 var target_node_id = $('ellipse[fill="#ffccff"]').siblings("title").text();
151 $('#source_node_id').val( source_node_id );
152 $('#target_node_id').val( target_node_id );
153 $('#dialog-form').dialog( 'open' );
155 $('body').unbind('mousemove');
156 $('body').unbind('mouseup');
157 self.ellipse.attr( 'fill', '#fff' );
158 self.ellipse.hover( self.enter_node, self.leave_node );
159 self.reset_elements();
162 this.cpos = function() {
163 return { x: self.ellipse.attr('cx'), y: self.ellipse.attr('cy') };
166 this.get_g = function() {
167 return self.ellipse.parent('g');
170 this.enter_node = function(evt) {
171 self.ellipse.attr( 'fill', '#ffccff' );
174 this.leave_node = function(evt) {
175 self.ellipse.attr( 'fill', '#fff' );
178 this.greyout_edges = function() {
179 $.each( self.node_elements, function(index, value) {
180 value.grey_out('.edge');
184 this.ungreyout_edges = function() {
185 $.each( self.node_elements, function(index, value) {
186 value.un_grey_out('.edge');
190 this.move_elements = function() {
191 $.each( self.node_elements, function(index, value) {
192 value.move(self.dx,self.dy);
196 this.reset_elements = function() {
197 $.each( self.node_elements, function(index, value) {
202 this.update_elements = function() {
203 self.node_elements = node_elements_for(self.ellipse);
206 self.set_draggable( true );
209 function svgshape( shape_element ) {
210 this.shape = shape_element;
211 this.move = function(dx,dy) {
212 this.shape.attr( "transform", "translate(" + dx + " " + dy + ")" );
214 this.reset = function() {
215 this.shape.attr( "transform", "translate( 0, 0 )" );
217 this.grey_out = function(filter) {
218 if( this.shape.parent(filter).size() != 0 ) {
219 this.shape.attr({'stroke':'#e5e5e5', 'fill':'#e5e5e5'});
222 this.un_grey_out = function(filter) {
223 if( this.shape.parent(filter).size() != 0 ) {
224 this.shape.attr({'stroke':'#000000', 'fill':'#000000'});
229 function svgpath( path_element, svg_element ) {
230 this.svg_element = svg_element;
231 this.path = path_element;
232 this.x = this.path.x;
233 this.y = this.path.y;
234 this.move = function(dx,dy) {
235 this.path.x = this.x + dx;
236 this.path.y = this.y + dy;
238 this.reset = function() {
239 this.path.x = this.x;
240 this.path.y = this.y;
242 this.grey_out = function(filter) {
243 if( this.svg_element.parent(filter).size() != 0 ) {
244 this.svg_element.attr('stroke', '#e5e5e5');
245 this.svg_element.siblings('text').attr('fill', '#e5e5e5');
248 this.un_grey_out = function(filter) {
249 if( this.svg_element.parent(filter).size() != 0 ) {
250 this.svg_element.attr('stroke', '#000000');
251 this.svg_element.siblings('text').attr('fill', '#000000');
256 function node_elements_for( ellipse ) {
257 node_elements = get_edge_elements_for( ellipse );
258 node_elements.push( new svgshape( ellipse.siblings('text') ) );
259 node_elements.push( new svgshape( ellipse ) );
260 return node_elements;
263 function get_edge_elements_for( ellipse ) {
264 edge_elements = new Array();
265 node_id = ellipse.siblings('title').text();
266 edge_in_pattern = new RegExp( node_id + '$' );
267 edge_out_pattern = new RegExp( '^' + node_id );
268 $.each( $('#svgenlargement .edge,#svgenlargement .relation').children('title'), function(index) {
269 title = $(this).text();
270 if( edge_in_pattern.test(title) ) {
271 polygon = $(this).siblings('polygon');
272 if( polygon.size() > 0 ) {
273 edge_elements.push( new svgshape( polygon ) );
275 path_segments = $(this).siblings('path')[0].pathSegList;
276 edge_elements.push( new svgpath( path_segments.getItem(path_segments.numberOfItems - 1), $(this).siblings('path') ) );
278 if( edge_out_pattern.test(title) ) {
279 path_segments = $(this).siblings('path')[0].pathSegList;
280 edge_elements.push( new svgpath( path_segments.getItem(0), $(this).siblings('path') ) );
283 return edge_elements;
286 function relation_factory() {
288 this.color_memo = null;
289 //TODO: colors hard coded for now
290 this.temp_color = '#FFA14F';
291 this.relation_colors = [ "#5CCCCC", "#67E667", "#F9FE72", "#6B90D4", "#FF7673", "#E467B3", "#AA67D5", "#8370D8", "#FFC173" ];
293 this.create_temporary = function( source_node_id, target_node_id ) {
294 var relation = $('#svgenlargement .relation').filter( function(index) {
295 var relation_id = $(this).children('title').text();
296 if( ( relation_id == ( source_node_id + '->' + target_node_id ) ) || ( relation_id == ( target_node_id + '->' + source_node_id ) ) ) {
300 if( relation.size() == 0 ) {
301 draw_relation( source_node_id, target_node_id, self.temp_color );
303 self.color_memo = relation.children('path').attr( 'stroke' );
304 relation.children('path').attr( 'stroke', self.temp_color );
307 this.remove_temporary = function() {
308 var path_element = $('#svgenlargement .relation').children('path[stroke="' + self.temp_color + '"]');
309 if( self.color_memo != null ) {
310 path_element.attr( 'stroke', self.color_memo );
311 self.color_memo = null;
313 var temporary = path_element.parent('g').remove();
318 this.create = function( source_node_id, target_node_id, color_index ) {
319 //TODO: Protect from (color_)index out of bound..
320 var relation_color = self.relation_colors[ color_index ];
321 var relation = draw_relation( source_node_id, target_node_id, relation_color );
322 get_node_obj( source_node_id ).update_elements();
323 get_node_obj( target_node_id ).update_elements();
326 this.toggle_active = function( relation_id ) {
327 var relation = $("#svgenlargement .relation:has(title:contains('" + relation_id + "'))");
328 var relation_path = relation.children('path');
329 if( !relation.data( 'active' ) ) {
330 relation_path.css( {'cursor':'pointer'} );
331 relation_path.mouseenter( function(event) {
332 outerTimer = setTimeout( function() {
333 timer = setTimeout( function() {
334 var title = relation.children('title').text();
335 var source_node_id = title.substring( 0, title.indexOf( "->" ) );
336 var target_node_id = title.substring( (title.indexOf( "->" ) + 2) );
337 $('#delete_source_node_id').val( source_node_id );
338 $('#delete_target_node_id').val( target_node_id );
339 self.showinfo(relation);
343 relation_path.mouseleave( function(event) {
344 clearTimeout(outerTimer);
345 if( timer != null ) { clearTimeout(timer); }
347 relation.data( 'active', true );
349 relation_path.unbind( 'mouseenter' );
350 relation_path.unbind( 'mouseleave' );
351 relation_path.css( {'cursor':'inherit'} );
352 relation.data( 'active', false );
355 this.showinfo = function(relation) {
356 var htmlstr = 'type: ' + relation.data( 'type' ) + '<br/>scope: ' + relation.data( 'scope' );
357 if( relation.data( 'note' ) ) {
358 htmlstr = htmlstr + '<br/>note: ' + relation.data( 'note' );
360 $('#delete-form-text').html( htmlstr );
361 var points = relation.children('path').attr('d').slice(1).replace('C',' ').split(' ');
362 var xs = parseFloat( points[0].split(',')[0] );
363 var xe = parseFloat( points[1].split(',')[0] );
364 var ys = parseFloat( points[0].split(',')[1] );
365 var ye = parseFloat( points[3].split(',')[1] );
366 var p = svg_root.createSVGPoint();
367 p.x = xs + ((xe-xs)*1.1);
368 p.y = ye - ((ye-ys)/2);
369 var ctm = svg_root.children[0].getScreenCTM();
370 var nx = p.matrixTransform(ctm).x;
371 var ny = p.matrixTransform(ctm).y;
372 var dialog_aria = $ ("div[aria-labelledby='ui-dialog-title-delete-form']");
373 $('#delete-form').dialog( 'open' );
374 dialog_aria.offset({ left: nx, top: ny });
376 this.remove = function( relation_id ) {
377 var relation = $("#svgenlargement .relation:has(title:contains('" + relation_id + "'))");
382 function draw_relation( source_id, target_id, relation_color ) {
383 var source_ellipse = get_ellipse( source_id );
384 var target_ellipse = get_ellipse( target_id );
385 var svg = $('#svgenlargement').children('svg').svg().svg('get');
386 var path = svg.createPath();
387 var sx = parseInt( source_ellipse.attr('cx') );
388 var rx = parseInt( source_ellipse.attr('rx') );
389 var sy = parseInt( source_ellipse.attr('cy') );
390 var ex = parseInt( target_ellipse.attr('cx') );
391 var ey = parseInt( target_ellipse.attr('cy') );
392 var relation = svg.group( $("#svgenlargement svg g"), {'class':'relation'} );
393 svg.title( relation, source_id + '->' + target_id );
394 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});
395 var relation_element = $('#svgenlargement .relation').filter( ':last' );
396 relation_element.insertBefore( $('#svgenlargement g g').filter(':first') );
397 return relation_element;
401 $(document).ready(function () {
404 relation_manager = new relation_factory();
405 $('#update_workspace_button').data('locked', false);
407 $('#enlargement').mousedown(function (event) {
410 .data('x', event.clientX)
411 .data('y', event.clientY)
412 .data('scrollLeft', this.scrollLeft)
413 stateTf = svg_root.children[0].getCTM().inverse();
414 var p = svg_root.createSVGPoint();
417 stateOrigin = p.matrixTransform(stateTf);
419 }).mouseup(function (event) {
420 $(this).data('down', false);
421 }).mousemove(function (event) {
422 if( timer != null ) { clearTimeout(timer); }
423 if ( ($(this).data('down') == true) && ($('#update_workspace_button').data('locked') == false) ) {
424 var p = svg_root.createSVGPoint();
427 p = p.matrixTransform(stateTf);
428 var matrix = stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y);
429 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
430 svg_root.children[0].setAttribute("transform", s);
432 }).mousewheel(function (event, delta) {
433 event.returnValue = false;
434 event.preventDefault();
435 if ( $('#update_workspace_button').data('locked') == false ) {
436 if( delta < -9 ) { delta = -9 };
437 var z = 1 + delta/10;
438 var g = svg_root.children[0];
439 if( (z<1 && (g.getScreenCTM().a * start_element_height) > 4.0) || (z>1 && (g.getScreenCTM().a * start_element_height) < 100) ) {
441 var p = root.createSVGPoint();
444 p = p.matrixTransform(g.getCTM().inverse());
445 var k = root.createSVGMatrix().translate(p.x, p.y).scale(z).translate(-p.x, -p.y);
446 var matrix = g.getCTM().multiply(k);
447 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
448 g.setAttribute("transform", s);
452 'overflow' : 'hidden',
453 'cursor' : '-moz-grab'
457 $( "#dialog-form" ).dialog({
464 $('#status').empty();
465 form_values = $('#collapse_node_form').serialize()
466 ncpath = getRelationshipURL();
467 $(':button :contains("Ok")').attr("disabled", true);
468 var jqjson = $.post( ncpath, form_values, function(data) {
469 $.each( data, function(item, source_target) {
470 var relation = relation_manager.create( source_target[0], source_target[1], $('#rel_type').attr('selectedIndex') );
471 relation.data( 'type', $('#rel_type :selected').text() );
472 relation.data( 'scope', $('#scope :selected').text() );
473 relation.data( 'note', $('#note').val() );
474 relation_manager.toggle_active( relation.children('title').text() );
476 $( "#dialog-form" ).dialog( "close" );
480 $( this ).dialog( "close" );
483 create: function(event, ui) {
484 $(this).data( 'relation_drawn', false );
485 //TODO? Err handling?
486 var basepath = getRelativePath();
487 var jqjson = $.getJSON( basepath + '/definitions', function(data) {
488 var types = data.types.sort();
489 $.each( types, function(index, value) {
490 $('#rel_type').append( $('<option>').attr( "value", value ).text(value) );
491 $('#keymaplist').append( $('<li>').css( "border-color", relation_manager.relation_colors[index] ).text(value) );
493 var scopes = data.scopes;
494 $.each( scopes, function(index, value) {
495 $('#scope').append( $('<option>').attr( "value", value ).text(value) );
500 relation_manager.create_temporary( $('#source_node_id').val(), $('#target_node_id').val() );
501 $(".ui-widget-overlay").css("background", "none");
502 $("#dialog_overlay").show();
503 $("#dialog_overlay").height( $("#enlargement_container").height() );
504 $("#dialog_overlay").width( $("#enlargement_container").innerWidth() );
505 $("#dialog_overlay").offset( $("#enlargement_container").offset() );
508 relation_manager.remove_temporary();
509 $( '#status' ).empty();
510 $("#dialog_overlay").hide();
512 }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
513 if( ( ajaxSettings.type == 'POST' ) && jqXHR.status == 403 ) {
514 var errobj = jQuery.parseJSON( jqXHR.responseText );
515 $('#status').append( '<p class="error">Error: ' + errobj.error + '</br>The relationship cannot be made.</p>' );
519 $( "#delete-form" ).dialog({
526 $( this ).dialog( "close" );
529 form_values = $('#delete_relation_form').serialize()
530 ncpath = getRelationshipURL()
531 var jqjson = $.ajax({ url: ncpath, data: form_values, success: function(data) {
532 $.each( data, function(item, source_target) {
533 relation_manager.remove( source_target[0] + '->' + source_target[1] );
535 $( "#delete-form" ).dialog( "close" );
536 }, dataType: 'json', type: 'DELETE' });
539 create: function(event, ui) {
540 var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ).css( 'width', '100%' );
541 buttonset.find( "button:contains('Cancel')" ).css( 'float', 'right' );
542 var dialog_aria = $("div[aria-labelledby='ui-dialog-title-delete-form']");
543 dialog_aria.mouseenter( function() {
544 if( mouseWait != null ) { clearTimeout(mouseWait) };
546 dialog_aria.mouseleave( function() {
547 mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
551 mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
557 $('#update_workspace_button').click( function() {
558 var svg_enlargement = $('#svgenlargement').svg().svg('get').root();
559 mouse_scale = svg_root.children[0].getScreenCTM().a;
560 if( $(this).data('locked') == true ) {
561 $('#svgenlargement ellipse' ).each( function( index ) {
562 if( $(this).data( 'node_obj' ) != null ) {
563 $(this).data( 'node_obj' ).ungreyout_edges();
564 $(this).data( 'node_obj' ).set_draggable( false );
565 var node_id = $(this).data( 'node_obj' ).get_id();
566 toggle_relation_active( node_id );
567 $(this).data( 'node_obj', null );
570 $(this).data('locked', false);
571 $(this).css('background-position', '0px 44px');
573 var left = $('#enlargement').offset().left;
574 var right = left + $('#enlargement').width();
575 var tf = svg_root.children[0].getScreenCTM().inverse();
576 var p = svg_root.createSVGPoint();
579 var cx_min = p.matrixTransform(tf).x;
581 var cx_max = p.matrixTransform(tf).x;
582 $('#svgenlargement ellipse').each( function( index ) {
583 var cx = parseInt( $(this).attr('cx') );
584 if( cx > cx_min && cx < cx_max) {
585 if( $(this).data( 'node_obj' ) == null ) {
586 $(this).data( 'node_obj', new node_obj( $(this) ) );
588 $(this).data( 'node_obj' ).set_draggable( true );
590 $(this).data( 'node_obj' ).greyout_edges();
591 var node_id = $(this).data( 'node_obj' ).get_id();
592 toggle_relation_active( node_id );
595 $(this).css('background-position', '0px 0px');
596 $(this).data('locked', true );
600 $('.helptag').popupWindow({
609 function toggle_relation_active( node_id ) {
610 $('#svgenlargement .relation').find( "title:contains('" + node_id + "')" ).each( function(index) {
611 matchid = new RegExp( "^" + node_id );
612 if( $(this).text().match( matchid ) != null ) {
613 relation_manager.toggle_active( $(this).text() );