[assets] Use dataset + use expressive names for some variables.

This commit is contained in:
Fabien Basmaison 2021-04-06 15:36:34 +02:00
parent d6ee136c10
commit 5d3d00f694
2 changed files with 21 additions and 19 deletions

View file

@ -54,18 +54,18 @@ let BookWyrm = new class {
history.back(); history.back();
} }
polling(el, delay) { polling(counter, delay) {
let poller = this; let poller = this;
delay = delay || 10000; delay = delay || 10000;
delay += (Math.random() * 1000); delay += (Math.random() * 1000);
setTimeout(function() { setTimeout(function() {
fetch('/api/updates/' + el.getAttribute('data-poll')) fetch('/api/updates/' + counter.dataset.poll)
.then(response => response.json()) .then(response => response.json())
.then(data => poller.updateCountElement(el, data)); .then(data => poller.updateCountElement(counter, data));
poller.polling(el, delay * 1.25); poller.polling(counter, delay * 1.25);
}, delay, el); }, delay, counter);
} }
updateCountElement(el, data) { updateCountElement(el, data) {
@ -84,15 +84,17 @@ let BookWyrm = new class {
this.addRemoveClass(hidden, 'hidden', !hidden); this.addRemoveClass(hidden, 'hidden', !hidden);
} }
toggleAction(e) { toggleAction(event) {
let el = e.currentTarget; let trigger = event.currentTarget;
let pressed = el.getAttribute('aria-pressed') == 'false'; let pressed = trigger.getAttribute('aria-pressed') == 'false';
let targetId = el.getAttribute('data-controls'); let targetId = trigger.dataset.controls;
// Unpress all triggers controlling the same target.
document.querySelectorAll('[data-controls="' + targetId + '"]') document.querySelectorAll('[data-controls="' + targetId + '"]')
.forEach(t => { .forEach(triggers => triggers.setAttribute(
t.setAttribute('aria-pressed', (t.getAttribute('aria-pressed') == 'false')) 'aria-pressed',
}); (triggers.getAttribute('aria-pressed') == 'false'))
);
if (targetId) { if (targetId) {
let target = document.getElementById(targetId); let target = document.getElementById(targetId);
@ -109,14 +111,14 @@ let BookWyrm = new class {
} }
// set checkbox, if appropriate // set checkbox, if appropriate
let checkbox = el.getAttribute('data-controls-checkbox'); let checkbox = trigger.dataset['controls-checkbox'];
if (checkbox) { if (checkbox) {
document.getElementById(checkbox).checked = !!pressed; document.getElementById(checkbox).checked = !!pressed;
} }
// set focus, if appropriate // set focus, if appropriate
let focus = el.getAttribute('data-focus-target'); let focus = trigger.dataset['focus-target'];
if (focus) { if (focus) {
let focusEl = document.getElementById(focus); let focusEl = document.getElementById(focus);
@ -130,7 +132,7 @@ let BookWyrm = new class {
interact(e) { interact(e) {
e.preventDefault(); e.preventDefault();
let identifier = e.target.getAttribute('data-id'); let identifier = e.target.dataset.id;
this.ajaxPost(e.target); this.ajaxPost(e.target);
@ -142,7 +144,7 @@ let BookWyrm = new class {
toggleMenu(e) { toggleMenu(e) {
let el = e.currentTarget; let el = e.currentTarget;
let expanded = el.getAttribute('aria-expanded') == 'false'; let expanded = el.getAttribute('aria-expanded') == 'false';
let targetId = el.getAttribute('data-controls'); let targetId = el.dataset.controls;
el.setAttribute('aria-expanded', expanded); el.setAttribute('aria-expanded', expanded);

View file

@ -15,8 +15,8 @@ let LocalStorageTools = new class {
// set javascript listeners // set javascript listeners
updateDisplay(e) { updateDisplay(e) {
// used in set reading goal // used in set reading goal
let key = e.target.getAttribute('data-id'); let key = e.target.dataset.id;
let value = e.target.getAttribute('data-value'); let value = e.target.dataset.value;
window.localStorage.setItem(key, value); window.localStorage.setItem(key, value);
@ -26,7 +26,7 @@ let LocalStorageTools = new class {
setDisplay(el) { setDisplay(el) {
// used in set reading goal // used in set reading goal
let key = el.getAttribute('data-hide'); let key = el.dataset.hide;
let value = window.localStorage.getItem(key); let value = window.localStorage.getItem(key);
BookWyrm.addRemoveClass(el, 'hidden', value); BookWyrm.addRemoveClass(el, 'hidden', value);