index.js 3.3 KB

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