/[Frey]/branches/no-pager/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 /branches/no-pager/lib/Frey/View/NoPager.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 743 - (show annotations)
Sat Dec 6 17:17:17 2008 UTC (15 years, 4 months ago) by dpavlin
File size: 4805 byte(s)
pass syntax check
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 'sort' => (
21 is => 'rw',
22 isa => 'Str',
23 );
24
25 has page => (
26 is => 'rw',
27 isa => 'Int',
28 default => 1,
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 my $p = {@_};
53
54 my ($search,$page) = ( $p->{search} , $p->{page} );
55
56 sub next_page {
57 my ($self) = @_;
58 return
59 qq|<div id="next_page">|
60 . join("\n", @_) . $self->json()
61 . qq|</div>|
62 ;
63 }
64
65 if (! $search || $search =~ m/^\s*$/) {
66 $v->{status} = 'Enter search query';
67 return $self->next_page;
68 }
69
70 if (! $page) {
71 $v->{status} = 'Error: no page number?';
72 return $self->next_page;
73 }
74
75 $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
76 $v->{search} = $search;
77
78 $v->{page} = $page;
79
80 my $node = new Search::Estraier::Node(%{ $self->config->{estraier} });
81
82 my $on_page = 30;
83 my $skip = ( $page - 1 ) * $on_page;
84
85 my $cond = new Search::Estraier::Condition;
86 $cond->set_phrase( $search );
87 $cond->set_max( $on_page * $page ); ## FIXME * $page is needed by hest 1.3.8
88 $cond->set_skip( $skip );
89 $cond->set_order( $p->{sort} ) if ($p->{sort});
90
91 my $nres = $node->search($cond, ( $self->config->{estraier}->{depth} || 0 ) );
92
93 my $out;
94
95 if (defined($nres)) {
96
97 $v->{hits} = $nres->hits;
98 $v->{time} = $nres->hint('TIME');
99
100 if ($v->{hits} == 0) {
101 $v->{status} = qq{<strong>No results for your search.</strong>};
102 return $self->next_page;
103 } elsif ($nres->doc_num == 0) {
104 $v->{status} = qq{<strong>Error getting results for page $page.</strong>};
105 return $self->next_page('<strong>No results found.</strong>');
106 }
107
108 $v->{max_page} = int( ($nres->hits + $on_page - 1) / $on_page );
109
110 $v->{status} = qq{
111 Got <b>$v->{hits}</b> results for <tt>$v->{search}</tt>
112 in <em>$v->{time} s</em>
113 };
114
115 sub html_snippet {
116 my $text = shift || return;
117 my $out = '';
118 foreach my $s (split(/[\n\r]{2}/, $text)) {
119 $out .= ' ... ' if ($out);
120 my ($pre,$hit,$post) = split(/\n/,$s,3);
121 $hit =~ s/\t.*$//;
122 $out
123 .= $self->html_escape( $pre )
124 . '<b>'
125 . $self->html_escape( $hit )
126 . '</b>'
127 . $self->html_escape( $post )
128 ;
129 }
130 return $out;
131 }
132
133 sub attr_regex {
134 my ($rdoc,$attr) = @_;
135 my $text = $rdoc->attr( $attr );
136 return unless defined($text);
137
138 if (my $r = $self->config->{estraier}->{attr_regex}->{$attr} ) {
139 my $do = '$text =~ ' . $r . ';';
140 eval $do;
141 if ($@) {
142 warn "eval $do failed: $@\n";
143 }
144 }
145 return $text;
146 }
147
148 my @template;
149 open(my $t, 'result.html') || die "result.html: $!";
150 while(<$t>) {
151 push @template, $_;
152 }
153 close($t);
154
155 # for each document in results
156 for my $i ( 0 ... $nres->doc_num - 1 ) {
157
158 my $rdoc = $nres->get_doc($i);
159 my $uri = attr_regex( $rdoc, '@uri' );
160 my $nr = $skip + $i + 1;
161
162 map {
163 my $l = $_;
164 $l =~ s/<%(.+?)%>/eval "$1"/ge;
165 $out .= $l;
166 } @template;
167
168 }
169
170 } else {
171 $out .= 'error: ' . $node->status;
172 }
173
174 if ($v->{page} == $v->{max_page}) {
175 $out .= $self->next_page('<br/><strong>All results shown</strong>');
176 } else {
177 $out .= $self->next_page(
178 '<br/><strong>Loading results...</strong><br/>',
179 'If you are using the scroll bar, release the mouse to see more results.'
180 );
181 }
182
183 return $out;
184
185 }
186
187 sub as_markup {
188 my ($self) = @_;
189
190 $self->add_css('static/Frey/NoPager.css');
191 $self->add_css('static/Frey/NoPager.js');
192
193 $self->add_js(qq|
194 $(document).ready( function() {
195 $.log.info('hook onchange to #search_form' );
196 $('#search_form').change( function() {
197 //logDebug('submit #search_form');
198 this.submit();
199 });
200 });
201 |);
202
203 return qq|
204 <div id="search_form_div">
205 <form id="search_form" method="get">
206
207 <input autocomplete="off" name="search" type="input" value="|, $self->search, qq|">
208 |, $self->sort, qq|
209 <input type="submit" class="submit" value="search">
210
211 <span id="status" class="note">
212 |, $v->{status}, qq|
213 </span>
214 </form>
215
216 <div style="margin-top: 3em;">
217 <!-- Dynamic Content -->
218 |, $self->results_as_markup, qq|
219
220 <!-- Back Button Content -->
221 <div style="position:absolute;top:0px;left:0px;visibility:hidden;" id="spacer">space</div>
222 <!-- footer at end of results -->
223 <div style="display:none;" id="footer">
224 Thanks for trying out no pager. Hope you like it.
225 </div>
226
227 </div>
228
229 </div>
230 |;
231 }
232
233 1;

  ViewVC Help
Powered by ViewVC 1.1.26