omlink.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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('/omlink/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(`/omlink/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. if (!confirm('Sicuro di voler cancellare la lista?'))
  55. ev.preventDefault();
  56. });
  57. }
  58. $(function () {
  59. SetupConfirmationDialog();
  60. refreshNotificationArea();
  61. $('form').on('submit', function () {
  62. $('form input,select').prop('disabled', false);
  63. setTimeout(function () {
  64. $('form input,select').prop('disabled', true);
  65. }, 500);
  66. });
  67. setInterval(() => {
  68. refreshNotificationArea();
  69. }, 10000);
  70. });