/src/baruwa/static/js/recent_messages.js

https://bitbucket.org/datopdog/baruwa · JavaScript · 85 lines · 62 code · 3 blank · 20 comment · 12 complexity · 68b6ab765cb83fc100a59806a9cfa116 MD5 · raw file

  1. //
  2. // Baruwa - Web 2.0 MailScanner front-end.
  3. // Copyright (C) 2010 Andrew Colin Kissa <andrew@topdog.za.net>
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License along
  16. // with this program; if not, write to the Free Software Foundation, Inc.,
  17. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. //
  19. // vim: ai ts=4 sts=4 et sw=4
  20. //
  21. function prevent_interupt_refresh(event){
  22. if(ax_in_progress){
  23. event.preventDefault();
  24. if(!$("#in-progress").length){
  25. $('.Grid_content').before('<div id="in-progress">'+gettext('Content refresh in progress, please wait for it to complete')+'</div>');
  26. setTimeout(function() {$('#in-progress').remove();}, 15050);
  27. window.scroll(0,0);
  28. }
  29. }
  30. }
  31. function build_table_from_json(){
  32. if(! ax_in_progress){
  33. if(last_ts != ''){
  34. $.ajaxSetup({'beforeSend':function(xhr){xhr.setRequestHeader("X-Last-Timestamp",last_ts);}});
  35. }
  36. $.getJSON('/messages/',json2html);
  37. if(auto_refresh){
  38. clearInterval(auto_refresh);
  39. clearTimeout(auto_refresh);
  40. }
  41. $("a").bind('click',prevent_interupt_refresh);
  42. setTimeout(build_table_from_json,60000);
  43. }
  44. }
  45. function do_table_sort(){
  46. full_messages_listing = false;
  47. $('.nojs').remove();
  48. ax_in_progress = false;
  49. $("#heading small").empty().append(gettext("[updated every 60 sec's]"));
  50. $("#my-spinner").ajaxSend(function(){
  51. $(this).empty().append('&nbsp;'+gettext('Refreshing...')).show();
  52. ax_error = false;
  53. ax_in_progress = true;
  54. })
  55. .ajaxStop(function() {
  56. if(!ax_error){
  57. var lu = lastupdatetime();
  58. $("#heading small").empty().append(gettext('[last update at ')+lu+']');
  59. $('#my-spinner').hide();
  60. ax_in_progress = false;
  61. if($("#in-progress").is(':visible')){
  62. $("#in-progress").hide();
  63. }
  64. }
  65. })
  66. .ajaxError(function(event, request, settings){
  67. if(request.status == 200){
  68. location.href=settings.url;
  69. }else{
  70. $(this).empty().append('<span class="ajax_error">'+gettext('Error connecting to server. check network!')+'</span>').show();
  71. ax_error = true;
  72. ax_in_progress = false;
  73. if($("#in-progress").is(':visible')){
  74. $("#in-progress").hide();
  75. }
  76. auto_refresh = setTimeout(build_table_from_json,60000);
  77. }
  78. });
  79. $('a').bind('click',prevent_interupt_refresh);
  80. }
  81. var auto_refresh = setInterval(build_table_from_json, 60000);
  82. $(document).ready(do_table_sort);