Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

245 строки
8.0 KiB

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