/[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 963 - (show annotations)
Wed Jan 7 20:31:51 2009 UTC (15 years, 3 months ago) by dpavlin
File size: 5539 byte(s)
massaged and refactored Frey::View::NoPager
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 documentation => 'Search for',
15 is => 'rw',
16 isa => 'Str',
17 # required => 1,
18 default => '',
19 );
20
21 has 'page' => (
22 documentation => 'display page',
23 is => 'rw',
24 isa => 'Int',
25 default => 1,
26 );
27
28 has 'on_page' => (
29 documentation => 'numer of items on page',
30 is => 'rw',
31 isa => 'Int',
32 default => 30,
33 );
34
35 has 'sort' => (
36 is => 'rw',
37 isa => 'Str',
38 default => '',
39 );
40
41 sub results_as_markup {
42 my ($self) = @_;
43
44 my ( $search,$page ) = ( $self->search , $self->page );
45
46 my $v = {
47 search => $search,
48 page => $page,
49 max_page => $page,
50 id => time() . rand(99),
51 };
52
53 sub next_page {
54 my $self = shift;
55 my $json = $self->html_escape( JSON::Syck::Dump( $v ) );
56
57 my $message = join("\n", @_);
58
59 # <textarea id="json" style="display:none">
60 return qq|
61 <div id="next_page">
62 $message
63 <textarea id="json">
64 $json
65 </textarea>
66 </div>
67 |
68 ;
69 }
70
71 if (! $search || $search =~ m/^\s*$/) {
72 $v->{status} = 'Enter search query';
73 return $self->next_page;
74 }
75
76 $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
77
78 my $node = new Search::Estraier::Node(%{ $self->config->{estraier} });
79
80 my $on_page = $self->on_page;
81 my $skip = ( $page - 1 ) * $on_page;
82
83 my $cond = new Search::Estraier::Condition;
84 $cond->set_phrase( $search );
85 $cond->set_max( $on_page * $page ); ## FIXME * $page is needed by hest 1.3.8
86 $cond->set_skip( $skip );
87 $cond->set_order( $self->sort ) if $self->sort;
88
89 warn "INFO: estraier for '$search' page $page with $on_page items, first $skip";
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 $result = q|
149 <div class="result">
150 <h1><% attr_regex( $rdoc, '@title' ) %></h1>
151 <p>
152 <% html_snippet( $rdoc->snippet ) %>
153 </p>
154 <span style="color: #888"><% $nr %></span>
155 <strong><% attr_regex( $rdoc, 'source' ) %></strong>
156 <em>
157 <% attr_regex( $rdoc, '@size' ) %> bytes
158 </em>
159 <% attr_regex( $rdoc, '@mdate' ) %>
160 <br/><a href="<% $uri %>"><tt><% $uri %></tt></a>
161 </div>
162 |;
163
164 my @result = split(/\n/, $result);
165 warn "# result template has ", $#result + 1, " lines";
166
167 # for each document in results
168 for my $i ( 0 ... $nres->doc_num - 1 ) {
169
170 my $rdoc = $nres->get_doc($i);
171 my $uri = attr_regex( $rdoc, '@uri' );
172 my $nr = $skip + $i + 1;
173
174 map {
175 my $l = $_;
176 $l =~ s/<%(.+?)%>/eval "$1"/ge;
177 $out .= $l;
178 } @result;
179
180 }
181
182 } else {
183 $out .= 'error: ' . $node->status;
184 }
185
186 if ($v->{page} == $v->{max_page}) {
187 $out .= $self->next_page('<br/><strong>All results shown</strong>');
188 } else {
189 $out .= $self->next_page(
190 '<br/><strong>Loading results...</strong><br/>',
191 'If you are using the scroll bar, release the mouse to see more results.'
192 );
193 }
194
195 warn "# v = ", dump( $v );
196
197 return ( $out, $v->{status} ) if wantarray;
198
199 $self->wrap_in_page( 0 ); # disable <body> and status bar for ajax call
200 return $out;
201
202 }
203
204 sub as_markup {
205 my ($self) = @_;
206
207 $self->add_css('static/Frey/View/NoPager.css');
208 $self->add_js ('static/Frey/View/NoPager.js');
209
210 $self->add_js(q|
211 $(document).ready( function() {
212 $.log.info('hook onchange to #search_form' );
213 $('#search_form').change( function() {
214 //logDebug('submit #search_form');
215 this.submit();
216 });
217 });
218 |);
219
220 my ( $results, $status ) = $self->results_as_markup;
221
222 my $html = join("\n",
223 qq|
224 <div id="search_form_div">
225 <form id="search_form" method="get">
226
227 <input autocomplete="off" name="search" type="input" value="|, $self->search, qq|">
228 |, $self->sort, qq|
229 <input type="submit" class="submit" value="search">
230
231 <span id="status" class="note">
232 $status
233 </span>
234
235 </form>
236 </div>
237
238 <div style="margin-top: 3em;">
239 <!-- Dynamic Content -->
240 $results
241
242 <!-- Back Button Content -->
243 <div style="position:absolute;top:0px;left:0px;visibility:hidden;" id="spacer">space</div>
244 <!-- footer at end of results -->
245 <div style="display:none;" id="footer">
246 Thanks for trying out no pager. Hope you like it.
247 </div>
248
249 </div>
250
251 |
252 );
253
254 return $html;
255 }
256
257 1;

  ViewVC Help
Powered by ViewVC 1.1.26