index.js 3.2 KB

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