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>
Jakub Narebski committed
Jun 10, 2008 at 19:21 UTC
7da0f3a46daac50782a71e8a6eb12355d49c419d
1 file changed
+39
-37
gitweb/gitweb.perl
+39
-37
@@ -3598,6 +3598,36 @@ sub fill_project_list_info {
3598
return @projects;
3599
}
3600
3601
+# print 'sort by' <th> element, either sorting by $key if $name eq $order
3602
+# (changing $list), or generating 'sort by $name' replay link otherwise
3603
+sub print_sort_th {
3604
+ my ($str_sort, $name, $order, $key, $header, $list) = @_;
3605
+ $key ||= $name;
3606
+ $header ||= ucfirst($name);
3607
+
3608
+ if ($order eq $name) {
3609
+ if ($str_sort) {
3610
+ @$list = sort {$a->{$key} cmp $b->{$key}} @$list;
3611
+ } else {
3612
+ @$list = sort {$a->{$key} <=> $b->{$key}} @$list;
3613
+ }
3614
+ print "<th>$header</th>\n";
3615
+ } else {
3616
+ print "<th>" .
3617
+ $cgi->a({-href => href(-replay=>1, order=>$name),
3618
+ -class => "header"}, $header) .
3619
+ "</th>\n";
3620
+ }
3621
+}
3622
+
3623
+sub print_sort_th_str {
3624
+ print_sort_th(1, @_);
3625
+}
3626
+
3627
+sub print_sort_th_num {
3628
+ print_sort_th(0, @_);
3629
+}
3630
+
3631
sub git_project_list_body {
3632
my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3633
@@ -3614,43 +3644,15 @@ sub git_project_list_body {
3644
if ($check_forks) {
3645
print "<th></th>\n";
3646
}
3617
- if ($order eq "project") {
3618
- @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3619
- print "<th>Project</th>\n";
3620
- } else {
3621
- print "<th>" .
3622
- $cgi->a({-href => href(project=>undef, order=>'project'),
3623
- -class => "header"}, "Project") .
3624
- "</th>\n";
3625
- }
3626
- if ($order eq "descr") {
3627
- @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3628
- print "<th>Description</th>\n";
3629
- } else {
3630
- print "<th>" .
3631
- $cgi->a({-href => href(project=>undef, order=>'descr'),
3632
- -class => "header"}, "Description") .
3633
- "</th>\n";
3634
- }
3635
- if ($order eq "owner") {
3636
- @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3637
- print "<th>Owner</th>\n";
3638
- } else {
3639
- print "<th>" .
3640
- $cgi->a({-href => href(project=>undef, order=>'owner'),
3641
- -class => "header"}, "Owner") .
3642
- "</th>\n";
3643
- }
3644
- if ($order eq "age") {
3645
- @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3646
- print "<th>Last Change</th>\n";
3647
- } else {
3648
- print "<th>" .
3649
- $cgi->a({-href => href(project=>undef, order=>'age'),
3650
- -class => "header"}, "Last Change") .
3651
- "</th>\n";
3652
- }
3653
- print "<th></th>\n" .
3647
+ print_sort_th_str('project', $order, 'path',
3648
+ 'Project', \@projects);
3649
+ print_sort_th_str('descr', $order, 'descr_long',
3650
+ 'Description', \@projects);
3651
+ print_sort_th_str('owner', $order, 'owner',
3652
+ 'Owner', \@projects);
3653
+ print_sort_th_num('age', $order, 'age',
3654
+ 'Last Change', \@projects);
3655
+ print "<th></th>\n" . # for links
3656
"</tr>\n";
3657
}
3658
my $alternate = 1;