Implementation of merge back-end & UI
Errietta Kostala [Fri, 15 May 2015 15:01:20 +0000 (15:01 +0000)]
lib/stemmaweb/Controller/Relation.pm
root/js/relationship.js
root/src/relate.tt

index 0a0579c..c40b8cb 100644 (file)
@@ -428,6 +428,87 @@ sub reading :Chained('text') :PathPart :Args(1) {
 
 }
 
+sub compress :Chained('text') :PathPart :Args(0) {
+       my( $self, $c ) = @_;
+       my $tradition = delete $c->stash->{'tradition'};
+       my $collation = $tradition->collation;
+       my $m = $c->model('Directory');
+
+       my @rids = $c->request->param('readings[]');
+       my @readings;
+
+       foreach my $rid (@rids) {
+               my $rdg = $collation->reading( $rid );
+
+               push @readings, $rdg;
+       }
+
+       my $len = scalar @readings;
+
+       if( $c->request->method eq 'POST' ) {
+               if( $c->stash->{'permission'} ne 'full' ) {
+                       $c->response->status( '403' );
+                       $c->stash->{'result'} = { 
+                               'error' => 'You do not have permission to modify this tradition.' };
+                       $c->detach('View::JSON');
+                       return;
+               }
+
+               # Sanity check: first save the original text of each witness.
+               my %origtext;
+               foreach my $wit ( $tradition->witnesses ) {
+                       $origtext{$wit->sigil} = $collation->path_text( $wit->sigil );
+                       if( $wit->is_layered ) {
+                               my $acsig = $wit->sigil . $collation->ac_label;
+                               $origtext{$acsig} = $collation->path_text( $acsig );
+                       }
+               }
+
+               my $first = 0;
+
+               for (my $i = 0; $i < $len; $i++) {
+                       my $rdg = $readings[$i];
+
+                       if ($rdg->is_combinable) {
+                               $first = $i;
+                               last;
+                       }
+               }
+
+               for (my $i = $first+1; $i < $len; $i++) {
+                       my $rdg = $readings[$first];
+                       my $next = $readings[$i];
+
+                       last unless $next->is_combinable;
+
+                       warn "Joining readings $rdg and $next\n";
+
+                       $collation->merge_readings( "$rdg", "$next", 1 );
+               }
+               
+               # Finally, make sure we haven't screwed anything up.
+               foreach my $wit ( $tradition->witnesses ) {
+                       my $pathtext = $collation->path_text( $wit->sigil );
+                       throw( "Text differs for witness " . $wit->sigil )
+                               unless $pathtext eq $origtext{$wit->sigil};
+                       if( $wit->is_layered ) {
+                               my $acsig = $wit->sigil . $collation->ac_label;
+                               $pathtext = $collation->path_text( $acsig );
+                               throw( "Layered text differs for witness " . $wit->sigil )
+                                       unless $pathtext eq $origtext{$acsig};
+                       }
+               }
+
+               $collation->relations->rebuild_equivalence();
+               $collation->calculate_ranks();
+
+               $m->save($collation);
+
+               $c->stash->{'result'} = {};
+               $c->forward('View::JSON');
+       }
+}
+
 =head2 merge
 
  POST relation/$textid/merge { data }
index 9a4a169..d079b5b 100644 (file)
@@ -5,6 +5,7 @@ var start_element_height = 0;
 var reltypes = {};
 var readingdata = {};
 var text_direction = 'LR';
