|
- $(document).ready(function() {
- addUser();
- listing();
- catalogue();
- onChangeDinAsset();
- });
-
- // 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("<p>" + message + "</p>");
- 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) {
- console.log($("#asset").val());
- $.ajax({
- url: "/get-benchmark-data",
- method: "get",
- data: { assetId: $("#asset").val() },
- success: function (data) {
- $("#cycle").attr({
- "min" : data.cycleMin,
- "max" : data.cycleMax
- }).val(data.cycleRecommended);
-
- $('#bandwidth')
- .find('option')
- .remove()
- .end()
- ;
-
- for (let i in data.benchmarkValues) {
- $('#bandwidth').append('<option value="' + i + '">' + data.benchmarkValues[i] + '</option>');
- }
- },
- 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"
- });
- }
- });
-
- }
|