78 lines
2.4 KiB
Plaintext
78 lines
2.4 KiB
Plaintext
(function collonadeClosure() {
|
|
'use strict';
|
|
|
|
document.addEventListener('DOMContentLoaded', function DOMContentLoaded() {
|
|
|
|
function setupAsync(wrapper) {
|
|
|
|
var table = wrapper.querySelector('#' + #{String $ dbtIdent});
|
|
var ths = Array.from(table.querySelectorAll('th.sortable'));
|
|
var pagination = wrapper.querySelector('#' + #{String $ dbtIdent} + '-pagination');
|
|
|
|
ths.forEach(function(th) {
|
|
th.addEventListener('click', clickHandler);
|
|
});
|
|
|
|
if (pagination) {
|
|
Array.from(pagination.querySelectorAll('.pagination-link'))
|
|
.forEach(function(p) {
|
|
p.addEventListener('click', clickHandler);
|
|
});
|
|
}
|
|
|
|
function clickHandler(event) {
|
|
event.preventDefault();
|
|
var url = new URL(window.location.origin + window.location.pathname + getClickDestination(this));
|
|
url.searchParams.set(#{String $ wIdent "table-only"}, 'yes');
|
|
updateTableFrom(url);
|
|
|
|
|
|
ths.forEach(function(th) {
|
|
// th.removeEventListener('click', clickHandler);
|
|
console.log('removed handler from', th);
|
|
});
|
|
}
|
|
|
|
function getClickDestination(el) {
|
|
console.log(el);
|
|
if (!el.querySelector('a')) {
|
|
return false;
|
|
}
|
|
return el.querySelector('a').getAttribute('href');
|
|
}
|
|
|
|
// fetches new sorted table from url with params and replaces contents of current table
|
|
function updateTableFrom(url) {
|
|
fetch(url, {
|
|
credentials: 'same-origin',
|
|
headers: {
|
|
'Accept': 'text/html'
|
|
}
|
|
}).then(function(response) {
|
|
if (!response.ok) {
|
|
throw ('Looks like there was a problem fetching ' + url.toString() + '. Status Code: ' + response.status);
|
|
}
|
|
return response.text();
|
|
}).then(function(data) {
|
|
// remove listeners
|
|
ths.forEach(function(th) {
|
|
th.removeEventListener('click', clickHandler);
|
|
});
|
|
|
|
// replace contents of table body
|
|
wrapper.innerHTML = data;
|
|
|
|
// set up async functionality again
|
|
setupAsync(wrapper);
|
|
table.querySelector('tbody').innerHTML = data;
|
|
}).catch(function(err) {
|
|
console.error(err);
|
|
});
|
|
}
|
|
}
|
|
|
|
var selector = '#' + #{String $ dbtIdent} + '-table-wrapper';
|
|
setupAsync(document.querySelector(selector));
|
|
});
|
|
})();
|