Skip to content

Instantly share code, notes, and snippets.

@jasonlyles
Created March 3, 2011 20:12
Show Gist options
  • Save jasonlyles/853438 to your computer and use it in GitHub Desktop.
Save jasonlyles/853438 to your computer and use it in GitHub Desktop.
var stIsIE=false;sorttable={init:function(){if(arguments.callee.done){return }arguments.callee.done=true;if(_timer){clearInterval(_timer)}if(!document.createElement||!document.getElementsByTagName){return }sorttable.DATE_RE=/^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;forEach(document.getElementsByTagName("table"),function(A){if(A.className.search(/\bsortable\b/)!=-1){sorttable.makeSortable(A)}})},makeSortable:function(B){if(B.getElementsByTagName("thead").length==0){the=document.createElement("thead");the.appendChild(B.rows[0]);B.insertBefore(the,B.firstChild)}if(B.tHead==null){B.tHead=B.getElementsByTagName("thead")[0]}if(B.tHead.rows.length!=1){return }sortbottomrows=[];for(var A=0;A<B.rows.length;A++){if(B.rows[A].className.search(/\bsortbottom\b/)!=-1){sortbottomrows[sortbottomrows.length]=B.rows[A]}}if(sortbottomrows){if(B.tFoot==null){tfo=document.createElement("tfoot");B.appendChild(tfo)}for(var A=0;A<sortbottomrows.length;A++){tfo.appendChild(sortbottomrows[A])}delete sortbottomrows}headrow=B.tHead.rows[0].cells;for(var A=0;A<headrow.length;A++){if(!headrow[A].className.match(/\bsorttable_nosort\b/)){mtch=headrow[A].className.match(/\bsorttable_([a-z0-9]+)\b/);if(mtch){override=mtch[1]}if(mtch&&typeof sorttable["sort_"+override]=="function"){headrow[A].sorttable_sortfunction=sorttable["sort_"+override]}else{headrow[A].sorttable_sortfunction=sorttable.guessType(B,A)}headrow[A].sorttable_columnindex=A;headrow[A].sorttable_tbody=B.tBodies[0];dean_addEvent(headrow[A],"click",function(D){if(this.className.search(/\bsorttable_sorted\b/)!=-1){sorttable.reverse(this.sorttable_tbody);this.className=this.className.replace("sorttable_sorted","sorttable_sorted_reverse");this.removeChild(document.getElementById("sorttable_sortfwdind"));sortrevind=document.createElement("span");sortrevind.id="sorttable_sortrevind";sortrevind.innerHTML=stIsIE?'&nbsp<font face="webdings">5</font>':"&nbsp;&#x25B4;";this.appendChild(sortrevind);return }if(this.className.search(/\bsorttable_sorted_reverse\b/)!=-1){sorttable.reverse(this.sorttable_tbody);this.className=this.className.replace("sorttable_sorted_reverse","sorttable_sorted");this.removeChild(document.getElementById("sorttable_sortrevind"));sortfwdind=document.createElement("span");sortfwdind.id="sorttable_sortfwdind";sortfwdind.innerHTML=stIsIE?'&nbsp<font face="webdings">6</font>':"&nbsp;&#x25BE;";this.appendChild(sortfwdind);return }theadrow=this.parentNode;forEach(theadrow.childNodes,function(E){if(E.nodeType==1){E.className=E.className.replace("sorttable_sorted_reverse","");E.className=E.className.replace("sorttable_sorted","")}});sortfwdind=document.getElementById("sorttable_sortfwdind");if(sortfwdind){sortfwdind.parentNode.removeChild(sortfwdind)}sortrevind=document.getElementById("sorttable_sortrevind");if(sortrevind){sortrevind.parentNode.removeChild(sortrevind)}this.className+=" sorttable_sorted";sortfwdind=document.createElement("span");sortfwdind.id="sorttable_sortfwdind";sortfwdind.innerHTML=stIsIE?'&nbsp<font face="webdings">6</font>':"&nbsp;&#x25BE;";this.appendChild(sortfwdind);row_array=[];col=this.sorttable_columnindex;rows=this.sorttable_tbody.rows;for(var C=0;C<rows.length;C++){row_array[row_array.length]=[sorttable.getInnerText(rows[C].cells[col]),rows[C]]}row_array.sort(this.sorttable_sortfunction);tb=this.sorttable_tbody;for(var C=0;C<row_array.length;C++){tb.appendChild(row_array[C][1])}delete row_array})}}},guessType:function(C,B){sortfn=sorttable.sort_alpha;for(var A=0;A<C.tBodies[0].rows.length;A++){text=sorttable.getInnerText(C.tBodies[0].rows[A].cells[B]);if(text!=""){if(text.match(/^-?[£$¤]?[\d,.]+%?$/)){return sorttable.sort_numeric}possdate=text.match(sorttable.DATE_RE);if(possdate){first=parseInt(possdate[1]);second=parseInt(possdate[2]);if(first>12){return sorttable.sort_ddmm}else{if(second>12){return sorttable.sort_mmdd}else{sortfn=sorttable.sort_ddmm}}}}}return sortfn},getInnerText:function(B){hasInputs=(typeof B.getElementsByTagName=="function")&&B.getElementsByTagName("input").length;if(B.getAttribute("sorttable_customkey")!=null){return B.getAttribute("sorttable_customkey")}else{if(typeof B.textContent!="undefined"&&!hasInputs){return B.textContent.replace(/^\s+|\s+$/g,"")}else{if(typeof B.innerText!="undefined"&&!hasInputs){return B.innerText.replace(/^\s+|\s+$/g,"")}else{if(typeof B.text!="undefined"&&!hasInputs){return B.text.replace(/^\s+|\s+$/g,"")}else{switch(B.nodeType){case 3:if(B.nodeName.toLowerCase()=="input"){return B.value.replace(/^\s+|\s+$/g,"")}case 4:return B.nodeValue.replace(/^\s+|\s+$/g,"");break;case 1:case 11:var C="";for(var A=0;A<B.childNodes.length;A++){C+=sorttable.getInnerText(B.childNodes[A])}return C.replace(/^\s+|\s+$/g,"");break;default:return""}}}}}},reverse:function(A){newrows=[];for(var B=0;B<A.rows.length;B++){newrows[newrows.length]=A.rows[B]}for(var B=newrows.length-1;B>=0;B--){A.appendChild(newrows[B])}delete newrows},sort_numeric:function(B,A){aa=parseFloat(B[0].replace(/[^0-9.-]/g,""));if(isNaN(aa)){aa=0}bb=parseFloat(A[0].replace(/[^0-9.-]/g,""));if(isNaN(bb)){bb=0}return aa-bb},sort_alpha:function(B,A){if(B[0]==A[0]){return 0}if(B[0]<A[0]){return -1}return 1},sort_ddmm:function(B,A){mtch=B[0].match(sorttable.DATE_RE);y=mtch[3];m=mtch[2];d=mtch[1];if(m.length==1){m="0"+m}if(d.length==1){d="0"+d}dt1=y+m+d;mtch=A[0].match(sorttable.DATE_RE);y=mtch[3];m=mtch[2];d=mtch[1];if(m.length==1){m="0"+m}if(d.length==1){d="0"+d}dt2=y+m+d;if(dt1==dt2){return 0}if(dt1<dt2){return -1}return 1},sort_mmdd:function(B,A){mtch=B[0].match(sorttable.DATE_RE);y=mtch[3];d=mtch[2];m=mtch[1];if(m.length==1){m="0"+m}if(d.length==1){d="0"+d}dt1=y+m+d;mtch=A[0].match(sorttable.DATE_RE);y=mtch[3];d=mtch[2];m=mtch[1];if(m.length==1){m="0"+m}if(d.length==1){d="0"+d}dt2=y+m+d;if(dt1==dt2){return 0}if(dt1<dt2){return -1}return 1},shaker_sort:function(F,D){var A=0;var C=F.length-1;var G=true;while(G){G=false;for(var B=A;B<C;++B){if(D(F[B],F[B+1])>0){var E=F[B];F[B]=F[B+1];F[B+1]=E;G=true}}C--;if(!G){break}for(var B=C;B>A;--B){if(D(F[B],F[B-1])<0){var E=F[B];F[B]=F[B-1];F[B-1]=E;G=true}}A++}}};if(document.addEventListener){document.addEventListener("DOMContentLoaded",sorttable.init,false);
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
if (this.readyState == "complete") {
sorttable.init(); // call the onload handler
}
};
/*@end @*/
}if(/WebKit/i.test(navigator.userAgent)){var _timer=setInterval(function(){if(/loaded|complete/.test(document.readyState)){sorttable.init()}},10)}window.onload=sorttable.init;function dean_addEvent(B,D,C){if(B.addEventListener){B.addEventListener(D,C,false)}else{if(!C.$$guid){C.$$guid=dean_addEvent.guid++}if(!B.events){B.events={}}var A=B.events[D];if(!A){A=B.events[D]={};if(B["on"+D]){A[0]=B["on"+D]}}A[C.$$guid]=C;B["on"+D]=handleEvent}}dean_addEvent.guid=1;function removeEvent(A,C,B){if(A.removeEventListener){A.removeEventListener(C,B,false)}else{if(A.events&&A.events[C]){delete A.events[C][B.$$guid]}}}function handleEvent(D){var C=true;D=D||fixEvent(((this.ownerDocument||this.document||this).parentWindow||window).event);var A=this.events[D.type];for(var B in A){this.$$handleEvent=A[B];if(this.$$handleEvent(D)===false){C=false}}return C}function fixEvent(A){A.preventDefault=fixEvent.preventDefault;A.stopPropagation=fixEvent.stopPropagation;return A}fixEvent.preventDefault=function(){this.returnValue=false};fixEvent.stopPropagation=function(){this.cancelBubble=true};if(!Array.forEach){Array.forEach=function(D,C,B){for(var A=0;A<D.length;A++){C.call(B,D[A],A,D)}}}Function.prototype.forEach=function(A,D,C){for(var B in A){if(typeof this.prototype[B]=="undefined"){D.call(C,A[B],B,A)}}};String.forEach=function(A,C,B){Array.forEach(A.split(""),function(E,D){C.call(B,E,D,A)})};var forEach=function(A,D,B){if(A){var C=Object;if(A instanceof Function){C=Function}else{if(A.forEach instanceof Function){A.forEach(D,B);return }else{if(typeof A=="string"){C=String}else{if(typeof A.length=="number"){C=Array}}}}C.forEach(A,D,B)}};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment