Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

277 řádky
10 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. console.log($('#form-risk').serializeArray());
  148. $.ajax({
  149. url: "/calculate-risk",
  150. method: "post",
  151. data: $('#form-risk').serializeArray(),
  152. success: function (data) {
  153. console.log(data);
  154. },
  155. error: function (xhr, msg, three) {
  156. messageBox(false, "Fehler bei der Anfrage.");
  157. },
  158. dataType: "json"
  159. });
  160. });
  161. }
  162. let savedCatalogueAnswer,
  163. savedCatalogueNote;
  164. $(document).ready(function() {
  165. savedCatalogueAnswer = $(".form--catalogue input[name='answer']:checked").val();
  166. savedCatalogueNote = $("#inputNote").val();
  167. });
  168. function catalogueAjaxForm(form_data, isOpenQuestion) {
  169. let ajaxRoute = isOpenQuestion ? "/catalogue-detail-unanswered" : "/catalogue-detail";
  170. $.ajax({
  171. url: ajaxRoute,
  172. method: "get",
  173. data: form_data,
  174. success: function (data) {
  175. $("#instruction").hide();
  176. catalogueAnswer = data.catalogueDetail['answer'];
  177. catalogueNote = data.catalogueDetail['note'];
  178. $("[data-js='progressbar'] div").css("width", "calc(" + data.catalogue['numAnswers'] / data.catalogue['numDetails'] * 100% + ")");
  179. $("[data-js='order-no']").text(data.catalogueDetail['orderNo']);
  180. $("[data-js='question-type'] span").text(data.question['questionType']);
  181. $("[data-js='question-text']").text(data.question['questionText']);
  182. $("input[name='answer']").prop('checked', false);
  183. if (catalogueAnswer != null) {
  184. $("input[value='" + catalogueAnswer + "']").prop('checked', true);
  185. savedCatalogueAnswer = catalogueAnswer;
  186. } else {
  187. savedCatalogueAnswer = (function () { return; })();
  188. }
  189. $("#inputNote").val("");
  190. if (catalogueNote !== null) {
  191. $("#inputNote").val(catalogueNote);
  192. savedCatalogueNote = catalogueNote;
  193. } else {
  194. savedCatalogueNote = "";
  195. }
  196. },
  197. error: function (xhr, msg, three) {
  198. messageBox(false, "Fehler bei der Anfrage.");
  199. },
  200. dataType: "json"
  201. });
  202. }
  203. function catalogue() {
  204. $("[data-js='last-question'], [data-js='next-question']").on("click", function(e){
  205. let form = $(this).parents(".form"),
  206. form_data = form.serializeObject(),
  207. isOpenQuestion = $(this).data("openquestion") === "yes";
  208. form_data["direction"] = $(this).data("js") === "last-question" ? 'prev' : 'next';
  209. if (savedCatalogueAnswer === form_data["answer"] && savedCatalogueNote === form_data["note"]) {
  210. catalogueAjaxForm(form_data, isOpenQuestion);
  211. } else {
  212. confirmAction = confirm("Sie haben ungespeicherte Eingaben. Möchten Sie fortfahren?");
  213. if (confirmAction === true) {
  214. catalogueAjaxForm(form_data, isOpenQuestion);
  215. }
  216. }
  217. });
  218. $("[data-js='save-question']").on("click", function(e){
  219. let form = $(this).parents(".form"),
  220. form_data = form.serializeObject();
  221. if ($("input[name='answer']:checked").val()) {
  222. $.ajax({
  223. url: "/catalogue-detail",
  224. method: "patch",
  225. data: form_data,
  226. success: function (data) {
  227. messageBox(true, "Ihre Antwort wurde gespeichert.");
  228. $("[data-js='progressbar'] div").css("width", "calc(" + data.catalogue['numAnswers'] / data.catalogue['numDetails'] * 100 + "%)");
  229. savedCatalogueAnswer = form_data["answer"];
  230. savedCatalogueNote = form_data["note"];
  231. if (data.catalogue['numAnswers'] === data.catalogue['numDetails']) {
  232. $("[data-js='finish-catalogue']").prop("disabled", false);
  233. }
  234. },
  235. error: function (xhr, msg, three) {
  236. messageBox(false, "Fehler bei der Anfrage.");
  237. },
  238. dataType: "json"
  239. });
  240. } else {
  241. messageBox(false, "Bitte wählen Sie eine Antwort.");
  242. }
  243. });
  244. $("[data-js='finish-catalogue']").on("click", function(e){
  245. e.preventDefault();
  246. let form = $(this).parents(".form"),
  247. form_data = form.serializeObject();
  248. if (confirm("Möchten Sie den Fragenkatalog abschließen? Änderungen sind danach nicht mehr möglich!") === true) {
  249. $.ajax({
  250. url: "/catalogue-finish",
  251. method: "patch",
  252. data: form_data,
  253. success: function (data) {
  254. window.location.href = "/catalogue-finished";
  255. },
  256. error: function (xhr, msg, three) {
  257. messageBox(false, "Fehler bei der Anfrage.");
  258. },
  259. dataType: "json"
  260. });
  261. }
  262. });
  263. }