-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortmytable.min.js
2 lines (2 loc) · 4.03 KB
/
sortmytable.min.js
1
2
/** SortMyTable 1.0.0 @license https://github.com/mysociety/sortmytable */
var SortMyTable=function(t){"use strict";class e{static defaults={formatHeaderButton:function(t){t.setAttribute("type","button"),t.insertAdjacentHTML("beforeend",'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 16" width="32" height="16" focusable="false" aria-hidden="true"><path class="up" d="M8.16 3.33a1 1 0 0 1 1.68 0l5.18 8.13a1 1 0 0 1-.84 1.54H3.82a1 1 0 0 1-.84-1.54l5.18-8.13Z"/><path class="down" d="M23.84 12.67a1 1 0 0 1-1.68 0l-5.18-8.13A1 1 0 0 1 17.82 3h10.36a1 1 0 0 1 .84 1.54l-5.18 8.13Z"/></svg>')},getSortableHeaders:function(t){return t.querySelectorAll("thead th")},getSortableRows:function(t){return this.getSortedRowsContainer(t).querySelectorAll("tr")},getSortableValue:function(t){let e;return e=void 0!==t.dataset.sortableValue?t.dataset.sortableValue:t.textContent,""===e||null==e?"":Number.isNaN(Number(e))?String(e):Number(e)},getSortedRowsContainer:function(t){return t.querySelector("tbody")},onSort:function(t,e,n){}};constructor(t,n){if(this.init=this.init.bind(this),this.createHeaderButtons=this.createHeaderButtons.bind(this),this.storeInitialSortOrder=this.storeInitialSortOrder.bind(this),this.sort=this.sort.bind(this),this.settings=Object.assign({},e.defaults,n),"string"==typeof t&&(t=document.querySelectorAll(t)),NodeList.prototype.isPrototypeOf(t)){if(1===t.length)return this.init(t[0]);this.instances=Array.from(t).map((function(t){return new e(t,n)}))}else{if(r(t))return this.init(t);if("object"==typeof t&&"jquery"in t){if(1===t.length)return this.init(t[0]);this.instances=t.get().map((function(t){return new e(t,n)}))}}}init(t){return t.sortmytable?t.sortmytable:(t.sortmytable=this,this.tableElement=t,this.storeInitialSortOrder(),this.createHeaderButtons(),this)}createHeaderButtons(){const t=this;t.settings.getSortableHeaders(t.tableElement).forEach((function(e,r){let i=n("button",e)[0];void 0===i&&(a(e,"button"),i=n("button",e)[0]),t.settings.formatHeaderButton(i),i.addEventListener("click",(function(){const n=e.getAttribute("aria-sort");null===n?t.sort(e,"ascending"):"ascending"===n?t.sort(e,"descending"):"descending"===n&&t.sort()}))}))}storeInitialSortOrder(){const t=this.settings.getSortableHeaders(this.tableElement),e=s(t,"[aria-sort]");1===e.length?(this.initialSortColumn=e[0],this.initialSortDirection=e[0].getAttribute("aria-sort")):this.settings.getSortableRows(this.tableElement).forEach((function(t,e){t.dataset.initialSortIndex=e}))}sort(t,e){const n=this;if(void 0===t)if(void 0!==n.initialSortColumn&&void 0!==n.initialSortDirection)n.sort(n.initialSortColumn,n.initialSortDirection);else{Array.from(n.settings.getSortableRows(n.tableElement)).sort((function(t,e){return t.dataset.initialSortIndex-e.dataset.initialSortIndex})).forEach((function(t){n.settings.getSortedRowsContainer(n.tableElement).appendChild(t)})),n.settings.getSortableHeaders(n.tableElement).forEach((function(t,e){t.removeAttribute("aria-sort")})),n.settings.onSort(n.tableElement)}else if(r(t)&&n.tableElement.contains(t))n.sort(o(t).length,e);else if(i(t)){let r=Array.from(n.settings.getSortableRows(n.tableElement)).sort((function(e,r){const o=n.settings.getSortableValue(e.children[t]),s=n.settings.getSortableValue(r.children[t]);return i(o)&&i(s)?o-s:o.toString().localeCompare(s)}));"descending"===e&&(r=r.reverse()),r.forEach((function(t){n.settings.getSortedRowsContainer(n.tableElement).appendChild(t)})),n.settings.getSortableHeaders(n.tableElement).forEach((function(n,r){r===t?n.setAttribute("aria-sort",e):n.removeAttribute("aria-sort")})),n.settings.onSort(n.tableElement,t,e)}}}const n=function(t,e=document){return e.querySelectorAll(t)},r=function(t){return Node.prototype.isPrototypeOf(t)},i=function(t){return"number"==typeof t},o=function(t){const e=[];for(;t=t.previousElementSibling;)e.push(t);return e},s=function(t,e){return Array.from(t).filter((function(t){return t.matches(e)}))},a=function(t,e="div"){for("string"==typeof e&&(e=document.createElement(e)),t.appendChild(e);t.firstChild!==e;)e.appendChild(t.firstChild);return t};return t.SortMyTable=e,t}({});