|
| 1 | +// Contact page: topic pills and the client-side "message sent" state. |
| 2 | +// The form is not wired to a backend yet, so submit only swaps the panels. |
| 3 | +(function () { |
| 4 | + var form = document.querySelector('[data-contact-form]'); |
| 5 | + if (!form) { |
| 6 | + return; |
| 7 | + } |
| 8 | + |
| 9 | + var topicInput = form.querySelector('[data-contact-topic]'); |
| 10 | + var pills = form.querySelectorAll('[data-topic]'); |
| 11 | + var sent = document.querySelector('[data-contact-sent]'); |
| 12 | + var sentTopic = sent.querySelector('[data-contact-sent-topic]'); |
| 13 | + var reset = sent.querySelector('[data-contact-reset]'); |
| 14 | + |
| 15 | + function currentPill() { |
| 16 | + return form.querySelector('[data-topic][aria-pressed="true"]'); |
| 17 | + } |
| 18 | + |
| 19 | + function selectTopic(id) { |
| 20 | + pills.forEach(function (pill) { |
| 21 | + pill.setAttribute('aria-pressed', String(pill.getAttribute('data-topic') === id)); |
| 22 | + }); |
| 23 | + topicInput.value = id; |
| 24 | + } |
| 25 | + |
| 26 | + pills.forEach(function (pill) { |
| 27 | + pill.addEventListener('click', function () { |
| 28 | + selectTopic(pill.getAttribute('data-topic')); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + form.addEventListener('submit', function (event) { |
| 33 | + event.preventDefault(); |
| 34 | + var pill = currentPill(); |
| 35 | + sentTopic.textContent = pill ? pill.textContent : ''; |
| 36 | + form.hidden = true; |
| 37 | + sent.hidden = false; |
| 38 | + sent.querySelector('h2').focus({ preventScroll: true }); |
| 39 | + }); |
| 40 | + |
| 41 | + reset.addEventListener('click', function () { |
| 42 | + form.reset(); |
| 43 | + selectTopic(pills[0].getAttribute('data-topic')); |
| 44 | + sent.hidden = true; |
| 45 | + form.hidden = false; |
| 46 | + form.querySelector('input:not([type="hidden"])').focus(); |
| 47 | + }); |
| 48 | +})(); |
0 commit comments