+var current_selected = [];
 
 jQuery.removeFromArray = function(value, arr) {
     return jQuery.grep(arr, function(elem, index) {
@@ -855,6 +856,38 @@ function merge_node( source_node_id, target_node_id ) {
     $( jq( source_node_id ) ).remove();    
 }
 
+function compress_nodes(readings) {
+    //add text of other readings to 1st reading
+    for (var i = 1; i < readings.length; i++) {
+        var first = get_ellipse(readings[0]);
+        var cur   = get_ellipse(readings[i]);
+
+        var first_title = first.parent().find('text')[0];
+        var cur_title   = cur.parent().find('text')[0];
+
+        first_title.textContent += " " + cur_title.textContent;
+    };
+
+    //delete all others
+    for (var i = 1; i < readings.length; i++) {
+        var node = get_ellipse(readings[i]);
+        var rid = readings[i-1] + '->' + readings[i];
+
+        //[].slice.call(s.getElementsByTagName('title')).find(function(elem){return elem.textContent=='r64.2->r66.2'}).parentNode.remove()
+
+        console.log(svg_root, svg_root_element);
+
+        console.log(rid);
+
+        [].slice.call(svg_root.getElementsByTagName('title'))
+            .find(function(elem){
+                return elem.textContent==rid
+            }).parentNode.remove();
+
+        node.parent().remove();
+    }
+}
+
 function Marquee() {
     
     var self = this;
@@ -944,19 +977,55 @@ function Marquee() {
                 }
             });
             if( $('ellipse[fill="#9999ff"]').size() > 0 ) {
+                current_selected = readings;
+
                 //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 ) );
-               });
-                               $.each( witnesses, function( index, value ) {
+                });
+                $.each( witnesses, function( index, value ) {
                     $('#detach_collated_form').append( 
                       '<input type="checkbox" name="witnesses[]" value="' + value 
                       + '">' + value + '<br>' ); 
                 });
                 $('#multiple_selected_readings').attr('value', readings.join(',') ); 
+
+                if ($('#action-merge')[0].checked) {
+                    $('#detach_collated_form').hide();
+                    $('#multipleselect-form-text').hide();
+
+                    $('#detach_btn').hide();
+                    $('#merge_btn').show();
+                } else {
+                    $('#detach_collated_form').show();
+                    $('#multipleselect-form-text').show();
+
+                    $('#detach_btn').show();
+                    $('#merge_btn').hide();
+                }
+
+                $('#action-detach').change(function() {
+                    if ($('#action-detach')[0].checked) {
+                        $('#detach_collated_form').show();
+
+                        $('#detach_btn').show();
+                        $('#merge_btn').hide();
+                    }
+                });
+
+                $('#action-merge').change(function() {
+                    if ($('#action-merge')[0].checked) {
+                        $('#detach_collated_form').hide();
+
+                        $('#detach_btn').hide();
+                        $('#merge_btn').show();
+                    }
+                });
+
                 $('#multipleselect-form').dialog( 'open' );
             }
             self.svg_rect.remove( $('#marquee') );
@@ -1243,10 +1312,12 @@ $(document).ready(function () {
     modal: true,
     buttons: {
         Cancel: function() { $( this ).dialog( "close" ); },
-        Detach: function ( evt ) { 
+        Detach: function ( evt ) {
+            evt.target.id = 'detach_btn';
+
             var self = $(this);
-                       var mybuttons = $(evt.target).closest('button').parent().find('button');
-                       mybuttons.button( 'disable' );
+            var mybuttons = $(evt.target).closest('button').parent().find('button');
+            mybuttons.button( 'disable' );
             var form_values = $('#detach_collated_form').serialize();
             ncpath = getTextURL( 'duplicate' );
             var jqjson = $.post( ncpath, form_values, function(data) {
@@ -1254,6 +1325,24 @@ $(document).ready(function () {
                 mybuttons.button("enable");
                 self.dialog( "close" );
             } );
+        },
+        Merge: function (evt) {
+            evt.target.id = 'merge_btn';
+
+            var self = $(this);
+            var mybuttons = $(evt.target).closest('button').parent().find('button');
+            mybuttons.button('disable');
+
+            var ncpath = getTextURL('compress');
+            var form_values = $('#detach_collated_form').serialize();
+
+            var jqjson = $.post(ncpath, form_values, function(data) {
+                compress_nodes(current_selected);
+                current_selected = [];
+
+                mybuttons.button('enable');
+                self.dialog('close');
+            });
         }
     },
     create: function(event, ui) {
@@ -1268,6 +1357,11 @@ $(document).ready(function () {
         $("#dialog_overlay").height( $("#enlargement_container").height() );
         $("#dialog_overlay").width( $("#enlargement_container").innerWidth() );
         $("#dialog_overlay").offset( $("#enlargement_container").offset() );
+
+        var mybuttons = $(this).parent().find('button');
+
+        mybuttons[1].id = 'detach_btn';
+        mybuttons[2].id = 'merge_btn';
     },
     close: function() { 
         marquee.unselect();
index 4d9d4e4..a8e4d89 100644 (file)
@@ -109,6 +109,19 @@ $(document).ready(function () {
 
        <div id="multipleselect-form" title="Multiple nodes selected...">
                <div id="multipleselect-form-status"></div>
+
+               Detach or merge?<br/><br/><br/>
+               
+               <form autocomplete="off">
+                       <label>
+                               <input id="action-detach" checked name='option' type='radio' /> Detach
+                       </label>
+
+                       <label>
+                               <input id="action-merge" name='option' type='radio' /> Merge
+                       </label>
+               </form>
+
                <div id="multipleselect-form-text">Select witness(es) to detach:</div>
                <form id="detach_collated_form" action="#">
                </form>