Skip to content

Commit

Permalink
paging
Browse files Browse the repository at this point in the history
  • Loading branch information
monken committed Jun 3, 2011
1 parent 11ab1c0 commit 2c1b239
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 4 deletions.
2 changes: 2 additions & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ version = 0.0.1
[Prereqs]
Template::Plugin::Number::Format = 0
Template::Plugin::JSON = 0
Template::Plugin::Page = 0
Plack::Middleware::Runtime = 0
Plack::Middleware::ReverseProxy = 0
DateTime::Format::HTTP = 0
DateTime::Format::ISO8601 = 0
Module::Find = 0
AnyEvent::HTTP = 0
JSON::XS = 0
URI::Query = 0
2 changes: 1 addition & 1 deletion lib/MetaCPAN/Web/Controller/Author.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sub index {
reverse => \1
}
}],
size => 200,
size => 1000,
});

($author & $releases)->(sub {
Expand Down
2 changes: 1 addition & 1 deletion lib/MetaCPAN/Web/Controller/Recent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use base 'MetaCPAN::Web::Controller';
sub index {
my ($self, $req) = @_;
my $cv = AE::cv;
$self->model('Release')->recent->(sub {
$self->model('Release')->recent($req->page)->(sub {
my ($data) = shift->recv;
my $latest = [map { $_->{_source} } @{$data->{hits}->{hits}}];
$cv->send({ recent => $latest, took =>$data->{took}, total => $data->{hits}->{total} });
Expand Down
1 change: 1 addition & 0 deletions lib/MetaCPAN/Web/Controller/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ sub index {
$self->model->request(
'/file/_search',
{ size => $req->parameters->{lucky} ? 1 : 20,
from => ( $req->page - 1 ) * 20,
query => {
custom_score => {
query => {
Expand Down
3 changes: 2 additions & 1 deletion lib/MetaCPAN/Web/Model/Release.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ sub get {
}

sub recent {
my $self = shift;
my ( $self, $page ) = @_;
$self->request(
'/release/_search',
{ size => 100,
from => ($page - 1) * 100,
query => { match_all => {} },
sort => [ { 'date' => { order => "desc" } } ] } );
}
Expand Down
22 changes: 22 additions & 0 deletions lib/MetaCPAN/Web/Request.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use strict;
use warnings;
use base 'Plack::Request';

use URI::Query;
use Encode;
use URI::Escape;

Expand Down Expand Up @@ -36,4 +37,25 @@ sub _decode {
map { decode $enc, $_, $CHECK } @_;
}

=head query_string_with
# QUERY_STRING is page=1&keyword=perl
$request->query_string_with( page => 2, pretty => 1 );
# return page=2&keyword=perl&pretty=1
=cut

sub query_string_with {
my $self = shift;
my $params = shift;
my $qq = URI::Query->new($self->parameters->flatten);
$qq->replace(%$params);
return $qq->stringify;
}

sub page {
my $page = shift->parameters->{p};
return $page && $page =~ /^\d+$/ ? $page : 1;
}

1;
10 changes: 10 additions & 0 deletions static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,14 @@ code { font-family: monospace; }

.olControlAttribution {
bottom: 0em !important;
}

.pager {
padding-top: 20px;
text-align: center;
font-size: 14px;
}

.pager a, .pager strong {
padding-left: 6px;
}
13 changes: 13 additions & 0 deletions templates/inc/pager.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="pager">
<%- USE pager = Page(size, page); pager.total_entries(total) %>
<%- IF pager.previous_page %>
<a href="?<% req.query_string_with( p = pager.previous_page ) %>"><strong>Previous</strong></a>&nbsp;&nbsp;&nbsp;
<% END %>
<% FOREACH p IN [page - 10 .. page + 10];
IF p < 1; NEXT; END;
IF p == page %><strong><% p %></strong><% ELSE %>
<a href="?<% req.query_string_with( p = p ) %>"><% p %></a> <% END; END %>
<% IF pager.next_page %>&nbsp;&nbsp;&nbsp;
<a href="?<% req.query_string_with( p = pager.next_page ) %>"><strong>Next</strong></a>
<% END %>
</div>
1 change: 1 addition & 0 deletions templates/preprocess.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

USE Number.Format;
USE JSON;
page = req.parameters.p || 1;
%>
1 change: 1 addition & 0 deletions templates/recent.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
</strong><% IF release.abstract %> - <% release.abstract %><% END %> (<a href="/author/<% release.author %>"><% release.author %></a>)
</div>
<% END %>
<% INCLUDE inc/pager.html size = 100 %>
</div>
1 change: 1 addition & 0 deletions templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
<!-- <% item.score %> -->
<br><br>
<% END %>
<% INCLUDE inc/pager.html size = 20 %>
</div>
2 changes: 1 addition & 1 deletion templates/wrapper.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<div class="ds">
<input type="text" name="q" size="41" id="search-input" value="<% req.parameters.get_all('q').join(' ') | html %>"><input type="submit" value="Search" class="g-button" />
</div>
<br><small><% IF total.defined; total | format_number %> result<% IF total != 1; 's'; END %> (<% took / 1000 %> seconds)<% END %></small></div>
<br><small><% IF total.defined; IF page > 1 %>Page <% page %> of <% END; total | format_number %> result<% IF total != 1; 's'; END %> (<% took / 1000 %> seconds)<% END %></small></div>

<div style="float: left; width: 168px"></div>
</div>
Expand Down

0 comments on commit 2c1b239

Please sign in to comment.