On going work on detaching of nodes/strands. Strands detachable now, not visibly...
Joris van Zundert [Wed, 26 Jun 2013 15:46:43 +0000 (17:46 +0200)]
root/js/detach_helpers.js [new file with mode: 0644]
root/js/relationship.js

diff --git a/root/js/detach_helpers.js b/root/js/detach_helpers.js
new file mode 100644 (file)
index 0000000..c5cdba8
--- /dev/null
@@ -0,0 +1,77 @@
+function edges_of( ellipse ) {
+  var edges = new Array();
+  var node_id = ellipse.parent().attr('id');
+  var edge_in_pattern = new RegExp( node_id + '$' );
+  $.each( $('#svgenlargement .edge'), function(index) {
+      title = $(this).children('title').text();
+      if( title.search( node_id ) > -1 ) {
+          var edge = new Edge( $(this) );
+          edge.node_id = node_id;
+          if( edge_in_pattern.test(title) ) {
+              edge.is_incoming  = true;
+          }
+          edges.push( edge );
+      }
+  } );
+  return edges;
+}
+
+function Edge( g_elem ) {
+    
+    var self = this;
+    
+    this.g_elem = g_elem;
+    this.witnesses = g_elem.children('text').text().split( /,\s*/ );
+    this.is_incoming = false;
+    
+    this.detach_witnesses = function( witnesses_to_detach ) {
+        var detached = [];
+        var left = '';
+        var clone = null;
+        $.each( witnesses_to_detach, function( index, witness_to_detach ) {
+            witness_index = self.witnesses.indexOf( witness_to_detach );
+            if( witness_index > -1 ) {  
+                self.witnesses.splice( witness_index, 1 );
+                detached.push( witness_to_detach );
+            }
+        } );
+        if( detached != '' ) {
+            clone = self.clone_for( detached );
+        }
+        var remaining = self.create_label( self.witnesses );
+        if( remaining == '' ) {
+            self.g_elem.remove();
+        } else {
+            self.g_elem.children('text').text( remaining );
+        }
+        return clone;
+    }
+    
+    this.get_label = function() {
+        return self.g_elem.children('text').text();
+    }
+    
+    this.create_label = function( witnesses ) {
+        var label = '';
+        $.each( witnesses, function( index, witness ) {
+            label = label + witness + ', ';
+        } );
+        label = label.replace( /, $/, '' );
+        return label;
+    }
+    
+    this.clone_for = function( witnesses ) {
+        var label = self.create_label( witnesses );
+        var clone = g_elem.clone();
+        clone.children('text').text( label );
+        clone = new Edge( clone );
+        clone.is_incoming = self.is_incoming;
+        return clone;
+    }
+    
+    this.attach = function( node_id_maybe ) {
+        //update title
+        return null;
+    }
+    
+}
index 05862d3..422709f 100644 (file)
@@ -661,9 +661,12 @@ function detach_node( readingsgohere ) {
         } );
     } );    
     
-    // Todo: .each is getting us in trouble most likely
-    // assuming one element in object readings for now
+    // Todo: .each is getting us in trouble most likely for
+    // duplicating edges in a strand that both are incoming and outgoing
+    // like b and c in -i-> a -ii-> b -iii-> c -iv->
     detached_edges = [];
+    
+    // here we detach witnesses from the existing edges accoring to what's being relayed by readings
     $.each( readings, function( node_id, reading ) {
         var edges = edges_of( get_ellipse( reading.orig_rdg ) );
         incoming_remaining = [];
@@ -685,32 +688,34 @@ function detach_node( readingsgohere ) {
                 } );
             }
         } );
+        
+        // After detachng we still need to check if for *all* readings
+        // an edge was detached. It may be that a witness was not
+        // explicitly named on an edge but was part of a 'majority' edge
+        // in which case we need to duplicate and name that edge after those
+        // remaining witnesses.
         if( outgoing_remaining.length > 0 ) {
-            console.log( 'jo' );
             $.each( edges, function( index, edge ) {
-                console.log( edge.get_label() );
                 if( edge.get_label() == 'majority' && !edge.is_incoming ) {
                     detached_edges.push( edge.clone_for( outgoing_remaining ) );
                 }
             } );
         }
         if( incoming_remaining.length > 0 ) {
-            console.log( 'je' );
             $.each( edges, function( index, edge ) {
                 if( edge.get_label() == 'majority' && edge.is_incoming ) {
                     detached_edges.push( edge.clone_for( outgoing_remaining ) );
                 }
             } );
         }
-        console.log( detached_edges );
     } );
             
+    console.log( detached_edges );
     //if not all witnesses of reading are detached in and out clone
       // clone remaining from 'majority in'
     //clone node with node_id
     // in all clones replace reading.orig_rdg with node_id
     // move cloned node up 20px
