Permalink
Browse files

Allow displaying the commit list page by page

  • Loading branch information...
1 parent 35d1031 commit 2f5e4b2f17cb1011e71bbc34caf6c7df62d0d3a9 @vpit vpit committed Jul 30, 2010
Showing with 77 additions and 3 deletions.
  1. +41 −3 cherrymaint.pm
  2. +2 −0 public/css/style.css
  3. +34 −0 views/index.tt
View
@@ -112,14 +112,48 @@ sub get_log {
}
get '/' => sub {
+ my $page = params->{page};
+ $page = 0 unless defined $page;
+ $page =~ /^[0-9]+$/ or die 'Invalid page number';
+
+ my $limit = params->{limit};
+ $limit = 0 unless defined $limit;
+ $limit =~ /^[0-9]+$/ or die 'Invalid limit';
+
my $user = get_user(@ENV{qw/REMOTE_ADDR REMOTE_PORT/});
my @log = get_log;
my $data = do {
my $lock = lock_datafile("$$-$user");
load_datafile;
};
+
+ my (@pages, $current_page);
+ if ($limit) {
+ my $max_pages = @log / $limit;
+ my $num = 0;
+ for (my $start = 0; $start <= $#log; $start += $limit) {
+ my $end = $start + $limit - 1;
+ $end = $#log if $end > $#log;
+ if ($num == $page) {
+ $current_page = [ $start => $end ];
+ }
+ push @pages, [ $num, $num == $page ? 1 : 0 ];
+ ++$num;
+ }
+ unless ($current_page) { # Page was out of bounds
+ $page = 0;
+ $current_page = [ 0 => -1 ];
+ }
+ } else {
+ $page = 0;
+ @pages = $current_page = [ 0 => $#log ];
+ }
+
+ my ($start, $end) = @$current_page;
my @commits;
- for my $log (@log) {
+ for my $i ($start .. $end) {
+ next if $i > $#log;
+ my $log = $log[$i];
my $commit = $log->[0];
my $status = $data->{$commit}->[0] || 0;
my $votes = $data->{$commit}->[1];
@@ -131,8 +165,12 @@ get '/' => sub {
};
}
template 'index', {
- commits => \@commits,
- user => $user,
+ commits => \@commits,
+ user => $user,
+ limit => $limit,
+ cur_page => $page,
+ last_page => $#pages,
+ pages => \@pages,
};
};
View
@@ -14,6 +14,8 @@ H1 { border-bottom: thin solid black }
color: #999;
}
+p.pages { text-align: center }
+
tr.status0 { background-color: white; color: black }
tr.status1 { background-color: #EEE; color: #999 }
tr.status2 { background-color: #5F5; color: black }
View
@@ -38,8 +38,42 @@ function markCommit(commit,value) {
}
});
}
+function reloadLimit(old_page, old_limit, new_limit) {
+ var start = old_page * old_limit;
+ var new_page = 0;
+ if (new_limit != 0) {
+ new_page = Math.floor(start / new_limit);
+ }
+ window.location = '/?page=' + new_page + '&limit=' + new_limit;
+}
</script>
<p>Welcome, <% user %>.</p>
+<p class="pages">
+<% FILTER collapse %>
+ <% IF cur_page > 0 %>
+<a href="/?page=<% cur_page - 1 %>&limit=<% limit %>">Newer</a>
+ <% ELSE %>
+Newer
+ <% END %>
+-
+ <% FOREACH page IN pages %>
+ <% IF page.1 %>
+<% page.0 %>
+ <% ELSE %>
+<a href="/?page=<% page.0 %>&limit=<% limit %>"><% page.0 %></a>
+ <% END %>
+ <% END %>
+-
+ <% IF cur_page < last_page %>
+<a href="/?page=<% cur_page + 1 %>&limit=<% limit %>">Older</a>
+ <% ELSE %>
+Older
+ <% END %>
+<% END %>
+-
+<label for="limit">Limit : </label>
+<input type="text" id="limit" size="2" value="<% limit %>" onChange="reloadLimit(<% cur_page %>, <% limit %>, this.value)">
+</p>
<table>
<% FOREACH commit IN commits %>
<tr id="tr<% commit.sha1 %>" class="status<% commit.status %>">

0 comments on commit 2f5e4b2

Please sign in to comment.