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

Annotation of /branches/no-pager/lib/Frey/View/NoPager.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 741 - (hide annotations)
Sat Dec 6 16:35:43 2008 UTC (15 years, 5 months ago) by dpavlin
File size: 5176 byte(s)
initial import of no pager code from
http://blog.rot13.org/2006/08/fun_with_jquery.html
1 dpavlin 741 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     );
18    
19     our $v = {
20     search => '',
21     hits => 0,
22     page => 0,
23     max_page => 0,
24     time => '',
25     id => time() . rand(99),
26     };
27    
28     our $json;
29    
30     sub json {
31     my ($self) = @_;
32     return
33     '<textarea id="json" style="display:none">' .
34     $self->html_escape( JSON::Syck::Dump( $v ) ) .
35     '</textarea>';
36     }
37    
38     sub sort_order {
39     my $out;
40    
41     my $sort = $q->param('sort');
42    
43     $out .= '<select name="sort" id="sort">';
44    
45     foreach my $s (@{ $config->{estraier}->{order} }) {
46     my ($text,$value) = %{$s};
47     $out .= qq{<option value="$value"} .
48     ( $sort eq $value ? ' selected' : '' ) .
49     qq{>$text</option>};
50     }
51    
52     $out .= '</select>';
53     }
54    
55     sub get_results {
56     my $self = shift;
57     my $p = {@_};
58    
59     my ($search,$page) = ( $p->{search} , $p->{page} );
60    
61     sub next_page {
62     return '<div id="next_page">' .
63     join("\n", @_) . $self->json() . '</div>';
64     }
65    
66     if (! $search || $search =~ m/^\s*$/) {
67     $v->{status} = 'Enter search query';
68     return next_page();
69     }
70    
71     if (! $page) {
72     $v->{status} = 'Error: no page number?';
73     return next_page();
74     }
75    
76     $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
77     $v->{search} = $search;
78    
79     $v->{page} = $page;
80    
81     my $node = new Search::Estraier::Node(%{ $config->{estraier} });
82    
83     my $on_page = 30;
84     my $skip = ( $page - 1 ) * $on_page;
85    
86     my $cond = new Search::Estraier::Condition;
87     $cond->set_phrase( $search );
88     $cond->set_max( $on_page * $page ); ## FIXME * $page is needed by hest 1.3.8
89     $cond->set_skip( $skip );
90     $cond->set_order( $p->{sort} ) if ($p->{sort});
91    
92     my $nres = $node->search($cond, ( $config->{estraier}->{depth} || 0 ) );
93    
94     my $out;
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 next_page();
104     } elsif ($nres->doc_num == 0) {
105     $v->{status} = qq{<strong>Error getting results for page $page.</strong>};
106     return next_page('<strong>No results found.</strong>');
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     $q->escapeHTML( $pre || '' ) . '<b>' .
125     $q->escapeHTML( $hit || '' ) . '</b>' .
126     $q->escapeHTML( $post || '');
127     }
128     return $out;
129     }
130    
131     sub attr_regex {
132     my ($rdoc,$attr) = @_;
133     my $text = $rdoc->attr( $attr );
134     return unless defined($text);
135    
136     if (my $r = $config->{estraier}->{attr_regex}->{$attr} ) {
137     my $do = '$text =~ ' . $r . ';';
138     eval $do;
139     if ($@) {
140     warn "eval $do failed: $@\n";
141     }
142     }
143     return $text;
144     }
145    
146     my @template;
147     open(my $t, 'result.html') || die "result.html: $!";
148     while(<$t>) {
149     push @template, $_;
150     }
151     close($t);
152    
153     # for each document in results
154     for my $i ( 0 ... $nres->doc_num - 1 ) {
155    
156     my $rdoc = $nres->get_doc($i);
157     my $uri = attr_regex( $rdoc, '@uri' );
158     my $nr = $skip + $i + 1;
159    
160     map {
161     my $l = $_;
162     $l =~ s/<%(.+?)%>/eval "$1"/ge;
163     $out .= $l;
164     } @template;
165    
166     }
167    
168     } else {
169     $out .= 'error: ' . $node->status;
170     }
171    
172     if ($v->{page} == $v->{max_page}) {
173     $out .= next_page('<br/><strong>All results shown</strong>');
174     } else {
175     $out .= next_page(
176     '<br/><strong>Loading results...</strong><br/>',
177     'If you are using the scroll bar, release the mouse to see more results.'
178     );
179     }
180    
181     return $out;
182    
183     }
184    
185     sub snippet_as_markup {
186     my ($self) = @_;
187    
188     $self->get_results(
189     search => $q->param('search') || '',
190     page => $q->param('page') || 0,
191     sort => $q->param('sort') || undef,
192     );
193     };
194    
195     sub as_markup {
196     my ($self) = @_;
197    
198     my $get_results = $self->get_results(
199     search => $q->param('search') || '',
200     page => 1,
201     sort => $q->param('sort') || undef,
202     );
203    
204     $self->add_css('static/Frey/NoPager.css');
205     $self->add_css('static/Frey/NoPager.js');
206    
207     $self->add_js(qq|
208     $(document).ready( function() {
209     $.log.info('hook onchange to #search_form' );
210     $('#search_form').change( function() {
211     //logDebug('submit #search_form');
212     this.submit();
213     });
214     });
215     |);
216    
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="<% $q->param('search') %>">
222     <input type="submit" class="submit" value="search">
223    
224     <span id="status" class="note">
225     |, $v->{status}, qq|
226     </span>
227     </form>
228    
229     <div style="margin-top: 3em;">
230     <!-- Dynamic Content -->
231     |, $get_results, qq|
232    
233     <!-- Back Button Content -->
234     <div style="position:absolute;top:0px;left:0px;visibility:hidden;" id="spacer">space</div>
235     <!-- footer at end of results -->
236     <div style="display:none;" id="footer">
237     Thanks for trying out no pager. Hope you like it.
238     </div>
239    
240     </div>
241    
242     </div>
243     |;
244     }
245    
246     1;

  ViewVC Help
Powered by ViewVC 1.1.26