6d2bf5f6c23af8936828869df3ea4563b693cb19
[frank.git] / report / sorttable.js
1 /*
2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
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.
10  *
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).
16  *
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.
20  *
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
23  * questions.
24  */
25 addEvent(window, "load", sortables_init);
26
27 var SORT_COLUMN_INDEX;
28
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++) {
34         thisTbl = tbls[ti];
35         if (((' '+thisTbl.className+' ').indexOf("report") != -1) && (thisTbl.id)) {
36             //initTable(thisTbl.id);
37             ts_makeSortable(thisTbl);
38         }
39     }
40 }
41
42 function ts_makeSortable(table) {
43     if (table.rows && table.rows.length > 0) {
44         var firstRow = table.rows[0];
45     }
46     if (!firstRow) return;
47     
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">&nbsp;&nbsp;&nbsp;</span></a>';
55     }
56 }
57
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
62         var str = "";
63         
64         var cs = el.childNodes;
65         var l = cs.length;
66         for (var i = 0; i < l; i++) {
67                 switch (cs[i].nodeType) {
68                         case 1: //ELEMENT_NODE
69                                 str += ts_getInnerText(cs[i]);
70                                 break;
71                         case 3: //TEXT_NODE
72                                 str += cs[i].nodeValue;
73                                 break;
74                 }
75         }
76         return str;
77 }
78
79 function ts_resortTable(lnk,clid) {
80     // get the span
81     var span;
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];
84     }
85     var spantext = ts_getInnerText(span);
86     var td = lnk.parentNode;
87     var column = clid || td.cellIndex;
88     var table = getParent(td,'TABLE');
89     
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]; }
104
105     newRows.sort(sortfn);
106
107     if (span.getAttribute("sortdir") == 'down') {
108         ARROW = '&nbsp;&nbsp;&uarr;';
109         newRows.reverse();
110         span.setAttribute('sortdir','up');
111     } else {
112         ARROW = '&nbsp;&nbsp;&darr;';
113         span.setAttribute('sortdir','down');
114     }
115     
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]);}
121     
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 = '&nbsp;&nbsp;&nbsp;';
128             }
129         }
130     }
131         
132     span.innerHTML = ARROW;
133 }
134
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
138                 return el;
139         else
140                 return getParent(el.parentNode, pTagName);
141 }
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);
148     } else {
149         yr = aa.substr(6,2);
150         if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
151         dt1 = yr+aa.substr(3,2)+aa.substr(0,2);
152     }
153     if (bb.length == 10) {
154         dt2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2);
155     } else {
156         yr = bb.substr(6,2);
157         if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }
158         dt2 = yr+bb.substr(3,2)+bb.substr(0,2);
159     }
160     if (dt1==dt2) return 0;
161     if (dt1<dt2) return -1;
162     return 1;
163 }
164
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);
169 }
170
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;
176     return aa-bb;
177 }
178
179 function ts_sort_percentage(a,b) { 
180
181     var s = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
182         var i = s.indexOf( "%" );
183     if (i != -1) {
184       s = s.substr( 0, i );
185     }
186     aa = parseFloat(s);
187     if (isNaN(aa)) aa = 0;
188    
189     var s = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
190         var i = s.indexOf( "%" );
191     if (i != -1) {
192       s = s.substr( 0, i );
193     }
194     bb = parseFloat(s);
195     if (isNaN(bb)) bb = 0;
196
197     return bb-aa;
198 }
199
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;
205     return 1;
206 }
207
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;
213     return 1;
214 }
215
216
217 function addEvent(elm, evType, fn, useCapture)
218 // addEvent and removeEvent
219 // cross-browser event handling for IE5+,  NS6 and Mozilla
220 // By Scott Andrew
221 {
222   if (elm.addEventListener){
223     elm.addEventListener(evType, fn, useCapture);
224     return true;
225   } else if (elm.attachEvent){
226     var r = elm.attachEvent("on"+evType, fn);
227     return r;
228   } else {
229     alert("Handler could not be removed");
230   }
231