From 06b4a447c6a3c1bd27085779f2f8123145359191 Mon Sep 17 00:00:00 2001 From: Veiko Aasa Date: Mon, 2 Dec 2019 18:01:37 +0300 Subject: [PATCH] app: fix javascript constant redeclaration error Closes #1715 Signed-off-by: Veiko Aasa Reviewed-by: Joseph Nuthalapati --- static/themes/default/js/app.template.js | 56 ++++++++++++------------ 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/static/themes/default/js/app.template.js b/static/themes/default/js/app.template.js index dc6c97102..95e56cd62 100644 --- a/static/themes/default/js/app.template.js +++ b/static/themes/default/js/app.template.js @@ -21,36 +21,38 @@ * in this page. */ -const appForm = document.querySelector('#app-form'); -const appToggleContainer = document.querySelector('#app-toggle-container'); -const appToggleButton = document.querySelector('#app-toggle-button'); -const appToggleInput = document.querySelector('#app-toggle-input'); +$(document).on('turbolinks:load', function() { + const appForm = document.querySelector('#app-form'); + const appToggleContainer = document.querySelector('#app-toggle-container'); + const appToggleButton = document.querySelector('#app-toggle-button'); + const appToggleInput = document.querySelector('#app-toggle-input'); -if (appForm && appToggleButton && appToggleInput && appToggleContainer) { - const onSubmit = (e) => { - e.preventDefault; - appToggleInput.checked = !appToggleInput.checked; - appForm.submit(); - }; + if (appForm && appToggleButton && appToggleInput && appToggleContainer) { + const onSubmit = (e) => { + e.preventDefault; + appToggleInput.checked = !appToggleInput.checked; + appForm.submit(); + }; - appToggleButton.addEventListener('click', onSubmit); + appToggleButton.addEventListener('click', onSubmit); - /** - * if javascript is enabled, this script will run and show the toggle button - */ + /** + * if javascript is enabled, this script will run and show the toggle button + */ - appToggleInput.parentElement.style.display = 'none'; - appToggleContainer.style.display = 'flex'; + appToggleInput.parentElement.style.display = 'none'; + appToggleContainer.style.display = 'flex'; - /* A basic form has only three elements: - * 1. An input tag with CSRF token - * 2. A div with form elements - * 3. A Submit button - * - * This kind of form can be completely hidden. - */ - if (appForm.children.length === 3) { - appForm.style.display = 'none'; - appForm.previousElementSibling.style.display = 'none'; + /* A basic form has only three elements: + * 1. An input tag with CSRF token + * 2. A div with form elements + * 3. A Submit button + * + * This kind of form can be completely hidden. + */ + if (appForm.children.length === 3) { + appForm.style.display = 'none'; + appForm.previousElementSibling.style.display = 'none'; + } } -} +});