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