206 lines
6.4 KiB
JavaScript
206 lines
6.4 KiB
JavaScript
(function collonadeClosure() {
|
|
'use strict';
|
|
|
|
window.utils = window.utils || {};
|
|
|
|
var HEADER_HEIGHT = 80;
|
|
var RESET_OPTIONS = [ 'scrollTo' ];
|
|
|
|
window.utils.asyncTable = function(wrapper, options) {
|
|
|
|
options = options || {};
|
|
var tableIdent = options.dbtIdent;
|
|
var shortCircuitHeader = options ? options.headerDBTableShortcircuit : null;
|
|
|
|
var ths = [];
|
|
var pageLinks = [];
|
|
var pagesizeForm;
|
|
var scrollTable;
|
|
|
|
function init() {
|
|
var table = wrapper.querySelector('#' + tableIdent);
|
|
|
|
if (!table) {
|
|
return;
|
|
}
|
|
|
|
scrollTable = wrapper.querySelector('.scrolltable');
|
|
|
|
// sortable table headers
|
|
ths = Array.from(table.querySelectorAll('th.sortable')).map(function(th) {
|
|
return { element: th };
|
|
});
|
|
|
|
// pagination links
|
|
var pagination = wrapper.querySelector('#' + tableIdent + '-pagination');
|
|
if (pagination) {
|
|
pageLinks = Array.from(pagination.querySelectorAll('.page-link')).map(function(link) {
|
|
return { element: link };
|
|
});
|
|
}
|
|
|
|
// pagesize form
|
|
pagesizeForm = wrapper.querySelector('#' + tableIdent + '-pagesize-form');
|
|
|
|
// take options into account
|
|
if (options && options.scrollTo) {
|
|
window.scrollTo(options.scrollTo);
|
|
}
|
|
|
|
if (options && options.horizPos && scrollTable) {
|
|
scrollTable.scrollLeft = options.horizPos;
|
|
}
|
|
|
|
setupListeners();
|
|
wrapper.classList.add('js-initialized');
|
|
}
|
|
|
|
function setupListeners() {
|
|
ths.forEach(function(th) {
|
|
th.clickHandler = function(event) {
|
|
var boundClickHandler = clickHandler.bind(this);
|
|
var horizPos = (scrollTable || {}).scrollLeft;
|
|
boundClickHandler(event, { horizPos });
|
|
};
|
|
th.element.addEventListener('click', th.clickHandler);
|
|
});
|
|
|
|
pageLinks.forEach(function(link) {
|
|
link.clickHandler = function(event) {
|
|
var boundClickHandler = clickHandler.bind(this);
|
|
var tableBoundingRect = scrollTable.getBoundingClientRect();
|
|
var tableOptions = {};
|
|
if (tableBoundingRect.top < HEADER_HEIGHT) {
|
|
tableOptions.scrollTo = {
|
|
top: (scrollTable.offsetTop || 0) - HEADER_HEIGHT,
|
|
left: scrollTable.offsetLeft || 0,
|
|
behavior: 'smooth',
|
|
};
|
|
}
|
|
boundClickHandler(event, tableOptions);
|
|
}
|
|
link.element.addEventListener('click', link.clickHandler);
|
|
});
|
|
|
|
if (pagesizeForm) {
|
|
var pagesizeSelect = pagesizeForm.querySelector('[name=' + tableIdent + '-pagesize]');
|
|
pagesizeSelect.addEventListener('change', changePagesizeHandler);
|
|
}
|
|
}
|
|
|
|
function removeListeners() {
|
|
ths.forEach(function(th) {
|
|
th.element.removeEventListener('click', th.clickHandler);
|
|
});
|
|
|
|
pageLinks.forEach(function(link) {
|
|
link.element.removeEventListener('click', link.clickHandler);
|
|
});
|
|
|
|
if (pagesizeForm) {
|
|
var pagesizeSelect = pagesizeForm.querySelector('[name=' + tableIdent + '-pagesize]')
|
|
pagesizeSelect.removeEventListener('change', changePagesizeHandler);
|
|
}
|
|
}
|
|
|
|
function clickHandler(event, tableOptions) {
|
|
event.preventDefault();
|
|
var url = new URL(window.location.origin + window.location.pathname + getClickDestination(this));
|
|
updateTableFrom(url, tableOptions);
|
|
}
|
|
|
|
function getClickDestination(el) {
|
|
if (!el.querySelector('a')) {
|
|
return '';
|
|
}
|
|
return el.querySelector('a').getAttribute('href');
|
|
}
|
|
|
|
function changePagesizeHandler(event) {
|
|
var currentTableUrl = options.currentUrl || window.location.href;
|
|
var url = getUrlWithUpdatedPagesize(currentTableUrl, event.target.value);
|
|
url = new URL(getUrlWithResetPagenumber(url));
|
|
updateTableFrom(url);
|
|
}
|
|
|
|
function getUrlWithUpdatedPagesize(url, pagesize) {
|
|
if (url.indexOf('pagesize') >= 0) {
|
|
return url.replace(/pagesize=(\d+|all)/, 'pagesize=' + pagesize);
|
|
} else if (url.indexOf('?') >= 0) {
|
|
return url += '&' + tableIdent + '-pagesize=' + pagesize;
|
|
}
|
|
|
|
return url += '?' + tableIdent + '-pagesize=' + pagesize;
|
|
}
|
|
|
|
function getUrlWithResetPagenumber(url) {
|
|
return url.replace(/-page=\d+/, '-page=0');
|
|
}
|
|
|
|
// fetches new sorted table from url with params and replaces contents of current table
|
|
function updateTableFrom(url, tableOptions) {
|
|
tableOptions = tableOptions || {};
|
|
fetch(url, {
|
|
credentials: 'same-origin',
|
|
headers: {
|
|
'Accept': 'text/html',
|
|
[shortCircuitHeader]: tableIdent
|
|
}
|
|
}).then(function(response) {
|
|
if (!response.ok) {
|
|
throw new Error('Looks like there was a problem fetching ' + url.href + '. Status Code: ' + response.status);
|
|
}
|
|
return response.text();
|
|
}).then(function(data) {
|
|
tableOptions.currentUrl = url.href;
|
|
removeListeners();
|
|
updateWrapperContents(data, tableOptions);
|
|
}).catch(function(err) {
|
|
console.error(err);
|
|
});
|
|
}
|
|
|
|
function updateWrapperContents(newHtml, tableOptions) {
|
|
tableOptions = tableOptions || {};
|
|
wrapper.innerHTML = newHtml;
|
|
wrapper.classList.remove("js-initialized");
|
|
|
|
// setup the wrapper and its components to behave async again
|
|
window.utils.teardown('asyncTable');
|
|
window.utils.teardown('form');
|
|
// merge global options and table specific options
|
|
var resetOptions = {};
|
|
Object.keys(options)
|
|
.filter(function(key) {
|
|
return !RESET_OPTIONS.includes(key);
|
|
})
|
|
.forEach(function(key) {
|
|
resetOptions[key] = options[key];
|
|
});
|
|
var combinedOptions = {};
|
|
combinedOptions = Object.keys(tableOptions)
|
|
.filter(function(key) {
|
|
return tableOptions.hasOwnProperty(key);
|
|
})
|
|
.map(function(key) {
|
|
return { key, value: tableOptions[key] }
|
|
})
|
|
.reduce(function(cumulatedOpts, opt) {
|
|
cumulatedOpts[opt.key] = opt.value;
|
|
return cumulatedOpts;
|
|
}, resetOptions);
|
|
|
|
window.utils.setup('asyncTable', wrapper, combinedOptions);
|
|
|
|
Array.from(wrapper.querySelectorAll('form')).forEach(function(form) {
|
|
window.utils.setup('form', form);
|
|
});
|
|
Array.from(wrapper.querySelectorAll('.modal')).forEach(function(modal) {
|
|
window.utils.setup('modal', modal);
|
|
});
|
|
}
|
|
|
|
init();
|
|
};
|
|
})();
|