chiark / gitweb /
Stripy tables.
[ypp-sc-tools.db-live.git] / yarrg / web / tabsort
1 <%doc>
2
3  This is part of the YARRG website.  YARRG is a tool and website
4  for assisting players of Yohoho Puzzle Pirates.
5
6  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7  Copyright (C) 2009 Clare Boothby
8
9   YARRG's client code etc. is covered by the ordinary GNU GPL (v3 or later).
10   The YARRG website is covered by the GNU Affero GPL v3 or later, which
11    basically means that every installation of the website will let you
12    download the source.
13
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU Affero General Public License as
16  published by the Free Software Foundation, either version 3 of the
17  License, or (at your option) any later version.
18
19  This program is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  GNU Affero General Public License for more details.
23
24  You should have received a copy of the GNU Affero General Public License
25  along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
27  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
28  are used without permission.  This program is not endorsed or
29  sponsored by Three Rings.
30
31
32  This Mason component generates Javascript for sorting tables in
33  DHTML.
34
35
36 </%doc>
37
38 <%args>
39 $table => 'ts_table'
40 $sortkeys => 'ts_sortkeys'
41 $throw => undef
42 $tbrow => undef
43 $rowclass => undef
44 $cols
45 </%args>
46
47 <%doc>
48         Numeric
49         SortKey
50         MapFn
51         NoSort
52         DoReverse
53 </%doc>
54
55 <&| script &>
56
57 %       my $sortfn= "ts_sort__$table";
58 function <% $sortfn %>(compar) {
59   debug('sorting compar='+compar);
60   var table= document.getElementById('<% $table %>');
61   var firstrow= table.getElementsByTagName('tr').item(0);
62   var tbody= firstrow.parentNode
63   var rows= tbody.childNodes
64   var newrows= new Array;
65   var finalrows= new Array;
66   var rowix= 0;
67 %       if (defined $throw) {
68   for (; rowix < rows.length; rowix++) {
69     var row= rows.item(rowix);
70     if (row.id == '<% $throw %>') break;
71     debug('skip row '+rowix+' [[ '+row+' ]] id='+row.id);
72   }
73   rowix++;
74 %       }
75   for (; rowix < rows.length; rowix++) {
76     var row= rows.item(rowix);
77 %       if (defined $tbrow) {
78     if (row.id == '<% $tbrow %>') break;
79 %       }
80     if (!row.id) { debug('noid row '+rowix+' [[ '+row+' ]]'); continue; }
81     if (row.tagName != 'TR') {
82       debug('no-tr row '+rowix+' tagName='+row.tagName+' [[ '+row+' ]]');
83       continue;
84     }
85     debug('process row '+rowix+' [[ '+row+' ]] id='+row.id);
86     newrows.push(row);
87   }
88   for (; rowix < rows.length; rowix++) {
89     var row= rows.item(rowix);
90     finalrows.push(row);
91     debug('final row '+rowix+' [[ '+row+' ]]');
92   }
93   newrows.sort(compar);
94 %       if (defined $rowclass) {
95   for (var rowix=0; rowix < newrows.length; rowix++) {
96     var row= newrows[rowix];
97     var classname= '<% $rowclass %>'+(rowix % 2);
98     debug('fix row '+rowix+' class '+classname);
99     row.className= classname;
100   }
101 %       }
102   newrows= newrows.concat(finalrows);
103   for (var rowix=0; rowix < newrows.length; rowix++) {
104     var row= newrows[rowix];
105     debug('add row '+rowix+' [[ '+row+' ]]');
106     tbody.appendChild(row);
107   }
108 }
109
110 % my %add_heads;
111 % foreach my $cix (0..$#$cols) {
112 %       my $col= $cols->[$cix];
113 %       my $thhtml= '';
114 %       next if $col->{NoSort};
115
116 %       my $mapfn= "ts_compar${cix}_map__$table";
117 function <% $mapfn %>(rowelement) {
118   var rowid = rowelement.id;
119 %       if ($col->{SortKey}) {
120   return <% $col->{SortKey} %>;
121 %       } else {
122 %               my $sk= "$sortkeys"."[$cix][rowid]";
123 %               if ($col->{MapFn}) {
124   return <% $col->{MapFn} %>(<% $sk %>);
125 %               } else {
126   return <% $sk %>;
127 %               }
128 %       }
129 }
130
131 %       my $comparefn= "ts_compar${cix}_cmp0__$table";
132 function <% $comparefn %>(a,b) {
133   var a_key = <% $mapfn %>(a);
134   var b_key = <% $mapfn %>(b);
135 %       if ($col->{Numeric}) {
136   return a_key - b_key
137 %       } else {
138   if (a_key < b_key) return -1;
139   if (a_key > b_key) return +1;
140   return 0;
141 %       }
142 }
143
144 %       foreach my $reverse (qw(1 0)) {
145 %               my $tcomparefn= "ts_compar${cix}_cmp${reverse}__$table";
146 %               if ($reverse) {
147 %                       next unless $col->{DoReverse};
148 function <% $tcomparefn %>(a,b) { return -<% $comparefn %>(a,b); }
149 %               }
150 %               $thhtml .= "<a href=\"javascript:$sortfn($tcomparefn)\">".
151 %                               ($reverse ? '&or;' : '&and;'). '</a>';
152 %       }
153 %       if (length $thhtml) {
154 %               $add_heads{$cix}= $thhtml;
155 %       }
156 % }
157
158 function ts_onload__<% $table %>() {
159   var ts_add_heads= <% to_json_protecttags(\%add_heads) %>;
160   var ctr= document.getElementById('<% defined($throw) ? $throw : $table %>');
161   var firstth= ctr.getElementsByTagName('th').item(0);
162   var thlist= firstth.parentNode.getElementsByTagName('th');
163   debug('thlist='+thlist);
164   debug('thlist.item(2)=' + thlist.item(2));
165   for (var cix in ts_add_heads) {
166     var ah = ts_add_heads[cix];
167     debug('appending to cix='+cix+' ah='+ah);
168     thlist.item(cix).innerHTML += ah;
169   }
170 }
171 </&>