add error handling for reading morphology update
[scpubgit/stemmatology.git] / stemmaweb / root / js / relationship.js
index 8f308db..3fa40d1 100644 (file)
@@ -52,8 +52,8 @@ function node_dblclick_listener( evt ) {
        // Set the easy properties first
        $('#reading-form').dialog( 'option', 'title', 'Reading information for "' + reading_info['text'] + '"' );
        $('#reading_id').val( reading_id );
-       $('#reading_is_nonsense').val( reading_info['is_nonsense'] );
-       $('#reading_grammar_invalid').val( reading_info['grammar_invalid'] );
+       $('#reading_is_nonsense').attr( 'checked', reading_info['is_nonsense'] );
+       $('#reading_grammar_invalid').attr( 'checked', reading_info['grammar_invalid'] );
        // Use .text as a backup for .normal_form
        var normal_form = reading_info['normal_form'];
        if( !normal_form ) {
@@ -75,14 +75,15 @@ function morphology_form ( lexlist ) {
        $('#morphology').empty();
        $.each( lexlist, function( idx, lex ) {
                var morphoptions = [];
-               $.each( lex['wordform_matchlist'], function( tdx, tag ) {
-                       var tagstr = stringify_wordform( tag );
-                       morphoptions.push( tagstr );
-               });
+               if( 'wordform_matchlist' in lex ) {
+                       $.each( lex['wordform_matchlist'], function( tdx, tag ) {
+                               var tagstr = stringify_wordform( tag );
+                               morphoptions.push( tagstr );
+                       });
+               }
                var formtag = 'morphology_' + idx;
-               //forminput.autocomplete('search', '');
                var formstr = '';
-               if( lex['form'] ) {
+               if( 'form' in lex ) {
                        formstr = stringify_wordform( lex['form'] );
                } 
                var form_morph_elements = morph_elements( 
@@ -94,8 +95,11 @@ function morphology_form ( lexlist ) {
 }
 
 function stringify_wordform ( tag ) {
-       var elements = tag.split(' // ');
-       return elements[1] + ' // ' + elements[2];
+       if( tag ) {
+               var elements = tag.split(' // ');
+               return elements[1] + ' // ' + elements[2];
+       }
+       return ''
 }
 
 function morph_elements ( formtag, formtxt, currform, morphoptions ) {
@@ -132,6 +136,26 @@ function color_inactive ( el ) {
        });
 }
 
+function relemmatize () {
+       // Send the reading for a new lemmatization and reopen the form.
+       var reading_id = $('#reading_id').val()
+       ncpath = getReadingURL( reading_id );
+       form_values = { 
+               'normal_form': $('#reading_normal_form').val(), 
+               'relemmatize': 1 };
+       var jqjson = $.post( ncpath, form_values, function( data ) {
+               // Update the form with the return
+               if( 'id' in data ) {
+                       // We got back a good answer. Stash it
+                       readingdata[reading_id] = data;
+                       // and regenerate the morphology form.
+                       morphology_form( data['lexemes'] );
+               } else {
+                       alert("Could not relemmatize as requested: " + data['error']);
+               }
+       });
+}
+
 // Initialize the SVG once it exists
 function svgEnlargementLoaded() {
        //Give some visual evidence that we are working
@@ -668,7 +692,8 @@ $(document).ready(function () {
         $("#dialog_overlay").hide();
     }
   }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
-      if( ( ajaxSettings.type == 'POST' ) && jqXHR.status == 403 ) {
+      if( ajaxSettings.url == getTextURL('relationships') 
+       && ajaxSettings.type == 'POST' && jqXHR.status == 403 ) {
          var errobj = jQuery.parseJSON( jqXHR.responseText );
           $('#status').append( '<p class="error">Error: ' + errobj.error + '</br>The relationship cannot be made.</p>' );
       }
@@ -723,11 +748,12 @@ $(document).ready(function () {
                        $( this ).dialog( "close" );
                },
                Update: function() {
+                       $('#reading_status').empty();
                        var reading_id = $('#reading_id').val()
                        form_values = {
                                'id' : reading_id,
-                               'is_nonsense': $('#reading_is_nonsense').val(),
-                               'grammar_invalid': $('#reading_grammar_invalid').val(),
+                               'is_nonsense': $('#reading_is_nonsense').is(':checked'),
+                               'grammar_invalid': $('#reading_grammar_invalid').is(':checked'),
                                'normal_form': $('#reading_normal_form').val() };
                        // Add the morphology values
                        $('.reading_morphology').each( function() {
@@ -753,31 +779,11 @@ $(document).ready(function () {
                }
        },
        create: function() {
-               // $('#reading_relemmatize').button();
-               $('#reading_relemmatize').click( function () {
-                       // Send the reading for a new lemmatization and reopen the form.
-                       alert( "Got a click function for relemmatize button" );
-                       var reading_id = $('#reading_id').val()
-                       ncpath = getReadingURL( reading_id );
-                       form_values = { 
-                               'normal_form': $('#reading_normal_form').val(), 
-                               'relemmatize': 1 };
-                       var jqjson = $.post( ncpath, form_values, function( data ) {
-                               // Update the form with the return
-                               if( 'reading_id' in data ) {
-                                       // We got back a good answer. Stash it
-                                       readingdata[reading_id] = data;
-                                       // and regenerate the morphology form.
-                                       morphology_form( data['lexemes'] );
-                               } // else throw an error with what is in ['error']
-                       });
-                       // Prevent submit
-                       return false;
-               });
        },
        open: function() {
         $(".ui-widget-overlay").css("background", "none");
         $("#dialog_overlay").show();
+        $('#reading_status').empty();
         $("#dialog_overlay").height( $("#enlargement_container").height() );
         $("#dialog_overlay").width( $("#enlargement_container").innerWidth() );
         $("#dialog_overlay").offset( $("#enlargement_container").offset() );
@@ -785,6 +791,12 @@ $(document).ready(function () {
        close: function() {
                $("#dialog_overlay").hide();
        }
+  }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
+      if( ajaxSettings.url.lastIndexOf( getReadingURL('') ) > -1
+       && ajaxSettings.type == 'POST' && jqXHR.status == 403 ) {
+         var errobj = jQuery.parseJSON( jqXHR.responseText );
+          $('#reading_status').append( '<p class="error">Error: ' + errobj.error + '</p>' );
+      }
   });