remove variant graph from front page, fix stemma scaling
[scpubgit/stemmatology.git] / stemmaweb / lib / stemmaweb / Controller / Root.pm
CommitLineData
5c9ecf66 1package stemmaweb::Controller::Root;
dbcf12a6 2use Moose;
3use namespace::autoclean;
3837c155 4use Text::Tradition::Analysis qw/ run_analysis /;
5
dbcf12a6 6
7BEGIN { extends 'Catalyst::Controller' }
8
9#
10# Sets the actions in this controller to be registered with no prefix
11# so they function identically to actions created in MyApp.pm
12#
13__PACKAGE__->config(namespace => '');
14
15=head1 NAME
16
5c9ecf66 17stemmaweb::Controller::Root - Root Controller for stemmaweb
dbcf12a6 18
19=head1 DESCRIPTION
20
6b70c348 21Serves up the main container pages.
dbcf12a6 22
6b70c348 23=head1 URLs
dbcf12a6 24
25=head2 index
26
6b70c348 27The root page (/). Serves the main container page, from which the various
28components will be loaded.
dbcf12a6 29
30=cut
31
32sub index :Path :Args(0) {
33 my ( $self, $c ) = @_;
34
6b70c348 35 $c->stash->{template} = 'index.tt';
36}
37
38=head1 Elements of index page
39
40=head2 directory
41
42 GET /directory
43
44Serves a snippet of HTML that lists the available texts. Eventually this will be available texts by user.
45
46=cut
2376359f 47sub directory :Local :Args(0) {
6b70c348 48 my( $self, $c ) = @_;
3837c155 49 my $m = $c->model('Directory');
6b70c348 50 # TODO not used yet, will load user texts later
51 my $user = $c->request->param( 'user' ) || 'ALL';
98a6cab2 52 my @textlist = $m->traditionlist();
6b70c348 53 $c->stash->{texts} = \@textlist;
54 $c->stash->{template} = 'directory.tt';
55}
56
cf9626aa 57=head2 variantgraph
58
59 GET /variantgraph/$textid
60
61Returns the variant graph for the text specified at $textid, in SVG form.
62
63=cut
64
65sub variantgraph :Local :Args(1) {
66 my( $self, $c, $textid ) = @_;
67 my $m = $c->model('Directory');
b365fbae 68 my $tradition = $m->tradition( $textid );
69 my $collation = $tradition->collation;
70 my $needsave = !$collation->has_cached_svg;
cf9626aa 71 $c->stash->{'result'} = $collation->as_svg;
0a900793 72 $m->save( $tradition ); # to save generate SVG in the cache
cf9626aa 73 $c->forward('View::SVG');
74}
75
6b70c348 76=head2 alignment
77
78 GET /alignment/$textid
79
80Returns an alignment table for the text specified at $textid.
81
82=cut
83
2376359f 84sub alignment :Local :Args(1) {
6b70c348 85 my( $self, $c, $textid ) = @_;
86 my $m = $c->model('Directory');
87 my $collation = $m->tradition( $textid )->collation;
7f52eac8 88 my $alignment = $collation->alignment_table;
6b70c348 89
90 # Turn the table, so that witnesses are by column and the rows
91 # are by rank.
92 my $wits = [ map { $_->{'witness'} } @{$alignment->{'alignment'}} ];
93 my $rows;
94 foreach my $i ( 0 .. $alignment->{'length'} - 1 ) {
95 my @rankrdgs = map { $_->{'tokens'}->[$i]->{'t'} }
96 @{$alignment->{'alignment'}};
97 push( @$rows, { 'rank' => $i+1, 'readings' => \@rankrdgs } );
98 }
6b70c348 99 $c->stash->{'witnesses'} = $wits;
100 $c->stash->{'table'} = $rows;
101 $c->stash->{'template'} = 'alignment.tt';
102}
103
104=head2 stemma
105
106 GET /stemma/$textid
107 POST /stemma/$textid, { 'dot' => $dot_string }
108
109Returns an SVG representation of the stemma hypothesis for the text. If
110the URL is called with POST and a new dot string, updates the stemma and
111returns the SVG as with GET.
112
113=cut
114
2376359f 115sub stemma :Local :Args(1) {
6b70c348 116 my( $self, $c, $textid ) = @_;
117 my $m = $c->model('Directory');
118 my $tradition = $m->tradition( $textid );
119
120 if( $c->req->method eq 'POST' ) {
121 # Update the stemma
122 my $dot = $c->request->body_params->{'dot'};
123 $tradition->add_stemma( $dot );
124 $m->store( $tradition );
125 }
126
3e420a82 127 $c->stash->{'result'} = $tradition->stemma_count
91b888ed 128 ? $tradition->stemma(0)->as_svg( { size => [ 500, 375 ] } )
80dad2e3 129 : '';
6b70c348 130 $c->forward('View::SVG');
dbcf12a6 131}
132
6b70c348 133=head2 stemmadot
12720144 134
6b70c348 135 GET /stemmadot/$textid
136
137Returns the 'dot' format representation of the current stemma hypothesis.
138
139=cut
140
2376359f 141sub stemmadot :Local :Args(1) {
6b70c348 142 my( $self, $c, $textid ) = @_;
143 my $m = $c->model('Directory');
144 my $tradition = $m->tradition( $textid );
145
146 $c->response->body( $tradition->stemma->editable );
147 $c->forward('View::Plain');
148}
12720144 149
dbcf12a6 150=head2 default
151
152Standard 404 error page
153
154=cut
155
156sub default :Path {
157 my ( $self, $c ) = @_;
158 $c->response->body( 'Page not found' );
159 $c->response->status(404);
160}
161
162=head2 end
163
164Attempt to render a view, if needed.
165
166=cut
167
168sub end : ActionClass('RenderView') {}
169
170=head1 AUTHOR
171
172Tara L Andrews
173
174=head1 LICENSE
175
176This library is free software. You can redistribute it and/or modify
177it under the same terms as Perl itself.
178
179=cut
180
181__PACKAGE__->meta->make_immutable;
182
1831;