// Oversættelses function function getCurrentLanguage() { const langAttr = document.documentElement.lang?.toLowerCase(); if (langAttr.startsWith("da")) return "da"; if (langAttr.startsWith("en")) return "en"; return "en"; // fallback } window.lang = getCurrentLanguage(); window.t = { da: { files: "Filer", viewFiles: "Se filer", confirmDelete: "Er du sikker på at du vil slette beskeden?", sendError: "Afsendelse fejlede", deleteError: "Sletning fejlede", missingInfo: "Mangler besked, spørgsmål eller kontakt-ID", lockedRequestMessage: "Denne anmodning er låst fordi status er:", }, en: { files: "Files", viewFiles: "View files", confirmDelete: "Are you sure you want to delete the message?", sendError: "Sending failed", deleteError: "Deletion failed", missingInfo: "Missing message, question or contact ID", lockedRequestMessage: "This request is locked because the status is:", }, }; // GLOBAL SCRIPT – kører altid function globalInit() { console.log("Global funktion kører..."); } // PAGE ONLY SCRIPTS function page_Home() { console.log("Forside script kører..."); } function page_homeLoggedIn() { console.log("Forside script kører..."); document.title = "Home"; } function page_Request() { console.log("Request script kører..."); document.addEventListener("DOMContentLoaded", function () { const el = document.querySelector(".entitylist"); if (el && el.offsetHeight < 709) { el.style.minHeight = "709px"; } }); } function page_requestCreate() { console.log("Request Create script kører..."); document .querySelectorAll('input[name*="e001_validationstatus"]') .forEach((el) => { el.setAttribute("disabled", "disabled"); }); } function page_requestEdit() { console.log("Request Edit script kører..."); setTimeout(() => { const validationSpan = document.getElementById("validationStatus"); const validationValue = validationSpan?.textContent?.trim().toLowerCase(); const isValid = validationValue === "true" || validationValue === "1"; if (isValid) { const sapInput = document.querySelector('[name*="e001_sapid"]'); const ioInput = document.querySelector('[name*="e001_ponoproject"]'); const typeInput = document.querySelector('[name*="e001_type"]'); const titleInput = document.querySelector('[name*="e001_title"]'); if (sapInput) sapInput.setAttribute("readonly", "readonly"); if (ioInput) ioInput.setAttribute("readonly", "readonly"); if (typeInput) { typeInput.addEventListener("mousedown", (e) => e.preventDefault()); typeInput.style.pointerEvents = "none"; typeInput.style.backgroundColor = "#e9ecef"; } if (titleInput) titleInput.setAttribute("readonly", "readonly"); } const params = new URLSearchParams(window.location.search); const id = params.get("id"); const regardingIdInput = document.querySelector( "#AttachFile_HiddenField_RegardingId" ); const container = document.querySelector(".ContainerForFile"); if (regardingIdInput && regardingIdInput.value && container) { const btn = document.createElement("a"); btn.href = "#"; btn.className = "btn button2"; btn.textContent = window.t[window.lang].viewFiles; btn.style.marginTop = "10px"; container.appendChild(btn); btn.addEventListener("click", function (e) { e.preventDefault(); const url = `https://energinet.sharepoint.com/:f:/r/sites/ChangeManagementProjects/Change%20Management/Attachments/${id}/Eksterne%20Dokumenter?csf=1&web=1&e=H0qWHo`; window.open(url, "_blank"); }); } // LOCKED STATUS - insert in .entity-form const lockedStatusValues = [ "432440003", "432440002", "432440005", "432440006", ]; const currentStatus = document .getElementById("changeStatus") ?.textContent?.trim(); const currentStatusLabel = document .getElementById("changeStatusLabel") ?.textContent?.trim(); const loggedInUserId = document .getElementById("loggedInUserId") ?.textContent?.trim() .toLowerCase(); const powerContactId = document .getElementById("powerContact") ?.textContent?.trim() .toLowerCase(); const createdById = document .getElementById("createdBy") ?.textContent?.trim() .toLowerCase(); const allowedCreators = [ "e500ee1d-8ee7-ef11-9341-000d3ab597ee", // systembruger "5f4d572c-4ef0-ef11-9342-6045bd9dc8e3", // test "0c4acb4a-ce45-f011-877a-7c1e525e01e3", // prod ]; const userIsAllowed = powerContactId === loggedInUserId && allowedCreators.includes(createdById); const isLockedStatus = lockedStatusValues.includes(currentStatus); if (isLockedStatus || !userIsAllowed) { const entityForm = document.querySelector(".entity-form"); if (!entityForm) return; // Vis kun låst besked hvis det er status der låser if (isLockedStatus) { const messageBox = document.createElement("div"); messageBox.innerHTML = `${ window.t[window.lang].lockedRequestMessage } ${currentStatusLabel}`; messageBox.style.backgroundColor = "#fff3cd"; messageBox.style.color = "#856404"; messageBox.style.padding = "12px"; messageBox.style.border = "1px solid #ffeeba"; messageBox.style.borderRadius = "4px"; messageBox.style.marginBottom = "16px"; messageBox.style.marginTop = "0"; messageBox.style.maxWidth = "850px"; entityForm.insertBefore(messageBox, entityForm.firstChild); } // Lås kun specifikke felter const sapInput = document.querySelector('[name*="e001_sapid"]'); const ioInput = document.querySelector('[name*="e001_ponoproject"]'); const typeInput = document.querySelector('[name*="e001_type"]'); const titleInput = document.querySelector('[name*="e001_title"]'); const issueAndCause = document.querySelector( '[name*="e001_issueandcause"]' ); if (sapInput) sapInput.setAttribute("readonly", "readonly"); if (ioInput) ioInput.setAttribute("readonly", "readonly"); if (typeInput) { typeInput.addEventListener("mousedown", (e) => e.preventDefault()); typeInput.style.pointerEvents = "none"; typeInput.style.backgroundColor = "#e9ecef"; } if (titleInput) titleInput.setAttribute("readonly", "readonly"); if (issueAndCause) issueAndCause.setAttribute("disabled", "disabled"); // Skjul knapper kun hvis status er låst if (isLockedStatus) { document .querySelector("#UploadButton") ?.style.setProperty("display", "none", "important"); document .querySelector("#UpdateButton") ?.style.setProperty("display", "none", "important"); } } }, 100); } function page_questionCreate() { console.log("Question-create script kører..."); const rawEl = document.getElementById("projectleaderinitials"); const initials = rawEl ? rawEl.innerText.trim() + "@energinet.dk" : null; const inputInterval = setInterval(function () { const responderInput = document.getElementById("e001_responders"); if (initials && responderInput) { responderInput.value = initials; responderInput.dispatchEvent(new Event("change")); clearInterval(inputInterval); console.log("Initialer sat:", initials); } }, 300); } function page_questionDetails() { console.log("Question-details script kører..."); window.sendMessage = function () { const message = document.getElementById("messageInput").value; const questionId = new URLSearchParams(window.location.search).get("id"); const contactId = document.getElementById("current-contact-id")?.dataset?.id; if (!message || !questionId || !contactId) { alert(window.t[window.lang].missingInfo); return; } // Hent dansk tid og konverter til ISO-format const dkTime = new Date() .toLocaleString("sv-SE", { timeZone: "Europe/Copenhagen" }) .replace(" ", "T"); const body = { e001_message: message, "e001_ChangeQuestion@odata.bind": `/e001_changequestions(${questionId})`, "e001_powerpagescontact@odata.bind": `/contacts(${contactId})`, e001_responderdate: dkTime, }; shell .ajaxSafePost({ type: "POST", contentType: "application/json", url: "/_api/e001_changequestionmessages", data: JSON.stringify(body), processData: false, global: false, }) .done(function (res) { console.log("Besked sendt:", res); document.getElementById("messageInput").value = ""; setTimeout(() => location.reload(), 1000); }) .fail(function (err) { console.error("Fejl ved afsendelse:", err?.responseText || err); alert( window.t[window.lang].sendError + ": " + (err?.responseText || "Ukendt fejl") ); }); }; window.deleteMessage = function (id) { if (!confirm(window.t[window.lang].confirmDelete)) return; shell .ajaxSafePost({ type: "DELETE", url: `/_api/e001_changequestionmessages(${id})`, contentType: "application/json", processData: false, global: false, }) .done(function () { console.log("Besked slettet:", id); setTimeout(() => location.reload(), 1000); }) .fail(function (err) { console.error("Fejl ved sletning:", err?.responseText || err); alert( window.t[window.lang].deleteError + ": " + (err?.responseText || "Ukendt fejl") ); }); }; document .getElementById("messageInput") .addEventListener("keydown", function (e) { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); sendMessage(); } }); } function routeByPath() { const path = window.location.pathname.toLowerCase(); if (path === "/" || path === "/home") { page_Home(); } else if (path.includes("/home-logged-in")) { page_homeLoggedIn(); } else if (path.includes("/request-create")) { page_requestCreate(); } else if (path.includes("/request-view")) { page_requestView(); } else if (path.includes("/request-edit")) { page_requestEdit(); } else if (path.includes("/requests")) { page_Request(); } else if (path.includes("/profile")) { page_Profile(); } else if (path.includes("/question-create")) { page_questionCreate(); } else if (path.includes("/question-details")) { page_questionDetails(); } } document.addEventListener("DOMContentLoaded", function () { globalInit(); routeByPath(); });