25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

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