chiark / gitweb /
Abbreviate some variable names in tabsort
[ypp-sc-tools.main.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 % print $m->content();
58
59 %       my $sortfn= "ts_s_$table";
60 function <% $sortfn %>(compar) {
61   debug('sorting compar='+compar);
62   var table= document.getElementById('<% $table %>');
63   var firstrow= table.getElementsByTagName('tr').item(0);
64   var tbody= firstrow.parentNode
65   var rows= tbody.childNodes
66   var newrows= new Array;
67   var finalrows= new Array;
68   var rowix= 0;
69 %       if (defined $throw) {
70   for (; rowix < rows.length; rowix++) {
71     var row= rows.item(rowix);
72     if (row.id == '<% $throw %>') break;
73     debug('skip row '+rowix+' [[ '+row+' ]] id='+row.id);
74   }
75   rowix++;
76 %       }
77   for (; rowix < rows.length; rowix++) {
78     var row= rows.item(rowix);
79 %       if (defined $tbrow) {
80     if (row.id == '<% $tbrow %>') break;
81 %       }
82     if (!row.id) { debug('noid row '+rowix+' [[ '+row+' ]]'); continue; }
83     if (row.tagName != 'TR') {
84       debug('no-tr row '+rowix+' tagName='+row.tagName+' [[ '+row+' ]]');
85       continue;
86     }
87     debug('process row '+rowix+' [[ '+row+' ]] id='+row.id);
88     newrows.push(row);
89   }
90   for (; rowix < rows.length; rowix++) {
91     var row= rows.item(rowix);
92     finalrows.push(row);
93     debug('final row '+rowix+' [[ '+row+' ]]');
94   }
95   newrows.sort(compar);
96 %       if (defined $rowclass) {
97   for (var rowix=0; rowix < newrows.length; rowix++) {
98     var row= newrows[rowix];
99     var classname= '<% $rowclass %>'+(rowix % 2);
100     debug('fix row '+rowix+' class '+classname);
101     row.className= classname;
102   }
103 %       }
104   newrows= newrows.concat(finalrows);
105   for (var rowix=0; rowix < newrows.length; rowix++) {
106     var row= newrows[rowix];
107     debug('add row '+rowix+' [[ '+row+' ]]');
108     tbody.appendChild(row);
109   }
110 }
111
112 % my %add_heads;
113 % foreach my $cix (0..$#$cols) {
114 %       my $col= $cols->[$cix];
115 %       my $thhtml= '';
116 %       next if $col->{NoSort};
117
118 %       my $mapfn= "ts_${cix}m_$table";
119 function <% $mapfn %>(re) {
120   var rowid = re.id;
121 %       if ($col->{SortKey}) {
122   return <% $col->{SortKey} %>;
123 %       } else {
124 %               my $sk= "$sortkeys"."[$cix][rowid]";
125 %               if ($col->{MapFn}) {
126   return <% $col->{MapFn} %>(<% $sk %>);
127 %               } else {
128   return <% $sk %>;
129 %               }
130 %       }
131 }
132
133 %       my $comparefn= "ts_${cix}c0_$table";
134 function <% $comparefn %>(a,b) {
135   var ak = <% $mapfn %>(a);
136   var bk = <% $mapfn %>(b);
137 %       if ($col->{Numeric}) {
138   return ak - bk
139 %       } else {
140   if (ak < bk) return -1;
141   if (ak > bk) return +1;
142   return 0;
143 %       }
144 }
145
146 %       foreach my $reverse (qw(1 0)) {
147 %               my $tcomparefn= "ts_${cix}c${reverse}_$table";
148 %               if ($reverse) {
149 %                       next unless $col->{DoReverse};
150 function <% $tcomparefn %>(a,b) { return -<% $comparefn %>(a,b); }
151
152 %               }
153 %               $thhtml .= "<a href=\"javascript:$sortfn($tcomparefn)\">".
154 %                               ($reverse ? '&or;' : '&and;'). '</a>';
155 %       }
156 %       if (length $thhtml) {
157 %               $add_heads{$cix}= $thhtml;
158 %       }
159 % }
160
161 function ts_onload__<% $table %>() {
162   var ts_add_heads= <% to_json_protecttags(\%add_heads) %>;
163   var ctr= document.getElementById('<% defined($throw) ? $throw : $table %>');
164   var firstth= ctr.getElementsByTagName('th').item(0);
165   var thlist= firstth.parentNode.getElementsByTagName('th');
166   debug('thlist='+thlist);
167   debug('thlist.item(2)=' + thlist.item(2));
168   for (var cix in ts_add_heads) {
169     var ah = ts_add_heads[cix];
170     debug('appending to cix='+cix+' ah='+ah);
171     thlist.item(cix).innerHTML += ah;
172   }
173 }
174
175 register_onload(ts_onload__<% $table %>);
176 </&>