-
Notifications
You must be signed in to change notification settings - Fork 394
/
utilities.min.js
1 lines (1 loc) · 5.34 KB
/
utilities.min.js
1
var SORT_COLUMN_INDEX,image_path="/img/",image_up="arrow-up.gif",image_down="arrow-down.gif",image_none="arrow-none.gif",europeandate=!0,alternate_row_colors=!0;addEvent(window,"load",sortables_init);var thead=!1;function sortables_init(){if(document.getElementsByTagName)for(tbls=document.getElementsByTagName("table"),ti=0;ti<tbls.length;ti++)thisTbl=tbls[ti],-1!=(" "+thisTbl.className+" ").indexOf("sortable")&&thisTbl.id&&ts_makeSortable(thisTbl)}function ts_makeSortable(e){if(e.rows&&e.rows.length>0)if(e.tHead&&e.tHead.rows.length>0){var t=e.tHead.rows[e.tHead.rows.length-1];thead=!0}else t=e.rows[0];if(t){for(var r=0;r<t.cells.length;r++){var s=t.cells[r],a=ts_getInnerText(s);"unsortable"!=s.className&&-1==s.className.indexOf("unsortable")&&(s.innerHTML='<a href="#" class="sortheader" onclick="ts_resortTable(this, '+r+');return false;">'+a+'<span class="sortarrow"> <img src="'+image_path+image_none+'" alt="↓"/></span></a>')}alternate_row_colors&&alternate(e)}}function ts_getInnerText(e){if("string"==typeof e)return e;if(void 0===e)return e;if(e.innerText)return e.innerText;for(var t="",r=e.childNodes,s=r.length,a=0;a<s;a++)switch(r[a].nodeType){case 1:t+=ts_getInnerText(r[a]);break;case 3:t+=r[a].nodeValue}return t}function ts_resortTable(e,t){for(var r,s=0;s<e.childNodes.length;s++)e.childNodes[s].tagName&&"span"==e.childNodes[s].tagName.toLowerCase()&&(r=e.childNodes[s]);ts_getInnerText(r);var a=e.parentNode,n=t||a.cellIndex,o=getParent(a,"TABLE");if(!(o.rows.length<=1)){for(var d="",l=1;""==d&&l<o.tBodies[0].rows.length;){"\x3c!--"!=(d=trim(d=ts_getInnerText(o.tBodies[0].rows[l].cells[n]))).substr(0,4)&&0!=d.length||(d=""),l++}if(""!=d){sortfn=ts_sort_caseinsensitive,d.match(/^\d\d[\/\.-][a-zA-z][a-zA-Z][a-zA-Z][\/\.-]\d\d\d\d$/)&&(sortfn=ts_sort_date),d.match(/^\d\d[\/\.-]\d\d[\/\.-]\d\d\d{2}?$/)&&(sortfn=ts_sort_date),d.match(/^-?[£$Û¢´]\d/)&&(sortfn=ts_sort_numeric),d.match(/^-?(\d+[,\.]?)+(E[-+][\d]+)?%?$/)&&(sortfn=ts_sort_numeric),SORT_COLUMN_INDEX=n;var i=new Array,c=new Array;for(k=0;k<o.tBodies.length;k++)for(l=0;l<o.tBodies[k].rows[0].length;l++)i[l]=o.tBodies[k].rows[0][l];for(k=0;k<o.tBodies.length;k++)if(thead)for(j=0;j<o.tBodies[k].rows.length;j++)c[j]=o.tBodies[k].rows[j];else for(j=1;j<o.tBodies[k].rows.length;j++)c[j-1]=o.tBodies[k].rows[j];for(c.sort(sortfn),"down"==r.getAttribute("sortdir")?(ARROW=' <img src="'+image_path+image_down+'" alt="↓"/>',c.reverse(),r.setAttribute("sortdir","up")):(ARROW=' <img src="'+image_path+image_up+'" alt="↑"/>',r.setAttribute("sortdir","down")),l=0;l<c.length;l++)(!c[l].className||c[l].className&&-1==c[l].className.indexOf("sortbottom"))&&o.tBodies[0].appendChild(c[l]);for(l=0;l<c.length;l++)c[l].className&&-1!=c[l].className.indexOf("sortbottom")&&o.tBodies[0].appendChild(c[l]);var _=document.getElementsByTagName("span");for(s=0;s<_.length;s++)"sortarrow"==_[s].className&&getParent(_[s],"table")==getParent(e,"table")&&(_[s].innerHTML=' <img src="'+image_path+image_none+'" alt="↓"/>');r.innerHTML=ARROW,alternate(o)}}}function getParent(e,t){return null==e?null:1==e.nodeType&&e.tagName.toLowerCase()==t.toLowerCase()?e:getParent(e.parentNode,t)}function sort_date(e){if(dt="00000000",11==e.length){switch(mtstr=e.substr(3,3),mtstr=mtstr.toLowerCase(),mtstr){case"jan":var t="01";break;case"feb":t="02";break;case"mar":t="03";break;case"apr":t="04";break;case"may":t="05";break;case"jun":t="06";break;case"jul":t="07";break;case"aug":t="08";break;case"sep":t="09";break;case"oct":t="10";break;case"nov":t="11";break;case"dec":t="12"}return dt=e.substr(7,4)+t+e.substr(0,2),dt}return 10==e.length?0==europeandate?(dt=e.substr(6,4)+e.substr(0,2)+e.substr(3,2),dt):(dt=e.substr(6,4)+e.substr(3,2)+e.substr(0,2),dt):8==e.length?(yr=e.substr(6,2),parseInt(yr)<50?yr="20"+yr:yr="19"+yr,1==europeandate?(dt=yr+e.substr(3,2)+e.substr(0,2),dt):(dt=yr+e.substr(0,2)+e.substr(3,2),dt)):dt}function ts_sort_date(e,t){return dt1=sort_date(ts_getInnerText(e.cells[SORT_COLUMN_INDEX])),dt2=sort_date(ts_getInnerText(t.cells[SORT_COLUMN_INDEX])),dt1==dt2?0:dt1<dt2?-1:1}function ts_sort_numeric(e,t){var r=ts_getInnerText(e.cells[SORT_COLUMN_INDEX]);r=clean_num(r);var s=ts_getInnerText(t.cells[SORT_COLUMN_INDEX]);return compare_numeric(r,s=clean_num(s))}function compare_numeric(e,t){e=parseFloat(e);e=isNaN(e)?0:e;t=parseFloat(t);return e-(t=isNaN(t)?0:t)}function ts_sort_caseinsensitive(e,t){return aa=ts_getInnerText(e.cells[SORT_COLUMN_INDEX]).toLowerCase(),bb=ts_getInnerText(t.cells[SORT_COLUMN_INDEX]).toLowerCase(),aa==bb?0:aa<bb?-1:1}function ts_sort_default(e,t){return aa=ts_getInnerText(e.cells[SORT_COLUMN_INDEX]),bb=ts_getInnerText(t.cells[SORT_COLUMN_INDEX]),aa==bb?0:aa<bb?-1:1}function addEvent(e,t,r,s){return e.addEventListener?(e.addEventListener(t,r,s),!0):e.attachEvent?e.attachEvent("on"+t,r):void alert("Handler could not be removed")}function clean_num(e){return e=e.replace(new RegExp(/[^-?0-9.]/g),"")}function trim(e){return e.replace(/^\s+|\s+$/g,"")}function alternate(e){for(var t=e.getElementsByTagName("tbody"),r=0;r<t.length;r++)for(var s=t[r].getElementsByTagName("tr"),a=0;a<s.length;a++)a%2==0?-1!=s[a].className.indexOf("odd")?s[a].className=s[a].className.replace("odd","even"):-1==s[a].className.indexOf("even")&&(s[a].className+=" even"):-1!=s[a].className.indexOf("even")?s[a].className=s[a].className.replace("even","odd"):-1==s[a].className.indexOf("odd")&&(s[a].className+=" odd")}