allow relationship colors in SVG download; work around inability to close Download...
[scpubgit/stemmaweb.git] / root / js / componentload.js
index 07fb93d..888bf69 100644 (file)
@@ -1,6 +1,7 @@
 // Global state variables
 var selectedTextID;
 var selectedTextInfo;
+var selectedTextEditable;
 var selectedStemmaID = -1;
 var stemmata = [];
 
@@ -29,6 +30,7 @@ function refreshDirectory () {
 // view pane. Calls load_textinfo.
 function loadTradition( textid, textname, editable ) {
        selectedTextID = textid;
+       selectedTextEditable = editable;
     // First insert the placeholder image and register an error handler
     $('#textinfo_load_status').empty();
     $('#stemma_graph').empty();
@@ -69,7 +71,7 @@ function loadTradition( textid, textname, editable ) {
        } else {
                selectedStemmaID = -1;
                }
-               load_stemma( selectedStemmaID, editable );
+               load_stemma( selectedStemmaID );
        // Set up the relationship mapper button
                $('#run_relater').attr( 'action', _get_url([ "relation", textid ]) );
                // Set up the download button
@@ -110,31 +112,31 @@ function load_textinfo() {
 }      
 
 // Enable / disable the appropriate buttons for paging through the stemma.
-function show_stemmapager ( editable ) {
+function show_stemmapager () {
       $('.pager_left_button').unbind('click').addClass( 'greyed_out' );
       $('.pager_right_button').unbind('click').addClass( 'greyed_out' );
       if( selectedStemmaID > 0 ) {
               $('.pager_left_button').click( function () {
-                      load_stemma( selectedStemmaID - 1, editable );
+                      load_stemma( selectedStemmaID - 1, selectedTextEditable );
               }).removeClass( 'greyed_out' );
       }       
       if( selectedStemmaID + 1 < stemmata.length ) {
               $('.pager_right_button').click( function () {
-                      load_stemma( selectedStemmaID + 1, editable );
+                      load_stemma( selectedStemmaID + 1, selectedTextEditable );
               }).removeClass( 'greyed_out' );
       }
 }
 
 // Load a given stemma SVG into the stemmagraph box.
-function load_stemma( idx, editable ) {
+function load_stemma( idx ) {
        // Load the stemma at idx
        selectedStemmaID = idx;
-       show_stemmapager( editable );
+       show_stemmapager( selectedTextEditable );
        $('#open_stemma_edit').hide();
        $('#run_stexaminer').hide();
        $('#stemma_identifier').empty();
        // Add the relevant Stemweb functionality
-       if( editable ) {
+       if( selectedTextEditable ) {
                if( selectedTextInfo.stemweb_jobid == 0 ) {
                        $('#open_stemweb_ui').show();
                        $('#query_stemweb_ui').hide();
@@ -146,7 +148,7 @@ function load_stemma( idx, editable ) {
        if( idx > -1 ) {
                // Load the stemma and its properties
                var stemmadata = stemmata[idx];
-               if( editable ) {
+               if( selectedTextEditable ) {
                        $('#open_stemma_edit').show();
                }
                if( stemmadata.directed ) {
@@ -227,20 +229,53 @@ function loadSVG(svgData) {
 }
 
 function set_stemma_interactive( svg_element ) {
-    $( "#root_tree_dialog_button_ok" ).click( function() {
-        // AJAX call goes here
-        } );
-    $.each( $( 'ellipse', svg_element ), function(index) {
-        $(this).click( function(evt) {
-            var dialog = $( '#root_tree_dialog' );
-            dialog.css( 'top', evt.pageY );
-            dialog.css( 'left', evt.pageX );
-            dialog.show();
-            root_tree_dialog_timeout = setTimeout( function() { $( '#root_tree_dialog' ).hide() }, 3000 );
-        } );
-        $(this).hover( function() { $(this).addClass( 'stemma_node_highlight' ) }, function() { $(this).removeClass( 'stemma_node_highlight' ) } );
-    } );   
+       if( selectedTextEditable ) {
+               $( "#root_tree_dialog_button_ok" ).click( function() {
+                       var requrl = _get_url([ "stemmaroot", selectedTextID, selectedStemmaID ]);
+                       var targetnode = $('#root_tree_dialog').data( 'selectedNode' );
+                       $.post( requrl, { root: targetnode }, function (data) {
+                               // Reload the new stemma
+                               stemmata[selectedStemmaID] = data;
+                               load_stemma( selectedStemmaID );
+                               // Put away the dialog
+                               $('#root_tree_dialog').data( 'selectedNode', null ).hide();
+                       } );
+               } ).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
+                       if( ajaxSettings.url.indexOf( 'stemmaroot' ) > -1 
+                               && ajaxSettings.type == 'POST' ) {
+                               display_error( jqXHR, $("#stemma_load_status") );
+                       }
+               } );
+               // TODO Clear error at some appropriate point
+               $.each( $( 'ellipse', svg_element ), function(index) {
+                       var ellipse = $(this);
+                       var g = ellipse.parent( 'g' );
+                       g.click( function(evt) {
+                               if( typeof root_tree_dialog_timeout !== 'undefined' ) { clearTimeout( root_tree_dialog_timeout ) };
+                               g.unbind( 'mouseleave' );
+                               var dialog = $( '#root_tree_dialog' );
+                               // Note which node triggered the dialog
+                               dialog.data( 'selectedNode', g.attr('id') );
+                               // Position the dialog
+                               dialog.hide();
+                               dialog.css( 'top', evt.pageY + 3 );
+                               dialog.css( 'left', evt.pageX + 3 );
+                               dialog.show();
+                               root_tree_dialog_timeout = setTimeout( function() {
+                                       $( '#root_tree_dialog' ).data( 'selectedNode', null ).hide();
+                                       ellipse.removeClass( 'stemma_node_highlight' );
+                                       g.mouseleave( function() { ellipse.removeClass( 'stemma_node_highlight' ) } );
+                               }, 3000 );
+                       } );
+                       g.mouseenter( function() { 
+                               $( 'ellipse.stemma_node_highlight' ).removeClass( 'stemma_node_highlight' );
+                               ellipse.addClass( 'stemma_node_highlight' ) 
+                       } );
+                       g.mouseleave( function() { ellipse.removeClass( 'stemma_node_highlight' ) } );
+               } );
+       }
 }
+
 // General-purpose error-handling function.
 // TODO make sure this gets used throughout, where appropriate.
 function display_error( jqXHR, el ) {
@@ -373,7 +408,7 @@ function _get_url( els ) {
        return basepath + els.join('/');
 }
 
-
+// TODO Attach unified ajaxError handler to document
 $(document).ready( function() {
        // See if we have the browser functionality we need
        // TODO Also think of a test for SVG readiness
@@ -537,52 +572,58 @@ $(document).ready( function() {
                        // populate the form.
                        var algorithmTypes = {};
                        var algorithmArgs = {};
-                       $.each( stemwebAlgorithms, function( i, o ) {
-                               if( o.model === 'algorithms.algorithm' ) {
-                                       // it's an algorithm.
-                                       algorithmTypes[ o.pk ] = o.fields;
-                               } else if( o.model == 'algorithms.algorithmarg' && o.fields.external ) {
-                                       // it's an option for an algorithm that we should display.
-                                       algorithmArgs[ o.pk ] = o.fields;
-                               }
-                       });
-                       $.each( algorithmTypes, function( pk, fields ) {
-                               var algopt = $('<option>').attr( 'value', pk ).append( fields.name );
-                               $('#stemweb_algorithm').append( algopt );
-                       });
-                       // Set up the relevant options for whichever algorithm is chosen.
-                       // "key" -> form name, option ID "stemweb_$key_opt"
-                       // "name" -> form label
-                       $('#stemweb_algorithm').change( function() {
-                               var pk = $(this).val();
-                               $('#stemweb_runtime_options').empty();
-                               $.each( algorithmTypes[pk].args, function( i, apk ) {
-                                       var argInfo = algorithmArgs[apk];
-                                       if( argInfo ) {
-                                               // Make the element ID
-                                               var optId = 'stemweb_' + argInfo.key + '_opt';
-                                               // Make the label
-                                               var optLabel = $('<label>').attr( 'for', optId )
-                                                       .append( argInfo.name + ": " );
-                                               var optCtrl;
-                                               var argType = argInfo.value;
-                                               if( argType === 'positive_integer' ) {
-                                                       // Make it an input field of smallish size.
-                                                       optCtrl = $('<input>').attr( 'size', 4 );
-                                               } else if ( argType === 'boolean' ) {
-                                                       // Make it a checkbox.
-                                                       optCtrl = $('<checkbox>');
-                                               }
-                                               // Add the name and element ID
-                                               optCtrl.attr( 'name', argInfo.key ).attr( 'id', optId );
-                                               // Append the label and the option itself to the form.
-                                               $('#stemweb_runtime_options').append( optLabel )
-                                                       .append( optCtrl ).append( $('<br>') );
+                       var requrl = _get_url([ "stemweb", "available" ]);
+                       $.getJSON( requrl, function( data ) {
+                               $.each( data, function( i, o ) {
+                                       if( o.model === 'algorithms.algorithm' ) {
+                                               // it's an algorithm.
+                                               algorithmTypes[ o.pk ] = o.fields;
+                                       } else if( o.model == 'algorithms.algorithmarg' && o.fields.external ) {
+                                               // it's an option for an algorithm that we should display.
+                                               algorithmArgs[ o.pk ] = o.fields;
                                        }
                                });
+                               // TODO if it is an empty object, disable Stemweb entirely.
+                               if( !jQuery.isEmptyObject( algorithmTypes ) ) {
+                                       $.each( algorithmTypes, function( pk, fields ) {
+                                               var algopt = $('<option>').attr( 'value', pk ).append( fields.name );
+                                               $('#stemweb_algorithm').append( algopt );
+                                       });
+                                       // Set up the relevant options for whichever algorithm is chosen.
+                                       // "key" -> form name, option ID "stemweb_$key_opt"
+                                       // "name" -> form label
+                                       $('#stemweb_algorithm').change( function() {
+                                               var pk = $(this).val();
+                                               $('#stemweb_runtime_options').empty();
+                                               $.each( algorithmTypes[pk].args, function( i, apk ) {
+                                                       var argInfo = algorithmArgs[apk];
+                                                       if( argInfo ) {
+                                                               // Make the element ID
+                                                               var optId = 'stemweb_' + argInfo.key + '_opt';
+                                                               // Make the label
+                                                               var optLabel = $('<label>').attr( 'for', optId )
+                                                                       .append( argInfo.name + ": " );
+                                                               var optCtrl;
+                                                               var argType = argInfo.value;
+                                                               if( argType === 'positive_integer' ) {
+                                                                       // Make it an input field of smallish size.
+                                                                       optCtrl = $('<input>').attr( 'size', 4 );
+                                                               } else if ( argType === 'boolean' ) {
+                                                                       // Make it a checkbox.
+                                                                       optCtrl = $('<checkbox>');
+                                                               }
+                                                               // Add the name and element ID
+                                                               optCtrl.attr( 'name', argInfo.key ).attr( 'id', optId );
+                                                               // Append the label and the option itself to the form.
+                                                               $('#stemweb_runtime_options').append( optLabel )
+                                                                       .append( optCtrl ).append( $('<br>') );
+                                                       }
+                                               });
+                                       });
+                                       $('#stemweb_algorithm').change();
+                               }
                        });
                        // Prime the initial options
-                       $('#stemweb_algorithm').change();
                },
                open: function(evt) {
                        $('#stemweb_run_status').empty();
@@ -595,6 +636,32 @@ $(document).ready( function() {
        }
        });
                
+       // Set up the download dialog
+       $('#download-dialog').dialog({
+               autoOpen: false,
+               height: 150,
+               width: 300,
+               modal: true,
+               buttons: {
+                       Download: function (evt) {
+                               var dlurl = _get_url([ "download", $('#download_tradition').val(), $('#download_format').val() ]);
+                               window.location = dlurl;
+                       },
+                       Done: function() {
+                               $('#download-dialog').dialog('close');
+                       }
+               },
+               open: function() {
+                       $('#download_tradition').attr('value', selectedTextID );
+               },
+       }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
+               $(event.target).parent().find('.ui-button').button("enable");
+       if( ajaxSettings.url.indexOf( 'download' ) > -1 
+               && ajaxSettings.type == 'POST' ) {
+                       display_error( jqXHR, $("#download_status") );
+       }
+       });
+
        $('#upload-collation-dialog').dialog({
                autoOpen: false,
                height: 360,