-    
 
 }
 
@@ -776,7 +781,6 @@ function Marquee() {
             var cx_max = p.matrixTransform(tf).x;
             var cy_max = p.matrixTransform(tf).y;
             //select any node with its center inside the marquee
-            var readings = [];
             //also merge witness sets from nodes
             var witnesses = [];
             $('#svgenlargement ellipse').each( function( index ) {
@@ -786,28 +790,16 @@ function Marquee() {
                     if( cy > cy_min && cy < cy_max) {
                         // we actually heve no real 'selected' state for nodes, except coloring
                         $(this).attr( 'fill', '#9999ff' );
-                        // Take note of the selected reading(s) and applicable witness(es)
-                        // so we can populate the multipleselect-form 
-                        readings.push( $(this).parent().attr('id') );
                         var this_witnesses = $(this).data( 'node_obj' ).get_witnesses();
                         witnesses = arrayUnique( witnesses.concat( this_witnesses ) );
                     }
                 }
             });
             if( $('ellipse[fill="#9999ff"]').size() > 0 ) {
-                //add intersection of witnesses sets to the multi select form and open it
-                $('#detach_collated_form').empty();
-                $.each( readings, function( index, value ) {
-                       $('#detach_collated_form').append( $('<input>').attr(
-                       "type", "hidden").attr("name", "readings[]").attr(
-                               "value", value ) );
-                   });
+                //add interesectio of witnesses sets to the multi select form and open it
                 $.each( witnesses, function( index, value ) {
-                    $('#detach_collated_form').append( 
-                       '<input type="checkbox" name="witnesses[]" value="' + value 
-                       + '">' + value + '<br>' );
+                    $('#multipleselect-form').append( '<input type="checkbox" name="witnesses" value="' + value + '">' + value + '<br>' );
                 });
-                $('#multiple_selected_readings').attr('value', readings.join(',') );
                 $('#multipleselect-form').dialog( 'open' );
             }
             self.svg_rect.remove( $('#marquee') );
@@ -1026,33 +1018,28 @@ $(document).ready(function () {
     close: function() {}
   });
 
+  var multipleselect_buttonset = {
+        cancel: function() { $( this ).dialog( "close" ); },
+        button1: function () {  },
+        button2: function() {  }
+  };   
+
   $( "#multipleselect-form" ).dialog({
     autoOpen: false,
     height: 150,
     width: 250,
     modal: true,
-    buttons: {
-       Cancel: function() { $( this ).dialog( "close" ); },
-       Detach: function ( evt ) { 
-               $(evt.target).button("disable");
-               var form_values = $('#detach_collated_form').serialize();
-                       ncpath = getTextURL( 'duplicate' );
-                       var jqjson = $.post( ncpath, form_values, function(data) {
-                               $.each( data, function(reading, newreading) { 
-                                       alert( "Would detach reading " + newreading['id'] + " from " + reading );
-                               });
-                               $(evt.target).button("enable");
-                       });
-               }
-    },
     create: function(event, ui) {
         var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ).css( 'width', '100%' );
         buttonset.find( "button:contains('Cancel')" ).css( 'float', 'right' );
     },
     open: function() {
         $( this ).dialog( "option", "width", 200 );
+        $( this ).dialog( "option", "buttons",
+            [{ text: "Button_1", click: multipleselect_buttonset['button1'] },
+             { text: "Button_2", click: multipleselect_buttonset['button2'] },
+             { text: "Cancel", click: multipleselect_buttonset['cancel'] }] );
         $(".ui-widget-overlay").css("background", "none");
-        $('#multipleselect-form-status').empty();
         $("#dialog_overlay").show();
         $("#dialog_overlay").height( $("#enlargement_container").height() );
         $("#dialog_overlay").width( $("#enlargement_container").innerWidth() );
@@ -1062,25 +1049,7 @@ $(document).ready(function () {
         marquee.unselect();
         $("#dialog_overlay").hide();
     }
-  }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
-               if( ajaxSettings.url == getTextURL('duplicate') 
-                       && ajaxSettings.type == 'POST' && jqXHR.status == 403 ) {
-                       var error;
-                       if( jqXHR.responseText.indexOf('do not have permission to modify') > -1 ) {
-                               error = 'You are not authorized to modify this tradition. (Try logging in again?)';
-                       } else {
-                               try {
-                                       var errobj = jQuery.parseJSON( jqXHR.responseText );
-                                       error = errobj.error + '</br>The relationship cannot be made.</p>';
-                               } catch(e) {
-                                       error = jqXHR.responseText;
-                               }
-                       }
-                       $('#multipleselect-form-status').append( '<p class="error">Error: ' + error );
-               }
-               $(event.target).parent().find('.ui-button').button("enable");
-       });
-
+  });
 
   // Helpers for relationship deletion