/[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 966 by dpavlin, Wed Jan 7 23:00:40 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 'on_page' => (      # FIXME Frey::Web->page, sigh!  has 'page' => (
22            documentation => 'display page',
23          is => 'rw',          is => 'rw',
24          isa => 'Int',          isa => 'Int',
25          default => 1,          default => 1,
26  );  );
27    
28    has 'on_page' => (
29            documentation => 'number of items on page',
30            is => 'rw',
31            isa => 'Int',
32            default => 20,
33    );
34    
35  has 'sort' => (  has 'sort' => (
36          is => 'rw',          is => 'rw',
37          isa => 'Str',          isa => 'Str',
38            default => '',
39  );  );
40    
 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>';  
 }  
   
41  sub results_as_markup {  sub results_as_markup {
42          my ($self) = @_;          my ($self) = @_;
43    
44          my ($search,$page) = ( $self->search , $self->on_page );          my ( $search,$page ) = ( $self->search , $self->page );
45    
46            my $v = {
47                    search => $search,
48                    page => $page,
49                    max_page => 0,
50                    id => time() . rand(99),
51            };
52    
53          sub next_page {          sub next_page {
54                  my $self = shift;                  my $self = shift;
55                  return                  my $v = shift;
56                     qq|<div id="next_page">|                  my $json = $self->html_escape( JSON::Syck::Dump( $v ) );
57                   . join("\n", @_) . $self->json()  
58                   . qq|</div>|                  my $message = join("\n", @_);
59                   ;  
60                    my $next_page = $v->{page} + 1;
61    
62                    return qq|
63                            <div id="next_page_$next_page">
64                                    $message
65                                    <textarea id="json" style="display:none">$json</textarea>
66                            </div>
67                    |
68                    ;
69          }          }
70    
71          if (! $search || $search =~ m/^\s*$/) {          if (! $search || $search =~ m/^\s*$/) {
72                  $v->{status} = 'Enter search query';                  $v->{status} = 'Enter search query';
73                  return $self->next_page;                  return $self->next_page( $v );
         }  
   
         if (! $page) {  
                 $v->{status} = 'Error: no page number?';  
                 return $self->next_page;  
74          }          }
75    
76          $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);          $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
77          $v->{search} = $search;          $v->{search} = $search;
78    
         $v->{page} = $page;  
   
79          my $node = new Search::Estraier::Node(%{ $self->config->{estraier} });          my $node = new Search::Estraier::Node(%{ $self->config->{estraier} });
80    
81          my $on_page = 30;          my $on_page = $self->on_page;
82          my $skip = ( $page - 1 ) * $on_page;          my $skip = ( $page - 1 ) * $on_page;
83    
84          my $cond = new Search::Estraier::Condition;          my $cond = new Search::Estraier::Condition;
85          $cond->set_phrase( $search );          $cond->set_phrase( $search );
86          $cond->set_max( $on_page * $page );     ## FIXME * $page is needed by hest 1.3.8          $cond->set_max( $on_page );
87          $cond->set_skip( $skip );          $cond->set_skip( $skip );
88          $cond->set_order( $self->sort ) if $self->sort;          $cond->set_order( $self->sort ) if $self->sort;
89    
90            warn "INFO: estraier for '$search' page $page with $on_page items, first $skip";
91    
92          my $nres = $node->search($cond, ( $self->config->{estraier}->{depth} || 0 ) );          my $nres = $node->search($cond, ( $self->config->{estraier}->{depth} || 0 ) );
93    
94          my $out;          my $out = qq|<!-- page $page -->|;
95    
96          if (defined($nres)) {          if (defined($nres)) {
97    
# Line 98  sub results_as_markup { Line 100  sub results_as_markup {
100    
101                  if ($v->{hits} == 0) {                  if ($v->{hits} == 0) {
102                          $v->{status} = qq{<strong>No results for your search.</strong>};                          $v->{status} = qq{<strong>No results for your search.</strong>};
103                          return $self->next_page;                          return $self->next_page( $v, qq|<strong>No results found.</strong>| );
104                  } elsif ($nres->doc_num == 0) {                  } elsif ($nres->doc_num == 0) {
105                          $v->{status} = qq{<strong>Error getting results for page $page.</strong>};                          $v->{status} = qq{<strong>Error getting results for page $page.</strong>};
106                          return $self->next_page('<strong>No results found.</strong>');                          return $self->next_page( $v );
107                  }                  }
108    
109                  $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 148  sub results_as_markup {
148    
149                  my $result = q|                  my $result = q|
150                          <div class="result">                          <div class="result">
151                          <h1><% attr_regex( $rdoc, '@title' ) %></h1>                          <span class="nr"><% $nr %></span>
152                            <h1 title="<% $nr %>"><% attr_regex( $rdoc, '@title' ) %></h1>
153                          <p>                          <p>
154                          <% html_snippet( $rdoc->snippet ) %>                          <% html_snippet( $rdoc->snippet ) %>
155                          </p>                          </p>
                         <span style="color: #888"><% $nr %></span>  
156                          <strong><% attr_regex( $rdoc, 'source' ) %></strong>                          <strong><% attr_regex( $rdoc, 'source' ) %></strong>
157                          <em>                          <em>
158                          <% attr_regex( $rdoc, '@size' ) %> bytes                          <% attr_regex( $rdoc, '@size' ) %> bytes
# Line 183  sub results_as_markup { Line 185  sub results_as_markup {
185          }          }
186    
187          if ($v->{page} == $v->{max_page}) {          if ($v->{page} == $v->{max_page}) {
188                  $out .= $self->next_page('<br/><strong>All results shown</strong>');                  $out .= $self->next_page( $v, qq|<br/><strong>All results shown</strong>|);
189          } else {          } else {
190                  $out .= $self->next_page(                  $out .= $self->next_page( $v, qq|
191                          '<br/><strong>Loading results...</strong><br/>',                          <br/><strong>Loading results...</strong>
192                          '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.
193                  );                  |);
194          }          }
195    
196          $self->wrap_in_page( 0 ); # disable <body> and status bar          warn "# v = ", dump( $v );
197    
198            return ( $out, $v->{status} ) if wantarray;
199    
200            $self->wrap_in_page( 0 ); # disable <body> and status bar for ajax call
201          return $out;          return $out;
202    
203  }  }
# Line 213  sub as_markup { Line 218  sub as_markup {
218                  });                  });
219          |);          |);
220    
221            my ( $results, $status ) = $self->results_as_markup;
222    
223            $self->add_css(q|
224                    #results {
225                            margin-top: 3em;
226                            margin-bottom: 3em;
227                    }
228            |);
229    
230          my $html = join("\n",          my $html = join("\n",
231          qq|          qq|
232                  <div id="search_form_div">                  <div id="search_form_div">
# Line 223  sub as_markup { Line 237  sub as_markup {
237                                  <input type="submit" class="submit" value="search">                                  <input type="submit" class="submit" value="search">
238    
239                                  <span id="status" class="note">                                  <span id="status" class="note">
240                                  |, $v->{status}, qq|                                          $status
241                                  </span>                                  </span>
242    
243                          </form>                          </form>
244                  </div>                  </div>
245    
246                  <div style="margin-top: 3em;">                  <div id="results">
247                  <!-- Dynamic Content -->                  <!-- Dynamic Content -->
248                          |, $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>  
   
249                  </div>                  </div>
250    
251          |          |
252          );          );
253    
         $self->wrap_in_page( 1 ); # FIXME results_as_markup will turn it off  
254          return $html;          return $html;
255  }  }
256    

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

  ViewVC Help
Powered by ViewVC 1.1.26