frontend.js 3.0 KB

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