From ffb3945e19ac47728077f0c4ef43dc3b4a0d0e2f Mon Sep 17 00:00:00 2001 From: Kumi Date: Tue, 2 Jul 2024 17:43:06 +0200 Subject: [PATCH] feat: add embedded quiz modal functionality Implemented support for triggering and closing an embedded quiz modal based on URL parameters. Refactored the URL parameter retrieval function and added conditional modal handling after AJAX success. Simplified feedback link handling and added modal management functions. This enhancement improves the user experience by enabling embedded quiz handling within modal dialogs --- global.js | 15 ++++++++++----- script.js | 28 +++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/global.js b/global.js index 8ee05de..7fc73d7 100644 --- a/global.js +++ b/global.js @@ -15,7 +15,7 @@ document.addEventListener("DOMContentLoaded", function () { // Function to get URL parameters function getUrlParameter(name) { url = document.location.href; - urlobj = new URL(url) + urlobj = new URL(url); return urlobj.searchParams.get(name); } @@ -53,6 +53,8 @@ document.addEventListener("DOMContentLoaded", function () { .then(() => { console.log("Bootstrap loaded successfully"); $(document).ready(function () { + var embedded = getUrlParameter("embed"); + // Set required attribute on inputs associated with labels containing $('label:has(i[title="Required field"])').each(function () { var inputId = $(this).attr("for"); @@ -91,16 +93,19 @@ document.addEventListener("DOMContentLoaded", function () { encodeURIComponent(submitButton.val()); } - console.log(formData); - // Perform the AJAX request $.ajax({ url: form.attr("action"), // Use the form's action attribute type: form.attr("method"), // Use the form's method attribute data: formData, success: function (response) { - // Call the nextContent function upon successful submission - window.top.nextContent(); + // Close the modal if `modal=1` is set + if (embedded === "1") { + window.top.closeQuizModal(); + } else { + // Else call the nextContent function + window.top.nextContent(); + } }, error: function (xhr, status, error) { // Handle any errors that occur during the AJAX request diff --git a/script.js b/script.js index 4250b55..59bb27c 100644 --- a/script.js +++ b/script.js @@ -26,8 +26,7 @@ function getFeedbackLinks() { } }); - // Return the 1-indexed list of href attributes - return feedbackLinks.map((href, index) => ({ index: index + 1, href })); + return feedbackLinks; } var currentScript = document.currentScript; @@ -136,6 +135,30 @@ loadScript( nextActivity = nextContent; window.top.nextContent = nextContent; + function openQuizModal(number) { + // Get the feedback link + var href = getFeedbackLinks()[number - 1]; + href = href.replace("view", "complete"); + + // Go to embed mode + href = href + "&embed=1&modal=1"; + + // Open in #regular-modal-iframe + $("#regular-modal-iframe").attr("src", href); + + // Show the modal + $("#regularModal").modal("show"); + } + + function closeQuizModal() { + $("#regularModal").modal("hide"); + $("#regular-modal-iframe").attr("src", "about:blank"); + } + + window.top.openQuizModal = openQuizModal; + window.top.openModal = openQuizModal; + window.top.closeQuizModal = closeQuizModal; + $(currentScript) .parent() .append( @@ -147,7 +170,6 @@ loadScript( var button = $("#contentButton-" + scriptId); button.click(function () { - console.log("Button clicked!"); showContent( contentUrl, $(currentScript).closest("li.section").attr("id")