chiark / gitweb /
Also cope with tbrow, and add all stuff from then onwards to bottom
[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 $cols
44 </%args>
45
46 <%doc>
47         Numeric
48         SortKey
49         MapFn
50         NoSort
51         DoReverse
52 </%doc>
53
54 <&| script &>
55
56 %       my $sortfn= "ts_sort__$table";
57 function <% $sortfn %>(compar) {
58   debug('sorting compar='+compar);
59   var table= document.getElementById('<% $table %>');
60   var firstrow= table.getElementsByTagName('tr').item(0);
61   var tbody= firstrow.parentNode
62   var rows= tbody.childNodes
63   var newrows= new Array;
64   var finalrows= new Array;
65   var rowix= 0;
66 %       if (defined $throw) {
67   for (; rowix < rows.length; rowix++) {
68     var row= rows.item(rowix);
69     if (row.id == '<% $throw %>') break;
70   }
71   rowix++;
72 %       }
73   for (; rowix < rows.length; rowix++) {
74     var row= rows.item(rowix);
75 %       if (defined $tbrow) {
76     if (row.id == '<% $tbrow %>') break;
77 %       }
78     if (!row.id) continue;
79     if (row.tagName != 'TR') continue;
80     debug('process row '+rowix+' [[ '+row+' ]] id='+row.id)
81     newrows.push(row);
82   }
83   for (; rowix < rows.length; rowix++) {
84     var row= rows.item(rowix);
85     finalrows.push(row);
86   }
87   newrows.sort(compar);
88   newrows= newrows.concat(finalrows);
89   for (var rowix=0; rowix < newrows.length; rowix++) {
90     var row= newrows[rowix];
91     debug('add row '+rowix+' [[ '+row+' ]]');
92     tbody.appendChild(row);
93   }
94 }
95
96 % my %add_heads;
97 % foreach my $cix (0..$#$cols) {
98 %       my $col= $cols->[$cix];
99 %       my $thhtml= '';
100 %       next if $col->{NoSort};
101
102 %       my $mapfn= "ts_compar${cix}_map__$table";
103 function <% $mapfn %>(rowelement) {
104   var rowid = rowelement.id;
105 %       if ($col->{SortKey}) {
106   return <% $col->{SortKey} %>;
107 %       } else {
108 %               my $sk= "$sortkeys"."[$cix][rowid]";
109 %               if ($col->{MapFn}) {
110   return <% $col->{MapFn} %>(<% $sk %>);
111 %               } else {
112   return <% $sk %>;
113 %               }
114 %       }
115 }
116
117 %       my $comparefn= "ts_compar${cix}_cmp0__$table";
118 function <% $comparefn %>(a,b) {
119   var a_key = <% $mapfn %>(a);
120   var b_key = <% $mapfn %>(b);
121 %       if ($col->{Numeric}) {
122   return a_key - b_key
123 %       } else {
124   if (a_key < b_key) return -1;
125   if (a_key > b_key) return +1;
126   return 0;
127 %       }
128 }
129
130 %       foreach my $reverse (qw(0 1)) {
131 %               my $tcomparefn= "ts_compar${cix}_cmp${reverse}__$table";
132 %               if ($reverse) {
133 %                       next unless $col->{DoReverse};
134 function <% $tcomparefn %>(a,b) { return -<% $comparefn %>(a,b); }
135 %               }
136 %               $thhtml .= "<a href=\"javascript:$sortfn($tcomparefn)\">".
137 %                               ($reverse ? '&or;' : '&and;'). '</a>';
138 %       }
139 %       if (length $thhtml) {
140 %               $add_heads{$cix}= $thhtml;
141 %       }
142 % }
143
144 function ts_onload__<% $table %>() {
145   var ts_add_heads= <% to_json_protecttags(\%add_heads) %>;
146   var ctr= document.getElementById('<% defined($throw) ? $throw : $table %>');
147   var firstth= ctr.getElementsByTagName('th').item(0);
148   var thlist= firstth.parentNode.getElementsByTagName('th');
149   debug('thlist='+thlist);
150   debug('thlist.item(2)=' + thlist.item(2));
151   for (var cix in ts_add_heads) {
152     var ah = ts_add_heads[cix];
153     debug('appending to cix='+cix+' ah='+ah);
154     thlist.item(cix).innerHTML += ah;
155   }
156 }
157 </&>