/[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 763 by dpavlin, Tue Dec 9 20:31:25 2008 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 => '',
19  );  );
20    
21  our $v = {  has 'page' => (
22          search => '',          documentation => 'display page',
23          hits => 0,          is => 'rw',
24          page => 0,          isa => 'Int',
25          max_page => 0,          default => 1,
26          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>';  
 }  
   
 sub sort_order {  
         my $out;  
   
         my $sort = $q->param('sort');  
27    
28          $out .= '<select name="sort" id="sort">';  has 'on_page' => (
29            documentation => 'number of items on page',
30            is => 'rw',
31            isa => 'Int',
32            default => 20,
33    );
34    
35          foreach my $s (@{ $config->{estraier}->{order} }) {  has 'sort' => (
36                  my ($text,$value) = %{$s};          is => 'rw',
37                  $out .= qq{<option value="$value"} .          isa => 'Str',
38                          ( $sort eq $value ? ' selected' : '' ) .          default => '',
39                          qq{>$text</option>};  );
         }  
40    
41          $out .= '</select>';  sub results_as_markup {
42  }          my ($self) = @_;
43    
44  sub get_results {          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 => 0,
50                    id => time() . rand(99),
51            };
52    
53          sub next_page {          sub next_page {
54                  return '<div id="next_page">' .                  my $self = shift;
55                          join("\n", @_) . $self->json() . '</div>';                  my $v = shift;
56                    my $json = $self->html_escape( JSON::Syck::Dump( $v ) );
57    
58                    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 next_page();                  return $self->next_page( $v );
         }  
   
         if (! $page) {  
                 $v->{status} = 'Error: no page number?';  
                 return 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    
79          $v->{page} = $page;          my $node = new Search::Estraier::Node(%{ $self->config->{estraier} });
80    
81          my $node = new Search::Estraier::Node(%{ $config->{estraier} });          my $on_page = $self->on_page;
   
         my $on_page = 30;  
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( $p->{sort} ) if ($p->{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, ( $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 100  sub get_results { Line 100  sub get_results {
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 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 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 120  sub get_results { Line 120  sub get_results {
120                                  $out .= ' ... ' if ($out);                                  $out .= ' ... ' if ($out);
121                                  my ($pre,$hit,$post) = split(/\n/,$s,3);                                  my ($pre,$hit,$post) = split(/\n/,$s,3);
122                                  $hit =~ s/\t.*$//;                                  $hit =~ s/\t.*$//;
123                                  $out .=                                  $out
124                                          $q->escapeHTML( $pre || '' ) . '<b>' .                                   .= $self->html_escape( $pre )
125                                          $q->escapeHTML( $hit || '' ) . '</b>' .                                   . '<b>'
126                                          $q->escapeHTML( $post || '');                                   . $self->html_escape( $hit )
127                                     . '</b>'
128                                     . $self->html_escape( $post )
129                                     ;
130                          }                          }
131                          return $out;                          return $out;
132                  }                  }
# Line 133  sub get_results { Line 136  sub get_results {
136                          my $text = $rdoc->attr( $attr );                          my $text = $rdoc->attr( $attr );
137                          return unless defined($text);                          return unless defined($text);
138    
139                          if (my $r = $config->{estraier}->{attr_regex}->{$attr} ) {                          if (my $r = $self->config->{estraier}->{attr_regex}->{$attr} ) {
140                                  my $do = '$text =~ ' . $r . ';';                                  my $do = '$text =~ ' . $r . ';';
141                                  eval $do;                                  eval $do;
142                                  if ($@) {                                  if ($@) {
# Line 143  sub get_results { Line 146  sub get_results {
146                          return $text;                          return $text;
147                  }                  }
148    
149                  my @template;                  my $result = q|
150                  open(my $t, 'result.html')  || die "result.html: $!";                          <div class="result">
151                  while(<$t>) {                          <span class="nr"><% $nr %></span>
152                          push @template, $_;                          <h1 title="<% $nr %>"><% attr_regex( $rdoc, '@title' ) %></h1>
153                  }                          <p>
154                  close($t);                          <% html_snippet( $rdoc->snippet ) %>
155                            </p>
156                            <strong><% attr_regex( $rdoc, 'source' ) %></strong>
157                            <em>
158                            <% attr_regex( $rdoc, '@size' ) %> bytes
159                            </em>
160                            <% attr_regex( $rdoc, '@mdate' ) %>
161                            <br/><a href="<% $uri %>"><tt><% $uri %></tt></a>
162                            </div>
163                    |;
164    
165                    my @result = split(/\n/, $result);
166                    warn "# result template has ", $#result + 1, " lines";
167    
168                  # for each document in results                  # for each document in results
169                  for my $i ( 0 ... $nres->doc_num - 1 ) {                  for my $i ( 0 ... $nres->doc_num - 1 ) {
# Line 161  sub get_results { Line 176  sub get_results {
176                                  my $l = $_;                                  my $l = $_;
177                                  $l =~ s/<%(.+?)%>/eval "$1"/ge;                                  $l =~ s/<%(.+?)%>/eval "$1"/ge;
178                                  $out .= $l;                                  $out .= $l;
179                          } @template;                          } @result;
180    
181                  }                  }
182    
# Line 170  sub get_results { Line 185  sub get_results {
185          }          }
186    
187          if ($v->{page} == $v->{max_page}) {          if ($v->{page} == $v->{max_page}) {
188                  $out .= 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 .= 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          return $out;          warn "# v = ", dump( $v );
197    
198  }          return ( $out, $v->{status} ) if wantarray;
199    
200  sub snippet_as_markup {          $self->wrap_in_page( 0 ); # disable <body> and status bar for ajax call
201          my ($self) = @_;          return $out;
202    
203          $self->get_results(  }
                 search => $q->param('search') || '',  
                 page => $q->param('page') || 0,  
                 sort => $q->param('sort') || undef,  
         );  
 };  
204    
205  sub as_markup {  sub as_markup {
206          my ($self) = @_;          my ($self) = @_;
207    
208          my $get_results = $self->get_results(          $self->add_css('static/Frey/View/NoPager.css');
209                  search => $q->param('search') || '',          $self->add_js ('static/Frey/View/NoPager.js');
                 page => 1,  
                 sort => $q->param('sort') || undef,  
         );  
   
         $self->add_css('static/Frey/NoPager.css');  
         $self->add_css('static/Frey/NoPager.js');  
210    
211          $self->add_js(qq|          $self->add_js(q|
212                  $(document).ready( function() {                  $(document).ready( function() {
213                          $.log.info('hook onchange to #search_form' );                          $.log.info('hook onchange to #search_form' );
214                          $('#search_form').change( function() {                          $('#search_form').change( function() {
# Line 214  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",
231          qq|          qq|
232                  <div id="search_form_div">                  <div id="search_form_div">
233                          <form id="search_form" method="get">                          <form id="search_form" method="get">
234                                    
235                          <input autocomplete="off" name="search" type="input" value="<% $q->param('search') %>">                                  <input autocomplete="off" name="search" type="input" value="|, $self->search, qq|">
236                          <input type="submit" class="submit" value="search">                                  |, $self->sort, qq|
237                                    <input type="submit" class="submit" value="search">
238    
239                                    <span id="status" class="note">
240                                            $status
241                                    </span>
242    
243                          <span id="status" class="note">                          </form>
244                          |, $v->{status}, qq|                  </div>
                         </span>  
                 </form>  
245    
246                  <div style="margin-top: 3em;">                  <div id="results">
247                  <!-- Dynamic Content -->                  <!-- Dynamic Content -->
248                          |, $get_results, 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          </div>          |
252          |;          );
253    
254            return $html;
255  }  }
256    
257  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26