-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33684cc
commit f53a85d
Showing
3 changed files
with
91 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -281,93 +281,7 @@ | |
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script> | ||
<script defer src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> | ||
|
||
<script> | ||
/** | ||
* Progressive enhancement when JavaScript is enabled to intercept form requests | ||
* and instead handle them with an asynchronous Fetch instead of refreshing the page. | ||
* | ||
* @param {Element} form | ||
* @param {?callback} okHandler | ||
* @param {?callback} errorHandler | ||
* @param {?callback} beforeCallHandler | ||
* @param {?callback} afterCallHandler | ||
*/ | ||
function registerAsyncForm(form, okHandler = null, errorHandler = null, beforeCallHandler = null, afterCallHandler = null) { | ||
form.addEventListener("submit", function (event) { | ||
// Disable default form submit | ||
event.preventDefault(); | ||
if (beforeCallHandler) { | ||
beforeCallHandler(); | ||
} | ||
fetch("", { | ||
method: "POST", | ||
body: new FormData(event.target), | ||
headers: new Headers({ | ||
"X-RC-Handler": "Async", | ||
}), | ||
}).then(response => { | ||
if (response.ok) { | ||
if (okHandler) { | ||
okHandler(response); | ||
} | ||
} else { | ||
if (errorHandler) { | ||
errorHandler(response); | ||
} else { | ||
console.error("Fetch request failed."); | ||
} | ||
} | ||
}).catch(error => { | ||
// Handle any network-related errors | ||
console.error("Network error:", error); | ||
}); | ||
if (afterCallHandler) { | ||
afterCallHandler(); | ||
} | ||
}); | ||
} | ||
document.querySelectorAll(".openInEditorForm").forEach(form => { | ||
registerAsyncForm(form); | ||
}); | ||
let createPageModal = null; | ||
document.addEventListener('DOMContentLoaded', function () { | ||
createPageModal = new bootstrap.Modal('#createPageModal'); | ||
}); | ||
const createPageForm = document.getElementById("createPageForm"); | ||
const createPageFormSubmit = document.getElementById("createPageButton"); | ||
const createPageFormError = document.getElementById("createPageFormError"); | ||
const createPageFormErrorContents = document.getElementById("createPageFormErrorContents"); | ||
registerAsyncForm(createPageForm, async function (response) { | ||
let data = await response.json(); | ||
createPageModal.hide(); | ||
Swal.fire({ | ||
title: 'Page created!', | ||
text: data.body, | ||
icon: 'success', | ||
timer: 3000, | ||
timerProgressBar: true, | ||
}) | ||
createPageForm.reset() | ||
}, async function (response) { | ||
let data = await response.json(); | ||
createPageFormError.style.display = 'block'; | ||
createPageFormErrorContents.innerText = data.error; | ||
}, function () { | ||
createPageFormSubmit.disabled = true; | ||
createPageFormError.style.display = 'none'; | ||
createPageFormErrorContents.innerText = ''; | ||
}, function () { | ||
createPageFormSubmit.disabled = false; | ||
}); | ||
</script> | ||
<script>{!! $dashboard->getScripts() !!}</script> | ||
@endif | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Progressive enhancement when JavaScript is enabled to intercept form requests | ||
* and instead handle them with an asynchronous Fetch instead of refreshing the page. | ||
* | ||
* @param {Element} form | ||
* @param {?callback} okHandler | ||
* @param {?callback} errorHandler | ||
* @param {?callback} beforeCallHandler | ||
* @param {?callback} afterCallHandler | ||
*/ | ||
function registerAsyncForm(form, okHandler = null, errorHandler = null, beforeCallHandler = null, afterCallHandler = null) { | ||
form.addEventListener("submit", function (event) { | ||
// Disable default form submit | ||
event.preventDefault(); | ||
|
||
if (beforeCallHandler) { | ||
beforeCallHandler(); | ||
} | ||
|
||
fetch("", { | ||
method: "POST", | ||
body: new FormData(event.target), | ||
headers: new Headers({ | ||
"X-RC-Handler": "Async", | ||
}), | ||
}).then(response => { | ||
if (response.ok) { | ||
if (okHandler) { | ||
okHandler(response); | ||
} | ||
} else { | ||
if (errorHandler) { | ||
errorHandler(response); | ||
} else { | ||
console.error("Fetch request failed."); | ||
} | ||
} | ||
}).catch(error => { | ||
// Handle any network-related errors | ||
console.error("Network error:", error); | ||
}); | ||
|
||
if (afterCallHandler) { | ||
afterCallHandler(); | ||
} | ||
}); | ||
} | ||
|
||
document.querySelectorAll(".openInEditorForm").forEach(form => { | ||
registerAsyncForm(form); | ||
}); | ||
|
||
let createPageModal = null; | ||
|
||
document.addEventListener('DOMContentLoaded', function () { | ||
createPageModal = new bootstrap.Modal('#createPageModal'); | ||
}); | ||
|
||
const createPageForm = document.getElementById("createPageForm"); | ||
const createPageFormSubmit = document.getElementById("createPageButton"); | ||
const createPageFormError = document.getElementById("createPageFormError"); | ||
const createPageFormErrorContents = document.getElementById("createPageFormErrorContents"); | ||
|
||
registerAsyncForm(createPageForm, async function (response) { | ||
let data = await response.json(); | ||
createPageModal.hide(); | ||
Swal.fire({ | ||
title: 'Page created!', | ||
text: data.body, | ||
icon: 'success', | ||
timer: 3000, | ||
timerProgressBar: true, | ||
}) | ||
createPageForm.reset() | ||
}, async function (response) { | ||
let data = await response.json(); | ||
createPageFormError.style.display = 'block'; | ||
createPageFormErrorContents.innerText = data.error; | ||
}, function () { | ||
createPageFormSubmit.disabled = true; | ||
createPageFormError.style.display = 'none'; | ||
createPageFormErrorContents.innerText = ''; | ||
}, function () { | ||
createPageFormSubmit.disabled = false; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters