bookwyrm/bookwyrm/static/js/localstorage.js
Fabien Basmaison 6b0a3ce4b1 [assets] Move localStorage chunks of code to their own file:
This should prevent a sync issue with updateDisplay not always being loaded on time.
2021-04-06 09:06:51 +02:00

29 lines
832 B
JavaScript

/* exported updateDisplay */
/* globals addRemoveClass */
// set javascript listeners
function updateDisplay(e) {
// used in set reading goal
var key = e.target.getAttribute('data-id');
var value = e.target.getAttribute('data-value');
window.localStorage.setItem(key, value);
document.querySelectorAll('[data-hide="' + key + '"]')
.forEach(t => setDisplay(t));
}
function setDisplay(el) {
// used in set reading goal
var key = el.getAttribute('data-hide');
var value = window.localStorage.getItem(key);
addRemoveClass(el, 'hidden', value);
}
// display based on localstorage vars
document.querySelectorAll('[data-hide]')
.forEach(t => setDisplay(t));
// update localstorage
Array.from(document.getElementsByClassName('set-display'))
.forEach(t => t.onclick = updateDisplay);