| 1 |
/** |
| 2 |
* sortable v3.2.3 |
| 3 |
* |
| 4 |
* https://www.npmjs.com/package/sortable-tablesort |
| 5 |
* https://github.com/tofsjonas/sortable |
| 6 |
* |
| 7 |
* Makes html tables sortable, No longer ie9+ 😢 |
| 8 |
* |
| 9 |
* Styling is done in css. |
| 10 |
* |
| 11 |
* Copyleft 2017 Jonas Earendel |
| 12 |
* |
| 13 |
* This is free and unencumbered software released into the public domain. |
| 14 |
* |
| 15 |
* Anyone is free to copy, modify, publish, use, compile, sell, or |
| 16 |
* distribute this software, either in source code form or as a compiled |
| 17 |
* binary, for any purpose, commercial or non-commercial, and by any |
| 18 |
* means. |
| 19 |
* |
| 20 |
* In jurisdictions that recognize copyright laws, the author or authors |
| 21 |
* of this software dedicate any and all copyright interest in the |
| 22 |
* software to the public domain. We make this dedication for the benefit |
| 23 |
* of the public at large and to the detriment of our heirs and |
| 24 |
* successors. We intend this dedication to be an overt act of |
| 25 |
* relinquishment in perpetuity of all present and future rights to this |
| 26 |
* software under copyright law. |
| 27 |
* |
| 28 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 29 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 30 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 31 |
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 32 |
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 33 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 34 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 35 |
* |
| 36 |
* For more information, please refer to <http://unlicense.org> |
| 37 |
* |
| 38 |
*/ |
| 39 |
document.addEventListener('click', function (e) { |
| 40 |
try { |
| 41 |
// allows for elements inside TH |
| 42 |
function findElementRecursive(element, tag) { |
| 43 |
return element.nodeName === tag ? element : findElementRecursive(element.parentNode, tag); |
| 44 |
} |
| 45 |
var ascending_table_sort_class = 'asc'; |
| 46 |
var no_sort_class = 'no-sort'; |
| 47 |
var null_last_class = 'n-last'; |
| 48 |
var table_class_name = 'sortable'; |
| 49 |
var alt_sort_1 = e.shiftKey || e.altKey; |
| 50 |
var element = findElementRecursive(e.target, 'TH'); |
| 51 |
var tr = element.parentNode; |
| 52 |
var thead = tr.parentNode; |
| 53 |
var table = thead.parentNode; |
| 54 |
function getValue(element) { |
| 55 |
var _a; |
| 56 |
var value = alt_sort_1 ? element.dataset.sortAlt : (_a = element.dataset.sort) !== null && _a !== void 0 ? _a : element.textContent; |
| 57 |
return value; |
| 58 |
} |
| 59 |
if (thead.nodeName === 'THEAD' && // sortable only triggered in `thead` |
| 60 |
table.classList.contains(table_class_name) && |
| 61 |
!element.classList.contains(no_sort_class) // .no-sort is now core functionality, no longer handled in CSS |
| 62 |
) { |
| 63 |
var column_index_1; |
| 64 |
var nodes = tr.cells; |
| 65 |
var tiebreaker_1 = +element.dataset.sortTbr; |
| 66 |
// Reset thead cells and get column index |
| 67 |
for (var i = 0; i < nodes.length; i++) { |
| 68 |
if (nodes[i] === element) { |
| 69 |
column_index_1 = +element.dataset.sortCol || i; |
| 70 |
} |
| 71 |
else { |
| 72 |
nodes[i].setAttribute('aria-sort', 'none'); |
| 73 |
} |
| 74 |
} |
| 75 |
var direction = 'descending'; |
| 76 |
if (element.getAttribute('aria-sort') === 'descending' || |
| 77 |
(table.classList.contains(ascending_table_sort_class) && element.getAttribute('aria-sort') !== 'ascending')) { |
| 78 |
direction = 'ascending'; |
| 79 |
} |
| 80 |
// Update the `th` class accordingly |
| 81 |
element.setAttribute('aria-sort', direction); |
| 82 |
var reverse_1 = direction === 'ascending'; |
| 83 |
var sort_null_last_1 = table.classList.contains(null_last_class); |
| 84 |
var compare_1 = function (a, b, index) { |
| 85 |
var x = getValue(b.cells[index]); |
| 86 |
var y = getValue(a.cells[index]); |
| 87 |
if (sort_null_last_1) { |
| 88 |
if (x === '' && y !== '') { |
| 89 |
return -1; |
| 90 |
} |
| 91 |
if (y === '' && x !== '') { |
| 92 |
return 1; |
| 93 |
} |
| 94 |
} |
| 95 |
var temp = +x - +y; |
| 96 |
var bool = isNaN(temp) ? x.localeCompare(y) : temp; |
| 97 |
return reverse_1 ? -bool : bool; |
| 98 |
}; |
| 99 |
// loop through all tbodies and sort them |
| 100 |
for (var i = 0; i < table.tBodies.length; i++) { |
| 101 |
var org_tbody = table.tBodies[i]; |
| 102 |
// Put the array rows in an array, so we can sort them... |
| 103 |
var rows = [].slice.call(org_tbody.rows, 0); |
| 104 |
// Sort them using Array.prototype.sort() |
| 105 |
rows.sort(function (a, b) { |
| 106 |
var bool = compare_1(a, b, column_index_1); |
| 107 |
return bool === 0 && !isNaN(tiebreaker_1) ? compare_1(a, b, tiebreaker_1) : bool; |
| 108 |
}); |
| 109 |
// Make an empty clone |
| 110 |
var clone_tbody = org_tbody.cloneNode(); |
| 111 |
// Put the sorted rows inside the clone |
| 112 |
clone_tbody.append.apply(clone_tbody, rows); |
| 113 |
// And finally replace the unsorted tbody with the sorted one |
| 114 |
table.replaceChild(clone_tbody, org_tbody); |
| 115 |
} |
| 116 |
} |
| 117 |
// eslint-disable-next-line no-unused-vars |
| 118 |
} |
| 119 |
catch (error) { |
| 120 |
// console.log(error) |
| 121 |
} |
| 122 |
}); |
| 123 |
|