mkdocs-benoit.jp.net/docs/javascripts/tablesort.min.js
2023-02-20 21:39:52 +09:00

1 line
No EOL
3 KiB
JavaScript

(function(){function e(t,n){if(!(this instanceof e))return new e(t,n);if(!t||t.tagName!=="TABLE")throw new Error("Element must be a table");this.init(t,n||{})}var t=[],n=function(e){var t;return!window.CustomEvent||typeof window.CustomEvent!="function"?(t=document.createEvent("CustomEvent"),t.initCustomEvent(e,!1,!1,void 0)):t=new CustomEvent(e),t},s=function(e,t){return e.getAttribute(t.sortAttribute||"data-sort")||e.textContent||e.innerText||""},a=function(e,t){return e=e.trim().toLowerCase(),t=t.trim().toLowerCase(),e===t?0:e<t?1:-1},o=function(e,t){return[].slice.call(e).find(function(e){return e.getAttribute("data-sort-column-key")===t})},i=function(e,t){return function(n,s){var o=e(n.td,s.td);return o===0?t?s.index-n.index:n.index-s.index:o}};e.extend=function(e,n,s){if(typeof n!="function"||typeof s!="function")throw new Error("Pattern and sort must be a function");t.push({name:e,pattern:n,sort:s})},e.prototype={init:function(e,t){var s,o,i,a,r,n=this;if(n.table=e,n.thead=!1,n.options=t,e.rows&&e.rows.length>0)if(e.tHead&&e.tHead.rows.length>0){for(s=0;s<e.tHead.rows.length;s++)if(e.tHead.rows[s].getAttribute("data-sort-method")==="thead"){o=e.tHead.rows[s];break}o||(o=e.tHead.rows[e.tHead.rows.length-1]),n.thead=!0}else o=e.rows[0];if(!o)return;r=function(){n.current&&n.current!==this&&n.current.removeAttribute("aria-sort"),n.current=this,n.sortTable(this)};for(s=0;s<o.cells.length;s++)i=o.cells[s],i.setAttribute("role","columnheader"),i.getAttribute("data-sort-method")!=="none"&&(i.tabindex=0,i.addEventListener("click",r,!1),i.getAttribute("data-sort-default")!==null&&(a=i));a&&(n.current=a,n.sortTable(a))},sortTable:function(e,r){var h,l=this,v=e.getAttribute("data-sort-column-key"),_=e.cellIndex,g=a,c="",p=[],d=l.thead?0:1,j=e.getAttribute("data-sort-method"),m=e.getAttribute("aria-sort");if(l.table.dispatchEvent(n("beforeSort")),r||(m==="ascending"?m="descending":m==="descending"?m="ascending":m=l.options.descending?"descending":"ascending",e.setAttribute("aria-sort",m)),l.table.rows.length<2)return;if(!j){for(;p.length<3&&d<l.table.tBodies[0].rows.length;)v?h=o(l.table.tBodies[0].rows[d].cells,v):h=l.table.tBodies[0].rows[d].cells[_],c=h?s(h,l.options):"",c=c.trim(),c.length>0&&p.push(c),d++;if(!p)return}for(d=0;d<t.length;d++)if(c=t[d],j){if(c.name===j){g=c.sort;break}}else if(p.every(c.pattern)){g=c.sort;break}l.col=_;for(d=0;d<l.table.tBodies.length;d++){var u,f=[],y={},b=0,w=0;if(l.table.tBodies[d].rows.length<2)continue;for(u=0;u<l.table.tBodies[d].rows.length;u++)c=l.table.tBodies[d].rows[u],c.getAttribute("data-sort-method")==="none"?y[b]=c:(v?h=o(c.cells,v):h=c.cells[l.col],f.push({tr:c,td:h?s(h,l.options):"",index:b})),b++;m==="descending"?f.sort(i(g,!0)):(f.sort(i(g,!1)),f.reverse());for(u=0;u<b;u++)y[u]?(c=y[u],w++):c=f[u-w].tr,l.table.tBodies[d].appendChild(c)}l.table.dispatchEvent(n("afterSort"))},refresh:function(){this.current!==void 0&&this.sortTable(this.current,!0)}},typeof module!="undefined"&&module.exports?module.exports=e:window.Tablesort=e})()