3 This is part of the YARRG website. YARRG is a tool and website
4 for assisting players of Yohoho Puzzle Pirates.
6 Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7 Copyright (C) 2009 Clare Boothby
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
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.
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.
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/>.
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.
32 This Mason component generates Javascript for sorting tables in
40 $sortkeys => 'ts_sortkeys'
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;
66 % if (defined $throw) {
67 for (; rowix < rows.length; rowix++) {
68 var row= rows.item(rowix);
69 if (row.id == '<% $throw %>') break;
70 debug('skip row '+rowix+' [[ '+row+' ]] id='+row.id);
74 for (; rowix < rows.length; rowix++) {
75 var row= rows.item(rowix);
76 % if (defined $tbrow) {
77 if (row.id == '<% $tbrow %>') break;
79 if (!row.id) { debug('noid row '+rowix+' [[ '+row+' ]]'); continue; }
80 if (row.tagName != 'TR') {
81 debug('no-tr row '+rowix+' tagName='+row.tagName+' [[ '+row+' ]]');
84 debug('process row '+rowix+' [[ '+row+' ]] id='+row.id);
87 for (; rowix < rows.length; rowix++) {
88 var row= rows.item(rowix);
90 debug('final row '+rowix+' [[ '+row+' ]]');
93 newrows= newrows.concat(finalrows);
94 for (var rowix=0; rowix < newrows.length; rowix++) {
95 var row= newrows[rowix];
96 debug('add row '+rowix+' [[ '+row+' ]]');
97 tbody.appendChild(row);
102 % foreach my $cix (0..$#$cols) {
103 % my $col= $cols->[$cix];
105 % next if $col->{NoSort};
107 % my $mapfn= "ts_compar${cix}_map__$table";
108 function <% $mapfn %>(rowelement) {
109 var rowid = rowelement.id;
110 % if ($col->{SortKey}) {
111 return <% $col->{SortKey} %>;
113 % my $sk= "$sortkeys"."[$cix][rowid]";
114 % if ($col->{MapFn}) {
115 return <% $col->{MapFn} %>(<% $sk %>);
122 % my $comparefn= "ts_compar${cix}_cmp0__$table";
123 function <% $comparefn %>(a,b) {
124 var a_key = <% $mapfn %>(a);
125 var b_key = <% $mapfn %>(b);
126 % if ($col->{Numeric}) {
129 if (a_key < b_key) return -1;
130 if (a_key > b_key) return +1;
135 % foreach my $reverse (qw(1 0)) {
136 % my $tcomparefn= "ts_compar${cix}_cmp${reverse}__$table";
138 % next unless $col->{DoReverse};
139 function <% $tcomparefn %>(a,b) { return -<% $comparefn %>(a,b); }
141 % $thhtml .= "<a href=\"javascript:$sortfn($tcomparefn)\">".
142 % ($reverse ? '∨' : '∧'). '</a>';
144 % if (length $thhtml) {
145 % $add_heads{$cix}= $thhtml;
149 function ts_onload__<% $table %>() {
150 var ts_add_heads= <% to_json_protecttags(\%add_heads) %>;
151 var ctr= document.getElementById('<% defined($throw) ? $throw : $table %>');
152 var firstth= ctr.getElementsByTagName('th').item(0);
153 var thlist= firstth.parentNode.getElementsByTagName('th');
154 debug('thlist='+thlist);
155 debug('thlist.item(2)=' + thlist.item(2));
156 for (var cix in ts_add_heads) {
157 var ah = ts_add_heads[cix];
158 debug('appending to cix='+cix+' ah='+ah);
159 thlist.item(cix).innerHTML += ah;