From 991d897ac75ddbd0122d61d175ba0f34380cf53a Mon Sep 17 00:00:00 2001 From: Fabien Basmaison Date: Tue, 6 Apr 2021 09:57:52 +0200 Subject: [PATCH] [assets] Listen to events as soon as possible. --- bookwyrm/static/js/bookwyrm.js | 38 ++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/bookwyrm/static/js/bookwyrm.js b/bookwyrm/static/js/bookwyrm.js index 0ac33e0f..ceab5176 100644 --- a/bookwyrm/static/js/bookwyrm.js +++ b/bookwyrm/static/js/bookwyrm.js @@ -3,42 +3,50 @@ let BookWyrm = new class { constructor() { - this.initOnLoad(); + this.initOnDOMLoaded(); + this.initReccuringTasks(); this.initEventListeners(); } initEventListeners() { // buttons that display or hide content document.querySelectorAll('[data-controls]') - .forEach(t => t.onclick = this.toggleAction.bind(this)); + .forEach(button => button.onclick = this.toggleAction.bind(this)); // javascript interactions (boost/fav) document.querySelectorAll('.interaction') - .forEach(t => t.onsubmit = this.interact.bind(this)); + .forEach(button => button.onsubmit = this.interact.bind(this)); // handle aria settings on menus document.querySelectorAll('.pulldown-menu') - .forEach(t => t.onclick = this.toggleMenu.bind(this)); + .forEach(button => button.onclick = this.toggleMenu.bind(this)); // hidden submit button in a form document.querySelectorAll('.hidden-form input') - .forEach(t => t.onchange = this.revealForm.bind(this)); - - // polling - document.querySelectorAll('[data-poll]') - .forEach(el => this.polling(el)); + .forEach(button => button.onchange = this.revealForm.bind(this)); // browser back behavior document.querySelectorAll('[data-back]') - .forEach(t => t.onclick = this.back); + .forEach(button => button.onclick = this.back); } - initOnLoad(){ - // set up javascript listeners - window.onload = function() { + /** + * Execute code once the DOM is loaded. + */ + initOnDOMLoaded() { + window.addEventListener('DOMContentLoaded', function() { document.querySelectorAll('.tab-group') - .forEach(t => new TabGroup(t)); - }; + .forEach(tabs => new TabGroup(tabs)); + }); + } + + /** + * Execute recurring tasks. + */ + initReccuringTasks() { + // Polling + document.querySelectorAll('[data-poll]') + .forEach(liveArea => this.polling(liveArea)); } back(e) {