/[Frey]/trunk/lib/Frey/View/NoPager.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/Frey/View/NoPager.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 765 by dpavlin, Tue Dec 9 20:31:27 2008 UTC revision 963 by dpavlin, Wed Jan 7 20:31:51 2009 UTC
# Line 11  use JSON::Syck; Line 11  use JSON::Syck;
11  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
12    
13  has search => (  has search => (
14            documentation => 'Search for',
15          is => 'rw',          is => 'rw',
16          isa => 'Str',          isa => 'Str',
17          required => 1,  #       required => 1,
18          default => '',          default => '',
19  );  );
20    
21  has 'sort' => (  has 'page' => (
22            documentation => 'display page',
23          is => 'rw',          is => 'rw',
24          isa => 'Str',          isa => 'Int',
25            default => 1,
26  );  );
27    
28  has page => (  has 'on_page' => (
29            documentation => 'numer of items on page',
30          is => 'rw',          is => 'rw',
31          isa => 'Int',          isa => 'Int',
32          default => 1,          default => 30,
33  );  );
34    
35  our $v = {  has 'sort' => (
36          search => '',          is => 'rw',
37          hits => 0,          isa => 'Str',
38          page => 0,          default => '',
39          max_page => 0,  );
         time => '',  
         id => time() . rand(99),  
 };  
   
 our $json;  
