properly disable OK button
[scpubgit/stemmatology.git] / stemmaweb / root / js / relationship.js
index 7f6ed87..6d8f242 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'] );
+       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 ) {
@@ -66,46 +66,105 @@ function node_dblclick_listener( evt ) {
        $('#reading_normal_form').attr( 'size', nfboxsize )
        $('#reading_normal_form').val( normal_form );
        // Now do the morphological properties.
+       morphology_form( reading_info['lexemes'] );
        // and then open the dialog.
+       $('#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( reading_info['lexemes'], function( idx, lex ) {
+       $.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;
-               var form_morph_elements = morph_elements( formtag );
-               form_morph_elements[0].text( lex['string'] + ': ' );
-               form_morph_elements[1].autocomplete({ 
-                       source: morphoptions, 
-                       minLength: 0
-               });
-               //forminput.autocomplete('search', '');
-               if( lex['form'] ) {
-                       var formstr = stringify_wordform( lex['form'] );
-                       form_morph_elements[1].val( formstr );
+               var formstr = '';
+               if( 'form' in lex ) {
+                       formstr = stringify_wordform( lex['form'] );
                } 
+               var form_morph_elements = morph_elements( 
+                       formtag, lex['string'], formstr, morphoptions );
                $.each( form_morph_elements, function( idx, el ) {
                        $('#morphology').append( el );
                });
        });
-       $('#reading-form').dialog("open");
 }
 
 function stringify_wordform ( tag ) {
-       return tag['lemma'] + ' // ' + tag['morphology'];
+       if( tag ) {
+               var elements = tag.split(' // ');
+               return elements[1] + ' // ' + elements[2];
+       }
+       return ''
 }
 
-function morph_elements ( formtag ) {
+function morph_elements ( formtag, formtxt, currform, morphoptions ) {
+       var clicktag = '(Click to select)';
+       if ( !currform ) {
+               currform = clicktag;
+       }
        var formlabel = $('<label/>').attr( 'id', 'label_' + formtag ).attr( 
-               'for', 'reading_' + formtag );
+               'for', 'reading_' + formtag ).text( formtxt + ': ' );
        var forminput = $('<input/>').attr( 'id', 'reading_' + formtag ).attr( 
-               'name', 'reading_' + formtag ).attr( 'size', '50' );
+               'name', 'reading_' + formtag ).attr( 'size', '50' ).attr(
+               'class', 'reading_morphology' ).val( currform );
+       forminput.autocomplete({ source: morphoptions, minLength: 0     });
+       forminput.focus( function() { 
+               if( $(this).val() == clicktag ) {
+                       $(this).val('');
+               }
+               $(this).autocomplete('search', '') 
+       });
        var morphel = [ formlabel, forminput, $('<br/>') ];
        return morphel;
 }
 
+function color_inactive ( el ) {
+       var reading_id = $(el).parent().attr('id');
+       var reading_info = readingdata[reading_id];
+       // 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'} );
+               }
+       });
+}
+
+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 = { 
+               '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']);
+               }
+               $('#relemmatize_pending').hide();
+       });
+}
+
 // Initialize the SVG once it exists
 function svgEnlargementLoaded() {
        //Give some visual evidence that we are working
@@ -122,7 +181,11 @@ function svgEnlargementLoaded() {
     $('#update_workspace_button').data('locked', false);
     $('#update_workspace_button').css('background-position', '0px 44px');
     //This is essential to make sure zooming and panning works properly.
-    $('#svgenlargement ellipse').attr( {stroke:'green', fill:'#b3f36d'} );
+       var rdgpath = getTextURL( 'readings' );
+               $.getJSON( rdgpath, function( data ) {
+               readingdata = data;
+           $('#svgenlargement ellipse').each( function( i, el ) { color_inactive( el ) });
+       });
     $('#svgenlargement ellipse').parent().dblclick( node_dblclick_listener );
     var graph_svg = $('#svgenlargement svg');
     var svg_g = $('#svgenlargement svg g')[0];
@@ -150,7 +213,7 @@ function svgEnlargementLoaded() {
     var transform = 'rotate(0) scale(' + scale + ') translate(4 ' + translate + ')';
     svg_g.setAttribute('transform', transform);
     //used to calculate min and max zoom level:
-    start_element_height = $('#__START__ ellipse')[0].getBBox().height;
+    start_element_height = $('#__START__').children('ellipse')[0].getBBox().height;
     add_relations( function() { $('#loading_overlay').hide(); });
 }
 
@@ -217,8 +280,8 @@ function node_obj(ellipse) {
       self.ellipse.siblings('text').attr('class', 'noselect draggable');
     } else {
       self.ellipse.siblings('text').attr('class', '');
-      $(self.ellipse).parent().unbind('mouseenter').unbind('mouseleave').unbind('mousedown');     
-      $(self.ellipse).attr( {stroke:'green', fill:'#b3f36d'} );
+         $(self.ellipse).parent().unbind( 'mouseenter' ).unbind( 'mouseleave' ).unbind( 'mousedown' );     
+      color_inactive( self.ellipse );
     }
   }
 
@@ -577,10 +640,6 @@ $(document).ready(function () {
     'cursor' : '-moz-grab'
   });
   
-  var rdgpath = getTextURL( 'readings' );
-  $.getJSON( rdgpath, function( data ) {
-       readingdata = data;
-  });
 
   $( "#dialog-form" ).dialog({
     autoOpen: false,
@@ -588,11 +647,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] );
@@ -604,7 +663,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' );
       },
@@ -642,9 +702,11 @@ $(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");
       }
   } );
 
@@ -696,23 +758,46 @@ $(document).ready(function () {
                Cancel: function() {
                        $( this ).dialog( "close" );
                },
-               Update: function() {
-                       form_values = $('#reading_data_form').serialize();
-                       ncpath = getReadingURL();
-                       var reading_element = get_node_obj( $('#reading_data_id').val() );
-                       $(':button :contains("Update")').attr("disabled", true);
+               Update: function( evt ) {
+                       // Disable the button
+                       $(evt.target).button("disable");
+                       $('#reading_status').empty();
+                       var reading_id = $('#reading_id').val()
+                       form_values = {
+                               'id' : reading_id,
+                               '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() {
+                               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);
                        var jqjson = $.post( ncpath, form_values, function(data) {
                                $.each( data, function(key, value) { 
-                                       reading_element.data( key, value );
+                                       reading_element[key] = value;
                                });
+                               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
+                       return false;
                }
        },
-       create: function() {},
+       create: 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() );
@@ -720,7 +805,15 @@ $(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>' );
+                 $(event.target).parent().find('.ui-button').button("enable");
+      }
   });
+  
 
   $('#update_workspace_button').click( function() {
      var svg_enlargement = $('#svgenlargement').svg().svg('get').root();