You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

notification.js 2.7 kB

Use AJAX for notifications table (#10961) * Use AJAX for notifications table Signed-off-by: Andrew Thornton <art27@cantab.net> * move to separate js Signed-off-by: Andrew Thornton <art27@cantab.net> * placate golangci-lint Signed-off-by: Andrew Thornton <art27@cantab.net> * Add autoupdating notification count Signed-off-by: Andrew Thornton <art27@cantab.net> * Fix wipeall Signed-off-by: Andrew Thornton <art27@cantab.net> * placate tests Signed-off-by: Andrew Thornton <art27@cantab.net> * Try hidden Signed-off-by: Andrew Thornton <art27@cantab.net> * Try hide and hidden Signed-off-by: Andrew Thornton <art27@cantab.net> * More auto-update improvements Only run checker on pages that have a count Change starting checker to 10s with a back-off to 60s if there is no change Signed-off-by: Andrew Thornton <art27@cantab.net> * string comparison! Signed-off-by: Andrew Thornton <art27@cantab.net> * as per @silverwind Signed-off-by: Andrew Thornton <art27@cantab.net> * add configurability as per @6543 Signed-off-by: Andrew Thornton <art27@cantab.net> * Add documentation as per @6543 Signed-off-by: Andrew Thornton <art27@cantab.net> * Use CSRF header not query Signed-off-by: Andrew Thornton <art27@cantab.net> * Further JS improvements Fix @etzelia update notification table request Fix @silverwind comments Co-Authored-By: silverwind <me@silverwind.io> Signed-off-by: Andrew Thornton <art27@cantab.net> * Simplify the notification count fns Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: silverwind <me@silverwind.io>
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const {AppSubUrl, csrf, NotificationSettings} = window.config;
  2. export function initNotificationsTable() {
  3. $('#notification_table .button').on('click', async function () {
  4. const data = await updateNotification(
  5. $(this).data('url'),
  6. $(this).data('status'),
  7. $(this).data('page'),
  8. $(this).data('q'),
  9. $(this).data('notification-id'),
  10. );
  11. $('#notification_div').replaceWith(data);
  12. initNotificationsTable();
  13. await updateNotificationCount();
  14. return false;
  15. });
  16. }
  17. export function initNotificationCount() {
  18. if (NotificationSettings.MinTimeout <= 0) {
  19. return;
  20. }
  21. const notificationCount = $('.notification_count');
  22. if (notificationCount.length > 0) {
  23. const fn = (timeout, lastCount) => {
  24. setTimeout(async () => {
  25. await updateNotificationCountWithCallback(fn, timeout, lastCount);
  26. }, timeout);
  27. };
  28. fn(NotificationSettings.MinTimeout, notificationCount.text());
  29. }
  30. }
  31. async function updateNotificationCountWithCallback(callback, timeout, lastCount) {
  32. const currentCount = $('.notification_count').text();
  33. if (lastCount !== currentCount) {
  34. callback(NotificationSettings.MinTimeout, currentCount);
  35. return;
  36. }
  37. const newCount = await updateNotificationCount();
  38. let needsUpdate = false;
  39. if (lastCount !== newCount) {
  40. needsUpdate = true;
  41. timeout = NotificationSettings.MinTimeout;
  42. } else if (timeout < NotificationSettings.MaxTimeout) {
  43. timeout += NotificationSettings.TimeoutStep;
  44. }
  45. callback(timeout, newCount);
  46. const notificationDiv = $('#notification_div');
  47. if (notificationDiv.length > 0 && needsUpdate) {
  48. const data = await $.ajax({
  49. type: 'GET',
  50. url: `${AppSubUrl}/notifications?${notificationDiv.data('params')}`,
  51. data: {
  52. 'div-only': true,
  53. }
  54. });
  55. notificationDiv.replaceWith(data);
  56. initNotificationsTable();
  57. }
  58. }
  59. async function updateNotificationCount() {
  60. const data = await $.ajax({
  61. type: 'GET',
  62. url: `${AppSubUrl}/api/v1/notifications/new`,
  63. headers: {
  64. 'X-Csrf-Token': csrf,
  65. },
  66. });
  67. const notificationCount = $('.notification_count');
  68. if (data.new === 0) {
  69. notificationCount.addClass('hidden');
  70. } else {
  71. notificationCount.removeClass('hidden');
  72. }
  73. notificationCount.text(`${data.new}`);
  74. return `${data.new}`;
  75. }
  76. async function updateNotification(url, status, page, q, notificationID) {
  77. if (status !== 'pinned') {
  78. $(`#notification_${notificationID}`).remove();
  79. }
  80. return $.ajax({
  81. type: 'POST',
  82. url,
  83. data: {
  84. _csrf: csrf,
  85. notification_id: notificationID,
  86. status,
  87. page,
  88. q,
  89. noredirect: true,
  90. },
  91. });
  92. }