better handling for errors, don't send filler text to server
[scpubgit/stemmatology.git] / stemmaweb / root / js / relationship.js
index 0c34643..cd3d867 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').attr( 'checked', reading_info['is_nonsense'] );
-       $('#reading_grammar_invalid').attr( 'checked', reading_info['grammar_invalid'] );
+       toggle_checkbox( $('#reading_is_nonsense'), reading_info['is_nonsense'] );
+       toggle_checkbox( $('#reading_grammar_invalid'), reading_info['grammar_invalid'] );
        // Use .text as a backup for .normal_form
        var normal_form = reading_info['normal_form'];
        if( !normal_form ) {
@@ -71,6 +71,13 @@ function node_dblclick_listener( evt ) {
        $('#reading-form').dialog("open");
 }
 
+function toggle_checkbox( box, value ) {
+       if( value == null ) {
+               value = false;
+       }
+       box.attr('checked', value );
+}
+
 function morphology_form ( lexlist ) {
        $('#morphology').empty();
        $.each( lexlist, function( idx, lex ) {
@@ -129,15 +136,18 @@ function color_inactive ( el ) {
        // If the reading info has any non-disambiguated lexemes, color it yellow;
        // otherwise color it green.
        $(el).attr( {stroke:'green', fill:'#b3f36d'} );
-       $.each( reading_info['lexemes'], function ( idx, lex ) {
-               if( !lex['is_disambiguated'] ) {
-                       $(el).attr( {stroke:'orange', fill:'#fee233'} );
-               }
-       });
+       if( reading_info ) {
+               $.each( reading_info['lexemes'], function ( idx, lex ) {
+                       if( !lex['is_disambiguated'] || lex['is_disambiguated'] == 0 ) {
+                               $(el).attr( {stroke:'orange', fill:'#fee233'} );
+                       }
+               });
+       }
 }
 
 function relemmatize () {
        // Send the reading for a new lemmatization and reopen the form.
+       $('#relemmatize_pending').show();
        var reading_id = $('#reading_id').val()
        ncpath = getReadingURL( reading_id );
        form_values = { 
@@ -153,6 +163,7 @@ function relemmatize () {
                } else {
                        alert("Could not relemmatize as requested: " + data['error']);
                }
+               $('#relemmatize_pending').hide();
        });
 }
 
@@ -638,11 +649,11 @@ $(document).ready(function () {
     width: 290,
     modal: true,
     buttons: {
-      "Ok": function() {
+      "Ok": function( evt ) {
+       $(evt.target).button("disable");
         $('#status').empty();
         form_values = $('#collapse_node_form').serialize();
         ncpath = getTextURL( 'relationships' );
-        $(':button :contains("Ok")').attr("disabled", true);
         var jqjson = $.post( ncpath, form_values, function(data) {
             $.each( data, function(item, source_target) { 
                var source_found = get_ellipse( source_target[0] );
@@ -654,7 +665,8 @@ $(document).ready(function () {
                                        relation.data( 'note', $('#note').val()  );
                                        relation_manager.toggle_active( relation.attr('id') );
                                }
-            });
+                               $(evt.target).button("enable");
+           });
             $( "#dialog-form" ).dialog( "close" );
         }, 'json' );
       },
@@ -692,10 +704,12 @@ $(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>' );
       }
+         $(event.target).parent().find('.ui-button').button("enable");
   } );
 
   $( "#delete-form" ).dialog({
@@ -746,7 +760,10 @@ $(document).ready(function () {
                Cancel: function() {
                        $( this ).dialog( "close" );
                },
-               Update: function() {
+               Update: function( evt ) {
+                       // Disable the button
+                       $(evt.target).button("disable");
+                       $('#reading_status').empty();
                        var reading_id = $('#reading_id').val()
                        form_values = {
                                'id' : reading_id,
@@ -755,14 +772,16 @@ $(document).ready(function () {
                                'normal_form': $('#reading_normal_form').val() };
                        // Add the morphology values
                        $('.reading_morphology').each( function() {
-                               var rmid = $(this).attr('id');
-                               rmid = rmid.substring(8);
-                               form_values[rmid] = $(this).val();
+                               if( $(this).val() != '(Click to select)' ) {
+                                       var rmid = $(this).attr('id');
+                                       rmid = rmid.substring(8);
+                                       form_values[rmid] = $(this).val();
+                               }
                        });
                        // Make the JSON call
                        ncpath = getReadingURL( reading_id );
                        var reading_element = readingdata[reading_id];
-                       $(':button :contains("Update")').attr("disabled", true);
+                       // $(':button :contains("Update")').attr("disabled", true);
                        var jqjson = $.post( ncpath, form_values, function(data) {
                                $.each( data, function(key, value) { 
                                        reading_element[key] = value;
@@ -770,6 +789,7 @@ $(document).ready(function () {
                                if( $('#update_workspace_button').data('locked') == false ) {
                                        color_inactive( get_ellipse( reading_id ) );
                                }
+                               $(evt.target).button("enable");
                                $( "#reading-form" ).dialog( "close" );
                        });
                        // Re-color the node if necessary
@@ -781,13 +801,22 @@ $(document).ready(function () {
        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() );
+        $("#reading-form").parent().find('.ui-button').button("enable");
        },
        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>' );
+      }
+         $(event.target).parent().find('.ui-button').button("enable");
   });