a8d04f9e |
1 | /** |
2 | * @file |
3 | * A JavaScript file for the contact form. |
4 | */ |
5 | |
6 | (function () { |
7 | |
8 | 'use strict'; |
9 | |
10 | const form = document.querySelector('.contact-form'); |
11 | const button = form.querySelector('.form-submit'); |
12 | const action = form.getAttribute('data-protect'); |
13 | const body = document.querySelector('body'); |
14 | |
15 | function activateForm() { |
16 | form.setAttribute('action', action); |
17 | button.removeAttribute('disabled'); |
18 | } |
19 | |
20 | // Display the hidden form. |
21 | form.classList.remove('hidden'); |
22 | |
23 | // Wait for a mouse to move, indicating they are human. |
24 | body.addEventListener('mousemove', () => activateForm()); |
25 | // Wait for a touch move event, indicating that they are human. |
26 | body.addEventListener('touchmove', () => activateForm()); |
27 | // A tab or enter key pressed can also indicate they are human. |
28 | body.addEventListener('keydown', function (e) { |
29 | if ((e.key === 'Tab') || (e.key === 'Enter')) { |
30 | activateForm(); |
31 | } |
32 | }); |
33 | |
34 | // Mark the form as submitted. |
35 | button.addEventListener('click', () => form.classList.add('js-submitted')); |
36 | |
37 | // Display messages. |
38 | if (location.search.substring(1) !== '') { |
39 | switch (location.search.substring(1)) { |
40 | case 'submitted': |
41 | document.querySelector('.contact-submitted').classList.remove('hidden'); |
42 | break; |
43 | |
44 | case 'error': |
45 | document.querySelector('.contact-error').classList.remove('hidden'); |
46 | break; |
47 | } |
48 | } |
49 | |
50 | })(); |