Skip to content

Commit 6e13632

Browse files
committed
remove checks on DOMContentLoaded and other wrapping
The javascript files are all loaded in their own scope, and after the document is fully parsed. There is no longer any need to wait for DOMContentLoaded or wrap them in an anonymous function.
1 parent 9f4d84e commit 6e13632

File tree

4 files changed

+91
-101
lines changed

4 files changed

+91
-101
lines changed
Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,36 @@
1-
(() => {
2-
"use strict";
1+
"use strict";
32

4-
for (const toggle of document.querySelectorAll('[data-toggle="slidepanel"]')) {
5-
const panel = document.querySelector(toggle.dataset.target);
6-
if (!panel) {
7-
continue;
8-
}
3+
for (const toggle of document.querySelectorAll('[data-toggle="slidepanel"]')) {
4+
const panel = document.querySelector(toggle.dataset.target);
5+
if (!panel) {
6+
continue;
7+
}
98

10-
const showAnim = new Animation( new KeyframeEffect(
11-
panel,
12-
{ transform: [ 'translateX(-100%)', 'translateX(0)' ] },
13-
200
14-
));
9+
const showAnim = new Animation( new KeyframeEffect(
10+
panel,
11+
{ transform: [ 'translateX(-100%)', 'translateX(0)' ] },
12+
200
13+
));
1514

16-
const hideAnim = new Animation(new KeyframeEffect(
17-
panel,
18-
{ transform: [ 'translateX(0)', 'translateX(-100%)' ] },
19-
200
20-
));
21-
hideAnim.addEventListener('finish', () => {
22-
panel.style.removeProperty('visibility');
23-
});
15+
const hideAnim = new Animation(new KeyframeEffect(
16+
panel,
17+
{ transform: [ 'translateX(0)', 'translateX(-100%)' ] },
18+
200
19+
));
20+
hideAnim.addEventListener('finish', () => {
21+
panel.style.removeProperty('visibility');
22+
});
2423

25-
toggle.addEventListener('click', function (e) {
26-
e.preventDefault();
24+
toggle.addEventListener('click', function (e) {
25+
e.preventDefault();
2726

28-
toggle.classList.toggle('slidepanel-visible');
29-
panel.style.visibility = 'visible';
30-
if (panel.classList.toggle('slidepanel-visible')) {
31-
showAnim.play();
32-
}
33-
else {
34-
hideAnim.play();
35-
}
36-
});
37-
}
38-
})();
27+
toggle.classList.toggle('slidepanel-visible');
28+
panel.style.visibility = 'visible';
29+
if (panel.classList.toggle('slidepanel-visible')) {
30+
showAnim.play();
31+
}
32+
else {
33+
hideAnim.play();
34+
}
35+
});
36+
}

root/static/js/dropdown.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
window.addEventListener('DOMContentLoaded', () => {
2-
for (const el of document.querySelectorAll('.dropdown select')) {
3-
el.addEventListener('change', e => {
4-
document.location.href = e.target.value;
5-
});
6-
}
7-
});
1+
for (const el of document.querySelectorAll('.dropdown select')) {
2+
el.addEventListener('change', e => {
3+
document.location.href = e.target.value;
4+
});
5+
}

