notifications.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import $ from 'jquery';
  2. import { config } from 'grav-config';
  3. import request from '../utils/request';
  4. const canFetchNotifications = () => config.notifications.enabled;
  5. const notificationsFilters = () => config.notifications.filters;
  6. class Notifications {
  7. static addShowAllInFeed() {
  8. $('#notifications ul').append('<li class="show-all" data-notification-action="show-all-notifications">Show all</li>');
  9. }
  10. static showNotificationInFeed(notification) {
  11. let notifications = $('#notifications').removeClass('hidden');
  12. let loader = notifications.find('.widget-loader').hide();
  13. let content = notifications.find('.widget-content > ul').show();
  14. loader.find('div').remove();
  15. loader.find('.fa-warning').removeClass('fa-warning').addClass('fa-refresh fa-spin');
  16. content
  17. .append(notification)
  18. .find('li:nth-child(n+11)').addClass('hidden'); // hide all items > 10
  19. if (content.find('li.hidden').length) {
  20. Notifications.addShowAllInFeed();
  21. }
  22. }
  23. static showNotificationInTop(notification) {
  24. const container = $('.top-notifications-container');
  25. const dummy = $('<div />').html(notification);
  26. container.removeClass('hidden').append(dummy.children());
  27. dummy.children().slideDown(150);
  28. }
  29. static showNotificationInDashboard(notification) {
  30. const container = $('.dashboard-notifications-container');
  31. const dummy = $('<div />').html(notification);
  32. container.removeClass('hidden').append(dummy.children());
  33. dummy.children().slideDown(150);
  34. }
  35. static showNotificationInPlugins(notification) {
  36. const container = $('.plugins-notifications-container');
  37. const dummy = $('<div />').html(notification);
  38. container.removeClass('hidden').append(dummy.children());
  39. dummy.children().slideDown(150);
  40. }
  41. static showNotificationInThemes(notification) {
  42. const container = $('.themes-notifications-container');
  43. const dummy = $('<div />').html(notification);
  44. container.removeClass('hidden').append(dummy.children());
  45. dummy.children().slideDown(150);
  46. }
  47. static processLocation(location, notification) {
  48. switch (location) {
  49. case 'feed':
  50. Notifications.showNotificationInFeed(notification);
  51. break;
  52. case 'top':
  53. if (!notification.read) {
  54. Notifications.showNotificationInTop(notification);
  55. }
  56. break;
  57. case 'dashboard':
  58. if (!notification.read) {
  59. Notifications.showNotificationInDashboard(notification);
  60. }
  61. break;
  62. case 'plugins':
  63. if (!notification.read) {
  64. Notifications.showNotificationInPlugins(notification);
  65. }
  66. break;
  67. case 'themes':
  68. if (!notification.read) {
  69. Notifications.showNotificationInThemes(notification);
  70. }
  71. break;
  72. }
  73. }
  74. // Grav.default.Notifications.fetch()
  75. fetch({ filter = notificationsFilters(), refresh = false } = {}) {
  76. if (!canFetchNotifications()) {
  77. return false;
  78. }
  79. let feed = $('#notifications');
  80. let loader = feed.find('.widget-loader');
  81. let content = feed.find('.widget-content > ul');
  82. loader.find('div').remove();
  83. loader.find('.fa-warning').removeClass('fa-warning').addClass('fa-refresh fa-spin');
  84. loader.show();
  85. content.hide();
  86. let processNotifications = (response) => {
  87. let notifications = response.notifications;
  88. $('#notifications').find('.widget-content > ul').empty();
  89. if (notifications) {
  90. Object.keys(notifications).forEach((location) => Notifications.processLocation(location, notifications[location]));
  91. }
  92. };
  93. request(`${config.base_url_relative}/task${config.param_sep}getNotifications`, {
  94. method: 'post',
  95. body: { refresh, filter }
  96. }, (response) => {
  97. processNotifications(response);
  98. }).catch(() => {
  99. let widget = $('#notifications .widget-content');
  100. widget
  101. .find('.widget-loader')
  102. .find('div').remove();
  103. widget
  104. .find('.widget-loader')
  105. .append('<div>Failed to retrieve notifications</div>')
  106. .find('.fa-spin')
  107. .removeClass('fa-spin fa-refresh').addClass('fa-warning');
  108. });
  109. }
  110. }
  111. let notifications = new Notifications();
  112. export default notifications;
  113. if (canFetchNotifications()) {
  114. notifications.fetch();
  115. /* Hide a notification and store it hidden
  116. // <a href="#" data-notification-action="hide-notification" data-notification-id="${notification.id}" class="close hide-notification"><i class="fa fa-close"></i></a>
  117. $(document).on('click', '[data-notification-action="hide-notification"]', (event) => {
  118. let notification_id = $(event.target).parents('.hide-notification').data('notification-id');
  119. let url = `${config.base_url_relative}/notifications.json/task${config.param_sep}hideNotification/notification_id${config.param_sep}${notification_id}`;
  120. request(url, { method: 'post' }, () => {});
  121. $(event.target).parents('.single-notification').hide();
  122. });
  123. */
  124. $(document).on('click', '[data-notification-action="hide-notification"]', (event) => {
  125. const target = $(event.currentTarget);
  126. const notification = target.parent();
  127. notification.slideUp(() => notification.remove());
  128. });
  129. $(document).on('click', '[data-notification-action="show-all-notifications"]', (event) => {
  130. $('#notifications .show-all').hide();
  131. $('#notifications .hidden').removeClass('hidden');
  132. });
  133. $(document).on('click', '[data-refresh="notifications"]', (event) => {
  134. event.preventDefault();
  135. notifications.fetch({ filter: ['feed'], refresh: true });
  136. });
  137. }