troodle-expcontent/script.js
Kumi 33f55824d8
fix: ensure conditional loading of jQuery & Bootstrap
Enhanced global.js and script.js to conditionally load jQuery and Bootstrap only if they are not already present. This change improves loading efficiency and reduces redundant script execution.

Additionally, fixed a bug in renderer.php by removing duplicate Bootstrap loading, which also reduces external dependency.

A new .gitignore file was added to ignore backup files with a .bak extension, helping to keep the repository clean.

Ensured error logging only displays error messages for better clarity.
2024-08-05 12:10:33 +02:00

185 lines
5.2 KiB
JavaScript

(function () {
function loadScript(url, integrity, crossorigin) {
return new Promise((resolve, reject) => {
const script = document.createElement("script");
script.src = url;
script.integrity = integrity;
script.crossOrigin = crossorigin;
script.onload = () => resolve(script);
script.onerror = () => reject(new Error(`Failed to load script: ${url}`));
document.head.appendChild(script);
});
}
function getFeedbackLinks() {
const links = document.querySelectorAll("a.stretched-link");
const feedbackLinks = [];
links.forEach((link) => {
const href = link.getAttribute("href");
if (href) {
if (href.includes("/feedback/")) {
feedbackLinks.push(href);
} else if (href.includes("/quiz/")) {
feedbackLinks.push(href);
}
}
});
return feedbackLinks;
}
var currentScript = document.currentScript;
function waitForGlobalVariable(variableName) {
return new Promise((resolve) => {
const checkVariable = setInterval(() => {
if (window[variableName]) {
clearInterval(checkVariable);
resolve();
}
}, 100);
});
}
waitForGlobalVariable("bootstrap")
.then(() => {
console.log("Bootstrap loaded successfully");
var myModal = $("#fullScreenModal");
myModal[0].addEventListener("shown.bs.modal", function () {
const modalIframe = window.$("#modal-iframe");
modalIframe.css("width", "100%");
modalIframe.css("height", "100%");
modalIframe.css("border", "none");
});
var scriptId = "script-" + Math.random().toString(36).substring(2, 9);
currentScript.id = scriptId;
var contentUrl = "/mod/exp360/view_content.php?id=" + $(currentScript).attr("data-activity-id");
function openInModal(url, block) {
$("#modal-iframe").attr("src", url);
if (block) {
$("#fullScreenModal").attr("data-block", block);
} else {
$("#fullScreenModal").removeAttr("data-block");
}
}
function showContent(url, block) {
bootstrap.Modal.getOrCreateInstance(
document.getElementById("fullScreenModal")
).show();
if (url) {
openInModal(url, block);
}
}
function hideContent() {
bootstrap.Modal.getOrCreateInstance(
document.getElementById("fullScreenModal")
).hide();
openInModal("about:blank");
}
async function nextContent() {
var modal = $("#fullScreenModal");
var currentLi = $("#" + modal.attr("data-block"));
var nextLi = currentLi.next("li.section");
var aLink = nextLi.find("a.stretched-link");
if (aLink.length > 0) {
let hrefValue = aLink.attr("href");
if (hrefValue.includes("feedback")) {
hrefValue = hrefValue.replace("view", "complete");
hrefValue = hrefValue + "&embed=1";
} else if (hrefValue.includes("/quiz/")) {
hrefValue = hrefValue.replace("view", "attempt");
try {
const response = await fetch(hrefValue);
hrefValue = response.url;
} catch (error) {
console.error('Error fetching the URL:', error);
}
hrefValue = hrefValue + "&embed=1";
}
return openInModal(hrefValue, nextLi.attr("id"));
} else {
var button = nextLi.find("button.btn-primary");
if (button.length > 0) {
return button.click();
}
}
hideContent();
}
function openUrlInQuizModal(href) {
$("#regular-modal-iframe").attr("src", href);
$("#regularModal").modal("show");
}
async function openQuizModal(number) {
let href = getFeedbackLinks()[number - 1];
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);
}
function closeQuizModal() {
$("#regularModal").modal("hide");
$("#regular-modal-iframe").attr("src", "about:blank");
}
// Attach global functions to window.top
window.top.nextContent = nextContent;
window.top.openQuizModal = openQuizModal;
window.top.openModal = openQuizModal;
window.top.closeQuizModal = closeQuizModal;
window.top.goToQuiz = window.top.nextContent;
$(currentScript)
.parent()
.append(
`<button id="contentButton-` +
scriptId +
`" class="btn btn-primary" type="button">360° Content</button>`
);
var button = $("#contentButton-" + scriptId);
button.click(function () {
showContent(
contentUrl,
$(currentScript).closest("li.section").attr("id")
);
});
})
.catch((error) => {
console.error(error.message);
});
})();