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.
 
 
 
 

220 linhas
8.4 KiB

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