|
@@ -0,0 +1,184 @@
|
|
1
|
+//$Header: /cvsroot/pmd/pmd/etc/xslt/sorttable.js,v 1.1 2005/06/28 13:51:49 tomcopeland Exp $
|
|
2
|
+addEvent(window, "load", sortables_init);
|
|
3
|
+
|
|
4
|
+var SORT_COLUMN_INDEX;
|
|
5
|
+
|
|
6
|
+function sortables_init() {
|
|
7
|
+ // Find all tables with class sortable and make them sortable
|
|
8
|
+ if (!document.getElementsByTagName) return;
|
|
9
|
+ tbls = document.getElementsByTagName("table");
|
|
10
|
+ for (ti=0;ti<tbls.length;ti++) {
|
|
11
|
+ thisTbl = tbls[ti];
|
|
12
|
+ if (((' '+thisTbl.className+' ').indexOf("sortable") != -1) && (thisTbl.id)) {
|
|
13
|
+ //initTable(thisTbl.id);
|
|
14
|
+ ts_makeSortable(thisTbl);
|
|
15
|
+ }
|
|
16
|
+ }
|
|
17
|
+}
|
|
18
|
+
|
|
19
|
+function ts_makeSortable(table) {
|
|
20
|
+ if (table.rows && table.rows.length > 0) {
|
|
21
|
+ var firstRow = table.rows[0];
|
|
22
|
+ }
|
|
23
|
+ if (!firstRow) return;
|
|
24
|
+
|
|
25
|
+ // We have a first row: assume it's the header, and make its contents clickable links
|
|
26
|
+ for (var i=0;i<firstRow.cells.length;i++) {
|
|
27
|
+ var cell = firstRow.cells[i];
|
|
28
|
+ var txt = ts_getInnerText(cell);
|
|
29
|
+ cell.innerHTML = '<a href="#" class="sortheader" onclick="ts_resortTable(this);return false;">'+txt+'<span class="sortarrow"> </span></a>';
|
|
30
|
+ }
|
|
31
|
+}
|
|
32
|
+
|
|
33
|
+function ts_getInnerText(el) {
|
|
34
|
+ if (typeof el == "string") return el;
|
|
35
|
+ if (typeof el == "undefined") { return el };
|
|
36
|
+ if (el.innerText) return el.innerText; //Not needed but it is faster
|
|
37
|
+ var str = "";
|
|
38
|
+
|
|
39
|
+ var cs = el.childNodes;
|
|
40
|
+ var l = cs.length;
|
|
41
|
+ for (var i = 0; i < l; i++) {
|
|
42
|
+ switch (cs[i].nodeType) {
|
|
43
|
+ case 1: //ELEMENT_NODE
|
|
44
|
+ str += ts_getInnerText(cs[i]);
|
|
45
|
+ break;
|
|
46
|
+ case 3: //TEXT_NODE
|
|
47
|
+ str += cs[i].nodeValue;
|
|
48
|
+ break;
|
|
49
|
+ }
|
|
50
|
+ }
|
|
51
|
+ return str;
|
|
52
|
+}
|
|
53
|
+
|
|
54
|
+function ts_resortTable(lnk) {
|
|
55
|
+ // get the span
|
|
56
|
+ var span;
|
|
57
|
+ for (var ci=0;ci<lnk.childNodes.length;ci++) {
|
|
58
|
+ if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];
|
|
59
|
+ }
|
|
60
|
+ var spantext = ts_getInnerText(span);
|
|
61
|
+ var td = lnk.parentNode;
|
|
62
|
+ var column = td.cellIndex;
|
|
63
|
+ var table = getParent(td,'TABLE');
|
|
64
|
+
|
|
65
|
+ // Work out a type for the column
|
|
66
|
+ if (table.rows.length <= 1) return;
|
|
67
|
+ var itm = ts_getInnerText(table.rows[1].cells[column]);
|
|
68
|
+ sortfn = ts_sort_caseinsensitive;
|
|
69
|
+ if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;
|
|
70
|
+ if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date;
|
|
71
|
+ if (itm.match(/^[£$]/)) sortfn = ts_sort_currency;
|
|
72
|
+ if (itm.match(/^[\d\.]+$/)) sortfn = ts_sort_numeric;
|
|
73
|
+ SORT_COLUMN_INDEX = column;
|
|
74
|
+ var firstRow = new Array();
|
|
75
|
+ var newRows = new Array();
|
|
76
|
+ for (i=0;i<table.rows[0].length;i++) { firstRow[i] = table.rows[0][i]; }
|
|
77
|
+ for (j=1;j<table.rows.length;j++) { newRows[j-1] = table.rows[j]; }
|
|
78
|
+
|
|
79
|
+ newRows.sort(sortfn);
|
|
80
|
+
|
|
81
|
+ if (span.getAttribute("sortdir") == 'down') {
|
|
82
|
+ ARROW = ' ↑';
|
|
83
|
+ newRows.reverse();
|
|
84
|
+ span.setAttribute('sortdir','up');
|
|
85
|
+ } else {
|
|
86
|
+ ARROW = ' ↓';
|
|
87
|
+ span.setAttribute('sortdir','down');
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
|
|
91
|
+ // don't do sortbottom rows
|
|
92
|
+ for (i=0;i<newRows.length;i++) { if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) table.tBodies[0].appendChild(newRows[i]);}
|
|
93
|
+ // do sortbottom rows only
|
|
94
|
+ for (i=0;i<newRows.length;i++) { if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)) table.tBodies[0].appendChild(newRows[i]);}
|
|
95
|
+
|
|
96
|
+ // Delete any other arrows there may be showing
|
|
97
|
+ var allspans = document.getElementsByTagName("span");
|
|
98
|
+ for (var ci=0;ci<allspans.length;ci++) {
|
|
99
|
+ if (allspans[ci].className == 'sortarrow') {
|
|
100
|
+ if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us?
|
|
101
|
+ allspans[ci].innerHTML = ' ';
|
|
102
|
+ }
|
|
103
|
+ }
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ span.innerHTML = ARROW;
|
|
107
|
+}
|
|
108
|
+
|
|
109
|
+function getParent(el, pTagName) {
|
|
110
|
+ if (el == null) return null;
|
|
111
|
+ else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) // Gecko bug, supposed to be uppercase
|
|
112
|
+ return el;
|
|
113
|
+ else
|
|
114
|
+ return getParent(el.parentNode, pTagName);
|
|
115
|
+}
|
|
116
|
+function ts_sort_date(a,b) {
|
|
117
|
+ // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
|
|
118
|
+ aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
|
|
119
|
+ bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
|
|
120
|
+ if (aa.length == 10) {
|
|
121
|
+ dt1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);
|
|
122
|
+ } else {
|
|
123
|
+ yr = aa.substr(6,2);
|
|
124
|
+ if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
|
|
125
|
+ dt1 = yr+aa.substr(3,2)+aa.substr(0,2);
|
|
126
|
+ }
|
|
127
|
+ if (bb.length == 10) {
|
|
128
|
+ dt2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);
|
|
129
|
+ } else {
|
|
130
|
+ yr = bb.substr(6,2);
|
|
131
|
+ if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
|
|
132
|
+ dt2 = yr+bb.substr(3,2)+bb.substr(0,2);
|
|
133
|
+ }
|
|
134
|
+ if (dt1==dt2) return 0;
|
|
135
|
+ if (dt1<dt2) return -1;
|
|
136
|
+ return 1;
|
|
137
|
+}
|
|
138
|
+
|
|
139
|
+function ts_sort_currency(a,b) {
|
|
140
|
+ aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
|
|
141
|
+ bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
|
|
142
|
+ return parseFloat(aa) - parseFloat(bb);
|
|
143
|
+}
|
|
144
|
+
|
|
145
|
+function ts_sort_numeric(a,b) {
|
|
146
|
+ aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));
|
|
147
|
+ if (isNaN(aa)) aa = 0;
|
|
148
|
+ bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]));
|
|
149
|
+ if (isNaN(bb)) bb = 0;
|
|
150
|
+ return aa-bb;
|
|
151
|
+}
|
|
152
|
+
|
|
153
|
+function ts_sort_caseinsensitive(a,b) {
|
|
154
|
+ aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();
|
|
155
|
+ bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();
|
|
156
|
+ if (aa==bb) return 0;
|
|
157
|
+ if (aa<bb) return -1;
|
|
158
|
+ return 1;
|
|
159
|
+}
|
|
160
|
+
|
|
161
|
+function ts_sort_default(a,b) {
|
|
162
|
+ aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
|
|
163
|
+ bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
|
|
164
|
+ if (aa==bb) return 0;
|
|
165
|
+ if (aa<bb) return -1;
|
|
166
|
+ return 1;
|
|
167
|
+}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+function addEvent(elm, evType, fn, useCapture)
|
|
171
|
+// addEvent and removeEvent
|
|
172
|
+// cross-browser event handling for IE5+, NS6 and Mozilla
|
|
173
|
+// By Scott Andrew
|
|
174
|
+{
|
|
175
|
+ if (elm.addEventListener){
|
|
176
|
+ elm.addEventListener(evType, fn, useCapture);
|
|
177
|
+ return true;
|
|
178
|
+ } else if (elm.attachEvent){
|
|
179
|
+ var r = elm.attachEvent("on"+evType, fn);
|
|
180
|
+ return r;
|
|
181
|
+ } else {
|
|
182
|
+ alert("Handler could not be removed");
|
|
183
|
+ }
|
|
184
|
+}
|