moviewyrm/bookwyrm/static/js/localstorage.js

45 lines
1.2 KiB
JavaScript
Raw Normal View History

/* exported LocalStorageTools */
/* globals BookWyrm */
2021-12-16 18:53:38 +00:00
let LocalStorageTools = new (class {
constructor() {
2021-12-16 18:53:38 +00:00
document.querySelectorAll("[data-hide]").forEach((t) => this.setDisplay(t));
2021-12-16 18:53:38 +00:00
document
.querySelectorAll(".set-display")
.forEach((t) => t.addEventListener("click", this.updateDisplay.bind(this)));
}
/**
* Update localStorage, then display content based on keys in localStorage.
*
* @param {Event} event
* @return {undefined}
*/
updateDisplay(event) {
2021-06-08 19:42:45 +00:00
// Used in set reading goal
let key = event.target.dataset.id;
let value = event.target.dataset.value;
window.localStorage.setItem(key, value);
2021-12-16 18:53:38 +00:00
document
.querySelectorAll('[data-hide="' + key + '"]')
.forEach((node) => this.setDisplay(node));
}
/**
* Toggle display of a DOM node based on its value in the localStorage.
*
* @param {object} node - DOM node to toggle.
* @return {undefined}
*/
setDisplay(node) {
2021-06-08 19:42:45 +00:00
// Used in set reading goal
let key = node.dataset.hide;
let value = window.localStorage.getItem(key);
2021-12-16 18:53:38 +00:00
BookWyrm.addRemoveClass(node, "is-hidden", value);
}
2021-12-16 18:53:38 +00:00
})();