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 //Set viewbox widht and height to widht and height of $('#svgenlargement svg').
25 //This is essential to make sure zooming and panning works properly.
26 $('#svgenlargement ellipse').attr( {stroke:'green', fill:'#b3f36d'} );
27 var graph_svg = $('#svgenlargement svg');
28 var svg_g = $('#svgenlargement svg g')[0];
29 svg_root = graph_svg.svg().svg('get').root();
30 svg_root.viewBox.baseVal.width = graph_svg.attr( 'width' );
31 svg_root.viewBox.baseVal.height = graph_svg.attr( 'height' );
32 //Now set scale and translate so svg height is about 150px and vertically centered in viewbox.
33 //This is just to create a nice starting enlargement.
34 var initial_svg_height = 250;
35 var scale = initial_svg_height/graph_svg.attr( 'height' );
36 var additional_translate = (graph_svg.attr( 'height' ) - initial_svg_height)/(2*scale);
37 var transform = svg_g.getAttribute('transform');
38 var translate = parseFloat( transform.match( /translate\([^\)]*\)/ )[0].split('(')[1].split(' ')[1].split(')')[0] );
39 translate += additional_translate;
40 var transform = 'rotate(0) scale(' + scale + ') translate(4 ' + translate + ')';
41 svg_g.setAttribute('transform', transform);
42 //used to calculate min and max zoom level:
43 start_element_height = $("#svgenlargement .node title:contains('#START#')").siblings('ellipse')[0].getBBox().height;
47 function add_relations() {
48 var basepath = getRelativePath();
49 var textrelpath = getRelationshipURL();
50 $.getJSON( basepath + '/definitions', function(data) {
51 var rel_types = data.types.sort();
52 $.getJSON( textrelpath,
54 $.each(data, function( index, rel_info ) {
55 var type_index = $.inArray(rel_info.type, rel_types);
56 if( type_index != -1 ) {
57 var relation = relation_manager.create( rel_info.source, rel_info.target, type_index );
58 relation.data( 'type', rel_info.type );
59 relation.data( 'scope', rel_info.scope );
60 var node_obj = get_node_obj(rel_info.source);
61 node_obj.set_draggable( false );
62 node_obj.ellipse.data( 'node_obj', null );
63 node_obj = get_node_obj(rel_info.target);
64 node_obj.set_draggable( false );
65 node_obj.ellipse.data( 'node_obj', null );
72 function get_ellipse( node_id ) {
73 return $('#svgenlargement .node').children('title').filter( function(index) {
74 return $(this).text() == node_id;
75 }).siblings('ellipse');
78 function get_node_obj( node_id ) {
79 var node_ellipse = get_ellipse( node_id );
80 if( node_ellipse.data( 'node_obj' ) == null ) {
81 node_ellipse.data( 'node_obj', new node_obj(node_ellipse) );
83 return node_ellipse.data( 'node_obj' );
86 function get_edge( edge_id ) {
87 return $('#svgenlargement .edge').filter( function(index) {
88 return $(this).children( 'title' ).text() == $('<div/>').html(edge_id).text() ;
92 function node_obj(ellipse) {
93 this.ellipse = ellipse;
100 this.node_elements = node_elements_for(self.ellipse);
102 this.get_id = function() {
103 return self.ellipse.siblings('title').text()
106 this.set_draggable = function( draggable ) {
108 self.ellipse.attr( {stroke:'black', fill:'#fff'} );
109 self.ellipse.mousedown( this.mousedown_listener );
110 self.ellipse.hover( this.enter_node, this.leave_node );
112 self.ellipse.unbind('mouseenter').unbind('mouseleave').unbind('mousedown');
113 self.ellipse.attr( {stroke:'green', fill:'#b3f36d'} );
117 this.mousedown_listener = function(evt) {
118 evt.stopPropagation();
119 self.x = evt.clientX;
120 self.y = evt.clientY;
121 $('body').mousemove( self.mousemove_listener );
122 $('body').mouseup( self.mouseup_listener );
123 self.ellipse.unbind('mouseenter').unbind('mouseleave')
124 self.ellipse.attr( 'fill', '#ff66ff' );
125 first_node_g_element = $("#svgenlargement g .node" ).filter( ":first" );
126 if( first_node_g_element.attr('id') !== self.get_g().attr('id') ) { self.get_g().insertBefore( first_node_g_element ) };
129 this.mousemove_listener = function(evt) {
130 self.dx = (evt.clientX - self.x) / mouse_scale;
131 self.dy = (evt.clientY - self.y) / mouse_scale;
132 self.move_elements();
135 this.mouseup_listener = function(evt) {
136 if( $('ellipse[fill="#ffccff"]').size() > 0 ) {
137 var source_node_id = self.ellipse.siblings('title').text();
138 var target_node_id = $('ellipse[fill="#ffccff"]').siblings("title").text();
139 $('#source_node_id').val( source_node_id );
140 $('#target_node_id').val( target_node_id );
141 $('#dialog-form').dialog( 'open' );
143 $('body').unbind('mousemove');
144 $('body').unbind('mouseup');
145 self.ellipse.attr( 'fill', '#fff' );
146 self.ellipse.hover( self.enter_node, self.leave_node );
147 self.reset_elements();
150 this.cpos = function() {
151 return { x: self.ellipse.attr('cx'), y: self.ellipse.attr('cy') };
154 this.get_g = function() {
155 return self.ellipse.parent('g');
158 this.enter_node = function(evt) {
159 self.ellipse.attr( 'fill', '#ffccff' );
162 this.leave_node = function(evt) {
163 self.ellipse.attr( 'fill', '#fff' );
166 this.greyout_edges = function() {
167 $.each( self.node_elements, function(index, value) {
168 value.grey_out('.edge');
172 this.ungreyout_edges = function() {
173 $.each( self.node_elements, function(index, value) {
174 value.un_grey_out('.edge');
178 this.move_elements = function() {
179 $.each( self.node_elements, function(index, value) {
180 value.move(self.dx,self.dy);
184 this.reset_elements = function() {
185 $.each( self.node_elements, function(index, value) {
190 this.update_elements = function() {
191 self.node_elements = node_elements_for(self.ellipse);
194 self.set_draggable( true );
197 function svgshape( shape_element ) {
198 this.shape = shape_element;
199 this.move = function(dx,dy) {
200 this.shape.attr( "transform", "translate(" + dx + " " + dy + ")" );
202 this.reset = function() {
203 this.shape.attr( "transform", "translate( 0, 0 )" );
205 this.grey_out = function(filter) {
206 if( this.shape.parent(filter).size() != 0 ) {
207 this.shape.attr({'stroke':'#e5e5e5', 'fill':'#e5e5e5'});
210 this.un_grey_out = function(filter) {
211 if( this.shape.parent(filter).size() != 0 ) {
212 this.shape.attr({'stroke':'#000000', 'fill':'#000000'});
217 function svgpath( path_element, svg_element ) {
218 this.svg_element = svg_element;
219 this.path = path_element;
220 this.x = this.path.x;
221 this.y = this.path.y;
222 this.move = function(dx,dy) {
223 this.path.x = this.x + dx;
224 this.path.y = this.y + dy;
226 this.reset = function() {
227 this.path.x = this.x;
228 this.path.y = this.y;
230 this.grey_out = function(filter) {
231 if( this.svg_element.parent(filter).size() != 0 ) {
232 this.svg_element.attr('stroke', '#e5e5e5');
233 this.svg_element.siblings('text').attr('fill', '#e5e5e5');
236 this.un_grey_out = function(filter) {
237 if( this.svg_element.parent(filter).size() != 0 ) {
238 this.svg_element.attr('stroke', '#000000');
239 this.svg_element.siblings('text').attr('fill', '#000000');
244 function node_elements_for( ellipse ) {
245 node_elements = get_edge_elements_for( ellipse );
246 node_elements.push( new svgshape( ellipse.siblings('text') ) );
247 node_elements.push( new svgshape( ellipse ) );
248 return node_elements;
251 function get_edge_elements_for( ellipse ) {
252 edge_elements = new Array();
253 node_id = ellipse.siblings('title').text();
254 edge_in_pattern = new RegExp( node_id + '$' );
255 edge_out_pattern = new RegExp( '^' + node_id );
256 $.each( $('#svgenlargement .edge,#svgenlargement .relation').children('title'), function(index) {
257 title = $(this).text();
258 if( edge_in_pattern.test(title) ) {
259 polygon = $(this).siblings('polygon');
260 if( polygon.size() > 0 ) {
261 edge_elements.push( new svgshape( polygon ) );
263 path_segments = $(this).siblings('path')[0].pathSegList;
264 edge_elements.push( new svgpath( path_segments.getItem(path_segments.numberOfItems - 1), $(this).siblings('path') ) );
266 if( edge_out_pattern.test(title) ) {
267 path_segments = $(this).siblings('path')[0].pathSegList;
268 edge_elements.push( new svgpath( path_segments.getItem(0), $(this).siblings('path') ) );
271 return edge_elements;
274 function relation_factory() {
276 this.color_memo = null;
277 //TODO: colors hard coded for now
278 this.temp_color = '#FFA14F';
279 this.relation_colors = [ "#5CCCCC", "#67E667", "#F9FE72", "#6B90D4", "#FF7673", "#E467B3", "#AA67D5", "#8370D8", "#FFC173" ];
281 this.create_temporary = function( source_node_id, target_node_id ) {
282 var relation = $('#svgenlargement .relation').filter( function(index) {
283 var relation_id = $(this).children('title').text();
284 if( ( relation_id == ( source_node_id + '->' + target_node_id ) ) || ( relation_id == ( target_node_id + '->' + source_node_id ) ) ) {
288 if( relation.size() == 0 ) {
289 draw_relation( source_node_id, target_node_id, self.temp_color );
291 self.color_memo = relation.children('path').attr( 'stroke' );
292 relation.children('path').attr( 'stroke', self.temp_color );
295 this.remove_temporary = function() {
296 var path_element = $('#svgenlargement .relation').children('path[stroke="' + self.temp_color + '"]');
297 if( self.color_memo != null ) {
298 path_element.attr( 'stroke', self.color_memo );
299 self.color_memo = null;
301 var temporary = path_element.parent('g').remove();
306 this.create = function( source_node_id, target_node_id, color_index ) {
307 //TODO: Protect from (color_)index out of bound..
308 var relation_color = self.relation_colors[ color_index ];
309 var relation = draw_relation( source_node_id, target_node_id, relation_color );
310 get_node_obj( source_node_id ).update_elements();
311 get_node_obj( target_node_id ).update_elements();
314 this.toggle_active = function( relation_id ) {
315 var relation = $("#svgenlargement .relation:has(title:contains('" + relation_id + "'))");
316 var relation_path = relation.children('path');
317 if( !relation.data( 'active' ) ) {
318 relation_path.css( {'cursor':'pointer'} );
319 relation_path.mouseenter( function(event) {
320 outerTimer = setTimeout( function() {
321 timer = setTimeout( function() {
322 var title = relation.children('title').text();
323 var source_node_id = title.substring( 0, title.indexOf( "->" ) );
324 var target_node_id = title.substring( (title.indexOf( "->" ) + 2) );
325 $('#delete_source_node_id').val( source_node_id );
326 $('#delete_target_node_id').val( target_node_id );
327 self.showinfo(relation);
331 relation_path.mouseleave( function(event) {
332 clearTimeout(outerTimer);
333 if( timer != null ) { clearTimeout(timer); }
335 relation.data( 'active', true );
337 relation_path.unbind( 'mouseenter' );
338 relation_path.unbind( 'mouseleave' );
339 relation_path.css( {'cursor':'inherit'} );
340 relation.data( 'active', false );
343 this.showinfo = function(relation) {
344 $('#delete-form-text').html( 'type: ' + relation.data( 'type' ) + '<br/>scope: ' + relation.data( 'scope' ) );
345 var points = relation.children('path').attr('d').slice(1).replace('C',' ').split(' ');
346 var xs = parseFloat( points[0].split(',')[0] );
347 var xe = parseFloat( points[1].split(',')[0] );
348 var ys = parseFloat( points[0].split(',')[1] );
349 var ye = parseFloat( points[3].split(',')[1] );
350 var p = svg_root.createSVGPoint();
351 p.x = xs + ((xe-xs)*1.1);
352 p.y = ye - ((ye-ys)/2);
353 var ctm = svg_root.children[0].getScreenCTM();
354 var nx = p.matrixTransform(ctm).x;
355 var ny = p.matrixTransform(ctm).y;
356 var dialog_aria = $ ("div[aria-labelledby='ui-dialog-title-delete-form']");
357 $('#delete-form').dialog( 'open' );
358 dialog_aria.offset({ left: nx, top: ny });
360 this.remove = function( relation_id ) {
361 var relation = $("#svgenlargement .relation:has(title:contains('" + relation_id + "'))");
366 function draw_relation( source_id, target_id, relation_color ) {
367 var source_ellipse = get_ellipse( source_id );
368 var target_ellipse = get_ellipse( target_id );
369 var svg = $('#svgenlargement').children('svg').svg().svg('get');
370 var path = svg.createPath();
371 var sx = parseInt( source_ellipse.attr('cx') );
372 var rx = parseInt( source_ellipse.attr('rx') );
373 var sy = parseInt( source_ellipse.attr('cy') );
374 var ex = parseInt( target_ellipse.attr('cx') );
375 var ey = parseInt( target_ellipse.attr('cy') );
376 var relation = svg.group( $("#svgenlargement svg g"), {'class':'relation'} );
377 svg.title( relation, source_id + '->' + target_id );
378 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});
379 var relation_element = $('#svgenlargement .relation').filter( ':last' );
380 relation_element.insertBefore( $('#svgenlargement g g').filter(':first') );
381 return relation_element;
385 $(document).ready(function () {
388 relation_manager = new relation_factory();
389 $('#update_workspace_button').data('locked', false);
391 $('#enlargement').mousedown(function (event) {
394 .data('x', event.clientX)
395 .data('y', event.clientY)
396 .data('scrollLeft', this.scrollLeft)
397 stateTf = svg_root.children[0].getCTM().inverse();
398 var p = svg_root.createSVGPoint();
401 stateOrigin = p.matrixTransform(stateTf);
403 }).mouseup(function (event) {
404 $(this).data('down', false);
405 }).mousemove(function (event) {
406 if( timer != null ) { clearTimeout(timer); }
407 if ( ($(this).data('down') == true) && ($('#update_workspace_button').data('locked') == false) ) {
408 var p = svg_root.createSVGPoint();
411 p = p.matrixTransform(stateTf);
412 var matrix = stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y);
413 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
414 svg_root.children[0].setAttribute("transform", s);
416 }).mousewheel(function (event, delta) {
417 event.returnValue = false;
418 event.preventDefault();
419 if ( $('#update_workspace_button').data('locked') == false ) {
420 if( delta < -9 ) { delta = -9 };
421 var z = 1 + delta/10;
422 var g = svg_root.children[0];
423 if( (z<1 && (g.getScreenCTM().a * start_element_height) > 4.0) || (z>1 && (g.getScreenCTM().a * start_element_height) < 100) ) {
425 var p = root.createSVGPoint();
428 p = p.matrixTransform(g.getCTM().inverse());
429 var k = root.createSVGMatrix().translate(p.x, p.y).scale(z).translate(-p.x, -p.y);
430 var matrix = g.getCTM().multiply(k);
431 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
432 g.setAttribute("transform", s);
436 'overflow' : 'hidden',
437 'cursor' : '-moz-grab'
441 $( "#dialog-form" ).dialog({
448 $('#status').empty();
449 form_values = $('#collapse_node_form').serialize()
450 ncpath = getRelationshipURL();
451 $(':button :contains("Ok")').attr("disabled", true);
452 var jqjson = $.post( ncpath, form_values, function(data) {
453 $.each( data, function(item, source_target) {
454 var relation = relation_manager.create( source_target[0], source_target[1], $('#rel_type').attr('selectedIndex') );
455 relation.data( 'type', $('#rel_type :selected').text() );
456 relation.data( 'scope', $('#scope :selected').text() );
457 relation_manager.toggle_active( relation.children('title').text() );
459 $( "#dialog-form" ).dialog( "close" );
463 $( this ).dialog( "close" );
466 create: function(event, ui) {
467 $(this).data( 'relation_drawn', false );
468 //TODO? Err handling?
469 var basepath = getRelativePath();
470 var jqjson = $.getJSON( basepath + '/definitions', function(data) {
471 var types = data.types.sort();
472 $.each( types, function(index, value) {
473 $('#rel_type').append( $('<option>').attr( "value", value ).text(value) );
474 $('#keymaplist').append( $('<li>').css( "border-color", relation_manager.relation_colors[index] ).text(value) );
476 var scopes = data.scopes;
477 $.each( scopes, function(index, value) {
478 $('#scope').append( $('<option>').attr( "value", value ).text(value) );
483 relation_manager.create_temporary( $('#source_node_id').val(), $('#target_node_id').val() );
484 $(".ui-widget-overlay").css("background", "none");
485 $("#dialog_overlay").show();
486 $("#dialog_overlay").height( $("#enlargement_container").height() );
487 $("#dialog_overlay").width( $("#enlargement_container").width() );
488 $("#dialog_overlay").offset( $("#enlargement_container").offset() );
491 relation_manager.remove_temporary();
492 $( '#status' ).empty();
493 $("#dialog_overlay").hide();
495 }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
496 if( ( ajaxSettings.type == 'POST' ) && jqXHR.status == 403 ) {
497 var errobj = jQuery.parseJSON( jqXHR.responseText );
498 $('#status').append( '<p class="error">Error: ' + errobj.error + '</br>The relationship cannot be made.</p>' );
502 $( "#delete-form" ).dialog({
509 $( this ).dialog( "close" );
512 form_values = $('#delete_relation_form').serialize()
513 ncpath = getRelationshipURL()
514 var jqjson = $.ajax({ url: ncpath, data: form_values, success: function(data) {
515 $.each( data, function(item, source_target) {
516 relation_manager.remove( source_target[0] + '->' + source_target[1] );
518 $( "#delete-form" ).dialog( "close" );
519 }, dataType: 'json', type: 'DELETE' });
522 create: function(event, ui) {
523 var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ).css( 'width', '100%' );
524 buttonset.find( "button:contains('Cancel')" ).css( 'float', 'right' );
525 var dialog_aria = $("div[aria-labelledby='ui-dialog-title-delete-form']");
526 dialog_aria.mouseenter( function() {
527 if( mouseWait != null ) { clearTimeout(mouseWait) };
529 dialog_aria.mouseleave( function() {
530 mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
534 mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
540 $('#update_workspace_button').click( function() {
541 var svg_enlargement = $('#svgenlargement').svg().svg('get').root();
542 mouse_scale = svg_root.children[0].getScreenCTM().a;
543 if( $(this).data('locked') == true ) {
544 $('#svgenlargement ellipse' ).each( function( index ) {
545 if( $(this).data( 'node_obj' ) != null ) {
546 $(this).data( 'node_obj' ).ungreyout_edges();
547 $(this).data( 'node_obj' ).set_draggable( false );
548 var node_id = $(this).data( 'node_obj' ).get_id();
549 toggle_relation_active( node_id );
550 $(this).data( 'node_obj', null );
553 $(this).data('locked', false);
554 $(this).css('background-position', '0px 44px');
556 var left = $('#enlargement').offset().left;
557 var right = left + $('#enlargement').width();
558 var tf = svg_root.children[0].getScreenCTM().inverse();
559 var p = svg_root.createSVGPoint();
562 var cx_min = p.matrixTransform(tf).x;
564 var cx_max = p.matrixTransform(tf).x;
565 $('#svgenlargement ellipse').each( function( index ) {
566 var cx = parseInt( $(this).attr('cx') );
567 if( cx > cx_min && cx < cx_max) {
568 if( $(this).data( 'node_obj' ) == null ) {
569 $(this).data( 'node_obj', new node_obj( $(this) ) );
571 $(this).data( 'node_obj' ).set_draggable( true );
573 $(this).data( 'node_obj' ).greyout_edges();
574 var node_id = $(this).data( 'node_obj' ).get_id();
575 toggle_relation_active( node_id );
578 $(this).css('background-position', '0px 0px');
579 $(this).data('locked', true );
583 function toggle_relation_active( node_id ) {
584 $('#svgenlargement .relation').find( "title:contains('" + node_id + "')" ).each( function(index) {
585 matchid = new RegExp( "^" + node_id );
586 if( $(this).text().match( matchid ) != null ) {
587 relation_manager.toggle_active( $(this).text() );