Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

notification.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. const {appSubUrl, csrfToken, notificationSettings} = window.config;
  2. let notificationSequenceNumber = 0;
  3. export function initNotificationsTable() {
  4. $('#notification_table .button').on('click', function () {
  5. (async () => {
  6. const data = await updateNotification(
  7. $(this).data('url'),
  8. $(this).data('status'),
  9. $(this).data('page'),
  10. $(this).data('q'),
  11. $(this).data('notification-id'),
  12. );
  13. if ($(data).data('sequence-number') === notificationSequenceNumber) {
  14. $('#notification_div').replaceWith(data);
  15. initNotificationsTable();
  16. }
  17. await updateNotificationCount();
  18. })();
  19. return false;
  20. });
  21. }
  22. async function receiveUpdateCount(event) {
  23. try {
  24. const data = JSON.parse(event.data);
  25. const notificationCount = document.querySelector('.notification_count');
  26. if (data.Count > 0) {
  27. notificationCount.classList.remove('hidden');
  28. } else {
  29. notificationCount.classList.add('hidden');
  30. }
  31. notificationCount.textContent = `${data.Count}`;
  32. await updateNotificationTable();
  33. } catch (error) {
  34. console.error(error, event);
  35. }
  36. }
  37. export function initNotificationCount() {
  38. const notificationCount = $('.notification_count');
  39. if (!notificationCount.length) {
  40. return;
  41. }
  42. if (notificationSettings.EventSourceUpdateTime > 0 && !!window.EventSource && window.SharedWorker) {
  43. // Try to connect to the event source via the shared worker first
  44. const worker = new SharedWorker(`${__webpack_public_path__}js/eventsource.sharedworker.js`, 'notification-worker');
  45. worker.addEventListener('error', (event) => {
  46. console.error(event);
  47. });
  48. worker.port.addEventListener('messageerror', () => {
  49. console.error('Unable to deserialize message');
  50. });
  51. worker.port.postMessage({
  52. type: 'start',
  53. url: `${window.location.origin}${appSubUrl}/user/events`,
  54. });
  55. worker.port.addEventListener('message', (event) => {
  56. if (!event.data || !event.data.type) {
  57. console.error(event);
  58. return;
  59. }
  60. if (event.data.type === 'notification-count') {
  61. const _promise = receiveUpdateCount(event.data);
  62. } else if (event.data.type === 'error') {
  63. console.error(event.data);
  64. } else if (event.data.type === 'logout') {
  65. if (event.data.data !== 'here') {
  66. return;
  67. }
  68. worker.port.postMessage({
  69. type: 'close',
  70. });
  71. worker.port.close();
  72. window.location.href = appSubUrl;
  73. } else if (event.data.type === 'close') {
  74. worker.port.postMessage({
  75. type: 'close',
  76. });
  77. worker.port.close();
  78. }
  79. });
  80. worker.port.addEventListener('error', (e) => {
  81. console.error(e);
  82. });
  83. worker.port.start();
  84. window.addEventListener('beforeunload', () => {
  85. worker.port.postMessage({
  86. type: 'close',
  87. });
  88. worker.port.close();
  89. });
  90. return;
  91. }
  92. if (notificationSettings.MinTimeout <= 0) {
  93. return;
  94. }
  95. const fn = (timeout, lastCount) => {
  96. setTimeout(() => {
  97. const _promise = updateNotificationCountWithCallback(fn, timeout, lastCount);
  98. }, timeout);
  99. };
  100. fn(notificationSettings.MinTimeout, notificationCount.text());
  101. }
  102. async function updateNotificationCountWithCallback(callback, timeout, lastCount) {
  103. const currentCount = $('.notification_count').text();
  104. if (lastCount !== currentCount) {
  105. callback(notificationSettings.MinTimeout, currentCount);
  106. return;
  107. }
  108. const newCount = await updateNotificationCount();
  109. let needsUpdate = false;
  110. if (lastCount !== newCount) {
  111. needsUpdate = true;
  112. timeout = notificationSettings.MinTimeout;
  113. } else if (timeout < notificationSettings.MaxTimeout) {
  114. timeout += notificationSettings.TimeoutStep;
  115. }
  116. callback(timeout, newCount);
  117. if (needsUpdate) {
  118. await updateNotificationTable();
  119. }
  120. }
  121. async function updateNotificationTable() {
  122. const notificationDiv = $('#notification_div');
  123. if (notificationDiv.length > 0) {
  124. const data = await $.ajax({
  125. type: 'GET',
  126. url: `${appSubUrl}/notifications?${notificationDiv.data('params')}`,
  127. data: {
  128. 'div-only': true,
  129. 'sequence-number': ++notificationSequenceNumber,
  130. }
  131. });
  132. if ($(data).data('sequence-number') === notificationSequenceNumber) {
  133. notificationDiv.replaceWith(data);
  134. initNotificationsTable();
  135. }
  136. }
  137. }
  138. async function updateNotificationCount() {
  139. const data = await $.ajax({
  140. type: 'GET',
  141. url: `${appSubUrl}/api/v1/notifications/new`,
  142. headers: {
  143. 'X-Csrf-Token': csrfToken,
  144. },
  145. });
  146. const notificationCount = $('.notification_count');
  147. if (data.new === 0) {
  148. notificationCount.addClass('hidden');
  149. } else {
  150. notificationCount.removeClass('hidden');
  151. }
  152. notificationCount.text(`${data.new}`);
  153. return `${data.new}`;
  154. }
  155. async function updateNotification(url, status, page, q, notificationID) {
  156. if (status !== 'pinned') {
  157. $(`#notification_${notificationID}`).remove();
  158. }
  159. return $.ajax({
  160. type: 'POST',
  161. url,
  162. data: {
  163. _csrf: csrfToken,
  164. notification_id: notificationID,
  165. status,
  166. page,
  167. q,
  168. noredirect: true,
  169. 'sequence-number': ++notificationSequenceNumber,
  170. },
  171. });
  172. }