repurpose outdated DB-save script as a DB-query script
[scpubgit/stemmatology.git] / stemmaweb / root / js / relationship.js
index 7921b1d..ed1b150 100644 (file)
@@ -1,8 +1,13 @@
 function getTextPath() {
     var currpath = window.location.pathname
+    // Get rid of trailing slash
     if( currpath.lastIndexOf('/') == currpath.length - 1 ) { 
        currpath = currpath.slice( 0, currpath.length - 1) 
     };
+    // Get rid of query parameters
+    if( currpath.lastIndexOf('?') != -1 ) {
+       currpath = currpath.slice( 0, currpath.lastIndexOf('?') );
+    };
     var path_elements = currpath.split('/');
     var textid = path_elements.pop();
     var basepath = path_elements.join( '/' );
@@ -50,7 +55,7 @@ function svgEnlargementLoaded() {
     var transform = 'rotate(0) scale(' + scale + ') translate(4 ' + translate + ')';
     svg_g.setAttribute('transform', transform);
     //used to calculate min and max zoom level:
-    start_element_height = $("#svgenlargement .node title:contains('#START#')").siblings('ellipse')[0].getBBox().height;
+    start_element_height = $("#svgenlargement .node title:contains('START#')").siblings('ellipse')[0].getBBox().height;
     add_relations( function() { $('#loading_overlay').hide(); });
 }
 
@@ -63,7 +68,9 @@ function add_relations( callback_fn ) {
         function(data) {
             $.each(data, function( index, rel_info ) {
                 var type_index = $.inArray(rel_info.type, rel_types);
-                if( type_index != -1 ) {
+                var source_found = get_ellipse( rel_info.source );
+                var target_found = get_ellipse( rel_info.target );
+                if( type_index != -1 && source_found.size() && target_found.size() ) {
                     var relation = relation_manager.create( rel_info.source, rel_info.target, type_index );
                     relation.data( 'type', rel_info.type );
                     relation.data( 'scope', rel_info.scope );
@@ -117,12 +124,14 @@ function node_obj(ellipse) {
   
   this.set_draggable = function( draggable ) {
     if( draggable ) {
-      self.ellipse.attr( {stroke:'black', fill:'#fff'} );
-      self.ellipse.mousedown( this.mousedown_listener );
-      self.ellipse.hover( this.enter_node, this.leave_node );  
+      $(self.ellipse).attr( {stroke:'black', fill:'#fff'} );
+      $(self.ellipse).parent().mousedown( this.mousedown_listener );
+      $(self.ellipse).parent().hover( this.enter_node, this.leave_node ); 
+      self.ellipse.siblings('text').attr('class', 'noselect draggable');
     } else {
-      self.ellipse.unbind('mouseenter').unbind('mouseleave').unbind('mousedown');
-      self.ellipse.attr( {stroke:'green', fill:'#b3f36d'} );
+      self.ellipse.siblings('text').attr('class', '');
+      $(self.ellipse).parent().unbind('mouseenter').unbind('mouseleave').unbind('mousedown');     
+      $(self.ellipse).attr( {stroke:'green', fill:'#b3f36d'} );
     }
   }
 
@@ -132,7 +141,7 @@ function node_obj(ellipse) {
     self.y = evt.clientY;
     $('body').mousemove( self.mousemove_listener );
     $('body').mouseup( self.mouseup_listener );
-    self.ellipse.unbind('mouseenter').unbind('mouseleave')
+    $(self.ellipse).parent().unbind('mouseenter').unbind('mouseleave')
     self.ellipse.attr( 'fill', '#ff66ff' );
     first_node_g_element = $("#svgenlargement g .node" ).filter( ":first" );
     if( first_node_g_element.attr('id') !== self.get_g().attr('id') ) { self.get_g().insertBefore( first_node_g_element ) };
@@ -142,20 +151,27 @@ function node_obj(ellipse) {
     self.dx = (evt.clientX - self.x) / mouse_scale;
     self.dy = (evt.clientY - self.y) / mouse_scale;
     self.move_elements();
+    evt.returnValue = false;
+    evt.preventDefault();
+    return false;
   }
 
   this.mouseup_listener = function(evt) {    
     if( $('ellipse[fill="#ffccff"]').size() > 0 ) {
         var source_node_id = self.ellipse.siblings('title').text();
+        var source_node_text = self.ellipse.siblings('text').text();
         var target_node_id = $('ellipse[fill="#ffccff"]').siblings("title").text();
+        var target_node_text = $('ellipse[fill="#ffccff"]').siblings("text").text();
         $('#source_node_id').val( source_node_id );
+        $('#source_node_text').val( source_node_text );
         $('#target_node_id').val( target_node_id );
+        $('#target_node_text').val( target_node_text );
         $('#dialog-form').dialog( 'open' );
     };
     $('body').unbind('mousemove');
     $('body').unbind('mouseup');
     self.ellipse.attr( 'fill', '#fff' );
-    self.ellipse.hover( self.enter_node, self.leave_node );
+    $(self.ellipse).parent().hover( self.enter_node, self.leave_node );
     self.reset_elements();
   }
 
@@ -243,12 +259,14 @@ function svgpath( path_element, svg_element ) {
       if( this.svg_element.parent(filter).size() != 0 ) {
           this.svg_element.attr('stroke', '#e5e5e5');
           this.svg_element.siblings('text').attr('fill', '#e5e5e5');
+          this.svg_element.siblings('text').attr('class', 'noselect');
       }
   }
   this.un_grey_out = function(filter) {
       if( this.svg_element.parent(filter).size() != 0 ) {
           this.svg_element.attr('stroke', '#000000');
           this.svg_element.siblings('text').attr('fill', '#000000');
+          this.svg_element.siblings('text').attr('class', '');
       }
   }
 }