chiark / gitweb /
Further refactoring to make query_commod small
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 16 Aug 2009 13:19:35 +0000 (14:19 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 16 Aug 2009 13:19:35 +0000 (14:19 +0100)
yarrg/web/lookup
yarrg/web/qtextstring
yarrg/web/query_commod
yarrg/web/query_route

index 64860fd5f9db94e83105e83b50ad43e433b286db..1b85ecc8b9d06b9dc5ba96b14b63e4cac99501c0 100755 (executable)
@@ -162,6 +162,13 @@ die if $styles{Query} =~ m/[^a-z]/;
 
 dbw_connect($styles{Ocean});
 
+my $results_head_done=0;
+my $someresults= sub {
+       return if $results_head_done;
+       $results_head_done=1;
+       print "\n<h1>Results</h1>\n";
+};
+
 </%perl>
 <%args>
 $debug => 0
@@ -169,7 +176,16 @@ $debug => 0
 
 <hr>
 
-<& "query_$styles{Query}", %baseqf, %queryqf, %styles, quri => $quri &>
+<& "query_$styles{Query}", %baseqf, %queryqf, %styles, quri => $quri,
+    someresults => $someresults,
+    emsgokorprint => sub {
+       my ($emsg) = @_;
+       return 1 unless defined $emsg and length $emsg;
+       $someresults->();
+       print $emsg;
+       return 0;
+    }
+ &>
 
 <p>
 
index 45f2e32a75c53f9b404cc93db52c858d84d8b99b..3b0bd6b72e65b5f942ff1eff70b0786d9901c903 100644 (file)
@@ -37,6 +37,8 @@
 </%doc>
 <%args>
 $thingstring
+$emsgstore
+$perresult
 $qa => $m->caller_args(1)->{'qa'}
 </%args>
 <%perl>
@@ -94,3 +96,19 @@ window.onload= ts_Needed;
  >
 <br>
 <div id="ts_results">&nbsp;</div><br>
+
+<%perl>
+if (length $thingstring) {
+       my ($emsg,$canonstring,$results)= $m->comp('qtextstringcheck',
+               what => $thingstring,
+               ocean => $qa->{Ocean},
+               string => $stringval,
+               format => 'return'
+       );
+       $$emsgstore= $emsg;
+
+       foreach my $entry (@$results) {
+               $perresult->(@$entry);
+       }
+}
+</%perl>
index 2358b4167cc373874dd8bf959f3099a610612eeb..eddc0975d05f067358c6835a81710d97f8039b8d 100644 (file)
 <%args>
 $quri
 $commodstring => '';
+$someresults
+$emsgokorprint
 </%args>
 
-% my $qa= \%ARGS;
+<%perl>
+my $emsg;
+my ($commodname,$commodid);
 
-%#---------- textbox, user enters route as string ----------
-% if (!$qa->{Dropdowns}) {
+my $qa= \%ARGS;
+</%perl>
 
 <h1>Select commodity</h1>
 
+%#---------- textbox, user enters route as string ----------
+% if (!$qa->{Dropdowns}) {
+
 Enter commodity (abbreviations are OK):<br>
 
 <form action="<% $quri->() |h %>" method="get">
 
-<&| qtextstring, qa => $qa, thingstring => 'commodstring' &>
+<&| qtextstring, qa => $qa, thingstring => 'commodstring', emsgstore => \$emsg,
+    perresult => sub { ($commodname,$commodid)= @_; }
+ &>
  size=80
 </&>
 
+% } else { #---------- dropdowns, user selects from menus ----------
+
+Not yet implemented.
+
+% } #---------- end of dropdowns, now common middle of page code ----------
+
 <input type=submit name=submit value="Go">
-% my $ours= sub { $_[0] =~ m/^commodstring/; };
+% my $ours= sub { $_[0] =~ m/^commodstring|^commodid/; };
 <& "lookup:formhidden", ours => $ours &>
 
 </form>
 
-% } else { #---------- dropdowns, user selects from menus ----------
+%#========== results ==========
+% $emsgokorprint->($emsg) or $commodid=undef;
 
-% } #---------- end of dropdowns, now common middle of page code ----------
+% if (defined $commodid) {
+%      $someresults->();
+COMMODITY <% $commodid %> named <% $commodname |h %>
+
+% }
index 8f6d99c49d29ed353f5f2fcdf6d88dc55180afac..66dee1dd08135de011871b15d71d963f7db235af 100644 (file)
 <%args>
 $quri
 $routestring => '';
+$someresults
+$emsgokorprint
 </%args>
-<%perl>
 
+<%perl>
+my $emsg;
 my @archipelagoes;
 my @islandids;
 my %islandid2;
@@ -56,14 +59,20 @@ Enter route (islands, or archipelagoes, separated by |s or commas;
 
 <form action="<% $quri->() |h %>" method="get">
 
-<&| qtextstring, qa => $qa, thingstring => 'routestring' &>
+<&| qtextstring, qa => $qa, thingstring => 'routestring', emsgstore => \$emsg,
+    perresult => sub {
+       my ($canonname, $island, $arch) = @_;
+       push @islandids, $island;
+       push @archipelagoes, defined $island ? undef : $arch;
+    }
+ &>
  size=80
 </&>
 
 % } else { #---------- dropdowns, user selects from menus ----------
 
 <%perl>
-my ($sth,$row);;
+my ($sth,$row);
 my @archlistdata;
 my %islandlistdata;
 $islandlistdata{'none'}= [ [ "none", "Select island..." ] ];
@@ -161,36 +170,9 @@ function ms_Setarch(dd) {
 </form>
 
 <%perl>
-#========== result computations ==========
+#========== results ==========
 
-my $results_head;
-$results_head= sub {
-       print "<h1>Results</h1>\n";
-       $results_head= sub { };
-};
-
-#---------- result computation - textstring ----------
-if (!$qa->{Dropdowns}) {
-  if (length $routestring) {
-       $results_head->();
-       my ($emsg,$canonstring,$results)= $m->comp('qtextstringcheck',
-               what => 'routestring',
-               ocean => $qa->{Ocean},
-               string => $routestring,
-               format => 'return'
-       );
-       if (length $emsg) {
-               print encode_entities($emsg);
-       } else {
-               foreach my $entry (@$results) {
-                       push @islandids, $entry->[1];
-                       push @archipelagoes,
-                               defined $entry->[1] ? undef : $entry->[2];
-               } 
-       }
-  }
-
-} else { #---------- results - dropdowns ----------
+$emsgokorprint->($emsg) or @islandids=();
 
 my $argorundef= sub {
        my ($dd,$base) = @_;
@@ -207,7 +189,7 @@ for my $dd (0..$qa->{Dropdowns}-1) {
                my $ii= $islandid2{$island};
                my $iarch= $ii->{Arch};
                if ($iarch ne $arch) {
-                       $results_head->();
+                       $someresults->();
 </%perl>
  Specified archipelago <% $arch %> but
  island <% $ii->{Name} %>
@@ -220,12 +202,9 @@ for my $dd (0..$qa->{Dropdowns}-1) {
        push @islandids, $island;
 }
 
-}#---------- result processing, common stuff
 </%perl>
 
 % if (@islandids) {
-%      $results_head->();
-
+%      $someresults->();
 <& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>
-
 % }