/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 957 - (show annotations)
Wed Jan 7 17:50:03 2009 UTC (15 years, 3 months ago) by dpavlin
File size: 5395 byte(s)
port a bit more of no-pager (next page still doesn't work)
1 package Frey::View::NoPager;
2 use Moose;
3
4 extends 'Frey';
5 with 'Frey::Web';
6 with 'Frey::Config';
7 with 'Frey::jQuery';
8
9 use Search::Estraier;
10 use JSON::Syck;
11 use Data::Dump qw/dump/;
12
13 has search => (
14 is => 'rw',
15 isa => 'Str',
16 # required => 1,
17 default => '',
18 );
19
20 has 'on_page' => ( # FIXME Frey::Web->page, sigh!
21 is => 'rw',
22 isa => 'Int',
23 default => 1,
24 );
25
26 has 'sort' => (
27 is => 'rw',
28 isa => 'Str',
29 );
30
31 our $v = {
32 search => '',
33 hits => 0,
34 page => 0,
35 max_page => 0,
36 time => '',
37 id => time() . rand(99),
38 };
39
40 our $json;
41
42 sub json {
43 my ($self) = @_;
44 return
45 '<textarea id="json" style="display:none">' .
46 $self->html_escape( JSON::Syck::Dump( $v ) ) .
47 '</textarea>';
48 }
49
50 sub results_as_markup {
51 my ($self) = @_;
52
53 my ($search,$page) = ( $self->search , $self->on_page );
54
55 sub next_page {
56 my $self = shift;
57 return
58 qq|<div id="next_page">|
59 . join("\n", @_) . $self->json()
60 . qq|</div>|
61 ;
62 }
63
64 if (! $search || $search =~ m/^\s*$/) {
65 $v->{status} = 'Enter search query';
66 return $self->next_page;
67 }
68
69 if (! $page) {
70 $v->{status} = 'Error: no page number?';
71 return $self->next_page;
72 }
73
74 $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
75 $v->{search} = $search;
76
77 $v->{page} = $page;
78
79 my $node = new Search::Estraier::Node(%{ $self->config->{estraier} });
80
81 my $on_page = 30;
82 my $skip = ( $page - 1 ) * $on_page;
83
84 my $cond = new Search::Estraier::Condition;
85 $cond->set_phrase( $search );
86 $cond->set_max( $on_page * $page ); ## FIXME * $page is needed by hest 1.3.8
87 $cond->set_skip( $skip );
88 $cond->set_order( $self->sort ) if $self->sort;
89
90 my $nres = $node->search($cond, ( $self->config->{estraier}->{depth} || 0 ) );
91
92 my $out;
93
94 if (defined($nres)) {
95
96 $v->{hits} = $nres->hits;
97 $v->{time} = $nres->hint('TIME');
98
99 if ($v->{hits} == 0) {
100 $v->{status} = qq{<strong>No results for your search.</strong>};
101 return $self->next_page;
102 } elsif ($nres->doc_num == 0) {
103 $v->{status} = qq{<strong>Error getting results for page $page.</strong>};
104 return $self->next_page('<strong>No results found.</strong>');
105 }
106
107 $v->{max_page} = int( ($nres->hits + $on_page - 1) / $on_page );
108
109 $v->{status} = qq{
110 Got <b>$v->{hits}</b> results for <tt>$v->{search}</tt>
111 in <em>$v->{time} s</em>
112 };
113
114 sub html_snippet {
115 my $text = shift || return;
116 my $out = '';
117 foreach my $s (split(/[\n\r]{2}/, $text)) {
118 $out .= ' ... ' if ($out);
119 my ($pre,$hit,$post) = split(/\n/,$s,3);
120 $hit =~ s/\t.*$//;
121 $out
122 .= $self->html_escape( $pre )
123 . '<b>'
124 . $self->html_escape( $hit )
125 . '</b>'
126 . $self->html_escape( $post )
127 ;
128 }
129 return $out;
130 }
131
132 sub attr_regex {
133 my ($rdoc,$attr) = @_;
134 my $text = $rdoc->attr( $attr );
135 return unless defined($text);
136
137 if (my $r = $self->config->{estraier}->{attr_regex}->{$attr} ) {
138 my $do = '$text =~ ' . $r . ';';
139 eval $do;
140 if ($@) {
141 warn "eval $do failed: $@\n";
142 }
143 }
144 return $text;
145 }
146
147 my $result = q|
148 <div class="result">
149 <h1><% attr_regex( $rdoc, '@title' ) %></h1>
150 <p>
151 <% html_snippet( $rdoc->snippet ) %>
152 </p>
153 <span style="color: #888"><% $nr %></span>
154 <strong><% attr_regex( $rdoc, 'source' ) %></strong>
155 <em>
156 <% attr_regex( $rdoc, '@size' ) %> bytes
157 </em>
158 <% attr_regex( $rdoc, '@mdate' ) %>
159 <br/><a href="<% $uri %>"><tt><% $uri %></tt></a>
160 </div>
161 |;
162
163 my @result = split(/\n/, $result);
164 warn "# result template has ", $#result + 1, " lines";
165
166 # for each document in results
167 for my $i ( 0 ... $nres->doc_num - 1 ) {
168
169 my $rdoc = $nres->get_doc($i);
170 my $uri = attr_regex( $rdoc, '@uri' );
171 my $nr = $skip + $i + 1;
172
173 map {
174 my $l = $_;
175 $l =~ s/<%(.+?)%>/eval "$1"/ge;
176 $out .= $l;
177 } @result;
178
179 }
180
181 } else {
182 $out .= 'error: ' . $node->status;
183 }
184
185 if ($v->{page} == $v->{max_page}) {
186 $out .= $self->next_page('<br/><strong>All results shown</strong>');
187 } else {
188 $out .= $self->next_page(
189 '<br/><strong>Loading results...</strong><br/>',
190 '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
195
196 return $out;
197
198 }
199
200 sub as_markup {
201 my ($self) = @_;
202
203 $self->add_css('static/Frey/View/NoPager.css');
204 $self->add_js ('static/Frey/View/NoPager.js');
205
206 $self->add_js(q|
207 $(document).ready( function() {
208 $.log.info('hook onchange to #search_form' );
209 $('#search_form').change( function() {
210 //logDebug('submit #search_form');
211 this.submit();
212 });
213 });
214 |);
215
216 my $html = join("\n",
217 qq|
218 <div id="search_form_div">
219 <form id="search_form" method="get">
220
221 <input autocomplete="off" name="search" type="input" value="|, $self->search, qq|">
222 |, $self->sort, qq|
223 <input type="submit" class="submit" value="search">
224
225 <span id="status" class="note">
226 |, $v->{status}, qq|
227 </span>
228
229 </form>
230 </div>
231
232 <div style="margin-top: 3em;">
233 <!-- Dynamic Content -->
234 |, $self->results_as_markup, qq|
235
236 <!-- Back Button Content -->
237 <div style="position:absolute;top:0px;left:0px;visibility:hidden;" id="spacer">space</div>
238 <!-- footer at end of results -->
239 <div style="display:none;" id="footer">
240 Thanks for trying out no pager. Hope you like it.
241 </div>
242
243 </div>
244
245 |
246 );
247
248 $self->wrap_in_page( 1 ); # FIXME results_as_markup will turn it off
249 return $html;
250 }
251
252 1;

  ViewVC Help
Powered by ViewVC 1.1.26