2021-04-06 07:44:59 +00:00
|
|
|
/* exported LocalStorageTools */
|
|
|
|
/* globals BookWyrm */
|
2021-03-31 15:07:28 +00:00
|
|
|
|
2021-04-06 07:44:59 +00:00
|
|
|
let LocalStorageTools = new class {
|
|
|
|
constructor() {
|
|
|
|
document.querySelectorAll('[data-hide]')
|
|
|
|
.forEach(t => this.setDisplay(t));
|
2021-03-18 16:02:07 +00:00
|
|
|
|
2021-04-06 07:44:59 +00:00
|
|
|
document.querySelectorAll('.set-display')
|
2021-04-06 09:37:23 +00:00
|
|
|
.forEach(t => t.addEventListener('click', this.updateDisplay.bind(this)));
|
2021-04-06 07:44:59 +00:00
|
|
|
}
|
2021-03-18 16:02:07 +00:00
|
|
|
|
2021-04-06 14:17:20 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2021-04-06 14:17:20 +00:00
|
|
|
let key = event.target.dataset.id;
|
|
|
|
let value = event.target.dataset.value;
|
2021-04-06 08:42:52 +00:00
|
|
|
|
2021-04-06 07:44:59 +00:00
|
|
|
window.localStorage.setItem(key, value);
|
2021-04-06 07:06:12 +00:00
|
|
|
|
2021-04-06 07:44:59 +00:00
|
|
|
document.querySelectorAll('[data-hide="' + key + '"]')
|
2021-04-06 14:17:20 +00:00
|
|
|
.forEach(node => this.setDisplay(node));
|
2021-04-06 07:44:59 +00:00
|
|
|
}
|
2021-04-06 07:06:12 +00:00
|
|
|
|
2021-04-06 14:17:20 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2021-04-06 14:17:20 +00:00
|
|
|
let key = node.dataset.hide;
|
2021-04-06 08:42:52 +00:00
|
|
|
let value = window.localStorage.getItem(key);
|
|
|
|
|
2021-04-07 15:31:00 +00:00
|
|
|
BookWyrm.addRemoveClass(node, 'is-hidden', value);
|
2021-04-06 07:44:59 +00:00
|
|
|
}
|
2021-06-08 19:42:45 +00:00
|
|
|
}();
|