gitweb: Separate generating 'sort by' table header

Extract generating table header cell, for tables which can be sorted
by its columns, into print_sort_th_str() and print_sort_th_num()
subroutines, and print_sort_th() driver subroutine.

This avoids repetition, and should make further improvements (like
JavaScript sorting) easier.  The subroutine uses now "replay" link,
so it is generic enough to be able to use it for other tables which
can be sorted by column, like for example 'heads' and 'tags' view
(sort by name, or sort by age).

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jakub Narebski 2008-06-10 19:21:44 +02:00 коммит произвёл Junio C Hamano
Родитель 6991341565
Коммит 7da0f3a46d
1 изменённых файлов: 39 добавлений и 37 удалений

Просмотреть файл

@ -3598,6 +3598,36 @@ sub fill_project_list_info {
return @projects; return @projects;
} }
# print 'sort by' <th> element, either sorting by $key if $name eq $order
# (changing $list), or generating 'sort by $name' replay link otherwise
sub print_sort_th {
my ($str_sort, $name, $order, $key, $header, $list) = @_;
$key ||= $name;
$header ||= ucfirst($name);
if ($order eq $name) {
if ($str_sort) {
@$list = sort {$a->{$key} cmp $b->{$key}} @$list;
} else {
@$list = sort {$a->{$key} <=> $b->{$key}} @$list;
}
print "<th>$header</th>\n";
} else {
print "<th>" .
$cgi->a({-href => href(-replay=>1, order=>$name),
-class => "header"}, $header) .
"</th>\n";
}
}
sub print_sort_th_str {
print_sort_th(1, @_);
}
sub print_sort_th_num {
print_sort_th(0, @_);
}
sub git_project_list_body { sub git_project_list_body {
my ($projlist, $order, $from, $to, $extra, $no_header) = @_; my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
@ -3614,43 +3644,15 @@ sub git_project_list_body {
if ($check_forks) { if ($check_forks) {
print "<th></th>\n"; print "<th></th>\n";
} }
if ($order eq "project") { print_sort_th_str('project', $order, 'path',
@projects = sort {$a->{'path'} cmp $b->{'path'}} @projects; 'Project', \@projects);
print "<th>Project</th>\n"; print_sort_th_str('descr', $order, 'descr_long',
} else { 'Description', \@projects);
print "<th>" . print_sort_th_str('owner', $order, 'owner',
$cgi->a({-href => href(project=>undef, order=>'project'), 'Owner', \@projects);
-class => "header"}, "Project") . print_sort_th_num('age', $order, 'age',
"</th>\n"; 'Last Change', \@projects);
} print "<th></th>\n" . # for links
if ($order eq "descr") {
@projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
print "<th>Description</th>\n";
} else {
print "<th>" .
$cgi->a({-href => href(project=>undef, order=>'descr'),
-class => "header"}, "Description") .
"</th>\n";
}
if ($order eq "owner") {
@projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
print "<th>Owner</th>\n";
} else {
print "<th>" .
$cgi->a({-href => href(project=>undef, order=>'owner'),
-class => "header"}, "Owner") .
"</th>\n";
}
if ($order eq "age") {
@projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
print "<th>Last Change</th>\n";
} else {
print "<th>" .
$cgi->a({-href => href(project=>undef, order=>'age'),
-class => "header"}, "Last Change") .
"</th>\n";
}
print "<th></th>\n" .
"</tr>\n"; "</tr>\n";
} }
my $alternate = 1; my $alternate = 1;