40    
41  sub json {  sub results_as_markup {
42          my ($self) = @_;          my ($self) = @_;
         return  
         '<textarea id="json" style="display:none">' .  
         $self->html_escape( JSON::Syck::Dump( $v ) ) .  
         '</textarea>';  
 }  
43    
44  sub results_as_markup {          my ( $search,$page ) = ( $self->search , $self->page );
         my $self = shift;  
         my $p = {@_};  
45    
46          my ($search,$page) = ( $p->{search} , $p->{page} );          my $v = {
47                    search => $search,
48                    page => $page,
49                    max_page => $page,
50                    id => time() . rand(99),
51            };
52    
53          sub next_page {          sub next_page {
54                  my ($self) = @_;                  my $self = shift;
55                  return                  my $json = $self->html_escape( JSON::Syck::Dump( $v ) );
56                     qq|<div id="next_page">|  
57                   . join("\n", @_) . $self->json()                  my $message = join("\n", @_);
58                   . qq|</div>|  
59                   ;                  # <textarea id="json" style="display:none">
60                    return qq|
61                            <div id="next_page">
62                                    $message
63                                    <textarea id="json">
64                                    $json
65                                    </textarea>
66                            </div>
67                    |
68                    ;
69          }          }
70    
71          if (! $search || $search =~ m/^\s*$/) {          if (! $search || $search =~ m/^\s*$/) {
# Line 67  sub results_as_markup { Line 73  sub results_as_markup {
73                  return $self->next_page;                  return $self->next_page;
74          }          }
75    
         if (! $page) {  
                 $v->{status} = 'Error: no page number?';  
                 return $self->next_page;  
         }  
   
76          $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);          $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
         $v->{search} = $search;  
   
         $v->{page} = $page;  
77    
78          my $node = new Search::Estraier::Node(%{ $self->config->{estraier} });          my $node = new Search::Estraier::Node(%{ $self->config->{estraier} });
79    
80          my $on_page = 30;          my $on_page = $self->on_page;
81          my $skip = ( $page - 1 ) * $on_page;          my $skip = ( $page - 1 ) * $on_page;
82    
83          my $cond = new Search::Estraier::Condition;          my $cond = new Search::Estraier::Condition;
84          $cond->set_phrase( $search );          $cond->set_phrase( $search );
85          $cond->set_max( $on_page * $page );     ## FIXME * $page is needed by hest 1.3.8          $cond->set_max( $on_page * $page );     ## FIXME * $page is needed by hest 1.3.8
86          $cond->set_skip( $skip );          $cond->set_skip( $skip );
87          $cond->set_order( $p->{sort} ) if ($p->{sort});          $cond->set_order( $self->sort ) if $self->sort;
88    
89            warn "INFO: estraier for '$search' page $page with $on_page items, first $skip";
90    
91          my $nres = $node->search($cond, ( $self->config->{estraier}->{depth} || 0 ) );          my $nres = $node->search($cond, ( $self->config->{estraier}->{depth} || 0 ) );
92    
# Line 145  sub results_as_markup { Line 145  sub results_as_markup {
145                          return $text;                          return $text;
146                  }                  }
147    
148                  my @template;                  my $result = q|
149                  open(my $t, 'result.html')  || die "result.html: $!";                          <div class="result">
150                  while(<$t>) {                          <h1><% attr_regex( $rdoc, '@title' ) %></h1>
151                          push @template, $_;                          <p>
152                  }                          <% html_snippet( $rdoc->snippet ) %>
153                  close($t);                          </p>
154                            <span style="color: #888"><% $nr %></span>
155                            <strong><% attr_regex( $rdoc, 'source' ) %></strong>
156                            <em>
157                            <% attr_regex( $rdoc, '@size' ) %> bytes
158                            </em>
159                            <% attr_regex( $rdoc, '@mdate' ) %>
160                            <br/><a href="<% $uri %>"><tt><% $uri %></tt></a>
161                            </div>
162                    |;
163    
164                    my @result = split(/\n/, $result);
165                    warn "# result template has ", $#result + 1, " lines";
166    
167                  # for each document in results                  # for each document in results
168                  for my $i ( 0 ... $nres->doc_num - 1 ) {                  for my $i ( 0 ... $nres->doc_num - 1 ) {
# Line 163  sub results_as_markup { Line 175  sub results_as_markup {
175                                  my $l = $_;                                  my $l = $_;
176                                  $l =~ s/<%(.+?)%>/eval "$1"/ge;                                  $l =~ s/<%(.+?)%>/eval "$1"/ge;
177                                  $out .= $l;                                  $out .= $l;
178                          } @template;                          } @result;
179    
180                  }                  }
181    
# Line 180  sub results_as_markup { Line 192  sub results_as_markup {
192                  );                  );
193          }          }
194    
195            warn "# v = ", dump( $v );
196    
197            return ( $out, $v->{status} ) if wantarray;
198    
199            $self->wrap_in_page( 0 ); # disable <body> and status bar for ajax call
200          return $out;          return $out;
201    
202  }  }
# Line 187  sub results_as_markup { Line 204  sub results_as_markup {
204  sub as_markup {  sub as_markup {
205          my ($self) = @_;          my ($self) = @_;
206    
207          $self->add_css('static/Frey/NoPager.css');          $self->add_css('static/Frey/View/NoPager.css');
208          $self->add_css('static/Frey/NoPager.js');          $self->add_js ('static/Frey/View/NoPager.js');
209    
210          $self->add_js(qq|          $self->add_js(q|
211                  $(document).ready( function() {                  $(document).ready( function() {
212                          $.log.info('hook onchange to #search_form' );                          $.log.info('hook onchange to #search_form' );
213                          $('#search_form').change( function() {                          $('#search_form').change( function() {
# Line 200  sub as_markup { Line 217  sub as_markup {
217                  });                  });
218          |);          |);
219    
220          return qq|          my ( $results, $status ) = $self->results_as_markup;
221    
222            my $html = join("\n",
223            qq|
224                  <div id="search_form_div">                  <div id="search_form_div">
225                          <form id="search_form" method="get">                          <form id="search_form" method="get">
226                                    
227                          <input autocomplete="off" name="search" type="input" value="|, $self->search, qq|">                                  <input autocomplete="off" name="search" type="input" value="|, $self->search, qq|">
228                          |, $self->sort, qq|                                  |, $self->sort, qq|
229                          <input type="submit" class="submit" value="search">                                  <input type="submit" class="submit" value="search">
230    
231                          <span id="status" class="note">                                  <span id="status" class="note">
232                          |, $v->{status}, qq|                                          $status
233                          </span>                                  </span>
234                  </form>  
235                            </form>
236                    </div>
237    
238                  <div style="margin-top: 3em;">                  <div style="margin-top: 3em;">
239                  <!-- Dynamic Content -->                  <!-- Dynamic Content -->
240                          |, $self->results_as_markup, qq|                          $results
241    
242                          <!-- Back Button Content -->                          <!-- Back Button Content -->
243                          <div style="position:absolute;top:0px;left:0px;visibility:hidden;" id="spacer">space</div>                          <div style="position:absolute;top:0px;left:0px;visibility:hidden;" id="spacer">space</div>
# Line 226  sub as_markup { Line 248  sub as_markup {
248    
249                  </div>                  </div>
250    
251          </div>          |
252          |;          );
253    
254            return $html;
255  }  }
256    
257  1;  1;

Legend:
Removed from v.765  
changed lines
  Added in v.963

  ViewVC Help
Powered by ViewVC 1.1.26