omlink.js 3.1 KB

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