Files
Redmine/vendor/javascript/tablesort.min.js
Marius Balteanu acf0f9019e Support Loofah for textile (#43643):
* Add TablesortScrubber to both CommonMark and Textile Formatters. It will now be determined on the server-side whether to use tablesort.
* Migrate tablesort to Stimulus controller.
* Update tablesort to v5.7.0.
* Switch tablesort to ESM.

Patch by Takashi Kato (user:tohosaku).



git-svn-id: https://svn.redmine.org/redmine/trunk@24357 e93f8b46-1217-0410-a6f0-8f06a7374b81
2026-01-23 16:05:42 +00:00

7 lines
3.3 KiB
JavaScript

/*!
* tablesort v5.7.0 (2026-01-03)
* http://tristen.ca/tablesort/demo/
* Copyright (c) 2026 ; Licensed MIT
*/
const m=[],v=function(n){if(!window.CustomEvent||typeof window.CustomEvent!="function"){const t=document.createEvent("CustomEvent");return t.initCustomEvent(n,!1,!1,void 0),t}else return new CustomEvent(n)},A=function(n,t){const e=t.sortAttribute||"data-sort";return n.hasAttribute(e)?n.getAttribute(e):n.textContent||n.innerText||""},C=function(n,t){return n=n.trim().toLowerCase(),t=t.trim().toLowerCase(),n===t?0:n<t?1:-1},E=function(n,t){return[].slice.call(n).find(function(e){return e.getAttribute("data-sort-column-key")===t})},x=function(n,t){return function(e,s){const i=n(e.td,s.td);return i===0?t?s.index-e.index:e.index-s.index:i}};class B{static extend(t,e,s){if(typeof e!="function"||typeof s!="function")throw new Error("Pattern and sort must be a function");m.push({name:t,pattern:e,sort:s})}constructor(t,e){if(!t||t.tagName!=="TABLE")throw new Error("Element must be a table");this.table=t,this.thead=!1,this.options=e||{};const s=this.getFirstRow(t);if(!s)return;const i=this.getDefaultSort(s);i&&(this.current=i,this.sortTable(i))}getFirstRow(t){let e;if(t.rows&&t.rows.length>0)if(t.tHead&&t.tHead.rows.length>0){for(let s=0;s<t.tHead.rows.length;s++)if(t.tHead.rows[s].getAttribute("data-sort-method")==="thead"){e=t.tHead.rows[s];break}e||(e=t.tHead.rows[t.tHead.rows.length-1]),this.thead=!0}else e=t.rows[0];return e}getDefaultSort(t){const e=i=>{this.current&&this.current!==i.target&&this.current.removeAttribute("aria-sort"),this.current=i.target,this.sortTable(i.target)};let s;for(let i=0;i<t.cells.length;i++){const l=t.cells[i];l.setAttribute("role","columnheader"),l.getAttribute("data-sort-method")!=="none"&&(l.tabIndex=0,l.addEventListener("click",e,!1),l.addEventListener("keydown",function(r){r.key==="Enter"&&(r.preventDefault(),e(r))}),l.getAttribute("data-sort-default")!==null&&(s=l))}return s}sortTable(t,e){let s=t.getAttribute("data-sort-column-key"),i=t.cellIndex,l=C,r="",f=[],c=this.thead?0:1,b=t.getAttribute("data-sort-method"),y=t.hasAttribute("data-sort-reverse"),u=t.getAttribute("aria-sort");if(this.table.dispatchEvent(v("beforeSort")),e||(u==="ascending"?u="descending":u==="descending"?u="ascending":u=!!this.options.descending!=y?"descending":"ascending",t.setAttribute("aria-sort",u)),!(this.table.rows.length<2)){if(!b){let o;for(;f.length<3&&c<this.table.tBodies[0].rows.length;)s?o=E(this.table.tBodies[0].rows[c].cells,s):o=this.table.tBodies[0].rows[c].cells[i],r=o?A(o,this.options):"",r=r.trim(),r.length>0&&f.push(r),c++;if(!f)return}for(let o=0;o<m.length;o++)if(r=m[o],b){if(r.name===b){l=r.sort;break}}else if(f.every(r.pattern)){l=r.sort;break}this.col=i;for(let o=0;o<this.table.tBodies.length;o++){let d=[],w={},h=0,p=0;if(!(this.table.tBodies[o].rows.length<2)){for(let a=0;a<this.table.tBodies[o].rows.length;a++){let g;r=this.table.tBodies[o].rows[a],r.getAttribute("data-sort-method")==="none"?w[h]=r:(s?g=E(r.cells,s):g=r.cells[this.col],d.push({tr:r,td:g?A(g,this.options):"",index:h})),h++}u==="descending"?d.sort(x(l,!0)):(d.sort(x(l,!1)),d.reverse());for(let a=0;a<h;a++)w[a]?(r=w[a],p++):r=d[a-p].tr,this.table.tBodies[o].appendChild(r)}}this.table.dispatchEvent(v("afterSort"))}}refresh(){this.current!==void 0&&this.sortTable(this.current,!0)}}export{B as default};