2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
25 addEvent(window, "load", sortables_init);
27 var SORT_COLUMN_INDEX;
29 function sortables_init() {
30 // Find all tables with class sortable and make them sortable
31 if (!document.getElementsByTagName) return;
32 tbls = document.getElementsByTagName("table");
33 for (ti=0;ti<tbls.length;ti++) {
35 if (((' '+thisTbl.className+' ').indexOf("report") != -1) && (thisTbl.id)) {
36 //initTable(thisTbl.id);
37 ts_makeSortable(thisTbl);
42 function ts_makeSortable(table) {
43 if (table.rows && table.rows.length > 0) {
44 var firstRow = table.rows[0];
46 if (!firstRow) return;
48 // We have a first row: assume it's the header, and make its contents clickable links
49 for (var i=0;i<firstRow.cells.length;i++) {
50 var cell = firstRow.cells[i];
51 var txt = ts_getInnerText(cell);
52 cell.innerHTML = '<a href="#" class="sortheader" '+
53 'onclick="ts_resortTable(this, '+i+');return false;">' +
54 txt+'<span class="sortarrow"> </span></a>';
58 function ts_getInnerText(el) {
59 if (typeof el == "string") return el;
60 if (typeof el == "undefined") { return el };
61 if (el.innerText) return el.innerText; //Not needed but it is faster
64 var cs = el.childNodes;
66 for (var i = 0; i < l; i++) {
67 switch (cs[i].nodeType) {
68 case 1: //ELEMENT_NODE
69 str += ts_getInnerText(cs[i]);
72 str += cs[i].nodeValue;
79 function ts_resortTable(lnk,clid) {
82 for (var ci=0;ci<lnk.childNodes.length;ci++) {
83 if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];
85 var spantext = ts_getInnerText(span);
86 var td = lnk.parentNode;
87 var column = clid || td.cellIndex;
88 var table = getParent(td,'TABLE');
90 // Work out a type for the column
91 if (table.rows.length <= 1) return;
92 var itm = ts_getInnerText(table.rows[1].cells[column]);
93 sortfn = ts_sort_caseinsensitive;
94 if (itm.match(/%/)) sortfn = ts_sort_percentage;
95 if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;
96 if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date;
97 if (itm.match(/^[£$]/)) sortfn = ts_sort_currency;
98 if (itm.match(/^[\d\.]+$/)) sortfn = ts_sort_numeric;
99 SORT_COLUMN_INDEX = column;
100 var firstRow = new Array();
101 var newRows = new Array();
102 for (i=0;i<table.rows[0].length;i++) { firstRow[i] = table.rows[0][i]; }
103 for (j=1;j<table.rows.length;j++) { newRows[j-1] = table.rows[j]; }
105 newRows.sort(sortfn);
107 if (span.getAttribute("sortdir") == 'down') {
108 ARROW = ' ↑';
110 span.setAttribute('sortdir','up');
112 ARROW = ' ↓';
113 span.setAttribute('sortdir','down');
116 // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
117 // don't do sortbottom rows
118 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]);}
119 // do sortbottom rows only
120 for (i=0;i<newRows.length;i++) { if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)) table.tBodies[0].appendChild(newRows[i]);}
122 // Delete any other arrows there may be showing
123 var allspans = document.getElementsByTagName("span");
124 for (var ci=0;ci<allspans.length;ci++) {
125 if (allspans[ci].className == 'sortarrow') {
126 if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us?
127 allspans[ci].innerHTML = ' ';
132 span.innerHTML = ARROW;
135 function getParent(el, pTagName) {
136 if (el == null) return null;
137 else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) // Gecko bug, supposed to be uppercase
140 return getParent(el.parentNode, pTagName);
142 function ts_sort_date(a,b) {
143 // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
144 aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
145 bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
146 if (aa.length == 10) {
147 dt1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2);
150 if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
151 dt1 = yr+aa.substr(3,2)+aa.substr(0,2);
153 if (bb.length == 10) {
154 dt2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);
157 if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
158 dt2 = yr+bb.substr(3,2)+bb.substr(0,2);
160 if (dt1==dt2) return 0;
161 if (dt1<dt2) return -1;
165 function ts_sort_currency(a,b) {
166 aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
167 bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,'');
168 return parseFloat(aa) - parseFloat(bb);
171 function ts_sort_numeric(a,b) {
172 aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));
173 if (isNaN(aa)) aa = 0;
174 bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]));
175 if (isNaN(bb)) bb = 0;
179 function ts_sort_percentage(a,b) {
181 var s = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
182 var i = s.indexOf( "%" );
184 s = s.substr( 0, i );
187 if (isNaN(aa)) aa = 0;
189 var s = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
190 var i = s.indexOf( "%" );
192 s = s.substr( 0, i );
195 if (isNaN(bb)) bb = 0;
200 function ts_sort_caseinsensitive(a,b) {
201 aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();
202 bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();
203 if (aa==bb) return 0;
204 if (aa<bb) return -1;
208 function ts_sort_default(a,b) {
209 aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
210 bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
211 if (aa==bb) return 0;
212 if (aa<bb) return -1;
217 function addEvent(elm, evType, fn, useCapture)
218 // addEvent and removeEvent
219 // cross-browser event handling for IE5+, NS6 and Mozilla
222 if (elm.addEventListener){
223 elm.addEventListener(evType, fn, useCapture);
225 } else if (elm.attachEvent){
226 var r = elm.attachEvent("on"+evType, fn);
229 alert("Handler could not be removed");