/[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 966 - (show annotations)
Wed Jan 7 23:00:40 2009 UTC (15 years, 3 months ago) by dpavlin
File size: 5407 byte(s)
rest of changes to make it work, some css design
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 => 'number of items on page',
30 is => 'rw',
31 isa => 'Int',
32 default => 20,
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 => 0,
50 id => time() . rand(99),
51 };
52
53 sub next_page {
54 my $self = shift;
55 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*$/) {
72 $v->{status} = 'Enter search query';
73 return $self->next_page( $v );
74 }
75
76 $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
77 $v->{search} = $search;
78
79 my $node = new Search::Estraier::Node(%{ $self->config->{estraier} });
80
81 my $on_page = $self->on_page;
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 );
87 $cond->set_skip( $skip );
88 $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, ( $self->config->{estraier}->{depth} || 0 ) );
93
94 my $out = qq|<!-- page $page -->|;
95
96 if (defined($nres)) {
97
98 $v->{hits} = $nres->hits;
99 $v->{time} = $nres->hint('TIME');
100
101 if ($v->{hits} == 0) {
102 $v->{status} = qq{<strong>No results for your search.</strong>};
103 return $self->next_page( $v, qq|<strong>No results found.</strong>| );
104 } elsif ($nres->doc_num == 0) {
105 $v->{status} = qq{<strong>Error getting results for page $page.</strong>};
106 return $self->next_page( $v );
107 }
108
109 $v->{max_page} = int( ($nres->hits + $on_page - 1) / $on_page );
110
111 $v->{status} = qq{
112 Got <b>$v->{hits}</b> results for <tt>$v->{search}</tt>
113 in <em>$v->{time} s</em>
114 };
115
116 sub html_snippet {
117 my $text = shift || return;
118 my $out = '';
119 foreach my $s (split(/[\n\r]{2}/, $text)) {
120 $out .= ' ... ' if ($out);
121 my ($pre,$hit,$post) = split(/\n/,$s,3);
122 $hit =~ s/\t.*$//;
123 $out
124 .= $self->html_escape( $pre )
125 . '<b>'
126 . $self->html_escape( $hit )
127 . '</b>'
128 . $self->html_escape( $post )
129 ;
130 }
131 return $out;
132 }
133
134 sub attr_regex {
135 my ($rdoc,$attr) = @_;
136 my $text = $rdoc->attr( $attr );
137 return unless defined($text);
138
139 if (my $r = $self->config->{estraier}->{attr_regex}->{$attr} ) {
140 my $do = '$text =~ ' . $r . ';';
141 eval $do;
142 if ($@) {
143 warn "eval $do failed: $@\n";
144 }
145 }
146 return $text;
147 }
148
149 my $result = q|
150 <div class="result">
151 <span class="nr"><% $nr %></span>
152 <h1 title="<% $nr %>"><% attr_regex( $rdoc, '@title' ) %></h1>
153 <p>
154 <% 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
169 for my $i ( 0 ... $nres->doc_num - 1 ) {
170
171 my $rdoc = $nres->get_doc($i);
172 my $uri = attr_regex( $rdoc, '@uri' );
173 my $nr = $skip + $i + 1;
174
175 map {
176 my $l = $_;
177 $l =~ s/<%(.+?)%>/eval "$1"/ge;
178 $out .= $l;
179 } @result;
180
181 }
182
183 } else {
184 $out .= 'error: ' . $node->status;
185 }
186
187 if ($v->{page} == $v->{max_page}) {
188 $out .= $self->next_page( $v, qq|<br/><strong>All results shown</strong>|);
189 } else {
190 $out .= $self->next_page( $v, qq|
191 <br/><strong>Loading results...</strong>
192 <br/>If you are using the scroll bar, release the mouse to see more results.
193 |);
194 }
195
196 warn "# v = ", dump( $v );
197
198 return ( $out, $v->{status} ) if wantarray;
199
200 $self->wrap_in_page( 0 ); # disable <body> and status bar for ajax call
201 return $out;
202
203 }
204
205 sub as_markup {
206 my ($self) = @_;
207
208 $self->add_css('static/Frey/View/NoPager.css');
209 $self->add_js ('static/Frey/View/NoPager.js');
210
211 $self->add_js(q|
212 $(document).ready( function() {
213 $.log.info('hook onchange to #search_form' );
214 $('#search_form').change( function() {
215 //logDebug('submit #search_form');
216 this.submit();
217 });
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|
232 <div id="search_form_div">
233 <form id="search_form" method="get">
234
235 <input autocomplete="off" name="search" type="input" value="|, $self->search, qq|">
236 |, $self->sort, qq|
237 <input type="submit" class="submit" value="search">
238
239 <span id="status" class="note">
240 $status
241 </span>
242
243 </form>
244 </div>
245
246 <div id="results">
247 <!-- Dynamic Content -->
248 $results
249 </div>
250
251 |
252 );
253
254 return $html;
255 }
256
257 1;

  ViewVC Help
Powered by ViewVC 1.1.26