frontend.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Main Javascript file
  3. *
  4. * @author Giancarlo Voci
  5. * @date 27/06/2018
  6. * @time 17:42
  7. */
  8. import './common';
  9. import {AjaxReq, fetchHeaders} from "./OMGUtils";
  10. import EditTableInit from "./edittable";
  11. let CUSTOM_NOTIFY = 0x4000;
  12. let REFRESH_FILTERS = CUSTOM_NOTIFY + 1;
  13. /* usefull if a blinking text is need, like in MaxMara project, you need to place this code in html:
  14. *
  15. * <nav class="navbar navbar-collapse p-0">
  16. * <span class="navbar-text blink" style="color: red" id="statusbartext"></span>
  17. * </nav>
  18. */
  19. function SetStatusBarText(msg) {
  20. $('#statusbartext').text(msg);
  21. setTimeout(() => {
  22. $('#statusbartext').text('');
  23. }, 60000);
  24. }
  25. function NotificationBuilder(notifications) {
  26. let tplNotify = '';
  27. let htmlNotificationArea = '';
  28. if (notifications.length === 0) {
  29. $('#notification_area_button').addClass('disabled');
  30. $('#sysmsgnum').empty();
  31. } else {
  32. let notificationCount = 0;
  33. notifications.forEach(function (element) {
  34. // If notification codes are >CUSTON_NOTIFY means that are special codes to skip in this logic
  35. if (element.code > CUSTOM_NOTIFY)
  36. return;
  37. tplNotify += `<a class="dropdown-item" href="javascript:NotificationDelete('${element.link}', ${element.code})">[${element.timestamp}] ${element.caption}<span class="ml-1 badge badge-danger">${element.count}</span></a>`;
  38. notificationCount++;
  39. });
  40. htmlNotificationArea = `
  41. ${tplNotify}
  42. <a id="notification_area_deleteall" class="dropdown-item text-center" href="javascript:NotificationDelete('#', 0)" >Cancella tutto</a>
  43. `;
  44. $('#notification_area_button').removeClass('disabled');
  45. $('#sysmsgnum').html(notificationCount);
  46. }
  47. $('#notification_area').html(htmlNotificationArea);
  48. }
  49. function refreshNotificationArea() {
  50. fetch(`${BASEURL}/api.system.getserverstatus`, {headers: fetchHeaders})
  51. .then(resp => {
  52. if (resp.status === 200)
  53. return resp.json();
  54. else
  55. return Promise.reject("getserverstatus: error");
  56. })
  57. .then(data => {
  58. NotificationBuilder(data.notify);
  59. });
  60. }
  61. function NotificationDelete(link, code) {
  62. fetch(`${BASEURL}/api.system.removenotification/${code}`, {method: "POST"})
  63. .then(() => {
  64. if ((link != null) && (link.length != 0) && (link != '#'))
  65. window.location.href = `${BASEURL}${link}`;
  66. })
  67. .catch(() => {
  68. alert('NotificationDelete error');
  69. });
  70. }
  71. function SetupConfirmationDialog() {
  72. $('.requestconfirm').on('click', function (ev) {
  73. let confirmText = $(this).data('confirmtext');
  74. if (!confirm(confirmText))
  75. ev.preventDefault();
  76. });
  77. }
  78. function ForceFocus() {
  79. setInterval(function () {
  80. $("[forcefocus]").focus();
  81. }, 1000);
  82. }
  83. document.addEventListener('DOMContentLoaded', function () {
  84. SetupConfirmationDialog();
  85. refreshNotificationArea();
  86. ForceFocus(); // INPUT AUTOFOCUS
  87. $('body').on('submit', 'form', function () {
  88. $('body').focus();
  89. $('form input,form select').prop('readonly', true);
  90. // Manual submit for more control
  91. this.submit();
  92. return false;
  93. });
  94. setInterval(() => {
  95. refreshNotificationArea();
  96. }, 3000);
  97. //EditTableInit('.datatables', `${BASEURL}/pickinglistcontroller`);
  98. window.NotificationDelete = NotificationDelete;
  99. });