mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-08 15:16:43 +01:00
Detect when page is loaded from back-forward-cache and undo the disabling
This commit is contained in:
parent
47ba4747b3
commit
77352a3c77
1 changed files with 39 additions and 0 deletions
|
@ -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) {
|
||||||
|
|
Loading…
Reference in a new issue