No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

252 líneas
9.3 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. console.log($("#asset").val());
  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(data.cycleRecommended);
  123. $('#bandwidth')
  124. .find('option')
  125. .remove()
  126. .end()
  127. ;
  128. for (let i in data.benchmarkValues) {
  129. $('#bandwidth').append('<option value="' + i + '">' + data.benchmarkValues[i] + '</option>');
  130. }
  131. },
  132. error: function (xhr, msg, three) {
  133. messageBox(false, "Fehler bei der Anfrage.");
  134. },
  135. dataType: "json"
  136. });
  137. });
  138. }
  139. let savedCatalogueAnswer,
  140. savedCatalogueNote;
  141. $(document).ready(function() {
  142. savedCatalogueAnswer = $(".form--catalogue input[name='answer']:checked").val();
  143. savedCatalogueNote = $("#inputNote").val();
  144. });
  145. function catalogueAjaxForm(form_data, isOpenQuestion) {
  146. let ajaxRoute = isOpenQuestion ? "/catalogue-detail-unanswered" : "/catalogue-detail";
  147. $.ajax({
  148. url: ajaxRoute,
  149. method: "get",
  150. data: form_data,
  151. success: function (data) {
  152. $("#instruction").hide();
  153. catalogueAnswer = data.catalogueDetail['answer'];
  154. catalogueNote = data.catalogueDetail['note'];
  155. $("[data-js='progressbar'] div").css("width", "calc(" + data.catalogue['numAnswers'] / data.catalogue['numDetails'] * 100% + ")");
  156. $("[data-js='order-no']").text(data.catalogueDetail['orderNo']);
  157. $("[data-js='question-type'] span").text(data.question['questionType']);
  158. $("[data-js='question-text']").text(data.question['questionText']);
  159. $("input[name='answer']").prop('checked', false);
  160. if (catalogueAnswer != null) {
  161. $("input[value='" + catalogueAnswer + "']").prop('checked', true);
  162. savedCatalogueAnswer = catalogueAnswer;
  163. } else {
  164. savedCatalogueAnswer = (function () { return; })();
  165. }
  166. $("#inputNote").val("");
  167. if (catalogueNote !== null) {
  168. $("#inputNote").val(catalogueNote);
  169. savedCatalogueNote = catalogueNote;
  170. } else {
  171. savedCatalogueNote = "";
  172. }
  173. },
  174. error: function (xhr, msg, three) {
  175. messageBox(false, "Fehler bei der Anfrage.");
  176. },
  177. dataType: "json"
  178. });
  179. }
  180. function catalogue() {
  181. $("[data-js='last-question'], [data-js='next-question']").on("click", function(e){
  182. let form = $(this).parents(".form"),
  183. form_data = form.serializeObject(),
  184. isOpenQuestion = $(this).data("openquestion") === "yes";
  185. form_data["direction"] = $(this).data("js") === "last-question" ? 'prev' : 'next';
  186. if (savedCatalogueAnswer === form_data["answer"] && savedCatalogueNote === form_data["note"]) {
  187. catalogueAjaxForm(form_data, isOpenQuestion);
  188. } else {
  189. confirmAction = confirm("Sie haben ungespeicherte Eingaben. Möchten Sie fortfahren?");
  190. if (confirmAction === true) {
  191. catalogueAjaxForm(form_data, isOpenQuestion);
  192. }
  193. }
  194. });
  195. $("[data-js='save-question']").on("click", function(e){
  196. let form = $(this).parents(".form"),
  197. form_data = form.serializeObject();
  198. if ($("input[name='answer']:checked").val()) {
  199. $.ajax({
  200. url: "/catalogue-detail",
  201. method: "patch",
  202. data: form_data,
  203. success: function (data) {
  204. messageBox(true, "Ihre Antwort wurde gespeichert.");
  205. $("[data-js='progressbar'] div").css("width", "calc(" + data.catalogue['numAnswers'] / data.catalogue['numDetails'] * 100 + "%)");
  206. savedCatalogueAnswer = form_data["answer"];
  207. savedCatalogueNote = form_data["note"];
  208. if (data.catalogue['numAnswers'] === data.catalogue['numDetails']) {
  209. $("[data-js='finish-catalogue']").prop("disabled", false);
  210. }
  211. },
  212. error: function (xhr, msg, three) {
  213. messageBox(false, "Fehler bei der Anfrage.");
  214. },
  215. dataType: "json"
  216. });
  217. } else {
  218. messageBox(false, "Bitte wählen Sie eine Antwort.");
  219. }
  220. });
  221. $("[data-js='finish-catalogue']").on("click", function(e){
  222. e.preventDefault();
  223. let form = $(this).parents(".form"),
  224. form_data = form.serializeObject();
  225. if (confirm("Möchten Sie den Fragenkatalog abschließen? Änderungen sind danach nicht mehr möglich!") === true) {
  226. $.ajax({
  227. url: "/catalogue-finish",
  228. method: "patch",
  229. data: form_data,
  230. success: function (data) {
  231. window.location.href = "/catalogue-finished";
  232. },
  233. error: function (xhr, msg, three) {
  234. messageBox(false, "Fehler bei der Anfrage.");
  235. },
  236. dataType: "json"
  237. });
  238. }
  239. });
  240. }