frontend.js 3.1 KB

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