Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

285 Zeilen
9.5 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. $('#benchmark')
  130. .find('option')
  131. .remove()
  132. .end()
  133. ;
  134. for (let i in data.benchmarkValues) {
  135. noBenchmarkData = true;
  136. $('#benchmark').append('<option value="' + i + '">' + data.benchmarkValues[i] + '</option>');
  137. }
  138. if (Object.keys(data.benchmarkValues).length <= 0) {
  139. $('#no-benchmark-data').show();
  140. $('#benchmark').prop( "disabled", true );
  141. } else {
  142. $('#no-benchmark-data').hide();
  143. $('#benchmark').prop( "disabled", false );
  144. }
  145. },
  146. error: function (xhr, msg, three) {
  147. messageBox(false, "Fehler bei der Anfrage.");
  148. },
  149. dataType: "json"
  150. });
  151. }
  152. function onCalculate() {
  153. $("#calculate").on("click", function (e) {
  154. e.preventDefault();
  155. if (checkInputs()) {
  156. $.ajax({
  157. url: "/calculate-risk",
  158. method: "post",
  159. data: $('#form-risk').serializeArray(),
  160. success: function (data) {
  161. $('#resInspection').text(data['recCycleInspection']);
  162. $('#resMaintenance').text(data['recCycleMaintenance']);
  163. let percentage = "-";
  164. if (data['costDiffCurRecPercentage'] !== null) {
  165. percentage = (parseFloat(data['costDiffCurRecPercentage']) * 100) + " %";
  166. }
  167. $('#resPercentage').text(percentage);
  168. let costDiff = "-";
  169. if (data['costDiffCurCycleRecCycle'] !== null) {
  170. costDiff = parseFloat(data['costDiffCurCycleRecCycle']).toFixed(2);
  171. costDiff = costDiff.replace(/\./g, ",") + " €";
  172. }
  173. $('#resEuro').text(costDiff);
  174. $("#result-box").addClass("result-existing");
  175. $('html, body').animate({
  176. scrollTop: $('#result-box').offset().top - 200
  177. }, 500);
  178. },
  179. error: function (xhr, msg, three) {
  180. messageBox(false, "Fehler bei der Anfrage.");
  181. },
  182. dataType: "json"
  183. });
  184. }
  185. });
  186. }
  187. function onCreateWorksheet() {
  188. $("#worksheet").on("click", function (e) {
  189. e.preventDefault();
  190. let result = window.confirm("Sie nutzen hiermit einen Ihrer Durchgänge.");
  191. if (result) {
  192. if (checkInputs()) {
  193. $.ajax({
  194. url: "/create-worksheet",
  195. method: "post",
  196. data: $('#form-risk').serializeArray(),
  197. success: function (data) {
  198. console.log(data);
  199. window.location = "/worksheet/" + data;
  200. },
  201. error: function (xhr, msg, three) {
  202. messageBox(false, "Fehler bei der Anfrage.");
  203. },
  204. dataType: "json"
  205. });
  206. }
  207. }
  208. });
  209. $("#create-worksheet").on("click", function (e) {
  210. e.preventDefault();
  211. let result = window.confirm("Sie nutzen hiermit einen Ihrer Durchgänge.");
  212. if (result) {
  213. $.ajax({
  214. url: "/create-worksheet",
  215. method: "post",
  216. data: {
  217. asset: $('#asset').val()
  218. },
  219. success: function (data) {
  220. console.log(data);
  221. window.location = "/worksheet/" + data;
  222. },
  223. error: function (xhr, msg, three) {
  224. messageBox(false, "Fehler bei der Anfrage.");
  225. },
  226. dataType: "json"
  227. });
  228. }
  229. });
  230. }
  231. function checkInputs() {
  232. let cycleVal = $("#cycle").val();
  233. if (cycleVal === '') {
  234. messageBox(false, "Bitte geben Sie Ihren aktuellen Zyklus ein.");
  235. return false;
  236. } else {
  237. let cycleMin = parseInt($("#cycle").attr("min"));
  238. if (cycleVal < cycleMin) {
  239. $("#cycle").val(cycleMin);
  240. messageBox(false, "Zyklus an Minimal-Wert '" + cycleMin + "' angepasst.");
  241. }
  242. let cycleMax = parseInt($("#cycle").attr("max"));
  243. if (cycleVal > cycleMax) {
  244. $("#cycle").val(cycleMax);
  245. messageBox(false, "Zyklus an Maximal-Wert '" + cycleMax + "' angepasst.");
  246. }
  247. }
  248. return true;
  249. }
  250. function downloadWorksheet() {
  251. $("#download").on("click", function (e) {
  252. e.preventDefault();
  253. $.ajax({
  254. url: "/create-worksheet",
  255. method: "post",
  256. data: $('#form-risk').serializeArray(),
  257. success: function (data) {
  258. console.log(data);
  259. window.location = "/download/" + data;
  260. },
  261. error: function (xhr, msg, three) {
  262. messageBox(false, "Fehler bei der Anfrage.");
  263. },
  264. dataType: "json"
  265. });
  266. });
  267. }