You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

258 lines
9.6 KiB

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