omlink.js 3.1 KB

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