many small bugfixes to display and server logic
Tara L Andrews [Sun, 2 Sep 2012 22:17:23 +0000 (00:17 +0200)]
stemmaweb/lib/stemmaweb/Controller/Root.pm
stemmaweb/root/js/componentload.js
stemmaweb/root/src/index.tt

index 494f5c1..9d18c67 100644 (file)
@@ -185,25 +185,40 @@ sub textinfo :Local :Args(1) {
                # Handle changes to owner-accessible parameters
                my $m = $c->model('Directory');
                my $changed;
-               # Handle scalar params
-               foreach my $param ( qw/ name language / ) {
-                       if( exists $params->{$param} ) {
-                               my $newval = delete $params->{$param};
-                               unless( $tradition->$param eq $newval ) {
-                                       try {
-                                               $tradition->$param( $newval );
-                                       } catch {
-                                               return _json_error( $c, 500, "Error setting $param to $newval" );
-                                       }
+               # Handle name param - easy
+               if( exists $params->{name} ) {
+                       my $newname = delete $params->{name};
+                       unless( $tradition->name eq $newname ) {
+                               try {
+                                       $tradition->name( $newname );
                                        $changed = 1;
+                               } catch {
+                                       return _json_error( $c, 500, "Error setting name to $newname" );
                                }
                        }
                }
+               # Handle language param, making Default => null
+               my $langval = delete $params->{language} || 'Default';
+               unless( $tradition->language eq $langval ) {
+                       try {
+                               $tradition->language( $langval );
+                               $changed = 1;
+                       } catch {
+                               return _json_error( $c, 500, "Error setting language to $langval" );
+                       }
+               }
+
                # Handle our boolean
+               my $ispublic = $tradition->public;
                if( delete $params->{'public'} ) {  # if it's any true value...
                        $tradition->public( 1 );
+                       $changed = 1 unless $ispublic;
+               } else {  # the checkbox was unchecked, ergo it should not be public
+                       $tradition->public( 0 );
+                       $changed = 1 if $ispublic;
                }
-               # Handle ownership changes
+               
+               # Handle ownership change
                my $newuser;
                if( exists $params->{'owner'} ) {
                        # Only admins can update user / owner
@@ -213,9 +228,9 @@ sub textinfo :Local :Args(1) {
                                        return _json_error( $c, 403, 
                                                "Only admin users can change tradition ownership" );
                                }
-                               $newuser = $m->lookup_user( $params->{'owner'} );
+                               $newuser = $m->find_user({ username => $newownerid });
                                unless( $newuser ) {
-                                       return _json_error( $c, 500, "No such user " . $params->{'owner'} );
+                                       return _json_error( $c, 500, "No such user " . $newownerid );
                                }
                                $newuser->add_tradition( $tradition );
                                $changed = 1;
@@ -300,6 +315,7 @@ sub stemma :Local :Args(2) {
                                if( $stemmaid eq 'n' ) {
                                        # We are adding a new stemma.
                                        $stemma = $tradition->add_stemma( 'dot' => $dot );
+                                       $stemmaid = $tradition->stemma_count - 1;
                                } elsif( $stemmaid < $tradition->stemma_count ) {
                                        # We are updating an existing stemma.
                                        $stemma = $tradition->stemma( $stemmaid );
@@ -321,13 +337,32 @@ sub stemma :Local :Args(2) {
        
        # For a GET or a successful POST request, return the SVG representation
        # of the stemma in question, if any.
-       $c->log->debug( "Received Accept header: " . $c->req->header('Accept') );
        if( !$stemma && $tradition->stemma_count > $stemmaid ) {
                $stemma = $tradition->stemma( $stemmaid );
        }
-       $c->stash->{'result'} = $stemma 
-               ? $stemma->as_svg( { size => [ 500, 375 ] } ) : '';
-       $c->forward('View::SVG');
+       my $stemma_xml = $stemma ? $stemma->as_svg( { size => [ 500, 375 ] } ) : '';
+       # What was requested, XML or JSON?
+       my $return_view = 'SVG';
+       if( my $accept_header = $c->req->header('Accept') ) {
+               $c->log->debug( "Received Accept header: $accept_header" );
+               foreach my $type ( split( /,\s*/, $accept_header ) ) {
+                       # If we were first asked for XML, return SVG
+                       last if $type =~ /^(application|text)\/xml$/;
+                       # If we were first asked for JSON, return JSON
+                       if( $type eq 'application/json' ) {
+                               $return_view = 'JSON';
+                               last;
+                       }
+               }
+       }
+       if( $return_view eq 'SVG' ) {
+               $c->stash->{'result'} = $stemma_xml;
+               $c->forward('View::SVG');
+       } else { # JSON
+               $stemma_xml =~ s/\n/ /mg;
+               $c->stash->{'result'} = { 'stemmaid' => $stemmaid, 'stemmasvg' => $stemma_xml };
+               $c->forward('View::JSON');
+       }
 }
 
 =head2 stemmadot
index 25ff0aa..99da7bd 100644 (file)
@@ -15,13 +15,13 @@ function loadTradition( textid, textname, editable ) {
     
     // Hide the functionality that is irrelevant
     if( editable ) {
-       $('#add_new_stemma').show();
-       $('#edit_current_stemma').show();
-       $('#edit_textinfo').show();
+       $('#open_stemma_add').show();
+       $('#open_stemma_edit').show();
+       $('#open_textinfo_edit').show();
     } else {
-       $('#add_new_stemma').hide();
-       $('#edit_current_stemma').hide();
-       $('#edit_textinfo').hide();
+       $('#open_stemma_add').hide();
+       $('#open_stemma_edit').hide();
+       $('#open_textinfo_edit').hide();
     }
 
     // Then get and load the actual content.
@@ -79,13 +79,18 @@ function load_stemma( idx ) {
 }
 
 function display_error( jqXHR, el ) {
-       var errobj = jQuery.parseJSON( jqXHR.responseText );
-       var msg;
-       if( errobj ) {
-               msg = "An error occurred: " + errobj.error;
+       var errmsg;
+       if( jqXHR.responseText == "" ) {
+               errmsg = "perhaps the server went down?"
        } else {
-               msg = "An error occurred; perhaps the server went down?"
+               var errobj;
+               try {
+                       errobj = jQuery.parseJSON( jqXHR.responseText );
+                       errmsg = errobj.error;
+               } catch ( parse_err ) {
+                       errmsg = "something went wrong on the server."
+               }
        }
-       var msghtml = $('<span>').attr('class', 'error').text( msg );
+       var msghtml = $('<span>').attr('class', 'error').text( "An error occurred: " + errmsg );
        $(el).empty().append( msghtml ).show();
 }
\ No newline at end of file
index b3cdb32..6ed683e 100644 (file)
@@ -47,6 +47,7 @@ $(document).ready( function() {
                modal: true,
                buttons: {
                        Save: function (evt) {
+                               $("#edit_textinfo_status").empty();
                                $(evt.target).button("disable");
                                var requrl = "[% c.uri_for( '/textinfo' ) %]/" + selectedTextID;
                                var reqparam = $('#edit_textinfo').serialize();
@@ -69,7 +70,12 @@ $(document).ready( function() {
                        // edit_(name, language, public, owner)
                        $.each([ 'name', 'language', 'owner' ], function( idx, k ) {
                                var fname = '#edit_' + k;
-                               $(fname).val( selectedTextInfo[k] );
+                               // Special case: language Default is basically language null
+                               if( k == 'language' && selectedTextInfo[k] == 'Default' ) {
+                                       $(fname).val( "" );
+                               } else {
+                                       $(fname).val( selectedTextInfo[k] );
+                               }
                        });
                        if( selectedTextInfo['public'] == true ) {
                                $('#edit_public').attr('checked','true');
@@ -94,6 +100,7 @@ $(document).ready( function() {
                modal: true,
                buttons: {
                        Save: function (evt) {
+                               $("#edit_stemma_status").empty();
                                $(evt.target).button("disable");
                                var stemmaseq = $('#stemmaseq').val();
                                var requrl = "[% c.uri_for( '/stemma' ) %]/" + selectedTextID + "/" + stemmaseq;
@@ -104,21 +111,15 @@ $(document).ready( function() {
                                $.post( requrl, reqparam, function (data) {
                                        // We received a stemma SVG string in return. 
                                        // Update the current stemma sequence number
-                                       if( stemmaseq == 'n' ) {
-                                               selectedStemmaID = stemmata.length;
-                                       } else {
-                                               selectedStemmaID = stemmaseq;
-                                       }
-                                       // Strip the carriage returns from the answer
-                                       var newsvg = data.replace(/(\r\n|\n|\r)/gm," ");
+                                       selectedStemmaID = data.stemmaid;
                                        // Stash the answer in our SVG array
-                                       stemmata[selectedStemmaID] = newsvg;
+                                       stemmata[selectedStemmaID] = data.stemmasvg;
                                        // Display the new stemma
                                        load_stemma( selectedStemmaID );
                                        // Reenable the button and close the form
-                                       $(evt.target).button("disable");
+                                       $(evt.target).button("enable");
                                        $('#stemma-edit-dialog').dialog('close');
-                               }, 'xml' );
+                               }, 'json' );
                        },
                        Cancel: function() {
                                $('#stemma-edit-dialog').dialog('close');
@@ -271,7 +272,7 @@ $(document).ready( function() {
                <input id="edit_public" type="checkbox" name="public"/><br/>
 [% IF c.user_exists -%]
 [% IF c.user.get_object.is_admin -%]
-               <label for="edit_owner">Publicly viewable: </label>
+               <label for="edit_owner">Tradition owner: </label>
                <input id="edit_owner" type="text" size="30" name="owner"/><br/>
 [% END -%]
 [% END -%]