request reading data on page load
Tara L Andrews [Fri, 4 May 2012 18:18:53 +0000 (20:18 +0200)]
lib/Text/Tradition/Collation/Reading.pm
stemmaweb/lib/stemmaweb/Controller/Relation.pm
stemmaweb/lib/stemmaweb/View/JSON.pm
stemmaweb/root/js/relationship.js

index 9d96421..c147db6 100644 (file)
@@ -144,6 +144,18 @@ has 'rank' => (
     
 ## For morphological analysis
 
+has 'grammar_invalid' => (
+       is => 'rw',
+       isa => 'Bool',
+       default => undef,
+       );
+       
+has 'is_nonsense' => (
+       is => 'rw',
+       isa => 'Bool',
+       default => 'undef',
+       );
+
 has 'normal_form' => (
        is => 'rw',
        isa => 'Str',
index 9a99e58..4809dec 100644 (file)
@@ -204,8 +204,79 @@ sub relationships :Chained('text') :PathPart :Args(0) {
                }       
        }
        $c->forward('View::JSON');
-}              
-               
+}
+
+=head2 readings
+
+ GET relation/$textid/readings
+
+Returns the list of readings defined for this text along with their metadata.
+
+=cut
+
+sub _reading_struct {
+       my( $reading ) = @_;
+       # Return a JSONable struct of the useful keys.  Keys meant to be writable
+       # have a true value; read-only keys have a false value.
+       my %read_write_keys = (
+               'id' => 0,
+               'text' => 0,
+               'is_meta' => 0,
+               'grammar_invalid' => 1,
+               'is_nonsense' => 1,
+               'normal_form' => 1,
+               'lexemes' => 1,  # special case?
+       );
+       my $struct = {};
+       map { $struct->{$_} = $reading->$_ } keys( %read_write_keys );
+       # Special case
+       $struct->{'lexemes'} = [ $reading->lexemes ];
+       return $struct;
+}
+
+sub readings :Chained('text') :PathPart :Args(0) {
+       my( $self, $c ) = @_;
+       my $tradition = delete $c->stash->{'tradition'};
+       my $collation = $tradition->collation;
+       my $m = $c->model('Directory');
+       if( $c->request->method eq 'GET' ) {
+               my $rdginfo = {};
+               foreach my $rdg ( $collation->readings ) {
+                       $rdginfo->{$rdg->id} = _reading_struct( $rdg );
+               }
+               $c->stash->{'result'} = $rdginfo;
+       }
+       $c->forward('View::JSON');
+}
+
+=head2 reading
+
+ GET relation/$textid/reading/$id
+
+Returns the list of readings defined for this text along with their metadata.
+
+ POST relation/$textid/reading/$id { request }
+Alters the reading according to the values in request. Returns 403 Forbidden if
+the alteration isn't allowed.
+
+=cut
+
+sub reading :Chained('text') :PathPart :Args(1) {
+       my( $self, $c, $reading_id ) = @_;
+       my $tradition = delete $c->stash->{'tradition'};
+       my $collation = $tradition->collation;
+       my $m = $c->model('Directory');
+       if( $c->request->method eq 'GET' ) {
+               my $rdg = $collation->reading( $reading_id );
+               $c->stash->{'result'} = $rdg ? _reading_struct( $rdg )
+                       : { 'error' => "No reading with ID $reading_id" };
+       } elsif ( $c->request->method eq 'POST' ) {
+               # TODO Update the reading if we can.
+       }
+       $c->forward('View::JSON');
+
+}
 
 =head2 end
 
index e8a9284..347c034 100644 (file)
@@ -3,6 +3,16 @@ package stemmaweb::View::JSON;
 use strict;
 use base 'Catalyst::View::JSON';
 
+use JSON::XS ();
+
+sub encode_json {
+       my( $self, $c, $data ) = @_;
+       my $json = JSON::XS->new->utf8->convert_blessed(1);
+       $json->encode( $data );
+}
+
+1;
+
 =head1 NAME
 
 stemmaweb::View::JSON - Catalyst JSON View
@@ -23,7 +33,3 @@ Tara Andrews
 
 This library is free software, you can redistribute it and/or modify
 it under the same terms as Perl itself.
-
-=cut
-
-1;
index 32869ee..ab7c3ca 100644 (file)
@@ -3,6 +3,7 @@ var svg_root = null;
 var svg_root_element = null;
 var start_element_height = 0;
 var reltypes = {};
+var readingdata = {};
 
 function getTextPath() {
     var currpath = window.location.pathname;
@@ -26,9 +27,14 @@ function getRelativePath() {
        return path_parts[0];
 }
 
-function getRelationshipURL() {
+function getTextURL( which ) {
        var path_parts = getTextPath();
-       return path_parts[0] + '/' + path_parts[1] + '/relationships';
+       return path_parts[0] + '/' + path_parts[1] + '/' + which;
+}
+
+function getReadingURL( reading_id ) {
+       var path_parts = getTextPath();
+       return path_parts[0] + '/' + path_parts[1] + '/reading/' + reading_id;
 }
 
 // Make an XML ID into a valid selector
@@ -116,7 +122,7 @@ function svgEnlargementLoaded() {
 
 function add_relations( callback_fn ) {
        var basepath = getRelativePath();
-       var textrelpath = getRelationshipURL();
+       var textrelpath = getTextURL( 'relationships' );
     $.getJSON( basepath + '/definitions', function(data) {
         var rel_types = data.types.sort();
         $.getJSON( textrelpath,
@@ -537,6 +543,10 @@ $(document).ready(function () {
     'cursor' : '-moz-grab'
   });
   
+  var rdgpath = getTextURL( 'readings' );
+  $.getJSON( rdgpath, function( data ) {
+       readingdata = data;
+  });
 
   $( "#dialog-form" ).dialog({
     autoOpen: false,
@@ -547,7 +557,7 @@ $(document).ready(function () {
       "Ok": function() {
         $('#status').empty();
         form_values = $('#collapse_node_form').serialize();
-        ncpath = getRelationshipURL();
+        ncpath = getTextURL( 'relationships' );
         $(':button :contains("Ok")').attr("disabled", true);
         var jqjson = $.post( ncpath, form_values, function(data) {
             $.each( data, function(item, source_target) { 
@@ -614,8 +624,8 @@ $(document).ready(function () {
             $( this ).dialog( "close" );
         },
         Delete: function() {
-          form_values = $('#delete_relation_form').serialize()
-          ncpath = getRelationshipURL()
+          form_values = $('#delete_relation_form').serialize();
+          ncpath = getTextURL( 'relationships' );
           var jqjson = $.ajax({ url: ncpath, data: form_values, success: function(data) {
               $.each( data, function(item, source_target) { 
                   relation_manager.remove( get_relation_id( source_target[0], source_target[1] ) );