chiark / gitweb /
Javascript sorting for query_age
[ypp-sc-tools.db-live.git] / yarrg / web / tabsort
1 <%args>
2 $table => 'ts_table'
3 $sortkeys => 'ts_sortkeys'
4 $cols
5 </%args>
6
7 <%doc>
8         NoSort
9         DoReverse
10 </%doc>
11
12 <&| script &>
13
14 %       my $sortfn= "ts_sort__$table";
15 function <% $sortfn %>(compar) {
16   debug('sorting compar='+compar);
17   var table= document.getElementById('<% $table %>');
18   var firstrow= table.getElementsByTagName('tr').item(0);
19   var tbody= firstrow.parentNode
20   var rows= tbody.childNodes
21   var newrows= new Array;
22   for (var rowix=0; rowix < rows.length; rowix++) {
23     var row= rows.item(rowix);
24     debug('process row '+rowix+' [[ '+row+' ]] id='+row.id)
25     if (!row.id) continue;
26     if (row.tagName != 'TR') continue;
27     newrows.push(row);
28   }
29   newrows.sort(compar);
30   for (var rowix=0; rowix < newrows.length; rowix++) {
31     var row= newrows[rowix];
32     debug('add row '+rowix+' [[ '+row+' ]]');
33     tbody.appendChild(row);
34   }
35 }
36
37 % my %add_heads;
38 % foreach my $cix (0..$#$cols) {
39 %       my $col= $cols->[$cix];
40 %       my $thhtml= '';
41 %       next if $col->{NoSort};
42
43 %       my $mapfn= "ts_compar${cix}_map__$table";
44 function <% $mapfn %>(rowelement) {
45   var rowid = rowelement.id;
46 %       if ($col->{SortKey}) {
47   var sk = <% $col->{SortKey} %>;
48   debug('map rowid='+rowid+' sk='+sk);
49   return sk;
50 %       } else {
51   return <% $sortkeys %>[<% $cix %>][rowid];
52 %       }
53 }
54
55 %       my $comparefn= "ts_compar${cix}_cmp0__$table";
56 function <% $comparefn %>(a,b) {
57   var a_key = <% $mapfn %>(a);
58   var b_key = <% $mapfn %>(b);
59 %       if ($col->{Numeric}) {
60   return a_key - b_key
61 %       } else {
62   if (a_key < b_key) return -1;
63   if (a_key > b_key) return +1;
64   return 0;
65 %       }
66 }
67
68 %       foreach my $reverse (qw(0 1)) {
69 %               my $tcomparefn= "ts_compar${cix}_cmp${reverse}__$table";
70 %               if ($reverse) {
71 %                       next unless $col->{DoReverse};
72 function <% $tcomparefn %>(a,b) { return -<% $comparefn %>(a,b); }
73 %               }
74 %               $thhtml .= "<a href=\"javascript:$sortfn($tcomparefn)\">".
75 %                               ($reverse ? '&or;' : '&and;'). '</a>';
76 %       }
77 %       if (length $thhtml) {
78 %               $add_heads{$cix}= $thhtml;
79 %       }
80 % }
81
82 function ts_onload__<% $table %>() {
83   var ts_add_heads= <% to_json_protecttags(\%add_heads) %>;
84   var table= document.getElementById('<% $table %>');
85   var firstth= table.getElementsByTagName('th').item(0);
86   var thlist= firstth.parentNode.getElementsByTagName('th');
87   debug('thlist='+thlist);
88   debug('thlist.item(2)=' + thlist.item(2));
89   for (var cix in ts_add_heads) {
90     var ah = ts_add_heads[cix];
91     debug('appending to cix='+cix+' ah='+ah);
92     thlist.item(cix).innerHTML += ah;
93   }
94 }
95 </&>