omlink.js 2.2 KB

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