refactor: simplify script loading and enhance quiz modal

Removed jQuery and Bootstrap loading logic in favor of directly waiting for Bootstrap, reducing complexity and potential errors. Introduced a flag to manage sequential opening of quiz modals, improving UX by ensuring transitions to subsequent quizzes occur seamlessly.
This commit is contained in:
Kumi 2024-09-06 11:09:44 +02:00
parent 33f55824d8
commit 85d50e19b8
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 19 additions and 30 deletions

View file

@ -48,30 +48,7 @@ document.addEventListener("DOMContentLoaded", function () {
`;
document.head.appendChild(style);
// Load jQuery if not already defined
const jQueryPromise = window.jQuery
? Promise.resolve()
: loadScript(
"https://code.jquery.com/jquery-3.7.1.min.js",
"sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=",
"anonymous"
).then(() => waitForGlobalVariable("$"));
jQueryPromise
.then(() => {
console.log("jQuery loaded successfully");
// Load Bootstrap if not already defined
const bootstrapPromise = window.bootstrap
? Promise.resolve()
: loadScript(
"https://stackpath.bootstrapcdn.com/bootstrap/5.3.3/js/bootstrap.bundle.min.js",
"sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz",
"anonymous"
).then(() => waitForGlobalVariable("bootstrap"));
return bootstrapPromise;
})
waitForGlobalVariable("bootstrap")
.then(() => {
console.log("Bootstrap loaded successfully");

View file

@ -29,6 +29,8 @@
var currentScript = document.currentScript;
let openNextFlag = false;
function waitForGlobalVariable(variableName) {
return new Promise((resolve) => {
const checkVariable = setInterval(() => {
@ -128,32 +130,42 @@
$("#regularModal").modal("show");
}
async function openQuizModal(number) {
// Function to open a quiz modal
async function openQuizModal(number, openNext = true) {
openNextFlag = openNext; // Store the flag in the global variable
let href = getFeedbackLinks()[number - 1];
const quizLink = $(`a.stretched-link[href="${href}"]`).closest("li.section");
if (href.includes("/feedback/")) {
href = href.replace("view", "complete");
} else if (href.includes("/quiz/")) {
href = href.replace("view", "attempt");
try {
const response = await fetch(href);
href = response.url;
} catch (error) {
console.error('Error fetching the URL:', error);
}
console.log(href);
}
href = href + "&embed=1&modal=1";
openUrlInQuizModal(href);
if (openNext) {
// Set the data-block attribute to the block containing the current quiz
$("#fullScreenModal").attr("data-block", quizLink.attr("id"));
}
}
// Function to close the quiz modal
function closeQuizModal() {
$("#regularModal").modal("hide");
$("#regular-modal-iframe").attr("src", "about:blank");
if (openNextFlag) {
nextContent(); // Open the next activity
}
}
// Attach global functions to window.top