/[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 957 by dpavlin, Wed Jan 7 17:50:03 2009 UTC revision 1133 by dpavlin, Tue Jun 30 15:10:55 2009 UTC
# Line 2  package Frey::View::NoPager; Line 2  package Frey::View::NoPager;
2  use Moose;  use Moose;
3    
4  extends 'Frey';  extends 'Frey';
5  with 'Frey::Web';  with 'Frey::Web', 'Frey::Config', 'Frey::jQuery';
 with 'Frey::Config';  
 with 'Frey::jQuery';  
6    
7  use Search::Estraier;  use Search::Estraier;
8  use JSON::Syck;  use JSON::Syck;
9  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
10    
11  has search => (  has search => (
12            documentation => 'Search for',
13          is => 'rw',          is => 'rw',
14          isa => 'Str',          isa => 'Str',
15  #       required => 1,  #       required => 1,
16          default => '',          default => '',
17  );  );
18    
19  has 'on_page' => (      # FIXME Frey::Web->page, sigh!  has 'page' => (
20            documentation => 'display page',
21          is => 'rw',          is => 'rw',
22          isa => 'Int',          isa => 'Int',
23          default => 1,          default => 1,
24  );  );
25    
26    has 'on_page' => (
27            documentation => 'number of items on page',
28            is => 'rw',
29            isa => 'Int',
30            default => 20,
31    );
32    
33  has 'sort' => (  has 'sort' => (
34          is => 'rw',          is => 'rw',
35          isa => 'Str',          isa => 'Str',
36            default => '',
37  );  );
38    
 our $v = {  
         search => '',  
         hits => 0,  
         page => 0,  
         max_page => 0,  
         time => '',  
         id => time() . rand(99),  
 };  
   
 our $json;  
   
 sub json {  
         my ($self) = @_;  
         return  
         '<textarea id="json" style="display:none">' .  
         $self->html_escape( JSON::Syck::Dump( $v ) ) .  
         '</textarea>';  
 }  
   
