remove 'meaning' relationship; apply fix to make ellipse text draggable
[scpubgit/stemmatology.git] / stemmaweb / root / js / relationship.js
1 function getTextPath() {
2     var currpath = window.location.pathname
3     // Get rid of trailing slash
4     if( currpath.lastIndexOf('/') == currpath.length - 1 ) { 
5         currpath = currpath.slice( 0, currpath.length - 1) 
6     };
7     // Get rid of query parameters
8     if( currpath.lastIndexOf('?') != -1 ) {
9         currpath = currpath.slice( 0, currpath.lastIndexOf('?') );
10     };
11     var path_elements = currpath.split('/');
12     var textid = path_elements.pop();
13     var basepath = path_elements.join( '/' );
14     var path_parts = [ basepath, textid ];
15     return path_parts;
16 }
17
18 function getRelativePath() {
19         var path_parts = getTextPath();
20         return path_parts[0];
21 }
22
23 function getRelationshipURL() {
24         var path_parts = getTextPath();
25         return path_parts[0] + '/' + path_parts[1] + '/relationships';
26 }
27
28 function svgEnlargementLoaded() {
29         //Give some visual evidence that we are working
30         $('#loading_overlay').show();
31         lo_height = $("#enlargement_container").outerHeight();
32         lo_width = $("#enlargement_container").outerWidth();
33         $("#loading_overlay").height( lo_height );
34         $("#loading_overlay").width( lo_width );
35         $("#loading_overlay").offset( $("#enlargement_container").offset() );
36         $("#loading_message").offset(
37                 { 'top': lo_height / 2 - $("#loading_message").height() / 2,
38                   'left': lo_width / 2 - $("#loading_message").width() / 2 });
39     //Set viewbox widht and height to widht and height of $('#svgenlargement svg').
40     //This is essential to make sure zooming and panning works properly.
41     $('#svgenlargement ellipse').attr( {stroke:'green', fill:'#b3f36d'} );
42     var graph_svg = $('#svgenlargement svg');
43     var svg_g = $('#svgenlargement svg g')[0];
44     svg_root = graph_svg.svg().svg('get').root();
45     svg_root.viewBox.baseVal.width = graph_svg.attr( 'width' );
46     svg_root.viewBox.baseVal.height = graph_svg.attr( 'height' );
47     //Now set scale and translate so svg height is about 150px and vertically centered in viewbox.
48     //This is just to create a nice starting enlargement.
49     var initial_svg_height = 250;
50     var scale = initial_svg_height/graph_svg.attr( 'height' );
51     var additional_translate = (graph_svg.attr( 'height' ) - initial_svg_height)/(2*scale);
52     var transform = svg_g.getAttribute('transform');
53     var translate = parseFloat( transform.match( /translate\([^\)]*\)/ )[0].split('(')[1].split(' ')[1].split(')')[0] );
54     translate += additional_translate;
55     var transform = 'rotate(0) scale(' + scale + ') translate(4 ' + translate + ')';
56     svg_g.setAttribute('transform', transform);
57     //used to calculate min and max zoom level:
58     start_element_height = $("#svgenlargement .node title:contains('START#')").siblings('ellipse')[0].getBBox().height;
59     add_relations( function() { $('#loading_overlay').hide(); });
60 }
61
62 function add_relations( callback_fn ) {
63         var basepath = getRelativePath();
64         var textrelpath = getRelationshipURL();
65     $.getJSON( basepath + '/definitions', function(data) {
66         var rel_types = data.types.sort();
67         $.getJSON( textrelpath,
68         function(data) {
69             $.each(data, function( index, rel_info ) {
70                 var type_index = $.inArray(rel_info.type, rel_types);
71                 var source_found = get_ellipse( rel_info.source );
72                 var target_found = get_ellipse( rel_info.target );
73                 if( type_index != -1 && source_found.size() && target_found.size() ) {
74                     var relation = relation_manager.create( rel_info.source, rel_info.target, type_index );
75                     relation.data( 'type', rel_info.type );
76                     relation.data( 'scope', rel_info.scope );
77                     relation.data( 'note', rel_info.note );
78                     var node_obj = get_node_obj(rel_info.source);
79                     node_obj.set_draggable( false );
80                     node_obj.ellipse.data( 'node_obj', null );
81                     node_obj = get_node_obj(rel_info.target);
82                     node_obj.set_draggable( false );
83                     node_obj.ellipse.data( 'node_obj', null );
84                 }
85             });
86             callback_fn.call();
87         });
88     });
89 }
90
91 function get_ellipse( node_id ) {
92   return $('#svgenlargement .node').children('title').filter( function(index) {
93     return $(this).text() == node_id;
94   }).siblings('ellipse');
95 }
96
97 function get_node_obj( node_id ) {
98     var node_ellipse = get_ellipse( node_id );
99     if( node_ellipse.data( 'node_obj' ) == null ) {
100         node_ellipse.data( 'node_obj', new node_obj(node_ellipse) );
101     };
102     return node_ellipse.data( 'node_obj' );
103 }
104
105 function get_edge( edge_id ) {
106   return $('#svgenlargement .edge').filter( function(index) {
107     return $(this).children( 'title' ).text() == $('<div/>').html(edge_id).text() ;
108   });
109 }
110
111 function node_obj(ellipse) {
112   this.ellipse = ellipse;
113   var self = this;
114   
115   this.x = 0;
116   this.y = 0;
117   this.dx = 0;
118   this.dy = 0;
119   this.node_elements = node_elements_for(self.ellipse);
120
121   this.get_id = function() {
122     return self.ellipse.siblings('title').text()
123   }
124   
125   this.set_draggable = function( draggable ) {
126     if( draggable ) {
127       $(self.ellipse).attr( {stroke:'black', fill:'#fff'} );
128       $(self.ellipse).parent().mousedown( this.mousedown_listener );
129       $(self.ellipse).parent().hover( this.enter_node, this.leave_node ); 
130       self.ellipse.siblings('text').attr('class', 'noselect draggable');
131     } else {
132       self.ellipse.siblings('text').attr('class', '');
133       $(self.ellipse).parent().unbind('mouseenter').unbind('mouseleave').unbind('mousedown');     
134       $(self.ellipse).attr( {stroke:'green', fill:'#b3f36d'} );
135     }
136   }
137
138   this.mousedown_listener = function(evt) {
139     evt.stopPropagation();
140     self.x = evt.clientX;
141     self.y = evt.clientY;
142     $('body').mousemove( self.mousemove_listener );
143     $('body').mouseup( self.mouseup_listener );
144     $(self.ellipse).parent().unbind('mouseenter').unbind('mouseleave')
145     self.ellipse.attr( 'fill', '#ff66ff' );
146     first_node_g_element = $("#svgenlargement g .node" ).filter( ":first" );
147     if( first_node_g_element.attr('id') !== self.get_g().attr('id') ) { self.get_g().insertBefore( first_node_g_element ) };
148   }
149
150   this.mousemove_listener = function(evt) {
151     self.dx = (evt.clientX - self.x) / mouse_scale;
152     self.dy = (evt.clientY - self.y) / mouse_scale;
153     self.move_elements();
154     evt.returnValue = false;
155     evt.preventDefault();
156     return false;
157   }
158
159   this.mouseup_listener = function(evt) {    
160     if( $('ellipse[fill="#ffccff"]').size() > 0 ) {
161         var source_node_id = self.ellipse.siblings('title').text();
162         var source_node_text = self.ellipse.siblings('text').text();
163         var target_node_id = $('ellipse[fill="#ffccff"]').siblings("title").text();
164         var target_node_text = $('ellipse[fill="#ffccff"]').siblings("text").text();
165         $('#source_node_id').val( source_node_id );
166         $('#source_node_text').val( source_node_text );
167         $('#target_node_id').val( target_node_id );
168         $('#target_node_text').val( target_node_text );
169         $('#dialog-form').dialog( 'open' );
170     };
171     $('body').unbind('mousemove');
172     $('body').unbind('mouseup');
173     self.ellipse.attr( 'fill', '#fff' );
174     $(self.ellipse).parent().hover( self.enter_node, self.leave_node );
175     self.reset_elements();
176   }
177
178   this.cpos = function() {
179     return { x: self.ellipse.attr('cx'), y: self.ellipse.attr('cy') };
180   }
181
182   this.get_g = function() {
183     return self.ellipse.parent('g');
184   }
185
186   this.enter_node = function(evt) {
187     self.ellipse.attr( 'fill', '#ffccff' );
188   }
189
190   this.leave_node = function(evt) {
191     self.ellipse.attr( 'fill', '#fff' );
192   }
193
194   this.greyout_edges = function() {
195       $.each( self.node_elements, function(index, value) {
196         value.grey_out('.edge');
197       });
198   }
199
200   this.ungreyout_edges = function() {
201       $.each( self.node_elements, function(index, value) {
202         value.un_grey_out('.edge');
203       });
204   }
205
206   this.move_elements = function() {
207     $.each( self.node_elements, function(index, value) {
208       value.move(self.dx,self.dy);
209     });
210   }
211
212   this.reset_elements = function() {
213     $.each( self.node_elements, function(index, value) {
214       value.reset();
215     });
216   }
217
218   this.update_elements = function() {
219       self.node_elements = node_elements_for(self.ellipse);
220   }
221
222   self.set_draggable( true );
223 }
224
225 function svgshape( shape_element ) {
226   this.shape = shape_element;
227   this.move = function(dx,dy) {
228     this.shape.attr( "transform", "translate(" + dx + " " + dy + ")" );
229   }
230   this.reset = function() {
231     this.shape.attr( "transform", "translate( 0, 0 )" );
232   }
233   this.grey_out = function(filter) {
234       if( this.shape.parent(filter).size() != 0 ) {
235           this.shape.attr({'stroke':'#e5e5e5', 'fill':'#e5e5e5'});
236       }
237   }
238   this.un_grey_out = function(filter) {
239       if( this.shape.parent(filter).size() != 0 ) {
240         this.shape.attr({'stroke':'#000000', 'fill':'#000000'});
241       }
242   }
243 }
244
245 function svgpath( path_element, svg_element ) {
246   this.svg_element = svg_element;
247   this.path = path_element;
248   this.x = this.path.x;
249   this.y = this.path.y;
250   this.move = function(dx,dy) {
251     this.path.x = this.x + dx;
252     this.path.y = this.y + dy;
253   }
254   this.reset = function() {
255     this.path.x = this.x;
256     this.path.y = this.y;
257   }
258   this.grey_out = function(filter) {
259       if( this.svg_element.parent(filter).size() != 0 ) {
260           this.svg_element.attr('stroke', '#e5e5e5');
261           this.svg_element.siblings('text').attr('fill', '#e5e5e5');
262           this.svg_element.siblings('text').attr('class', 'noselect');
263       }
264   }
265   this.un_grey_out = function(filter) {
266       if( this.svg_element.parent(filter).size() != 0 ) {
267           this.svg_element.attr('stroke', '#000000');
268           this.svg_element.siblings('text').attr('fill', '#000000');
269           this.svg_element.siblings('text').attr('class', '');
270       }
271   }
272 }
273
274 function node_elements_for( ellipse ) {
275   node_elements = get_edge_elements_for( ellipse );
276   node_elements.push( new svgshape( ellipse.siblings('text') ) );
277   node_elements.push( new svgshape( ellipse ) );
278   return node_elements;
279 }
280
281 function get_edge_elements_for( ellipse ) {
282   edge_elements = new Array();
283   node_id = ellipse.siblings('title').text();
284   edge_in_pattern = new RegExp( node_id + '$' );
285   edge_out_pattern = new RegExp( '^' + node_id );
286   $.each( $('#svgenlargement .edge,#svgenlargement .relation').children('title'), function(index) {
287     title = $(this).text();
288     if( edge_in_pattern.test(title) ) {
289         polygon = $(this).siblings('polygon');
290         if( polygon.size() > 0 ) {
291             edge_elements.push( new svgshape( polygon ) );
292         }
293         path_segments = $(this).siblings('path')[0].pathSegList;
294         edge_elements.push( new svgpath( path_segments.getItem(path_segments.numberOfItems - 1), $(this).siblings('path') ) );
295     }
296     if( edge_out_pattern.test(title) ) {
297       path_segments = $(this).siblings('path')[0].pathSegList;
298       edge_elements.push( new svgpath( path_segments.getItem(0), $(this).siblings('path') ) );
299     }
300   });
301   return edge_elements;
302
303
304 function relation_factory() {
305     var self = this;
306     this.color_memo = null;
307     //TODO: colors hard coded for now
308     this.temp_color = '#FFA14F';
309     this.relation_colors = [ "#5CCCCC", "#67E667", "#F9FE72", "#6B90D4", "#FF7673", "#E467B3", "#AA67D5", "#8370D8", "#FFC173" ];
310
311     this.create_temporary = function( source_node_id, target_node_id ) {
312         var relation = $('#svgenlargement .relation').filter( function(index) {
313             var relation_id = $(this).children('title').text();
314             if( ( relation_id == ( source_node_id + '->' + target_node_id ) ) || ( relation_id == ( target_node_id + '->' + source_node_id ) ) ) {
315                 return true;
316             } 
317         } );
318         if( relation.size() == 0 ) { 
319             draw_relation( source_node_id, target_node_id, self.temp_color );
320         } else {
321             self.color_memo = relation.children('path').attr( 'stroke' );
322             relation.children('path').attr( 'stroke', self.temp_color );
323         }
324     }
325     this.remove_temporary = function() {
326         var path_element = $('#svgenlargement .relation').children('path[stroke="' + self.temp_color + '"]');
327         if( self.color_memo != null ) {
328             path_element.attr( 'stroke', self.color_memo );
329             self.color_memo = null;
330         } else {
331             var temporary = path_element.parent('g').remove();
332             temporary.empty();
333             temporary = null; 
334         }
335     }
336     this.create = function( source_node_id, target_node_id, color_index ) {
337         //TODO: Protect from (color_)index out of bound..
338         var relation_color = self.relation_colors[ color_index ];
339         var relation = draw_relation( source_node_id, target_node_id, relation_color );
340         get_node_obj( source_node_id ).update_elements();
341         get_node_obj( target_node_id ).update_elements();
342         return relation;
343     }
344     this.toggle_active = function( relation_id ) {
345         var relation = $("#svgenlargement .relation:has(title:contains('" + relation_id + "'))");
346         var relation_path = relation.children('path');
347         if( !relation.data( 'active' ) ) {
348             relation_path.css( {'cursor':'pointer'} );
349             relation_path.mouseenter( function(event) { 
350                 outerTimer = setTimeout( function() { 
351                     timer = setTimeout( function() { 
352                         var title = relation.children('title').text();
353                         var source_node_id = title.substring( 0, title.indexOf( "->" ) );
354                         var target_node_id = title.substring( (title.indexOf( "->" ) + 2) );
355                         $('#delete_source_node_id').val( source_node_id );
356                         $('#delete_target_node_id').val( target_node_id );
357                         self.showinfo(relation); 
358                     }, 500 ) 
359                 }, 1000 );
360             });
361             relation_path.mouseleave( function(event) {
362                 clearTimeout(outerTimer); 
363                 if( timer != null ) { clearTimeout(timer); } 
364             });
365             relation.data( 'active', true );
366         } else {
367             relation_path.unbind( 'mouseenter' );
368             relation_path.unbind( 'mouseleave' );
369             relation_path.css( {'cursor':'inherit'} );
370             relation.data( 'active', false );
371         }
372     }
373     this.showinfo = function(relation) {
374         var htmlstr = 'type: ' + relation.data( 'type' ) + '<br/>scope: ' + relation.data( 'scope' );
375         if( relation.data( 'note' ) ) {
376                 htmlstr = htmlstr + '<br/>note: ' + relation.data( 'note' );
377         }
378         $('#delete-form-text').html( htmlstr );
379         var points = relation.children('path').attr('d').slice(1).replace('C',' ').split(' ');
380         var xs = parseFloat( points[0].split(',')[0] );
381         var xe = parseFloat( points[1].split(',')[0] );
382         var ys = parseFloat( points[0].split(',')[1] );
383         var ye = parseFloat( points[3].split(',')[1] );
384         var p = svg_root.createSVGPoint();
385         p.x = xs + ((xe-xs)*1.1);
386         p.y = ye - ((ye-ys)/2);
387         var ctm = svg_root.children[0].getScreenCTM();
388         var nx = p.matrixTransform(ctm).x;
389         var ny = p.matrixTransform(ctm).y;
390         var dialog_aria = $ ("div[aria-labelledby='ui-dialog-title-delete-form']");
391         $('#delete-form').dialog( 'open' );
392         dialog_aria.offset({ left: nx, top: ny });
393     }
394     this.remove = function( relation_id ) {
395         var relation = $("#svgenlargement .relation:has(title:contains('" + relation_id + "'))");
396         relation.remove();
397     }
398 }
399
400 function draw_relation( source_id, target_id, relation_color ) {
401     var source_ellipse = get_ellipse( source_id );
402     var target_ellipse = get_ellipse( target_id );
403     var svg = $('#svgenlargement').children('svg').svg().svg('get');
404     var path = svg.createPath(); 
405     var sx = parseInt( source_ellipse.attr('cx') );
406     var rx = parseInt( source_ellipse.attr('rx') );
407     var sy = parseInt( source_ellipse.attr('cy') );
408     var ex = parseInt( target_ellipse.attr('cx') );
409     var ey = parseInt( target_ellipse.attr('cy') );
410     var relation = svg.group( $("#svgenlargement svg g"), {'class':'relation'} );
411     svg.title( relation, source_id + '->' + target_id );
412     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});
413     var relation_element = $('#svgenlargement .relation').filter( ':last' );
414     relation_element.insertBefore( $('#svgenlargement g g').filter(':first') );
415     return relation_element;
416 }
417
418
419 $(document).ready(function () {
420     
421   timer = null;
422   relation_manager = new relation_factory();
423   $('#update_workspace_button').data('locked', false);
424   
425   $('#enlargement').mousedown(function (event) {
426     $(this)
427         .data('down', true)
428         .data('x', event.clientX)
429         .data('y', event.clientY)
430         .data('scrollLeft', this.scrollLeft)
431         stateTf = svg_root.children[0].getCTM().inverse();
432         var p = svg_root.createSVGPoint();
433         p.x = event.clientX;
434         p.y = event.clientY;
435         stateOrigin = p.matrixTransform(stateTf);
436         return false;
437   }).mouseup(function (event) {
438         $(this).data('down', false);
439   }).mousemove(function (event) {
440     if( timer != null ) { clearTimeout(timer); } 
441     if ( ($(this).data('down') == true) && ($('#update_workspace_button').data('locked') == false) ) {
442         var p = svg_root.createSVGPoint();
443         p.x = event.clientX;
444         p.y = event.clientY;
445         p = p.matrixTransform(stateTf);
446         var matrix = stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y);
447         var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
448         svg_root.children[0].setAttribute("transform", s);
449     }
450   }).mousewheel(function (event, delta) {
451     event.returnValue = false;
452     event.preventDefault();
453     if ( $('#update_workspace_button').data('locked') == false ) {
454         if( delta < -9 ) { delta = -9 }; 
455         var z = 1 + delta/10;
456         var g = svg_root.children[0];
457         if( (z<1 && (g.getScreenCTM().a * start_element_height) > 4.0) || (z>1 && (g.getScreenCTM().a * start_element_height) < 100) ) {
458             var root = svg_root;
459             var p = root.createSVGPoint();
460             p.x = event.clientX;
461             p.y = event.clientY;
462             p = p.matrixTransform(g.getCTM().inverse());
463             var k = root.createSVGMatrix().translate(p.x, p.y).scale(z).translate(-p.x, -p.y);
464             var matrix = g.getCTM().multiply(k);
465             var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
466             g.setAttribute("transform", s);
467         }
468     }
469   }).css({
470     'overflow' : 'hidden',
471     'cursor' : '-moz-grab'
472   });
473   
474
475   $( "#dialog-form" ).dialog({
476     autoOpen: false,
477     height: 270,
478     width: 290,
479     modal: true,
480     buttons: {
481       "Ok": function() {
482         $('#status').empty();
483         form_values = $('#collapse_node_form').serialize()
484         ncpath = getRelationshipURL();
485         $(':button :contains("Ok")').attr("disabled", true);
486         var jqjson = $.post( ncpath, form_values, function(data) {
487             $.each( data, function(item, source_target) { 
488                 var relation = relation_manager.create( source_target[0], source_target[1], $('#rel_type').attr('selectedIndex') );
489                 relation.data( 'type', $('#rel_type :selected').text()  );
490                 relation.data( 'scope', $('#scope :selected').text()  );
491                 relation.data( 'note', $('#note').val()  );
492                 relation_manager.toggle_active( relation.children('title').text() );
493             });
494             $( "#dialog-form" ).dialog( "close" );
495         }, 'json' );
496       },
497       Cancel: function() {
498           $( this ).dialog( "close" );
499       }
500     },
501     create: function(event, ui) { 
502         $(this).data( 'relation_drawn', false );
503         //TODO? Err handling?
504                 var basepath = getRelativePath();
505         var jqjson = $.getJSON( basepath + '/definitions', function(data) {
506             var types = data.types.sort();
507             $.each( types, function(index, value) {   
508                  $('#rel_type').append( $('<option>').attr( "value", value ).text(value) ); 
509                  $('#keymaplist').append( $('<li>').css( "border-color", relation_manager.relation_colors[index] ).text(value) ); 
510             });
511             var scopes = data.scopes;
512             $.each( scopes, function(index, value) {   
513                  $('#scope').append( $('<option>').attr( "value", value ).text(value) ); 
514             });
515         });        
516     },
517     open: function() {
518         relation_manager.create_temporary( $('#source_node_id').val(), $('#target_node_id').val() );
519         $(".ui-widget-overlay").css("background", "none");
520         $("#dialog_overlay").show();
521         $("#dialog_overlay").height( $("#enlargement_container").height() );
522         $("#dialog_overlay").width( $("#enlargement_container").innerWidth() );
523         $("#dialog_overlay").offset( $("#enlargement_container").offset() );
524     },
525     close: function() {
526         relation_manager.remove_temporary();
527         $( '#status' ).empty();
528         $("#dialog_overlay").hide();
529     }
530   }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
531       if( ( ajaxSettings.type == 'POST' ) && jqXHR.status == 403 ) {
532           var errobj = jQuery.parseJSON( jqXHR.responseText );
533           $('#status').append( '<p class="error">Error: ' + errobj.error + '</br>The relationship cannot be made.</p>' );
534       }
535   } );
536
537   $( "#delete-form" ).dialog({
538     autoOpen: false,
539     height: 135,
540     width: 160,
541     modal: false,
542     buttons: {
543         Cancel: function() {
544             $( this ).dialog( "close" );
545         },
546         Delete: function() {
547           form_values = $('#delete_relation_form').serialize()
548           ncpath = getRelationshipURL()
549           var jqjson = $.ajax({ url: ncpath, data: form_values, success: function(data) {
550               $.each( data, function(item, source_target) { 
551                   relation_manager.remove( source_target[0] + '->' + source_target[1] );
552               });
553               $( "#delete-form" ).dialog( "close" );
554           }, dataType: 'json', type: 'DELETE' });
555         }
556     },
557     create: function(event, ui) {
558         var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ).css( 'width', '100%' );
559         buttonset.find( "button:contains('Cancel')" ).css( 'float', 'right' );
560         var dialog_aria = $("div[aria-labelledby='ui-dialog-title-delete-form']");  
561         dialog_aria.mouseenter( function() {
562             if( mouseWait != null ) { clearTimeout(mouseWait) };
563         })
564         dialog_aria.mouseleave( function() {
565             mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
566         })
567     },
568     open: function() {
569         mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
570     },
571     close: function() {
572     }
573   });
574
575   $('#update_workspace_button').click( function() {
576      var svg_enlargement = $('#svgenlargement').svg().svg('get').root();
577      mouse_scale = svg_root.children[0].getScreenCTM().a;
578      if( $(this).data('locked') == true ) {
579          $('#svgenlargement ellipse' ).each( function( index ) {
580              if( $(this).data( 'node_obj' ) != null ) {
581                  $(this).data( 'node_obj' ).ungreyout_edges();
582                  $(this).data( 'node_obj' ).set_draggable( false );
583                  var node_id = $(this).data( 'node_obj' ).get_id();
584                  toggle_relation_active( node_id );
585                  $(this).data( 'node_obj', null );
586              }
587          })
588          $(this).data('locked', false);
589          $(this).css('background-position', '0px 44px');
590      } else {
591          var left = $('#enlargement').offset().left;
592          var right = left + $('#enlargement').width();
593          var tf = svg_root.children[0].getScreenCTM().inverse(); 
594          var p = svg_root.createSVGPoint();
595          p.x=left;
596          p.y=100;
597          var cx_min = p.matrixTransform(tf).x;
598          p.x=right;
599          var cx_max = p.matrixTransform(tf).x;
600          $('#svgenlargement ellipse').each( function( index ) {
601              var cx = parseInt( $(this).attr('cx') );
602              if( cx > cx_min && cx < cx_max) { 
603                  if( $(this).data( 'node_obj' ) == null ) {
604                      $(this).data( 'node_obj', new node_obj( $(this) ) );
605                  } else {
606                      $(this).data( 'node_obj' ).set_draggable( true );
607                  }
608                  $(this).data( 'node_obj' ).greyout_edges();
609                  var node_id = $(this).data( 'node_obj' ).get_id();
610                  toggle_relation_active( node_id );
611              }
612          });
613          $(this).css('background-position', '0px 0px');
614          $(this).data('locked', true );
615      }
616   });
617   
618   $('.helptag').popupWindow({ 
619           height:500, 
620           width:800, 
621           top:50, 
622           left:50,
623           scrollbars:1 
624   }); 
625
626   
627   function toggle_relation_active( node_id ) {
628       $('#svgenlargement .relation').find( "title:contains('" + node_id +  "')" ).each( function(index) {
629           matchid = new RegExp( "^" + node_id );
630           if( $(this).text().match( matchid ) != null ) {
631               relation_manager.toggle_active( $(this).text() );
632           };
633       });
634   }
635
636 });
637
638