muck about with serialization of lexeme wordforms; allow individual lexeme addressing
[scpubgit/stemmatology.git] / stemmaweb / root / js / relationship.js
index 3c271c4..b513197 100644 (file)
@@ -42,6 +42,91 @@ function jq(myid) {
        return '#' + myid.replace(/(:|\.)/g,'\\$1');
 }
 
+// Actions for opening the reading panel
+function node_dblclick_listener( evt ) {
+       // Open the reading dialogue for the given node.
+       // First get the reading info
+       var reading_id = $(this).attr('id');
+       var reading_info = readingdata[reading_id];
+       // and then populate the dialog box with it.
+       // 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'] );
+       // Use .text as a backup for .normal_form
+       var normal_form = reading_info['normal_form'];
+       if( !normal_form ) {
+               normal_form = reading_info['text'];
+       }
+       var nfboxsize = 10;
+       if( normal_form.length > 9 ) {
+               nfboxsize = normal_form.length + 1;
+       }
+       $('#reading_normal_form').attr( 'size', nfboxsize )
+       $('#reading_normal_form').val( normal_form );
+       // Now do the morphological properties.
+       // and then open the dialog.
+       $('#morphology').empty();
+       $.each( reading_info['lexemes'], function( idx, lex ) {
+               var morphoptions = [];
+               $.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'] ) {
+                       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'];
+}
+
+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 ).text( formtxt + ': ' );
+       var forminput = $('<input/>').attr( 'id', 'reading_' + formtag ).attr( 
+               'name', 'reading_' + formtag ).attr( 'size', '50' ).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'} );
+               }
+       });
+}
+
+// Initialize the SVG once it exists
 function svgEnlargementLoaded() {
        //Give some visual evidence that we are working
        $('#loading_overlay').show();
@@ -57,8 +142,12 @@ 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'} );
-    $('#svgenlargement ellipse').dblclick( node_dblclick_listener );
+       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];
     if (!svg_g) return;
@@ -85,7 +174,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(); });
 }
 
@@ -152,8 +241,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 );
     }
   }
 
@@ -323,60 +412,6 @@ function get_edge_elements_for( ellipse ) {
   return edge_elements;
 } 
 
-function node_dblclick_listener( evt ) {
-       // Open the reading dialogue for the given node.
-       // First get the reading info
-       var reading_id = $(this).attr('id');
-       var reading_info = readingdata[reading_id];
-       // and then populate the dialog box with it.
-       // 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'] );
-       // Use .text as a backup for .normal_form
-       var normal_form = reading_info['normal_form'];
-       if( !normal_form ) {
-               normal_form = reading_info['text'];
-       }
-       $('#reading_normal_form').val( normal_form );
-       // Now do the morphological properties.
-       // and then open the dialog.
-       var form_morph_elements = [];
-       $.each( reading_info['lexemes'], function( idx, lex ) {
-               var morphoptions = [];
-               $.each( lex['wordform_matchlist'], function( tdx, tag ) {
-                       var tagstr = stringify_wordform( tag );
-                       morphoptions.push( tagstr );
-               });
-               var formtag = 'morphology_' + idx;
-               var formlabel = $('#label_morphology_0').clone();
-               formlabel.attr('id', 'label_' + formtag );
-               formlabel.text( lex['string'] + ': ' );
-               var forminput = $('#reading_morphology_0').clone();
-               forminput.attr('id', 'reading_' + formtag );
-               forminput.attr('name', 'reading_' + formtag );
-               forminput.autocomplete({ source: morphoptions, minLength: 0, disabled: false });
-               //forminput.autocomplete('search', '');
-               if( lex['form'] ) {
-                       var formstr = stringify_wordform( lex['form'] );
-                       forminput.val( formstr );
-               } else {
-                       forminput.val( '' );
-               }
-               form_morph_elements.push( formlabel, forminput, $('<br/>') );
-       });
-       $('#morphology').empty();
-       $.each( form_morph_elements, function( idx, el ) {
-               $('#morphology').append( el );
-       });
-       $('#reading-form').dialog("open");
-}
-
-function stringify_wordform ( tag ) {
-       return tag['lemma'] + ' // ' + tag['morphology'];
-}
-
 function relation_factory() {
     var self = this;
     this.color_memo = null;
@@ -566,10 +601,6 @@ $(document).ready(function () {
     'cursor' : '-moz-grab'
   });
   
-  var rdgpath = getTextURL( 'readings' );
-  $.getJSON( rdgpath, function( data ) {
-       readingdata = data;
-  });
 
   $( "#dialog-form" ).dialog({
     autoOpen: false,
@@ -710,6 +741,8 @@ $(document).ready(function () {
                $("#dialog_overlay").hide();
        }
   });
+  
+  $('#reading_relemmatize').button();
 
   $('#update_workspace_button').click( function() {
      var svg_enlargement = $('#svgenlargement').svg().svg('get').root();