Skip to content
Snippets Groups Projects
Commit fa54ee85 authored by Sebastian Kehr's avatar Sebastian Kehr :rowboat_tone2: Committed by Dorian Merz
Browse files

refs #15903 [master-v5] browser-cached lightbox forms

* uses JS to cache form inputs during modal lifetime
* ignore input fields for passwords for better security
parent 9d804fbb
Branches
Tags
No related merge requests found
'use strict';
document.addEventListener('DOMContentLoaded', function () {
if (typeof sessionStorage === 'undefined' || typeof MutationObserver !== 'function') {
return;
}
const cache = sessionStorage;
const cacheKeyPrefix = 'lightbox_form_';
function clearCache() {
(new Array(cache.length)).fill().map((_, index) => cache.key(index))
.forEach(key => cache.removeItem(key));
}
const modal = document.querySelector('#modal .modal-body');
const config = {attributes: false, childList: true, subtree: false};
const observer = new MutationObserver(function () {
const form = modal.querySelector('form:not([data-non-cached])');
if (!form) {
return;
}
form.addEventListener('submit', clearCache);
const cacheKeyBase = Array.from(form.attributes).map(attr => attr.value);
const cacheKey = cacheKeyPrefix + btoa(cacheKeyBase.join());
const inputs = Array.from(form.querySelectorAll('input:not([type=password])'));
JSON.parse(cache.getItem(cacheKey) || '[]').forEach(function (value, index) {
inputs[index].value = value;
});
modal.querySelectorAll('a:not([data-reset-forms])').forEach(function (link) {
link.onclick = function () {
var cachedItems = JSON.stringify(inputs.map(input => input.value));
cache.setItem(cacheKey, cachedItems);
};
});
});
clearCache();
document.addEventListener('VuFind.lightbox.closed', clearCache);
observer.observe(modal, config);
});
......@@ -4,6 +4,7 @@ return [
'js' => [
'openurl.js',
'check_item_statuses.js',
'lightbox_form_cache.js',
],
'helpers' => [
'aliases' => [
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment