2025-05-07 11:08:03 -04:00
|
|
|
(function () {
|
2017-09-15 14:10:21 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
2025-05-07 11:08:03 -04:00
|
|
|
const disableSubmitButtons = function (event) {
|
2025-10-21 09:35:41 -04:00
|
|
|
const form = event.currentTarget;
|
|
|
|
|
const submitButton = form.querySelector('input[type="submit"], button[type="submit"]');
|
2017-09-15 14:10:21 +01:00
|
|
|
|
2025-10-21 09:35:41 -04:00
|
|
|
if (!submitButton) return;
|
|
|
|
|
|
|
|
|
|
if (submitButton.dataset.clicked === 'true') {
|
2017-09-15 14:10:21 +01:00
|
|
|
event.preventDefault();
|
2025-05-07 11:08:03 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2017-09-15 14:10:21 +01:00
|
|
|
|
2025-10-21 09:35:41 -04:00
|
|
|
submitButton.dataset.clicked = 'true';
|
2017-09-15 14:10:21 +01:00
|
|
|
|
2025-10-06 09:38:54 -04:00
|
|
|
// Add loading spinner for Send/Schedule/Cancel buttons
|
2025-10-21 09:35:41 -04:00
|
|
|
const buttonName = submitButton.getAttribute('name')?.toLowerCase();
|
2025-05-07 11:08:03 -04:00
|
|
|
if (["send", "schedule", "cancel"].includes(buttonName)) {
|
2025-10-06 09:38:54 -04:00
|
|
|
// Use setTimeout with minimal delay to allow form submission to proceed first
|
|
|
|
|
setTimeout(() => {
|
2025-10-21 09:35:41 -04:00
|
|
|
submitButton.disabled = true;
|
2017-09-15 14:10:21 +01:00
|
|
|
|
2025-10-06 09:38:54 -04:00
|
|
|
// Add loading spinner and aria-busy attribute for accessibility
|
2025-10-21 09:35:41 -04:00
|
|
|
if (submitButton.querySelector('.loading-spinner') === null) {
|
|
|
|
|
submitButton.setAttribute('aria-busy', 'true');
|
|
|
|
|
submitButton.insertAdjacentHTML('beforeend', '<span class="loading-spinner" role="status" aria-label="Sending"></span>');
|
2025-10-06 09:38:54 -04:00
|
|
|
}
|
2025-05-07 11:08:03 -04:00
|
|
|
|
2025-10-06 09:38:54 -04:00
|
|
|
// Disable Cancel button too
|
2025-10-21 09:35:41 -04:00
|
|
|
const cancelButtons = Array.from(document.querySelectorAll('button[name]')).filter(button => {
|
|
|
|
|
return button.getAttribute('name')?.toLowerCase() === 'cancel';
|
2025-10-06 09:38:54 -04:00
|
|
|
});
|
2025-10-21 09:35:41 -04:00
|
|
|
cancelButtons.forEach(button => button.disabled = true);
|
2025-10-06 09:38:54 -04:00
|
|
|
}, 50); // Small delay to ensure form submits first
|
2025-05-07 11:08:03 -04:00
|
|
|
} else {
|
2025-10-21 09:35:41 -04:00
|
|
|
setTimeout(() => renableSubmitButton(submitButton)(), 1500);
|
2025-03-14 13:47:02 -07:00
|
|
|
}
|
2017-09-15 14:10:21 +01:00
|
|
|
};
|
|
|
|
|
|
2025-10-21 09:35:41 -04:00
|
|
|
const renableSubmitButton = (submitButton) => () => {
|
|
|
|
|
submitButton.dataset.clicked = '';
|
|
|
|
|
submitButton.disabled = false;
|
|
|
|
|
submitButton.setAttribute('aria-busy', 'false');
|
|
|
|
|
const spinner = submitButton.querySelector('.loading-spinner');
|
|
|
|
|
if (spinner) spinner.remove(); // clean up spinner
|
2017-09-15 14:10:21 +01:00
|
|
|
};
|
|
|
|
|
|
2025-10-21 09:35:41 -04:00
|
|
|
document.querySelectorAll('form').forEach(form => {
|
|
|
|
|
form.addEventListener('submit', disableSubmitButtons);
|
|
|
|
|
});
|
2017-09-15 14:10:21 +01:00
|
|
|
})();
|