39  sub results_as_markup {  sub results_as_markup {
40          my ($self) = @_;          my ($self) = @_;
41    
42          my ($search,$page) = ( $self->search , $self->on_page );          my ( $search,$page ) = ( $self->search , $self->page );
43    
44            my $v = {
45                    search => $search,
46                    page => $page,
47                    max_page => 0,
48                    id => time() . rand(99),
49            };
50    
51          sub next_page {          sub next_page {
52                  my $self = shift;                  my $self = shift;
53                  return                  my $v = shift;
54                     qq|<div id="next_page">|                  my $json = $self->html_escape( JSON::Syck::Dump( $v ) );
55                   . join("\n", @_) . $self->json()  
56                   . qq|</div>|                  my $message = join("\n", @_);
57                   ;  
58                    my $next_page = $v->{page} + 1;
59    
60                    return qq|
61                            <div id="next_page_$next_page">
62                                    $message
63                                    <textarea id="json" style="display:none">$json</textarea>
64                            </div>
65                    |
66                    ;
67          }          }
68    
69          if (! $search || $search =~ m/^\s*$/) {          if (! $search || $search =~ m/^\s*$/) {
70                  $v->{status} = 'Enter search query';                  $v->{status} = 'Enter search query';
71                  return $self->next_page;                  return $self->next_page( $v );
         }  
   
         if (! $page) {  
                 $v->{status} = 'Error: no page number?';  
                 return $self->next_page;  
72          }          }
73    
74          $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);          $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
75          $v->{search} = $search;          $v->{search} = $search;
76    
         $v->{page} = $page;  
   
77          my $node = new Search::Estraier::Node(%{ $self->config->{estraier} });          my $node = new Search::Estraier::Node(%{ $self->config->{estraier} });
78    
79          my $on_page = 30;          my $on_page = $self->on_page;
80          my $skip = ( $page - 1 ) * $on_page;          my $skip = ( $page - 1 ) * $on_page;
81    
82          my $cond = new Search::Estraier::Condition;          my $cond = new Search::Estraier::Condition;
83          $cond->set_phrase( $search );          $cond->set_phrase( $search );
84          $cond->set_max( $on_page * $page );     ## FIXME * $page is needed by hest 1.3.8          $cond->set_max( $on_page );
85          $cond->set_skip( $skip );          $cond->set_skip( $skip );
86          $cond->set_order( $self->sort ) if $self->sort;          $cond->set_order( $self->sort ) if $self->sort;
87    
88            warn "INFO: estraier for '$search' page $page with $on_page items, first $skip";
89    
90          my $nres = $node->search($cond, ( $self->config->{estraier}->{depth} || 0 ) );          my $nres = $node->search($cond, ( $self->config->{estraier}->{depth} || 0 ) );
91    
92          my $out;          my $out = qq|<!-- page $page -->|;
93    
94          if (defined($nres)) {          if (defined($nres)) {
95    
# Line 98  sub results_as_markup { Line 98  sub results_as_markup {
98    
99                  if ($v->{hits} == 0) {                  if ($v->{hits} == 0) {
100                          $v->{status} = qq{<strong>No results for your search.</strong>};                          $v->{status} = qq{<strong>No results for your search.</strong>};
101                          return $self->next_page;                          return $self->next_page( $v, qq|<strong>No results found.</strong>| );
102                  } elsif ($nres->doc_num == 0) {                  } elsif ($nres->doc_num == 0) {
103                          $v->{status} = qq{<strong>Error getting results for page $page.</strong>};                          $v->{status} = qq{<strong>Error getting results for page $page.</strong>};
104                          return $self->next_page('<strong>No results found.</strong>');                          return $self->next_page( $v );
105                  }                  }
106    
107                  $v->{max_page} = int( ($nres->hits + $on_page - 1) / $on_page );                  $v->{max_page} = int( ($nres->hits + $on_page - 1) / $on_page );
# Line 146  sub results_as_markup { Line 146  sub results_as_markup {
146    
147                  my $result = q|                  my $result = q|
148                          <div class="result">                          <div class="result">
149                          <h1><% attr_regex( $rdoc, '@title' ) %></h1>                          <span class="nr"><% $nr %></span>
150                            <h1 title="<% $nr %>"><% attr_regex( $rdoc, '@title' ) %></h1>
151                          <p>                          <p>
152                          <% html_snippet( $rdoc->snippet ) %>                          <% html_snippet( $rdoc->snippet ) %>
153                          </p>                          </p>
                         <span style="color: #888"><% $nr %></span>  
154                          <strong><% attr_regex( $rdoc, 'source' ) %></strong>                          <strong><% attr_regex( $rdoc, 'source' ) %></strong>
155                          <em>                          <em>
156                          <% attr_regex( $rdoc, '@size' ) %> bytes                          <% attr_regex( $rdoc, '@size' ) %> bytes
# Line 183  sub results_as_markup { Line 183  sub results_as_markup {
183          }          }
184    
185          if ($v->{page} == $v->{max_page}) {          if ($v->{page} == $v->{max_page}) {
186                  $out .= $self->next_page('<br/><strong>All results shown</strong>');                  $out .= $self->next_page( $v, qq|<br/><strong>All results shown</strong>|);
187          } else {          } else {
188                  $out .= $self->next_page(                  $out .= $self->next_page( $v, qq|
189                          '<br/><strong>Loading results...</strong><br/>',                          <br/><strong>Loading results...</strong>
190                          'If you are using the scroll bar, release the mouse to see more results.'                          <br/>If you are using the scroll bar, release the mouse to see more results.
191                  );                  |);
192          }          }
193    
194          $self->wrap_in_page( 0 ); # disable <body> and status bar          warn "# v = ", dump( $v );
195    
196            return ( $out, $v->{status} ) if wantarray;
197    
198            $self->wrap_in_page( 0 ); # disable <body> and status bar for ajax call
199          return $out;          return $out;
200    
201  }  }
# Line 213  sub as_markup { Line 216  sub as_markup {
216                  });                  });
217          |);          |);
218    
219            my ( $results, $status ) = $self->results_as_markup;
220    
221            $self->add_css(q|
222                    #results {
223                            margin-top: 3em;
224                            margin-bottom: 3em;
225                    }
226            |);
227    
228          my $html = join("\n",          my $html = join("\n",
229          qq|          qq|
230                  <div id="search_form_div">                  <div id="search_form_div">
# Line 223  sub as_markup { Line 235  sub as_markup {
235                                  <input type="submit" class="submit" value="search">                                  <input type="submit" class="submit" value="search">
236    
237                                  <span id="status" class="note">                                  <span id="status" class="note">
238                                  |, $v->{status}, qq|                                          $status
239                                  </span>                                  </span>
240    
241                          </form>                          </form>
242                  </div>                  </div>
243    
244                  <div style="margin-top: 3em;">                  <div id="results">
245                  <!-- Dynamic Content -->                  <!-- Dynamic Content -->
246                          |, $self->results_as_markup, qq|                          $results
   
                         <!-- Back Button Content -->  
                         <div style="position:absolute;top:0px;left:0px;visibility:hidden;" id="spacer">space</div>  
                         <!-- footer at end of results -->  
                         <div style="display:none;" id="footer">  
                                 Thanks for trying out no pager. Hope you like it.  
                         </div>  
   
247                  </div>                  </div>
248    
249          |          |
250          );          );
251    
         $self->wrap_in_page( 1 ); # FIXME results_as_markup will turn it off  
252          return $html;          return $html;
253  }  }
254    
255    __PACKAGE__->meta->make_immutable;
256    no Moose;
257    
258  1;  1;

Legend:
Removed from v.957  
changed lines
  Added in v.1133

  ViewVC Help
Powered by ViewVC 1.1.26