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.
 
 
 
 

239 lines
7.8 KiB

  1. $(document).ready(function() {
  2. addUser();
  3. listing();
  4. onChangeDinAsset();
  5. onCalculate();
  6. onWorksheet();
  7. downloadWorksheet();
  8. });
  9. // Message Box for alerts
  10. function messageBox(status, message) {
  11. let msg = $("#message");
  12. if (status) {
  13. msg.removeClass("failure").addClass("success");
  14. } else {
  15. msg.removeClass("success").addClass("failure");
  16. }
  17. msg.empty().append("<p>" + message + "</p>");
  18. msg.slideDown(300, function() {
  19. window.setTimeout(function() {
  20. msg.slideUp(300);
  21. }, 1400);
  22. })
  23. }
  24. // Add new user
  25. function addUser() {
  26. // Add new user
  27. $(".add-user [data-js='listing--details']").on("click", function(e) {
  28. let form = $(this).parents(".form"),
  29. form_data = form.serializeObject();
  30. if (form[0].checkValidity()) {
  31. e.preventDefault();
  32. form_data["active"] = $("#inputActive").is(":checked") ? 1 : 0;
  33. $.ajax({
  34. url: "/admin/user",
  35. method: "post",
  36. data: form_data,
  37. success: function (data) {
  38. messageBox(true, "User erfolgreich angelegt. Sie werden nun zur Übersicht weitergeleitet.");
  39. window.setTimeout(function() {
  40. window.location.href = "/admin/dashboard";
  41. }, 2000);
  42. },
  43. error: function (xhr, status, statusmsg) {
  44. console.log(xhr);
  45. messageBox(false, xhr.responseJSON);
  46. },
  47. dataType: "json"
  48. });
  49. }
  50. });
  51. }
  52. // List users and edit them
  53. function listing() {
  54. // Open user details
  55. $("[data-js='listing--opener']").on("click", function() {
  56. let that = $(this).parents("li");
  57. if (that.hasClass("open")) {
  58. that.find(".listing--details").slideUp(500);
  59. that.removeClass("open");
  60. } else {
  61. $("[data-js='listing--opener']").parents("li").removeClass("open");
  62. $(".listing--details").slideUp(500);
  63. that.find(".listing--details").slideDown(500);
  64. that.addClass("open");
  65. }
  66. });
  67. // Send AJAX request for user edit
  68. $(".listing [data-js='listing--details']").on("click", function(e) {
  69. let form = $(this).parents(".form"),
  70. form_data = form.serializeObject(),
  71. userId = form_data["id"];
  72. if (form[0].checkValidity()) {
  73. e.preventDefault();
  74. form_data["active"] = $("#inputActive" + userId).is(":checked") ? 1 : 0;
  75. $.ajax({
  76. url: "/admin/user",
  77. method: "patch",
  78. data: form_data,
  79. success: function (data) {
  80. messageBox(true, "User erfolgreich aktualisiert.");
  81. },
  82. error: function (xhr, status, statusmsg) {
  83. console.log(xhr);
  84. messageBox(false, xhr.responseJSON);
  85. },
  86. dataType: "json"
  87. });
  88. }
  89. });
  90. // Send new password to user
  91. $("[data-js='listing--password']").on("click", function(e) {
  92. let userId = $(this).parents("li").data("userid"),
  93. userName = $(this).parents("li").data("username"),
  94. confirmAction = confirm("Möchten Sie dem User " + userName + " ein neues Passwort zuweisen?");
  95. if (confirmAction === true) {
  96. $.ajax({
  97. url: "/admin/new-password",
  98. method: "post",
  99. data: {
  100. id: userId
  101. },
  102. success: function (data) {
  103. messageBox(true, "Passwort erfolgreich geändert. E-Mail ist an User " + userName + " versendet worden.");
  104. },
  105. error: function (xhr, msg, three) {
  106. messageBox(false, "Fehler beim Erstellen eines neuen Passworts.");
  107. },
  108. dataType: "json"
  109. });
  110. }
  111. });
  112. }
  113. function onChangeDinAsset() {
  114. $("#asset").on("change", function(e) {
  115. $.ajax({
  116. url: "/get-benchmark-data",
  117. method: "get",
  118. data: { assetId: $("#asset").val() },
  119. success: function (data) {
  120. $("#cycle").attr({
  121. "min" : data.cycleMin,
  122. "max" : data.cycleMax
  123. }).val('');
  124. $('#benchmark')
  125. .find('option')
  126. .remove()
  127. .end()
  128. ;
  129. for (let i in data.benchmarkValues) {
  130. noBenchmarkData = true;
  131. $('#benchmark').append('<option value="' + i + '">' + data.benchmarkValues[i] + '</option>');
  132. }
  133. if (Object.keys(data.benchmarkValues).length < 1) {
  134. $('#no-benchmark-data').show();
  135. } else {
  136. $('#no-benchmark-data').hide();
  137. }
  138. },
  139. error: function (xhr, msg, three) {
  140. messageBox(false, "Fehler bei der Anfrage.");
  141. },
  142. dataType: "json"
  143. });
  144. });
  145. }
  146. function onCalculate() {
  147. $("#calculate").on("click", function (e) {
  148. e.preventDefault();
  149. if (checkInputs()) {
  150. $.ajax({
  151. url: "/calculate-risk",
  152. method: "post",
  153. data: $('#form-risk').serializeArray(),
  154. success: function (data) {
  155. console.log(data);
  156. $('#resInspection').text(data['recCycleInspection']);
  157. $('#resMaintenance').text(data['recCycleMaintenance']);
  158. $('#resPercentage').text(data['costDiffCurRecPercentage'] + " %");
  159. $('#resEuro').text(data['costDiffCurCycleRecCycle'] + " €");
  160. },
  161. error: function (xhr, msg, three) {
  162. messageBox(false, "Fehler bei der Anfrage.");
  163. },
  164. dataType: "json"
  165. });
  166. }
  167. });
  168. }
  169. function onWorksheet() {
  170. $("#worksheet").on("click", function (e) {
  171. e.preventDefault();
  172. if (checkInputs()) {
  173. $.ajax({
  174. url: "/create-worksheet",
  175. method: "post",
  176. data: $('#form-risk').serializeArray(),
  177. success: function (data) {
  178. console.log(data);
  179. window.location = "/worksheet/" + data;
  180. },
  181. error: function (xhr, msg, three) {
  182. messageBox(false, "Fehler bei der Anfrage.");
  183. },
  184. dataType: "json"
  185. });
  186. }
  187. });
  188. }
  189. function checkInputs() {
  190. let cycleVal = $("#cycle").val();
  191. if (cycleVal === '') {
  192. messageBox(false, "Bitte geben Sie Ihren aktuellen Zyklus ein.");
  193. return false;
  194. } else {
  195. let cycleMin = parseInt($("#cycle").attr("min"));
  196. if (cycleVal < cycleMin) {
  197. $("#cycle").val(cycleMin);
  198. messageBox(false, "Zyklus an Minimal-Wert '" + cycleMin + "' angepasst.");
  199. }
  200. let cycleMax = parseInt($("#cycle").attr("max"));
  201. if (cycleVal > cycleMax) {
  202. $("#cycle").val(cycleMax);
  203. messageBox(false, "Zyklus an Maximal-Wert '" + cycleMax + "' angepasst.");
  204. }
  205. }
  206. return true;
  207. }
  208. function downloadWorksheet() {
  209. $("#download").on("click", function (e) {
  210. e.preventDefault();
  211. $.ajax({
  212. url: "/create-worksheet",
  213. method: "post",
  214. data: $('#form-risk').serializeArray(),
  215. success: function (data) {
  216. console.log(data);
  217. window.location = "/download/" + data;
  218. },
  219. error: function (xhr, msg, three) {
  220. messageBox(false, "Fehler bei der Anfrage.");
  221. },
  222. dataType: "json"
  223. });
  224. });
  225. }