Detect when page is loaded from back-forward-cache and undo the disabling

This commit is contained in:
KoloMl 2025-02-03 23:21:06 +04:00
parent 47ba4747b3
commit 77352a3c77

View file

@ -27,6 +27,27 @@ function elementForEmbeddedImage({ camo_url, type }) {
return makeEl(tagName, { className: 'scraper-preview--image', src: objectUrl }); return makeEl(tagName, { className: 'scraper-preview--image', src: objectUrl });
} }
/**
* Execute the code when page was shown from back-forward cache.
* @param {() => void} callback
*/
function oncePersistedPageShown(callback) {
/**
* @param {PageTransitionEvent} event
*/
function onPageShown(event) {
if (!event.persisted) {
return;
}
window.removeEventListener('pageshow', onPageShown);
callback();
}
window.addEventListener('pageshow', onPageShown);
}
function setupImageUpload() { function setupImageUpload() {
const imgPreviews = $('#js-image-upload-previews'); const imgPreviews = $('#js-image-upload-previews');
if (!imgPreviews) return; if (!imgPreviews) return;
@ -64,17 +85,21 @@ function setupImageUpload() {
imgPreviews.appendChild(label); imgPreviews.appendChild(label);
}); });
} }
function showError() { function showError() {
clearEl(imgPreviews); clearEl(imgPreviews);
showEl(scraperError); showEl(scraperError);
enableFetch(); enableFetch();
} }
function hideError() { function hideError() {
hideEl(scraperError); hideEl(scraperError);
} }
function disableFetch() { function disableFetch() {
fetchButton.setAttribute('disabled', ''); fetchButton.setAttribute('disabled', '');
} }
function enableFetch() { function enableFetch() {
fetchButton.removeAttribute('disabled'); fetchButton.removeAttribute('disabled');
} }
@ -236,11 +261,25 @@ function setupImageUpload() {
return; return;
} }
const originalButtonText = submitButton.innerText;
submitButton.disabled = true; submitButton.disabled = true;
submitButton.innerText = 'Please wait...'; submitButton.innerText = 'Please wait...';
// delay is needed because Safari stops the submit if the button is immediately disabled // delay is needed because Safari stops the submit if the button is immediately disabled
requestAnimationFrame(() => submitButton.setAttribute('disabled', 'disabled')); requestAnimationFrame(() => submitButton.setAttribute('disabled', 'disabled'));
// Rolling back the disabled state when user navigated back to the form.
oncePersistedPageShown(() => {
if (!submitButton.disabled) {
return;
}
submitButton.disabled = false;
submitButton.innerText = originalButtonText;
submitButton.removeAttribute('disabled');
});
} }
function submitHandler(event) { function submitHandler(event) {