omlink.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Main Javascript file
  3. *
  4. * @author Open Mind
  5. * @date 27/06/2018
  6. * @time 17:42
  7. */
  8. var fetchHeaders = {
  9. 'Accept': 'text/html',
  10. 'Content-Type': 'text/html',
  11. 'X-Requested-With': 'XMLHttpRequest',
  12. 'Cache-Control': 'no-cache',
  13. 'Pragma': 'no-cache',
  14. 'Expires': '0'
  15. };
  16. function NotificationBuilder(notifications) {
  17. let tplNotify = '';
  18. let htmlNotificationArea = '';
  19. if (notifications.length === 0) {
  20. $('#notification_area_button').addClass('disabled');
  21. $('#sysmsgnum').empty();
  22. } else {
  23. notifications.forEach(function (element) {
  24. 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>`;
  25. });
  26. htmlNotificationArea = `
  27. ${tplNotify}
  28. <a id="notification_area_deleteall" class="dropdown-item text-center" href="javascript:NotificationDelete('#', 0)" >Cancella tutto</a>
  29. `;
  30. $('#notification_area_button').removeClass('disabled');
  31. $('#sysmsgnum').html(notifications.length);
  32. }
  33. $('#notification_area').html(htmlNotificationArea);
  34. }
  35. function refreshNotificationArea() {
  36. fetch(`${BASEURL}/api.system.getserverstatus`, {headers: fetchHeaders})
  37. .then(resp => {
  38. if (resp.status === 200)
  39. return resp.json();
  40. else
  41. return Promise.reject("getserverstatus: error");
  42. })
  43. .then(data => {
  44. NotificationBuilder(data.notify);
  45. })
  46. .catch(err => {
  47. console.log(err);
  48. });
  49. }
  50. function NotificationDelete(link, code) {
  51. fetch(`${BASEURL}/api.system.removenotification/${code}`, {method: "POST"})
  52. .then(() => {
  53. if (link !== '#')
  54. window.location.href = `${BASEURL}${link}`;
  55. })
  56. .catch(() => {
  57. alert('NotificationDelete error');
  58. });
  59. }
  60. function SetupConfirmationDialog() {
  61. $('.requestconfirm').on('click', function (ev) {
  62. let confirmText = $(this).data('confirmtext');
  63. if (!confirm(confirmText))
  64. ev.preventDefault();
  65. });
  66. }
  67. function ForceFocus() {
  68. setInterval(function () {
  69. $("[autofocus]").focus();
  70. }, 1000);
  71. }
  72. $(function () {
  73. SetupConfirmationDialog();
  74. refreshNotificationArea();
  75. ForceFocus(); // INPUT AUTOFOCUS
  76. $('#filters #filterIndex').val(currentFilterIdx);
  77. $('body').on('submit', 'form', function () {
  78. $('form input,form select').prop('disabled', false);
  79. setTimeout(function () {
  80. $('form input,form select').prop('disabled', true);
  81. }, 500);
  82. });
  83. setInterval(() => {
  84. refreshNotificationArea();
  85. }, 3000);
  86. });