Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

292 linhas
11 KiB

  1. $(document).ready(function() {
  2. addUser();
  3. listing();
  4. catalogue();
  5. onChangeDinAsset();
  6. onFormSubmit();
  7. });
  8. // Message Box for alerts
  9. function messageBox(status, message) {
  10. let msg = $("#message");
  11. if (status) {
  12. msg.removeClass("failure").addClass("success");
  13. } else {
  14. msg.removeClass("success").addClass("failure");
  15. }
  16. msg.empty().append("<p>" + message + "</p>");
  17. msg.slideDown(300, function() {
  18. window.setTimeout(function() {
  19. msg.slideUp(300);
  20. }, 1400);
  21. })
  22. }
  23. // Add new user
  24. function addUser() {
  25. // Add new user
  26. $(".add-user [data-js='listing--details']").on("click", function(e) {
  27. let form = $(this).parents(".form"),
  28. form_data = form.serializeObject();
  29. if (form[0].checkValidity()) {
  30. e.preventDefault();
  31. form_data["active"] = $("#inputActive").is(":checked") ? 1 : 0;
  32. $.ajax({
  33. url: "/admin/user",
  34. method: "post",
  35. data: form_data,
  36. success: function (data) {
  37. messageBox(true, "User erfolgreich angelegt. Sie werden nun zur Übersicht weitergeleitet.");
  38. window.setTimeout(function() {
  39. window.location.href = "/admin/dashboard";
  40. }, 2000);
  41. },
  42. error: function (xhr, status, statusmsg) {
  43. console.log(xhr);
  44. messageBox(false, xhr.responseJSON);
  45. },
  46. dataType: "json"
  47. });
  48. }
  49. });
  50. }
  51. // List users and edit them
  52. function listing() {
  53. // Open user details
  54. $("[data-js='listing--opener']").on("click", function() {
  55. let that = $(this).parents("li");
  56. if (that.hasClass("open")) {
  57. that.find(".listing--details").slideUp(500);
  58. that.removeClass("open");
  59. } else {
  60. $("[data-js='listing--opener']").parents("li").removeClass("open");
  61. $(".listing--details").slideUp(500);
  62. that.find(".listing--details").slideDown(500);
  63. that.addClass("open");
  64. }
  65. });
  66. // Send AJAX request for user edit
  67. $(".listing [data-js='listing--details']").on("click", function(e) {
  68. let form = $(this).parents(".form"),
  69. form_data = form.serializeObject(),
  70. userId = form_data["id"];
  71. if (form[0].checkValidity()) {
  72. e.preventDefault();
  73. form_data["active"] = $("#inputActive" + userId).is(":checked") ? 1 : 0;
  74. $.ajax({
  75. url: "/admin/user",
  76. method: "patch",
  77. data: form_data,
  78. success: function (data) {
  79. messageBox(true, "User erfolgreich aktualisiert.");
  80. },
  81. error: function (xhr, status, statusmsg) {
  82. console.log(xhr);
  83. messageBox(false, xhr.responseJSON);
  84. },
  85. dataType: "json"
  86. });
  87. }
  88. });
  89. // Send new password to user
  90. $("[data-js='listing--password']").on("click", function(e) {
  91. let userId = $(this).parents("li").data("userid"),
  92. userName = $(this).parents("li").data("username"),
  93. confirmAction = confirm("Möchten Sie dem User " + userName + " ein neues Passwort zuweisen?");
  94. if (confirmAction === true) {
  95. $.ajax({
  96. url: "/admin/new-password",
  97. method: "post",
  98. data: {
  99. id: userId
  100. },
  101. success: function (data) {
  102. messageBox(true, "Passwort erfolgreich geändert. E-Mail ist an User " + userName + " versendet worden.");
  103. },
  104. error: function (xhr, msg, three) {
  105. messageBox(false, "Fehler beim Erstellen eines neuen Passworts.");
  106. },
  107. dataType: "json"
  108. });
  109. }
  110. });
  111. }
  112. function onChangeDinAsset() {
  113. $("#asset").on("change", function(e) {
  114. $.ajax({
  115. url: "/get-benchmark-data",
  116. method: "get",
  117. data: { assetId: $("#asset").val() },
  118. success: function (data) {
  119. $("#cycle").attr({
  120. "min" : data.cycleMin,
  121. "max" : data.cycleMax
  122. }).val('');
  123. $('#benchmark')
  124. .find('option')
  125. .remove()
  126. .end()
  127. ;
  128. for (let i in data.benchmarkValues) {
  129. noBenchmarkData = true;
  130. $('#benchmark').append('<option value="' + i + '">' + data.benchmarkValues[i] + '</option>');
  131. }
  132. if (Object.keys(data.benchmarkValues).length < 1) {
  133. $('#no-benchmark-data').show();
  134. } else {
  135. $('#no-benchmark-data').hide();
  136. }
  137. },
  138. error: function (xhr, msg, three) {
  139. messageBox(false, "Fehler bei der Anfrage.");
  140. },
  141. dataType: "json"
  142. });
  143. });
  144. }
  145. function onFormSubmit() {
  146. $("#submit").on("click", function(e) {
  147. e.preventDefault();
  148. let cycleVal = $("#cycle").val();
  149. if (cycleVal === '') {
  150. messageBox(false, "Bitte geben Sie Ihren aktuellen Zyklus ein.");
  151. } else {
  152. let cycleMin = parseInt($("#cycle").attr("min"));
  153. if (cycleVal < cycleMin) {
  154. $("#cycle").val(cycleMin);
  155. messageBox(false, "Zyklus an Minimal-Wert '" + cycleMin + "' angepasst.");
  156. }
  157. let cycleMax = parseInt($("#cycle").attr("max"));
  158. if (cycleVal > cycleMax) {
  159. $("#cycle").val(cycleMax);
  160. messageBox(false, "Zyklus an Maximal-Wert '" + cycleMax + "' angepasst.");
  161. }
  162. $.ajax({
  163. url: "/calculate-risk",
  164. method: "post",
  165. data: $('#form-risk').serializeArray(),
  166. success: function (data) {
  167. console.log(data);
  168. },
  169. error: function (xhr, msg, three) {
  170. messageBox(false, "Fehler bei der Anfrage.");
  171. },
  172. dataType: "json"
  173. });
  174. }
  175. });
  176. }
  177. let savedCatalogueAnswer,
  178. savedCatalogueNote;
  179. $(document).ready(function() {
  180. savedCatalogueAnswer = $(".form--catalogue input[name='answer']:checked").val();
  181. savedCatalogueNote = $("#inputNote").val();
  182. });
  183. function catalogueAjaxForm(form_data, isOpenQuestion) {
  184. let ajaxRoute = isOpenQuestion ? "/catalogue-detail-unanswered" : "/catalogue-detail";
  185. $.ajax({
  186. url: ajaxRoute,
  187. method: "get",
  188. data: form_data,
  189. success: function (data) {
  190. $("#instruction").hide();
  191. catalogueAnswer = data.catalogueDetail['answer'];
  192. catalogueNote = data.catalogueDetail['note'];
  193. $("[data-js='progressbar'] div").css("width", "calc(" + data.catalogue['numAnswers'] / data.catalogue['numDetails'] * 100% + ")");
  194. $("[data-js='order-no']").text(data.catalogueDetail['orderNo']);
  195. $("[data-js='question-type'] span").text(data.question['questionType']);
  196. $("[data-js='question-text']").text(data.question['questionText']);
  197. $("input[name='answer']").prop('checked', false);
  198. if (catalogueAnswer != null) {
  199. $("input[value='" + catalogueAnswer + "']").prop('checked', true);
  200. savedCatalogueAnswer = catalogueAnswer;
  201. } else {
  202. savedCatalogueAnswer = (function () { return; })();
  203. }
  204. $("#inputNote").val("");
  205. if (catalogueNote !== null) {
  206. $("#inputNote").val(catalogueNote);
  207. savedCatalogueNote = catalogueNote;
  208. } else {
  209. savedCatalogueNote = "";
  210. }
  211. },
  212. error: function (xhr, msg, three) {
  213. messageBox(false, "Fehler bei der Anfrage.");
  214. },
  215. dataType: "json"
  216. });
  217. }
  218. function catalogue() {
  219. $("[data-js='last-question'], [data-js='next-question']").on("click", function(e){
  220. let form = $(this).parents(".form"),
  221. form_data = form.serializeObject(),
  222. isOpenQuestion = $(this).data("openquestion") === "yes";
  223. form_data["direction"] = $(this).data("js") === "last-question" ? 'prev' : 'next';
  224. if (savedCatalogueAnswer === form_data["answer"] && savedCatalogueNote === form_data["note"]) {
  225. catalogueAjaxForm(form_data, isOpenQuestion);
  226. } else {
  227. confirmAction = confirm("Sie haben ungespeicherte Eingaben. Möchten Sie fortfahren?");
  228. if (confirmAction === true) {
  229. catalogueAjaxForm(form_data, isOpenQuestion);
  230. }
  231. }
  232. });
  233. $("[data-js='save-question']").on("click", function(e){
  234. let form = $(this).parents(".form"),
  235. form_data = form.serializeObject();
  236. if ($("input[name='answer']:checked").val()) {
  237. $.ajax({
  238. url: "/catalogue-detail",
  239. method: "patch",
  240. data: form_data,
  241. success: function (data) {
  242. messageBox(true, "Ihre Antwort wurde gespeichert.");
  243. $("[data-js='progressbar'] div").css("width", "calc(" + data.catalogue['numAnswers'] / data.catalogue['numDetails'] * 100 + "%)");
  244. savedCatalogueAnswer = form_data["answer"];
  245. savedCatalogueNote = form_data["note"];
  246. if (data.catalogue['numAnswers'] === data.catalogue['numDetails']) {
  247. $("[data-js='finish-catalogue']").prop("disabled", false);
  248. }
  249. },
  250. error: function (xhr, msg, three) {
  251. messageBox(false, "Fehler bei der Anfrage.");
  252. },
  253. dataType: "json"
  254. });
  255. } else {
  256. messageBox(false, "Bitte wählen Sie eine Antwort.");
  257. }
  258. });
  259. $("[data-js='finish-catalogue']").on("click", function(e){
  260. e.preventDefault();
  261. let form = $(this).parents(".form"),
  262. form_data = form.serializeObject();
  263. if (confirm("Möchten Sie den Fragenkatalog abschließen? Änderungen sind danach nicht mehr möglich!") === true) {
  264. $.ajax({
  265. url: "/catalogue-finish",
  266. method: "patch",
  267. data: form_data,
  268. success: function (data) {
  269. window.location.href = "/catalogue-finished";
  270. },
  271. error: function (xhr, msg, three) {
  272. messageBox(false, "Fehler bei der Anfrage.");
  273. },
  274. dataType: "json"
  275. });
  276. }
  277. });
  278. }