This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.tablesorter.min.js
executable file
·2 lines (2 loc) · 13.8 KB
/
jquery.tablesorter.min.js
1
2
/* TableSorter 2.0 HACKED ACCORDING TO http://stackoverflow.com/questions/6627737/jquery-tablesorter-how-to-disable-sorting-on-a-column-by-using-a-class-instead */
(function($){$.extend({tablesorter:new function(){function benchmark(e,t){log(e+","+((new Date).getTime()-t.getTime())+"ms")}function log(e){if(typeof console!="undefined"&&typeof console.debug!="undefined"){console.log(e)}else{alert(e)}}function buildParserCache(e,t){if(e.config.debug){var n=""}if(e.tBodies.length==0)return;var r=e.tBodies[0].rows;if(r[0]){var i=[],s=r[0].cells,o=s.length;for(var u=0;u<o;u++){var a=false;if($.metadata&&$(t[u]).metadata()&&$(t[u]).metadata().sorter){a=getParserById($(t[u]).metadata().sorter)}else if(e.config.headers[u]&&e.config.headers[u].sorter){a=getParserById(e.config.headers[u].sorter)}if(!a){a=detectParserForColumn(e,r,-1,u)}if(e.config.debug){n+="column:"+u+" parser:"+a.id+"\n"}i.push(a)}}if(e.config.debug){log(n)}return i}function detectParserForColumn(e,t,n,r){var i=parsers.length,s=false,o=false,u=true;while(o==""&&u){n++;if(t[n]){s=getNodeFromRowAndCellIndex(t,n,r);o=trimAndGetNodeText(e.config,s);if(e.config.debug){log("Checking if value was empty on row:"+n)}}else{u=false}}for(var a=1;a<i;a++){if(parsers[a].is(o,e,s)){return parsers[a]}}return parsers[0]}function getNodeFromRowAndCellIndex(e,t,n){return e[t].cells[n]}function trimAndGetNodeText(e,t){return $.trim(getElementText(e,t))}function getParserById(e){var t=parsers.length;for(var n=0;n<t;n++){if(parsers[n].id.toLowerCase()==e.toLowerCase()){return parsers[n]}}return false}function buildCache(e){if(e.config.debug){var t=new Date}var n=e.tBodies[0]&&e.tBodies[0].rows.length||0,r=e.tBodies[0].rows[0]&&e.tBodies[0].rows[0].cells.length||0,i=e.config.parsers,s={row:[],normalized:[]};for(var o=0;o<n;++o){var u=$(e.tBodies[0].rows[o]),a=[];if(u.hasClass(e.config.cssChildRow)){s.row[s.row.length-1]=s.row[s.row.length-1].add(u);continue}s.row.push(u);for(var f=0;f<r;++f){a.push(i[f].format(getElementText(e.config,u[0].cells[f]),e,u[0].cells[f]))}a.push(s.normalized.length);s.normalized.push(a);a=null}if(e.config.debug){benchmark("Building cache for "+n+" rows:",t)}return s}function getElementText(e,t){var n="";if(!t)return"";if(!e.supportsTextContent)e.supportsTextContent=t.textContent||false;if(e.textExtraction=="simple"){if(e.supportsTextContent){n=t.textContent}else{if(t.childNodes[0]&&t.childNodes[0].hasChildNodes()){n=t.childNodes[0].innerHTML}else{n=t.innerHTML}}}else{if(typeof e.textExtraction=="function"){n=e.textExtraction(t)}else{n=$(t).text()}}return n}function appendToTable(e,t){if(e.config.debug){var n=new Date}var r=t,i=r.row,s=r.normalized,o=s.length,u=s[0].length-1,a=$(e.tBodies[0]),f=[];for(var l=0;l<o;l++){var c=s[l][u];f.push(i[c]);if(!e.config.appender){var h=i[c].length;for(var p=0;p<h;p++){a[0].appendChild(i[c][p])}}}if(e.config.appender){e.config.appender(e,f)}f=null;if(e.config.debug){benchmark("Rebuilt table:",n)}applyWidget(e);setTimeout(function(){$(e).trigger("sortEnd")},0)}function buildHeaders(e){if(e.config.debug){var t=new Date}var n=$.metadata?true:false;var r=computeTableHeaderCellIndexes(e);$tableHeaders=$(e.config.selectorHeaders,e).each(function(t){this.column=r[this.parentNode.rowIndex+"-"+this.cellIndex];this.order=formatSortingOrder(e.config.sortInitialOrder);this.count=this.order;if(checkHeaderMetadata(this)||checkHeaderOptions(e,t))this.sortDisabled=true;if(checkHeaderOptionsSortingLocked(e,t))this.order=this.lockedOrder=checkHeaderOptionsSortingLocked(e,t);if(!this.sortDisabled){var n=$(this).addClass(e.config.cssHeader);if(e.config.onRenderHeader)e.config.onRenderHeader.apply(n)}e.config.headerList[t]=this});if(e.config.debug){benchmark("Built headers:",t);log($tableHeaders)}return $tableHeaders}function computeTableHeaderCellIndexes(e){var t=[];var n={};var r=e.getElementsByTagName("THEAD")[0];var i=r.getElementsByTagName("TR");for(var s=0;s<i.length;s++){var o=i[s].cells;for(var u=0;u<o.length;u++){var a=o[u];var f=a.parentNode.rowIndex;var l=f+"-"+a.cellIndex;var c=a.rowSpan||1;var h=a.colSpan||1;var p;if(typeof t[f]=="undefined"){t[f]=[]}for(var d=0;d<t[f].length+1;d++){if(typeof t[f][d]=="undefined"){p=d;break}}n[l]=p;for(var d=f;d<f+c;d++){if(typeof t[d]=="undefined"){t[d]=[]}var v=t[d];for(var m=p;m<p+h;m++){v[m]="x"}}}}return n}function checkCellColSpan(e,t,n){var r=[],i=e.tHead.rows,s=i[n].cells;for(var o=0;o<s.length;o++){var u=s[o];if(u.colSpan>1){r=r.concat(checkCellColSpan(e,headerArr,n++))}else{if(e.tHead.length==1||u.rowSpan>1||!i[n+1]){r.push(u)}}}return r}function checkHeaderMetadata(e){if($(e).hasClass("not-sortable")){return true}return false}function checkHeaderOptions(e,t){if(e.config.headers[t]&&e.config.headers[t].sorter===false){return true}return false}function checkHeaderOptionsSortingLocked(e,t){if(e.config.headers[t]&&e.config.headers[t].lockedOrder)return e.config.headers[t].lockedOrder;return false}function applyWidget(e){var t=e.config.widgets;var n=t.length;for(var r=0;r<n;r++){getWidgetById(t[r]).format(e)}}function getWidgetById(e){var t=widgets.length;for(var n=0;n<t;n++){if(widgets[n].id.toLowerCase()==e.toLowerCase()){return widgets[n]}}}function formatSortingOrder(e){if(typeof e!="Number"){return e.toLowerCase()=="desc"?1:0}else{return e==1?1:0}}function isValueInArray(e,t){var n=t.length;for(var r=0;r<n;r++){if(t[r][0]==e){return true}}return false}function setHeadersCss(e,t,n,r){t.removeClass(r[0]).removeClass(r[1]);var i=[];t.each(function(e){if(!this.sortDisabled){i[this.column]=$(this)}});var s=n.length;for(var o=0;o<s;o++){i[n[o][0]].addClass(r[n[o][1]])}}function fixColumnWidth(e,t){var n=e.config;if(n.widthFixed){var r=$("<colgroup>");$("tr:first td",e.tBodies[0]).each(function(){r.append($("<col>").css("width",$(this).width()))});$(e).prepend(r)}}function updateHeaderSortCount(e,t){var n=e.config,r=t.length;for(var i=0;i<r;i++){var s=t[i],o=n.headerList[s[0]];o.count=s[1];o.count++}}function multisort(table,sortList,cache){if(table.config.debug){var sortTime=new Date}var dynamicExp="var sortWrapper = function(a,b) {",l=sortList.length;for(var i=0;i<l;i++){var c=sortList[i][0];var order=sortList[i][1];var s=table.config.parsers[c].type=="text"?order==0?makeSortFunction("text","asc",c):makeSortFunction("text","desc",c):order==0?makeSortFunction("numeric","asc",c):makeSortFunction("numeric","desc",c);var e="e"+i;dynamicExp+="var "+e+" = "+s;dynamicExp+="if("+e+") { return "+e+"; } ";dynamicExp+="else { "}var orgOrderCol=cache.normalized[0].length-1;dynamicExp+="return a["+orgOrderCol+"]-b["+orgOrderCol+"];";for(var i=0;i<l;i++){dynamicExp+="}; "}dynamicExp+="return 0; ";dynamicExp+="}; ";if(table.config.debug){benchmark("Evaling expression:"+dynamicExp,new Date)}eval(dynamicExp);cache.normalized.sort(sortWrapper);if(table.config.debug){benchmark("Sorting on "+sortList.toString()+" and dir "+order+" time:",sortTime)}return cache}function makeSortFunction(e,t,n){var r="a["+n+"]",i="b["+n+"]";if(e=="text"&&t=="asc"){return"("+r+" == "+i+" ? 0 : ("+r+" === null ? Number.POSITIVE_INFINITY : ("+i+" === null ? Number.NEGATIVE_INFINITY : ("+r+" < "+i+") ? -1 : 1 )));"}else if(e=="text"&&t=="desc"){return"("+r+" == "+i+" ? 0 : ("+r+" === null ? Number.POSITIVE_INFINITY : ("+i+" === null ? Number.NEGATIVE_INFINITY : ("+i+" < "+r+") ? -1 : 1 )));"}else if(e=="numeric"&&t=="asc"){return"("+r+" === null && "+i+" === null) ? 0 :("+r+" === null ? Number.POSITIVE_INFINITY : ("+i+" === null ? Number.NEGATIVE_INFINITY : "+r+" - "+i+"));"}else if(e=="numeric"&&t=="desc"){return"("+r+" === null && "+i+" === null) ? 0 :("+r+" === null ? Number.POSITIVE_INFINITY : ("+i+" === null ? Number.NEGATIVE_INFINITY : "+i+" - "+r+"));"}}function makeSortText(e){return"((a["+e+"] < b["+e+"]) ? -1 : ((a["+e+"] > b["+e+"]) ? 1 : 0));"}function makeSortTextDesc(e){return"((b["+e+"] < a["+e+"]) ? -1 : ((b["+e+"] > a["+e+"]) ? 1 : 0));"}function makeSortNumeric(e){return"a["+e+"]-b["+e+"];"}function makeSortNumericDesc(e){return"b["+e+"]-a["+e+"];"}function sortText(e,t){if(table.config.sortLocaleCompare)return e.localeCompare(t);return e<t?-1:e>t?1:0}function sortTextDesc(e,t){if(table.config.sortLocaleCompare)return t.localeCompare(e);return t<e?-1:t>e?1:0}function sortNumeric(e,t){return e-t}function sortNumericDesc(e,t){return t-e}function getCachedSortType(e,t){return e[t].type}var parsers=[],widgets=[];this.defaults={cssHeader:"header",cssAsc:"headerSortUp",cssDesc:"headerSortDown",cssChildRow:"expand-child",sortInitialOrder:"asc",sortMultiSortKey:"shiftKey",sortForce:null,sortAppend:null,sortLocaleCompare:true,textExtraction:"simple",parsers:{},widgets:[],widgetZebra:{css:["even","odd"]},headers:{},widthFixed:false,cancelSelection:true,sortList:[],headerList:[],dateFormat:"us",decimal:"/.|,/g",onRenderHeader:null,selectorHeaders:"thead th",debug:false};this.benchmark=benchmark;this.construct=function(e){return this.each(function(){if(!this.tHead||!this.tBodies)return;var t,n,r,i,s,o=0,u;this.config={};s=$.extend(this.config,$.tablesorter.defaults,e);t=$(this);$.data(this,"tablesorter",s);r=buildHeaders(this);this.config.parsers=buildParserCache(this,r);i=buildCache(this);var a=[s.cssDesc,s.cssAsc];fixColumnWidth(this);r.click(function(e){if(_gui.sortdisable)return;var n=t[0].tBodies[0]&&t[0].tBodies[0].rows.length||0;if(!this.sortDisabled&&n>0){t.trigger("sortStart");var o=$(this);var u=this.column;this.order=this.count++%2;if(this.lockedOrder)this.order=this.lockedOrder;if(!e[s.sortMultiSortKey]){s.sortList=[];if(s.sortForce!=null){var f=s.sortForce;for(var l=0;l<f.length;l++){if(f[l][0]!=u){s.sortList.push(f[l])}}}s.sortList.push([u,this.order])}else{if(isValueInArray(u,s.sortList)){for(var l=0;l<s.sortList.length;l++){var c=s.sortList[l],h=s.headerList[c[0]];if(c[0]==u){h.count=c[1];h.count++;c[1]=h.count%2}}}else{s.sortList.push([u,this.order])}}setTimeout(function(){setHeadersCss(t[0],r,s.sortList,a);appendToTable(t[0],multisort(t[0],s.sortList,i))},1);return false}}).mousedown(function(){if(s.cancelSelection){this.onselectstart=function(){return false};return false}});t.bind("update",function(){var e=this;setTimeout(function(){e.config.parsers=buildParserCache(e,r);i=buildCache(e)},1)}).bind("updateCell",function(e,t){var n=this.config;var r=[t.parentNode.rowIndex-1,t.cellIndex];i.normalized[r[0]][r[1]]=n.parsers[r[1]].format(getElementText(n,t),t)}).bind("sorton",function(e,t){$(this).trigger("sortStart");s.sortList=t;var n=s.sortList;updateHeaderSortCount(this,n);setHeadersCss(this,r,n,a);appendToTable(this,multisort(this,n,i))}).bind("appendCache",function(){appendToTable(this,i)}).bind("applyWidgetId",function(e,t){getWidgetById(t).format(this)}).bind("applyWidgets",function(){applyWidget(this)});if($.metadata&&$(this).metadata()&&$(this).metadata().sortlist){s.sortList=$(this).metadata().sortlist}if(s.sortList.length>0){t.trigger("sorton",[s.sortList])}applyWidget(this)})};this.addParser=function(e){var t=parsers.length,n=true;for(var r=0;r<t;r++){if(parsers[r].id.toLowerCase()==e.id.toLowerCase()){n=false}}if(n){parsers.push(e)}};this.addWidget=function(e){widgets.push(e)};this.formatFloat=function(e){var t=parseFloat(e);return isNaN(t)?0:t};this.formatInt=function(e){var t=parseInt(e);return isNaN(t)?0:t};this.isDigit=function(e,t){return/^[-+]?\d*$/.test($.trim(e.replace(/[,.']/g,"")))};this.clearTableBody=function(e){if($.browser.msie){function t(){while(this.firstChild)this.removeChild(this.firstChild)}t.apply(e.tBodies[0])}else{e.tBodies[0].innerHTML=""}}}});$.fn.extend({tablesorter:$.tablesorter.construct});var ts=$.tablesorter;ts.addParser({id:"text",is:function(e){return true},format:function(e){return $.trim(e.toLocaleLowerCase())},type:"text"});ts.addParser({id:"digit",is:function(e,t){var n=t.config;return $.tablesorter.isDigit(e,n)},format:function(e){return $.tablesorter.formatFloat(e)},type:"numeric"});ts.addParser({id:"currency",is:function(e){return/^[£$€?.]/.test(e)},format:function(e){return $.tablesorter.formatFloat(e.replace(new RegExp(/[£$€]/g),""))},type:"numeric"});ts.addParser({id:"ipAddress",is:function(e){return/^\d{2,3}[\.]\d{2,3}[\.]\d{2,3}[\.]\d{2,3}$/.test(e)},format:function(e){var t=e.split("."),n="",r=t.length;for(var i=0;i<r;i++){var s=t[i];if(s.length==2){n+="0"+s}else{n+=s}}return $.tablesorter.formatFloat(n)},type:"numeric"});ts.addParser({id:"url",is:function(e){return/^(https?|ftp|file):\/\/$/.test(e)},format:function(e){return jQuery.trim(e.replace(new RegExp(/(https?|ftp|file):\/\//),""))},type:"text"});ts.addParser({id:"isoDate",is:function(e){return/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(e)},format:function(e){return $.tablesorter.formatFloat(e!=""?(new Date(e.replace(new RegExp(/-/g),"/"))).getTime():"0")},type:"numeric"});ts.addParser({id:"percent",is:function(e){return/\%$/.test($.trim(e))},format:function(e){return $.tablesorter.formatFloat(e.replace(new RegExp(/%/g),""))},type:"numeric"});ts.addParser({id:"usLongDate",is:function(e){return e.match(new RegExp(/^[A-Za-z]{3,10}\.? [0-9]{1,2}, ([0-9]{4}|'?[0-9]{2}) (([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(AM|PM)))$/))},format:function(e){return $.tablesorter.formatFloat((new Date(e)).getTime())},type:"numeric"});ts.addParser({id:"shortDate",is:function(e){return/\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}/.test(e)},format:function(e,t){var n=t.config;e=e.replace(/\-/g,"/");if(n.dateFormat=="us"){e=e.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/,"$3/$1/$2")}else if(n.dateFormat=="uk"){e=e.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/,"$3/$2/$1")}else if(n.dateFormat=="dd/mm/yy"||n.dateFormat=="dd-mm-yy"){e=e.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2})/,"$1/$2/$3")}return $.tablesorter.formatFloat((new Date(e)).getTime())},type:"numeric"});ts.addParser({id:"time",is:function(e){return/^(([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(am|pm)))$/.test(e)},format:function(e){return $.tablesorter.formatFloat((new Date("2000/01/01 "+e)).getTime())},type:"numeric"});ts.addParser({id:"metadata",is:function(e){return false},format:function(e,t,n){var r=t.config,i=!r.parserMetadataName?"sortValue":r.parserMetadataName;return $(n).metadata()[i]},type:"numeric"})})(jQuery);