/[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 858 - (show annotations)
Tue Dec 16 14:25:52 2008 UTC (15 years, 4 months ago) by dpavlin
File size: 4830 byte(s)
single quote cleanup
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 = shift;
52
53 my ($search,$page) = ( $self->search , $self->on_page );
54
55 sub next_page {
56 my ($self) = @_;
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 @template;
148 open(my $t, 'result.html') || die "result.html: $!";
149 while(<$t>) {
150 push @template, $_;
151 }
152 close($t);
153
154 # for each document in results
155 for my $i ( 0 ... $nres->doc_num - 1 ) {
156
157 my $rdoc = $nres->get_doc($i);
158 my $uri = attr_regex( $rdoc, '@uri' );
159 my $nr = $skip + $i + 1;
160
161 map {
162 my $l = $_;
163 $l =~ s/<%(.+?)%>/eval "$1"/ge;
164 $out .= $l;
165 } @template;
166
167 }
168
169 } else {
170 $out .= 'error: ' . $node->status;
171 }
172
173 if ($v->{page} == $v->{max_page}) {
174 $out .= $self->next_page('<br/><strong>All results shown</strong>');
175 } else {
176 $out .= $self->next_page(
177 '<br/><strong>Loading results...</strong><br/>',
178 'If you are using the scroll bar, release the mouse to see more results.'
179 );
180 }
181
182 return $out;
183
184 }
185
186 sub as_markup {
187 my ($self) = @_;
188
189 $self->add_css('static/Frey/NoPager.css');
190 $self->add_js ('static/Frey/NoPager.js');
191
192 $self->add_js(q|
193 $(document).ready( function() {
194 $.log.info('hook onchange to #search_form' );
195 $('#search_form').change( function() {
196 //logDebug('submit #search_form');
197 this.submit();
198 });
199 });
200 |);
201
202 return qq|
203 <div id="search_form_div">
204 <form id="search_form" method="get">
205
206 <input autocomplete="off" name="search" type="input" value="|, $self->search, qq|">
207 |, $self->sort, qq|
208 <input type="submit" class="submit" value="search">
209
210 <span id="status" class="note">
211 |, $v->{status}, qq|
212 </span>
213 </form>
214
215 <div style="margin-top: 3em;">
216 <!-- Dynamic Content -->
217 |, $self->results_as_markup, qq|
218
219 <!-- Back Button Content -->
220 <div style="position:absolute;top:0px;left:0px;visibility:hidden;" id="spacer">space</div>
221 <!-- footer at end of results -->
222 <div style="display:none;" id="footer">
223 Thanks for trying out no pager. Hope you like it.
224 </div>
225
226 </div>
227
228 </div>
229 |;
230 }
231
232 1;

  ViewVC Help
Powered by ViewVC 1.1.26