Don't show merge option for detach
[scpubgit/stemmaweb.git] / root / js / detach_helpers.js
index c5cdba8..c9f035a 100644 (file)
@@ -1,13 +1,14 @@
 function edges_of( ellipse ) {
   var edges = new Array();
   var node_id = ellipse.parent().attr('id');
-  var edge_in_pattern = new RegExp( node_id + '$' );
+  var edge_outgoing_pattern = new RegExp( '^' + node_id + '-' );
+  var edge_incoming_pattern = new RegExp( node_id + '$' );
   $.each( $('#svgenlargement .edge'), function(index) {
       title = $(this).children('title').text();
-      if( title.search( node_id ) > -1 ) {
+      if( edge_outgoing_pattern.test(title) || edge_incoming_pattern.test(title) ) {
           var edge = new Edge( $(this) );
           edge.node_id = node_id;
-          if( edge_in_pattern.test(title) ) {
+          if( edge_incoming_pattern.test(title) ) {
               edge.is_incoming  = true;
           }
           edges.push( edge );
@@ -23,6 +24,8 @@ function Edge( g_elem ) {
     this.g_elem = g_elem;
     this.witnesses = g_elem.children('text').text().split( /,\s*/ );
     this.is_incoming = false;
+    this.start_node_id = g_elem.children('title').text().split('-')[0];
+    this.end_node_id = g_elem.children('title').text().split('>')[1];
     
     this.detach_witnesses = function( witnesses_to_detach ) {
         var detached = [];
@@ -64,14 +67,89 @@ function Edge( g_elem ) {
         var label = self.create_label( witnesses );
         var clone = g_elem.clone();
         clone.children('text').text( label );
-        clone = new Edge( clone );
+        var duplicate_data = g_elem.data( 'repositioned' );
+        if( duplicate_data != null ) {
+            clone.data( 'repositioned', duplicate_data );
+        }
+        clone = new Edge( clone );        
         clone.is_incoming = self.is_incoming;
         return clone;
     }
     
-    this.attach = function( node_id_maybe ) {
-        //update title
-        return null;
+    this.attach_witnesses = function( witnesses ) {
+        self.witnesses = self.witnesses.concat( witnesses );
+        self.g_elem.children( 'text' ).text( self.create_label( self.witnesses ) );
+        var edge_weight = 0.8 + ( 0.2 * self.witnesses.length );
+        self.g_elem.children( 'path' ).attr( 'stroke-width', edge_weight );
+    }
+    
+    this.attach_endpoint = function( target_node_id ) {
+        // first let's find out if the startpoint might also be linked to the target already
+        // in that case we need to remove this edge and transfer the witnesses to the 
+        // appropriate edge of the target_node
+        $.each( edges_of( get_ellipse( target_node_id ) ), function( index, target_edge ) {
+            if( (self != null) && (target_edge.is_incoming == true) ) {
+                if( self.start_node_id == target_edge.start_node_id) {
+                    target_edge.attach_witnesses( self.witnesses );
+                    self.g_elem.remove();
+                    self = null;
+                }
+            }
+        } );
+        // if not let's really move the end pointer towards the target node
+        if( self != null ) {
+            var polygon = self.g_elem.children( 'polygon' ); 
+            if( polygon.size() > 0 ) {
+                var end_point_arrowhead = new svgshape( polygon );
+                var path_segments = self.g_elem.children('path')[0].pathSegList;
+                var edge_path = new svgpath( path_segments.getItem(path_segments.numberOfItems - 1), self.g_elem.children('path') );
+                var target_ellipse = get_ellipse( target_node_id );
+                var target_cx = parseFloat( target_ellipse.attr( 'cx' ) );
+                var target_cy = parseFloat( target_ellipse.attr( 'cy' ) );
+                var target_rx = parseFloat( target_ellipse.attr( 'rx' ) );
+                var source_ellipse = get_ellipse( self.end_node_id );
+                var source_cx = parseFloat( source_ellipse.attr( 'cx' ) );
+                var source_cy = parseFloat( source_ellipse.attr( 'cy' ) );
+                var source_rx = parseFloat( source_ellipse.attr( 'rx' ) );
+                var dx = (target_cx - target_rx) - (source_cx - source_rx);
+                var dy = (target_cy - source_cy);
+                end_point_arrowhead.reposition( dx, dy );
+                edge_path.reposition( dx, dy );
+                // var new_title = g_elem.children('title').text().replace( self.end_node_id, target_node_id );
+                // console.log( new_title );
+            }
+        }
+    }
+
+    this.attach_startpoint = function( target_node_id ) {
+        // first let's find out if the endpoint might also be linked to the target already
+        // in that case we need to remove this edge and transfer the witnesses to the 
+        // appropriate edge of the target_node
+        $.each( edges_of( get_ellipse( target_node_id ) ), function( index, target_edge ) {
+            if( (self != null) && (target_edge.is_incoming != true ) ) {
+                if( self.end_node_id == target_edge.end_node_id) {
+                    target_edge.attach_witnesses( self.witnesses );
+                    self.g_elem.remove();
+                    self = null;
+                }
+            }
+        } );
+        // if not let's really move the start point towards the target node
+        if( self != null ) {
+            var path_segments = self.g_elem.children('path')[0].pathSegList;                
+            var edge_path = new svgpath( path_segments.getItem(0), self.g_elem.children('path') );
+            var target_ellipse = get_ellipse( target_node_id );
+            var target_cx = parseFloat( target_ellipse.attr( 'cx' ) );
+            var target_cy = parseFloat( target_ellipse.attr( 'cy' ) );
+            var target_rx = parseFloat( target_ellipse.attr( 'rx' ) );
+            var source_ellipse = get_ellipse( self.start_node_id );
+            var source_cx = parseFloat( source_ellipse.attr( 'cx' ) );
+            var source_cy = parseFloat( source_ellipse.attr( 'cy' ) );
+            var source_rx = parseFloat( source_ellipse.attr( 'rx' ) );
+            var dx = (target_cx + target_rx) - (source_cx + source_rx);
+            var dy = (target_cy - source_cy);
+            edge_path.reposition( dx, dy );
+        }
     }
     
 }