From 85d50e19b84a191c32861ac119a2c99e53329a5b Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 6 Sep 2024 11:09:44 +0200 Subject: [PATCH] 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. --- global.js | 27 ++------------------------- script.js | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/global.js b/global.js index 30a4d72..25c91bf 100644 --- a/global.js +++ b/global.js @@ -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"); @@ -203,4 +180,4 @@ document.addEventListener("DOMContentLoaded", function () { console.error(error.message); }); } -}); \ No newline at end of file +}); diff --git a/script.js b/script.js index 141127c..d3f0a96 100644 --- a/script.js +++ b/script.js @@ -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