$(document).ready(function() { addUser(); listing(); catalogue(); onChangeDinAsset(); onFormSubmit(); }); // Message Box for alerts function messageBox(status, message) { let msg = $("#message"); if (status) { msg.removeClass("failure").addClass("success"); } else { msg.removeClass("success").addClass("failure"); } msg.empty().append("

" + message + "

"); msg.slideDown(300, function() { window.setTimeout(function() { msg.slideUp(300); }, 1400); }) } // Add new user function addUser() { // Add new user $(".add-user [data-js='listing--details']").on("click", function(e) { let form = $(this).parents(".form"), form_data = form.serializeObject(); if (form[0].checkValidity()) { e.preventDefault(); form_data["active"] = $("#inputActive").is(":checked") ? 1 : 0; $.ajax({ url: "/admin/user", method: "post", data: form_data, success: function (data) { messageBox(true, "User erfolgreich angelegt. Sie werden nun zur Übersicht weitergeleitet."); window.setTimeout(function() { window.location.href = "/admin/dashboard"; }, 2000); }, error: function (xhr, status, statusmsg) { console.log(xhr); messageBox(false, xhr.responseJSON); }, dataType: "json" }); } }); } // List users and edit them function listing() { // Open user details $("[data-js='listing--opener']").on("click", function() { let that = $(this).parents("li"); if (that.hasClass("open")) { that.find(".listing--details").slideUp(500); that.removeClass("open"); } else { $("[data-js='listing--opener']").parents("li").removeClass("open"); $(".listing--details").slideUp(500); that.find(".listing--details").slideDown(500); that.addClass("open"); } }); // Send AJAX request for user edit $(".listing [data-js='listing--details']").on("click", function(e) { let form = $(this).parents(".form"), form_data = form.serializeObject(), userId = form_data["id"]; if (form[0].checkValidity()) { e.preventDefault(); form_data["active"] = $("#inputActive" + userId).is(":checked") ? 1 : 0; $.ajax({ url: "/admin/user", method: "patch", data: form_data, success: function (data) { messageBox(true, "User erfolgreich aktualisiert."); }, error: function (xhr, status, statusmsg) { console.log(xhr); messageBox(false, xhr.responseJSON); }, dataType: "json" }); } }); // Send new password to user $("[data-js='listing--password']").on("click", function(e) { let userId = $(this).parents("li").data("userid"), userName = $(this).parents("li").data("username"), confirmAction = confirm("Möchten Sie dem User " + userName + " ein neues Passwort zuweisen?"); if (confirmAction === true) { $.ajax({ url: "/admin/new-password", method: "post", data: { id: userId }, success: function (data) { messageBox(true, "Passwort erfolgreich geändert. E-Mail ist an User " + userName + " versendet worden."); }, error: function (xhr, msg, three) { messageBox(false, "Fehler beim Erstellen eines neuen Passworts."); }, dataType: "json" }); } }); } function onChangeDinAsset() { $("#asset").on("change", function(e) { $.ajax({ url: "/get-benchmark-data", method: "get", data: { assetId: $("#asset").val() }, success: function (data) { $("#cycle").attr({ "min" : data.cycleMin, "max" : data.cycleMax }).val(''); $('#benchmark') .find('option') .remove() .end() ; for (let i in data.benchmarkValues) { noBenchmarkData = true; $('#benchmark').append(''); } if (Object.keys(data.benchmarkValues).length < 1) { $('#no-benchmark-data').show(); } else { $('#no-benchmark-data').hide(); } }, error: function (xhr, msg, three) { messageBox(false, "Fehler bei der Anfrage."); }, dataType: "json" }); }); } function onFormSubmit() { $("#submit").on("click", function(e) { e.preventDefault(); let cycleVal = $("#cycle").val(); if (cycleVal === '') { messageBox(false, "Bitte geben Sie Ihren aktuellen Zyklus ein."); } else { let cycleMin = parseInt($("#cycle").attr("min")); if (cycleVal < cycleMin) { $("#cycle").val(cycleMin); messageBox(false, "Zyklus an Minimal-Wert '" + cycleMin + "' angepasst."); } let cycleMax = parseInt($("#cycle").attr("max")); if (cycleVal > cycleMax) { $("#cycle").val(cycleMax); messageBox(false, "Zyklus an Maximal-Wert '" + cycleMax + "' angepasst."); } $.ajax({ url: "/calculate-risk", method: "post", data: $('#form-risk').serializeArray(), success: function (data) { console.log(data); }, error: function (xhr, msg, three) { messageBox(false, "Fehler bei der Anfrage."); }, dataType: "json" }); } }); } let savedCatalogueAnswer, savedCatalogueNote; $(document).ready(function() { savedCatalogueAnswer = $(".form--catalogue input[name='answer']:checked").val(); savedCatalogueNote = $("#inputNote").val(); }); function catalogueAjaxForm(form_data, isOpenQuestion) { let ajaxRoute = isOpenQuestion ? "/catalogue-detail-unanswered" : "/catalogue-detail"; $.ajax({ url: ajaxRoute, method: "get", data: form_data, success: function (data) { $("#instruction").hide(); catalogueAnswer = data.catalogueDetail['answer']; catalogueNote = data.catalogueDetail['note']; $("[data-js='progressbar'] div").css("width", "calc(" + data.catalogue['numAnswers'] / data.catalogue['numDetails'] * 100% + ")"); $("[data-js='order-no']").text(data.catalogueDetail['orderNo']); $("[data-js='question-type'] span").text(data.question['questionType']); $("[data-js='question-text']").text(data.question['questionText']); $("input[name='answer']").prop('checked', false); if (catalogueAnswer != null) { $("input[value='" + catalogueAnswer + "']").prop('checked', true); savedCatalogueAnswer = catalogueAnswer; } else { savedCatalogueAnswer = (function () { return; })(); } $("#inputNote").val(""); if (catalogueNote !== null) { $("#inputNote").val(catalogueNote); savedCatalogueNote = catalogueNote; } else { savedCatalogueNote = ""; } }, error: function (xhr, msg, three) { messageBox(false, "Fehler bei der Anfrage."); }, dataType: "json" }); } function catalogue() { $("[data-js='last-question'], [data-js='next-question']").on("click", function(e){ let form = $(this).parents(".form"), form_data = form.serializeObject(), isOpenQuestion = $(this).data("openquestion") === "yes"; form_data["direction"] = $(this).data("js") === "last-question" ? 'prev' : 'next'; if (savedCatalogueAnswer === form_data["answer"] && savedCatalogueNote === form_data["note"]) { catalogueAjaxForm(form_data, isOpenQuestion); } else { confirmAction = confirm("Sie haben ungespeicherte Eingaben. Möchten Sie fortfahren?"); if (confirmAction === true) { catalogueAjaxForm(form_data, isOpenQuestion); } } }); $("[data-js='save-question']").on("click", function(e){ let form = $(this).parents(".form"), form_data = form.serializeObject(); if ($("input[name='answer']:checked").val()) { $.ajax({ url: "/catalogue-detail", method: "patch", data: form_data, success: function (data) { messageBox(true, "Ihre Antwort wurde gespeichert."); $("[data-js='progressbar'] div").css("width", "calc(" + data.catalogue['numAnswers'] / data.catalogue['numDetails'] * 100 + "%)"); savedCatalogueAnswer = form_data["answer"]; savedCatalogueNote = form_data["note"]; if (data.catalogue['numAnswers'] === data.catalogue['numDetails']) { $("[data-js='finish-catalogue']").prop("disabled", false); } }, error: function (xhr, msg, three) { messageBox(false, "Fehler bei der Anfrage."); }, dataType: "json" }); } else { messageBox(false, "Bitte wählen Sie eine Antwort."); } }); $("[data-js='finish-catalogue']").on("click", function(e){ e.preventDefault(); let form = $(this).parents(".form"), form_data = form.serializeObject(); if (confirm("Möchten Sie den Fragenkatalog abschließen? Änderungen sind danach nicht mehr möglich!") === true) { $.ajax({ url: "/catalogue-finish", method: "patch", data: form_data, success: function (data) { window.location.href = "/catalogue-finished"; }, error: function (xhr, msg, three) { messageBox(false, "Fehler bei der Anfrage."); }, dataType: "json" }); } }); }