From 5d3d00f6946dd6c88c8e0f086fcdd1125b98abfe Mon Sep 17 00:00:00 2001 From: Fabien Basmaison Date: Tue, 6 Apr 2021 15:36:34 +0200 Subject: [PATCH] [assets] Use `dataset` + use expressive names for some variables. --- bookwyrm/static/js/bookwyrm.js | 34 ++++++++++++++++-------------- bookwyrm/static/js/localstorage.js | 6 +++--- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/bookwyrm/static/js/bookwyrm.js b/bookwyrm/static/js/bookwyrm.js index 6be1d4fb0..a434d36a3 100644 --- a/bookwyrm/static/js/bookwyrm.js +++ b/bookwyrm/static/js/bookwyrm.js @@ -54,18 +54,18 @@ let BookWyrm = new class { history.back(); } - polling(el, delay) { + polling(counter, delay) { let poller = this; delay = delay || 10000; delay += (Math.random() * 1000); setTimeout(function() { - fetch('/api/updates/' + el.getAttribute('data-poll')) + fetch('/api/updates/' + counter.dataset.poll) .then(response => response.json()) - .then(data => poller.updateCountElement(el, data)); - poller.polling(el, delay * 1.25); - }, delay, el); + .then(data => poller.updateCountElement(counter, data)); + poller.polling(counter, delay * 1.25); + }, delay, counter); } updateCountElement(el, data) { @@ -84,15 +84,17 @@ let BookWyrm = new class { this.addRemoveClass(hidden, 'hidden', !hidden); } - toggleAction(e) { - let el = e.currentTarget; - let pressed = el.getAttribute('aria-pressed') == 'false'; - let targetId = el.getAttribute('data-controls'); + toggleAction(event) { + let trigger = event.currentTarget; + let pressed = trigger.getAttribute('aria-pressed') == 'false'; + let targetId = trigger.dataset.controls; + // Un‑press all triggers controlling the same target. document.querySelectorAll('[data-controls="' + targetId + '"]') - .forEach(t => { - t.setAttribute('aria-pressed', (t.getAttribute('aria-pressed') == 'false')) - }); + .forEach(triggers => triggers.setAttribute( + 'aria-pressed', + (triggers.getAttribute('aria-pressed') == 'false')) + ); if (targetId) { let target = document.getElementById(targetId); @@ -109,14 +111,14 @@ let BookWyrm = new class { } // set checkbox, if appropriate - let checkbox = el.getAttribute('data-controls-checkbox'); + let checkbox = trigger.dataset['controls-checkbox']; if (checkbox) { document.getElementById(checkbox).checked = !!pressed; } // set focus, if appropriate - let focus = el.getAttribute('data-focus-target'); + let focus = trigger.dataset['focus-target']; if (focus) { let focusEl = document.getElementById(focus); @@ -130,7 +132,7 @@ let BookWyrm = new class { interact(e) { e.preventDefault(); - let identifier = e.target.getAttribute('data-id'); + let identifier = e.target.dataset.id; this.ajaxPost(e.target); @@ -142,7 +144,7 @@ let BookWyrm = new class { toggleMenu(e) { let el = e.currentTarget; let expanded = el.getAttribute('aria-expanded') == 'false'; - let targetId = el.getAttribute('data-controls'); + let targetId = el.dataset.controls; el.setAttribute('aria-expanded', expanded); diff --git a/bookwyrm/static/js/localstorage.js b/bookwyrm/static/js/localstorage.js index 17adf2bf3..5d978c2f9 100644 --- a/bookwyrm/static/js/localstorage.js +++ b/bookwyrm/static/js/localstorage.js @@ -15,8 +15,8 @@ let LocalStorageTools = new class { // set javascript listeners updateDisplay(e) { // used in set reading goal - let key = e.target.getAttribute('data-id'); - let value = e.target.getAttribute('data-value'); + let key = e.target.dataset.id; + let value = e.target.dataset.value; window.localStorage.setItem(key, value); @@ -26,7 +26,7 @@ let LocalStorageTools = new class { setDisplay(el) { // used in set reading goal - let key = el.getAttribute('data-hide'); + let key = el.dataset.hide; let value = window.localStorage.getItem(key); BookWyrm.addRemoveClass(el, 'hidden', value);