Files
Apaxy/apaxy/theme/apaxy.js
oupala f8670f0d59 fix: filter content works again
This commit fixes a bug where the filter content was not working any more on recent browsers.

close #125
2019-03-22 15:06:43 +01:00

77 lines
2.3 KiB
JavaScript

// fix links when not adding a / at the end of the URI
var uri = window.location.pathname.substr(1);
if (uri.substring(uri.length - 1) !== '/') {
var indexes = document.getElementsByClassName('indexcolname');
for (let i of indexes) {
var a = i.getElementsByTagName('a')[0];
a.href = '/' + a.getAttribute('href', 2);
}
}
// content filtering, based on "light javascript table filter" by Chris Coyier
// https://codepen.io/chriscoyier/pen/tIuBL - MIT License
(function(document) {
'use strict';
var TableFilter = (function(Arr) {
// the search bar element
var _input;
// find all rows of all tables and call _filter on them
function _onInputEvent(e) {
_input = e.target;
var tables = document.getElementsByTagName('table');
Arr.forEach.call(tables, function(table) {
Arr.forEach.call(table.tBodies, function(tbody) {
Arr.forEach.call(tbody.rows, _filter);
});
});
}
// show or hide a row based on the value of _input
function _filter(row) {
// skip "special" rows
if (row.className.indexOf('indexhead') != -1 || row.className.indexOf('parent') != -1) {
return;
}
// only check the 'name' field
var text = row.getElementsByTagName('td')[1].textContent.toLowerCase();
var val = _input.value.toLowerCase();
// change display type to show / hide this row
row.style.display = text.indexOf(val) === -1 ? 'none' : 'table-row';
}
return {
init: function() {
// grab the 1st child and add the indexhead class. tr:nth-child(1)
var row = document.getElementsByTagName('tr')[0];
// some versions of apache already add this class
if (row !== null && row.className.indexOf('indexhead') == -1) {
row.className += ' indexhead';
}
// grab the 2nd child and add the parent class. tr:nth-child(2)
row = document.getElementsByTagName('tr')[1];
// when apaxy is installed at doc root, there is no "parent directory" row
if (row !== null && row.getElementsByTagName('td')[1].textContent === 'Parent Directory') {
row.className += ' parent';
}
// find the search box and bind the input event
document.getElementById('filter').oninput = _onInputEvent;
}
};
})(Array.prototype);
document.addEventListener('readystatechange', function() {
if (document.readyState === 'complete') {
TableFilter.init();
}
});
})(document);