|
|
@@ -431,21 +408,32 @@ tr.file:hover td .zip {
}
}
};
- function toggle(className) {
- var order = getUrlParameter('order');
- var elements = document.getElementsByClassName(className);
- for(var i = 0, length = elements.length; i < length; i++) {
- var currHref = elements[i].href;
- if(order=='desc') {
- var chg = currHref.replace('desc', 'asc');
- elements[i].href = chg;
- }
- if(order=='asc') {
- var chg = currHref.replace('asc', 'desc');
- elements[i].href = chg;
- }
- }
- };
+ // Sort the listing by the column of th, toggling the direction on repeated clicks
+ function sortBy(th) {
+ var key = th.getAttribute('data-key');
+ var asc = th.getAttribute('aria-sort') != 'ascending';
+ document.querySelectorAll('th[aria-sort]').forEach(function(el) { el.removeAttribute('aria-sort'); });
+ th.setAttribute('aria-sort', asc ? 'ascending' : 'descending');
+ var value = function(tr) {
+ if (key == 'name') return tr.querySelector('.name').textContent.trim();
+ if (key == 'size') return Number(tr.children[2].getAttribute('data-order'));
+ var time = tr.querySelector('time');
+ return time ? time.getAttribute('datetime') : '';
+ };
+ var rows = Array.prototype.slice.call(document.querySelectorAll('tr.file'));
+ rows.sort(function(a, b) {
+ // Directories first when sorting by name
+ var byDir = key == 'name' ? b.classList.contains('dir') - a.classList.contains('dir') : 0;
+ var va = value(a), vb = value(b);
+ var cmp = typeof va == 'string' ? va.localeCompare(vb, undefined, {numeric: true, sensitivity: 'base'}) : va - vb;
+ return byDir || (asc ? cmp : -cmp);
+ });
+ rows.forEach(function(tr) { tr.parentNode.appendChild(tr); });
+ return false;
+ }
+ // Show how the server sorted the listing from the ?sort= and ?order= parameters
+ var sorted = document.querySelector('th[data-key="' + (getUrlParameter('sort') || 'name').replace('dirfirst', '') + '"]');
+ if (sorted) sorted.setAttribute('aria-sort', getUrlParameter('order') == 'desc' ? 'descending' : 'ascending');
function readableFileSize(size) {
var units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
var i = 0;
|