misago-notifications.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. $(function() {
  2. var ajax_cache = null;
  3. var $container = $('.user-notifications-nav');
  4. var $link = $container.children('a');
  5. function notifications_handler(data) {
  6. if (ajax_cache != null && data.count != ajax_cache.count) {
  7. ajax_cache = null;
  8. if ($container.hasClass('open')) {
  9. $container.find('.btn-refresh').fadeIn();
  10. if (data.count > 0) {
  11. $container.find('.btn-read-all').fadeIn();
  12. } else {
  13. $container.find('.btn-read-all').fadeOut();
  14. }
  15. }
  16. }
  17. }
  18. if (is_authenticated) {
  19. $.misago_ui().observer("misago_notifications", notifications_handler);
  20. }
  21. var $display = $container.find('.display');
  22. var $loader = $container.find('.loader');
  23. function handle_list_response(data) {
  24. ajax_cache = data;
  25. $loader.hide();
  26. $display.html(data.html);
  27. $display.show();
  28. $.misago_dom().changed();
  29. $link.tooltip('destroy');
  30. }
  31. function fetch_list() {
  32. $.get($link.attr('href'), function(data) {
  33. handle_list_response(data)
  34. });
  35. }
  36. $container.on('click', '.btn-refresh', function() {
  37. $display.hide();
  38. $loader.show();
  39. fetch_list();
  40. $link.tooltip('destroy');
  41. });
  42. $container.on('show.bs.dropdown', function () {
  43. if (ajax_cache == null) {
  44. fetch_list();
  45. } else {
  46. $link.tooltip('destroy');
  47. }
  48. });
  49. $container.on('hide.bs.dropdown', function() {
  50. misago_tooltip($link);
  51. $container.find('.btn-refresh').hide();
  52. });
  53. $container.on('submit', '.read-all-notifications', function() {
  54. $display.hide();
  55. $loader.show();
  56. $.post($link.attr('href'), $(this).serialize(), function(data) {
  57. handle_list_response(data);
  58. $.misago_ui().query_server();
  59. });
  60. return false;
  61. });
  62. });