-
Notifications
You must be signed in to change notification settings - Fork 0
/
tablesort.min.js
1 lines (1 loc) · 3.43 KB
/
tablesort.min.js
1
!function(){function a(a,b){if("TABLE"!==a.tagName)throw new Error("Element must be a table");this.init(a,b||{})}a.prototype={init:function(a,b){var d,c=this;if(this.thead=!1,this.options=b,this.options.d=b.descending||!1,a.rows&&a.rows.length>0&&(a.tHead&&a.tHead.rows.length>0?(d=a.tHead.rows[a.tHead.rows.length-1],c.thead=!0):d=a.rows[0]),d)for(var e=function(){for(var b=g(h,"tr").getElementsByTagName("th"),d=0;d<b.length;d++)(l(b[d],"sort-up")||l(b[d],"sort-down"))&&b[d]!==this&&(b[d].className=b[d].className.replace(" sort-down","").replace(" sort-up",""));c.current=this,c.sortTable(this)},f=0;f<d.cells.length;f++){var h=d.cells[f];l(h,"no-sort")||(h.className+=" sort-header",m(h,"click",e))}},getFirstDataRowIndex:function(){return this.thead?0:1},sortTable:function(a,b){var m,c=this,d=a.cellIndex,n=g(a,"table"),o="",p=c.getFirstDataRowIndex();if(!(n.rows.length<=1)){for(;""===o&&p<n.tBodies[0].rows.length;)o=h(n.tBodies[0].rows[p].cells[d]),o=j(o),("<!--"===o.substr(0,4)||0===o.length)&&(o=""),p++;if(""!==o){var q=function(a,b){var d=h(a.cells[c.col]).toLowerCase(),e=h(b.cells[c.col]).toLowerCase();return d===e?0:e>d?1:-1},r=function(a,b){var d=h(a.cells[c.col]),e=h(b.cells[c.col]);return d=k(d),e=k(e),i(e,d)},s=function(a,b){var d=h(a.cells[c.col]).toLowerCase(),e=h(b.cells[c.col]).toLowerCase();return f(e)-f(d)};m=o.match(/^-?[\xa3\x24\xdb\xa2\xb4\u20ac] ?\d/)||o.match(/^-?\d+\s*[\u20ac]/)||o.match(/^-?(\d+[,\.]?)+(E[\-+][\d]+)?%?$/)?r:e(o)?s:q,this.col=d;var v,t=[],u={},w=0;for(p=0;p<n.tBodies.length;p++)for(v=0;v<n.tBodies[p].rows.length;v++){var x=n.tBodies[p].rows[v];l(x,"no-sort")?u[w]=x:t.push(x),w++}t.sort(m),b||(c.options.d?l(a,"sort-up")?(a.className=a.className.replace(/ sort-up/,""),a.className+=" sort-down"):(a.className=a.className.replace(/ sort-down/,""),a.className+=" sort-up"):l(a,"sort-down")?(a.className=a.className.replace(/ sort-down/,""),a.className+=" sort-up"):(a.className=a.className.replace(/ sort-up/,""),a.className+=" sort-down")),l(a,"sort-down")&&t.reverse();var y=0;for(p=0;w>p;p++){var z;u[p]?(z=u[p],y++):z=t[p-y],n.tBodies[0].appendChild(z)}}}},refresh:function(){void 0!==this.current&&this.sortTable(this.current,!0)}};var b=/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\.?\,?\s*/i,c=/\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}/,d=/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i,e=function(a){return-1!==(-1!==a.search(b)||-1!==a.search(c)||a.search(-1!==d))&&!isNaN(f(a))},f=function(a){return a=a.replace(/\-/g,"/"),a=a.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2})/,"$1/$2/$3"),new Date(a).getTime()},g=function(a,b){return null===a?null:1===a.nodeType&&a.tagName.toLowerCase()===b.toLowerCase()?a:g(a.parentNode,b)},h=function(a){var b=this;if("string"==typeof a||"undefined"==typeof a)return a;var c=a.getAttribute("data-sort")||"";if(c)return c;if(a.textContent)return a.textContent;if(a.innerText)return a.innerText;for(var d=a.childNodes,e=d.length,f=0;e>f;f++)switch(d[f].nodeType){case 1:c+=b.getInnerText(d[f]);break;case 3:c+=d[f].nodeValue}return c},i=function(a,b){var c=parseFloat(a),d=parseFloat(b);return a=isNaN(c)?0:c,b=isNaN(d)?0:d,a-b},j=function(a){return a.replace(/^\s+|\s+$/g,"")},k=function(a){return a.replace(/[^\-?0-9.]/g,"")},l=function(a,b){return(" "+a.className+" ").indexOf(" "+b+" ")>-1},m=function(a,b,c){a.attachEvent?(a["e"+b+c]=c,a[b+c]=function(){a["e"+b+c](window.event)},a.attachEvent("on"+b,a[b+c])):a.addEventListener(b,c,!1)};"undefined"!=typeof module&&module.exports?module.exports=a:window.Tablesort=a}();