add more robust download functionality; convert to new API in Stexaminer::graphsvg
Tara L Andrews [Mon, 20 Jan 2014 22:16:06 +0000 (23:16 +0100)]
lib/stemmaweb/Controller/Root.pm
lib/stemmaweb/Controller/Stexaminer.pm
lib/stemmaweb/View/CSV.pm [new file with mode: 0644]
lib/stemmaweb/View/GraphML.pm
lib/stemmaweb/View/SVG.pm
lib/stemmaweb/View/TSV.pm [new file with mode: 0644]
root/js/componentload.js
root/src/index.tt
t/view_CSV.t [new file with mode: 0644]
t/view_TSV.t [new file with mode: 0644]

index bc86c30..29e1414 100644 (file)
@@ -513,26 +513,31 @@ sub stemmaroot :Local :Args(2) {
 
 =head2 download
 
- GET /download/$textid
+ GET /download/$textid/$format
  
-Returns the full XML definition of the tradition and its stemmata, if any.
+Returns a file for download of the tradition in the requested format.
  
 =cut
 
-sub download :Local :Args(1) {
-       my( $self, $c, $textid ) = @_;
+sub download :Local :Args(2) {
+       my( $self, $c, $textid, $format ) = @_;
        my $tradition = $c->model('Directory')->tradition( $textid );
        unless( $tradition ) {
                return _json_error( $c, 404, "No tradition with ID $textid" );
        }
        my $ok = _check_permission( $c, $tradition );
        return unless $ok;
+
+       my $outmethod = "as_" . lc( $format );
+       my $view = "View::$format";
+       $c->stash->{'name'} = $tradition->name();
+       $c->stash->{'download'} = 1;
        try {
-               $c->stash->{'result'} = $tradition->collation->as_graphml();
+               $c->stash->{'result'} = $tradition->collation->$outmethod();
        } catch( Text::Tradition::Error $e ) {
                return _json_error( $c, 500, $e->message );
        }
-       $c->forward('View::GraphML');
+       $c->forward( $view );
 }
 
 ####################
index ff4f69b..a7b8af8 100644 (file)
@@ -157,9 +157,7 @@ sub graphsvg :Local {
        my( $self, $c ) = @_;
        my $dot = $c->request->param('dot');
        my @layerwits = $c->request->param('layerwits[]');
-       open my $stemma_fh, '<', \$dot;
-       binmode( $stemma_fh, ':encoding(UTF-8)' );
-       my $tempstemma = Text::Tradition::Stemma->new( 'dot' => $stemma_fh );
+       my $tempstemma = Text::Tradition::Stemma->new( 'dot' => $dot );
        my $svgopts = {};
        if( @layerwits ) {
                $svgopts->{'layerwits'} = \@layerwits;
diff --git a/lib/stemmaweb/View/CSV.pm b/lib/stemmaweb/View/CSV.pm
new file mode 100644 (file)
index 0000000..e39c9f1
--- /dev/null
@@ -0,0 +1,40 @@
+package stemmaweb::View::CSV;
+use Moose;
+use namespace::autoclean;
+
+extends 'Catalyst::View';
+
+sub process {
+        my( $self, $c ) = @_;
+        $c->res->content_type( 'text/csv' );
+        $c->res->content_encoding( 'UTF-8' );
+        $c->res->header( 'Content-Disposition', 
+               sprintf( "attachment; filename=\"%s.csv\"", $c->stash->{name} ) );
+        $c->res->output( $c->stash->{result} );
+}
+
+=head1 NAME
+
+stemmaweb::View::CSV - Catalyst View
+
+=head1 DESCRIPTION
+
+Catalyst View.
+
+
+=encoding utf8
+
+=head1 AUTHOR
+
+Tara L Andrews
+
+=head1 LICENSE
+
+This library is free software. You can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+__PACKAGE__->meta->make_immutable;
+
+1;
index 3560dfc..5178bcd 100644 (file)
@@ -7,6 +7,10 @@ sub process {
        my( $self, $c ) = @_;
        $c->res->content_type( 'application/graphml+xml' );
        $c->res->content_encoding( 'UTF-8' );
+       if( $c->stash->{download} ) {
+               $c->res->header( 'Content-Disposition', 
+                       sprintf( "attachment; filename=\"%s.xml\"", $c->stash->{name} ) );
+       }
        $c->res->output( $c->stash->{result} );
 }
 
index b8e4673..13419e3 100644 (file)
@@ -2,13 +2,16 @@ package stemmaweb::View::SVG;
 
 use strict;
 use base 'Catalyst::View';
-use Encode qw( decode_utf8 );
 
 sub process {
-        my( $self, $c ) = @_;
-        $c->res->content_type( 'image/svg+xml' );
-        $c->res->content_encoding( 'UTF-8' );
-        $c->res->output( decode_utf8( $c->stash->{result} ) );
+       my( $self, $c ) = @_;
+       $c->res->content_type( 'image/svg+xml' );
+       $c->res->content_encoding( 'UTF-8' );
+       if( $c->stash->{download} ) {
+               $c->res->header( 'Content-Disposition', 
+                       sprintf( "attachment; filename=\"%s.svg\"", $c->stash->{name} ) );
+       }
+       $c->res->output( $c->stash->{result} );
 }
 
 1;
diff --git a/lib/stemmaweb/View/TSV.pm b/lib/stemmaweb/View/TSV.pm
new file mode 100644 (file)
index 0000000..261a8dd
--- /dev/null
@@ -0,0 +1,40 @@
+package stemmaweb::View::TSV;
+use Moose;
+use namespace::autoclean;
+
+extends 'Catalyst::View';
+
+sub process {
+        my( $self, $c ) = @_;
+        $c->res->content_type( 'text/tab-separated-values' );
+        $c->res->content_encoding( 'UTF-8' );
+        $c->res->header( 'Content-Disposition', 
+               sprintf( "attachment; filename=\"%s.tsv\"", $c->stash->{name} ) );
+        $c->res->output( $c->stash->{result} );
+}
+
+=head1 NAME
+
+stemmaweb::View::TSV - Catalyst View
+
+=head1 DESCRIPTION
+
+Catalyst View.
+
+
+=encoding utf8
+
+=head1 AUTHOR
+
+Tara L Andrews
+
+=head1 LICENSE
+
+This library is free software. You can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+__PACKAGE__->meta->make_immutable;
+
+1;
index 3f2f264..cd9307f 100644 (file)
@@ -636,6 +636,33 @@ $(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;
+                               $('download-dialog').dialog('close');
+                       },
+                       Cancel: 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,
index 884a68e..924a82d 100644 (file)
@@ -91,11 +91,12 @@ var textOnLoad = "[% withtradition %]";
               <span id='relatebutton_label'>View collation and relationships</span>
             </div>
           </form>
-          <a id="dl_tradition" href="" download="file.xml">
-            <div class="button" id="download_button">
-              <span id='dlbutton_label'>Download tradition as XML</span>
+          <form id="dl_tradition" action="" method="GET" name="run_downloader">
+            <div class="button" id="download_button"
+               onClick="$('#download-dialog').dialog('open');">
+              <span id='dlbutton_label'>Download tradition</span>
             </div>
-          </a>
+          </form>
       </div>
       <div id="stemma_load_status"></div>
       <div id="stemma_graph"></div>
@@ -176,6 +177,25 @@ var textOnLoad = "[% withtradition %]";
       </div>
     </div>
     
+    <!-- Data download dialog box -->
+    <div id="download-dialog" title="Download tradition data">
+      <div id="download_container">
+       <form id="download_form">
+               <input id="download_tradition" type="hidden" name="tradition"/><br/>
+               <label for="download_format">Choose a format for download: </label>
+               <select id="download_format" name="format">
+                       <option value="GraphML">Native XML format</option>
+                       <option value="CSV">Comma-separated values (collation only)</option>
+                       <option value="TSV">Tab-separated values (collation only)</option>
+                       <option value="SVG">SVG graph display (collation and relationships)</option>                            
+                       <!-- option value="tei_ps" -->
+                       <!-- option value="tei_dea" -->
+               </select>
+       </form>
+       <div id="download_status"></div>
+      </div>
+    </div>    
+    
     <!-- File upload dialog box -->
     <div id="upload-collation-dialog" title="Upload a collation">
       <div id="upload_container">
diff --git a/t/view_CSV.t b/t/view_CSV.t
new file mode 100644 (file)
index 0000000..75e00bf
--- /dev/null
@@ -0,0 +1,8 @@
+use strict;
+use warnings;
+use Test::More;
+
+
+BEGIN { use_ok 'stemmaweb::View::CSV' }
+
+done_testing();
diff --git a/t/view_TSV.t b/t/view_TSV.t
new file mode 100644 (file)
index 0000000..66c2441
--- /dev/null
@@ -0,0 +1,8 @@
+use strict;
+use warnings;
+use Test::More;
+
+
+BEGIN { use_ok 'stemmaweb::View::TSV' }
+
+done_testing();