root/static/js/recaptcha.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
window.addEventListener('DOMContentLoaded', async () => {
2-
'use strict';
1+
'use strict';
32

4-
function recaptcha_prepare () {
5-
return new Promise ((resolve, reject) => {
6-
const recaptcha_script = document.createElement('script');
7-
recaptcha_script.setAttribute('async', '');
8-
recaptcha_script.setAttribute('defer', '');
9-
recaptcha_script.setAttribute('src', 'https://www.google.com/recaptcha/api.js?render=explicit');
10-
recaptcha_script.addEventListener('load', () => {
11-
window.grecaptcha.ready(() => resolve(window.grecaptcha));
12-
});
13-
recaptcha_script.addEventListener('error', () => reject('Error loading reCAPTCHA'));
14-
document.head.appendChild(recaptcha_script);
3+
function recaptcha_prepare () {
4+
return new Promise ((resolve, reject) => {
5+
const recaptcha_script = document.createElement('script');
6+
recaptcha_script.setAttribute('async', '');
7+
recaptcha_script.setAttribute('defer', '');
8+
recaptcha_script.setAttribute('src', 'https://www.google.com/recaptcha/api.js?render=explicit');
9+
recaptcha_script.addEventListener('load', () => {
10+
window.grecaptcha.ready(() => resolve(window.grecaptcha));
1511
});
16-
}
17-
18-
const recaptcha_div = document.querySelector('.g-recaptcha');
19-
if (!recaptcha_div) {
20-
return;
21-
}
12+
recaptcha_script.addEventListener('error', () => reject('Error loading reCAPTCHA'));
13+
document.head.appendChild(recaptcha_script);
14+
});
15+
}
2216

17+
const recaptcha_div = document.querySelector('.g-recaptcha');
18+
if (recaptcha_div) {
2319
const recaptcha_form = recaptcha_div.closest('form');
2420

2521
const grecaptcha = await recaptcha_prepare();
2622
grecaptcha.render(recaptcha_div, {
2723
callback: () => recaptcha_form.submit()
2824
});
29-
});
25+
}

root/static/js/search.js

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,49 @@
1-
window.addEventListener('DOMContentLoaded', () => {
2-
const searchBarBtn = document.querySelector('.searchbar-btn');
3-
const searchBarForm = document.querySelector('.searchbar-form');
4-
const searchBarInput = document.querySelector('.searchbar-form input[type="text"]');
1+
const searchBarBtn = document.querySelector('.searchbar-btn');
2+
const searchBarForm = document.querySelector('.searchbar-form');
3+
const searchBarInput = document.querySelector('.searchbar-form input[type="text"]');
54

6-
searchBarBtn.addEventListener('click', () => {
7-
searchBarForm.classList.remove('visible-md');
8-
searchBarForm.classList.remove('visible-lg');
9-
searchBarForm.classList.add('searchbar-open');
10-
searchBarInput.focus();
5+
searchBarBtn.addEventListener('click', () => {
6+
searchBarForm.classList.remove('visible-md');
7+
searchBarForm.classList.remove('visible-lg');
8+
searchBarForm.classList.add('searchbar-open');
9+
searchBarInput.focus();
1110

12-
// Set the width of the autocomplete suggestions when searchbar button gets clicked
13-
const autocompleteSuggestions = document.querySelector('.autocomplete-suggestions');
14-
const searchBarFormWidth = `${searchBarForm.offsetWidth}px`;
15-
autocompleteSuggestions.style.width = searchBarFormWidth;
16-
});
11+
// Set the width of the autocomplete suggestions when searchbar button gets clicked
12+
const autocompleteSuggestions = document.querySelector('.autocomplete-suggestions');
13+
const searchBarFormWidth = `${searchBarForm.offsetWidth}px`;
14+
autocompleteSuggestions.style.width = searchBarFormWidth;
15+
});
1716

18-
const showSearchBar = () => {
19-
const searchBarOpenInput = document.querySelector('.searchbar-form.searchbar-open input[type="text"]');
20-
if (searchBarOpenInput) {
21-
if (searchBarInput === document.activeElement) {
22-
searchBarForm.classList.add('searchbar-open');
23-
searchBarForm.classList.remove('visible-md');
24-
searchBarForm.classList.remove('visible-lg');
25-
} else {
26-
searchBarForm.classList.remove('searchbar-open');
27-
searchBarForm.classList.add('visible-md');
28-
searchBarForm.classList.add('visible-lg');
29-
}
17+
const showSearchBar = () => {
18+
const searchBarOpenInput = document.querySelector('.searchbar-form.searchbar-open input[type="text"]');
19+
if (searchBarOpenInput) {
20+
if (searchBarInput === document.activeElement) {
21+
searchBarForm.classList.add('searchbar-open');
22+
searchBarForm.classList.remove('visible-md');
23+
searchBarForm.classList.remove('visible-lg');
24+
} else {
25+
searchBarForm.classList.remove('searchbar-open');
26+
searchBarForm.classList.add('visible-md');
27+
searchBarForm.classList.add('visible-lg');
3028
}
3129
}
30+
}
3231

33-
document.body.addEventListener('click', showSearchBar);
32+
document.body.addEventListener('click', showSearchBar);
3433

35-
window.addEventListener('resize', () => {
36-
const searchBarOpenInput = document.querySelector('.searchbar-form.searchbar-open input[type="text"]');
37-
if (searchBarOpenInput) {
38-
if (searchBarInput === document.activeElement) {
39-
if (document.body.clientWidth >= 992) {
40-
searchBarForm.classList.remove('searchbar-open');
41-
searchBarForm.classList.add('visible-md');
42-
searchBarForm.classList.add('visible-lg');
43-
} else {
44-
searchBarForm.classList.add('searchbar-open');
45-
searchBarForm.classList.remove('visible-md');
46-
searchBarForm.classList.remove('visible-lg');
47-
}
34+
window.addEventListener('resize', () => {
35+
const searchBarOpenInput = document.querySelector('.searchbar-form.searchbar-open input[type="text"]');
36+
if (searchBarOpenInput) {
37+
if (searchBarInput === document.activeElement) {
38+
if (document.body.clientWidth >= 992) {
39+
searchBarForm.classList.remove('searchbar-open');
40+
searchBarForm.classList.add('visible-md');
41+
searchBarForm.classList.add('visible-lg');
42+
} else {
43+
searchBarForm.classList.add('searchbar-open');
44+
searchBarForm.classList.remove('visible-md');
45+
searchBarForm.classList.remove('visible-lg');
4846
}
4947
}
50-
});
48+
}
5149
});

0 commit comments

Comments
 (0)