Error handling when compressing readings
Errietta Kostala [Wed, 27 May 2015 15:34:12 +0000 (15:34 +0000)]
lib/stemmaweb/Controller/Relation.pm
root/css/relationship.css
root/js/relationship.js
root/src/relate.tt

index c40b8cb..fd5a452 100644 (file)
@@ -10,6 +10,14 @@ use TryCatch;
 BEGIN { extends 'Catalyst::Controller' }
 
 
+sub throw {
+       Text::Tradition::Error->throw(
+               'ident' => 'Collation error',
+               'message' => $_[0],
+               );
+}
+
+
 =head1 NAME
 
 stemmaweb::Controller::Relation - Controller for the relationship mapper
@@ -475,36 +483,60 @@ sub compress :Chained('text') :PathPart :Args(0) {
                        }
                }
 
+               my @nodes;
+               push @nodes, "$readings[$first]";
+
                for (my $i = $first+1; $i < $len; $i++) {
                        my $rdg = $readings[$first];
                        my $next = $readings[$i];
 
                        last unless $next->is_combinable;
+                       push @nodes, "$next";
 
-                       warn "Joining readings $rdg and $next\n";
+                       try {
+                               $collation->merge_readings( "$rdg", "$next", 1 );
+                       } catch ($e) {
+                               $c->stash->{result} = {
+                                       error_msg => $e->message,
+                               };
 
-                       $collation->merge_readings( "$rdg", "$next", 1 );
+                               $c->detach('View::JSON');
+                       }
                }
                
-               # 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};
+               try {
+                       # 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};
+                               }
                        }
+               } catch ($e) {
+                       $c->stash->{result} = {
+                               error_msg => $e->message,
+                       };
+
+                       $c->detach('View::JSON');
                }
 
+
                $collation->relations->rebuild_equivalence();
                $collation->calculate_ranks();
 
                $m->save($collation);
 
-               $c->stash->{'result'} = {};
+
+               $c->stash->{'result'} = {
+                       success => 1,
+                       nodes   => \@nodes,
+               };
+
                $c->forward('View::JSON');
        }
 }
index 98f03b7..fbe46ce 100644 (file)
@@ -233,3 +233,7 @@ user-select: none;
     background-color: #FFFFCC;
     z-index: 51; /* Cf. #update_workspace_button */
 }
+
+.error {
+    color: red;
+}
index d079b5b..bf62e6b 100644 (file)
@@ -5,7 +5,6 @@ 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) {
@@ -858,11 +857,12 @@ function merge_node( source_node_id, target_node_id ) {
 
 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 first = get_ellipse(readings[0]);
+    var first_title = first.parent().find('text')[0];
+
+    for (var i = 1; i < readings.length; i++) {
+        var cur         = get_ellipse(readings[i]);
         var cur_title   = cur.parent().find('text')[0];
 
         first_title.textContent += " " + cur_title.textContent;
@@ -977,8 +977,6 @@ 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();
 
@@ -1011,6 +1009,7 @@ function Marquee() {
                 $('#action-detach').change(function() {
                     if ($('#action-detach')[0].checked) {
                         $('#detach_collated_form').show();
+                        $('#multipleselect-form-text').show();
 
                         $('#detach_btn').show();
                         $('#merge_btn').hide();
@@ -1020,6 +1019,7 @@ function Marquee() {
                 $('#action-merge').change(function() {
                     if ($('#action-merge')[0].checked) {
                         $('#detach_collated_form').hide();
+                        $('#multipleselect-form-text').hide();
 
                         $('#detach_btn').hide();
                         $('#merge_btn').show();
@@ -1337,11 +1337,18 @@ $(document).ready(function () {
             var form_values = $('#detach_collated_form').serialize();
 
             var jqjson = $.post(ncpath, form_values, function(data) {
-                compress_nodes(current_selected);
-                current_selected = [];
+                if (data.success) {
+                    if (data.nodes) {
+                        compress_nodes(data.nodes);
+                    }
+
+                    mybuttons.button('enable');
+                    self.dialog('close');
+                } else if (data.error_msg) {
+                    document.getElementById('duplicate-merge-error').innerHTML = data.error_msg;
+                    mybuttons.button('enable');
 
-                mybuttons.button('enable');
-                self.dialog('close');
+                }
             });
         }
     },
index a8e4d89..016ae38 100644 (file)
@@ -109,8 +109,11 @@ $(document).ready(function () {
 
        <div id="multipleselect-form" title="Multiple nodes selected...">
                <div id="multipleselect-form-status"></div>
+       
+               <div class="error" id="duplicate-merge-error">
+               </div>
 
-               Detach or merge?<br/><br/><br/>
+               Detach or merge?<br/>
                
                <form autocomplete="off">
                        <label>