PageRenderTime 24ms CodeModel.GetById 17ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/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
Possible License(s): GPL-2.0
 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//
21function 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
32function build_table_from_json(){
33    if(! ax_in_progress){
34        if(last_ts != ''){
35            $.ajaxSetup({'beforeSend':function(xhr){xhr.setRequestHeader("X-Last-Timestamp",last_ts);}});
36        }
37        $.getJSON('/messages/',json2html); 
38        if(auto_refresh){
39            clearInterval(auto_refresh);
40            clearTimeout(auto_refresh);
41        }
42        $("a").bind('click',prevent_interupt_refresh);
43        setTimeout(build_table_from_json,60000);
44    }
45}
46
47function do_table_sort(){
48    full_messages_listing = false;
49    $('.nojs').remove();
50    ax_in_progress = false;
51	$("#heading small").empty().append(gettext("[updated every 60 sec's]"));
52    $("#my-spinner").ajaxSend(function(){
53	    $(this).empty().append('&nbsp;'+gettext('Refreshing...')).show();
54	    ax_error = false;
55        ax_in_progress = true;
56    })
57    .ajaxStop(function() {
58	    if(!ax_error){
59		    var lu = lastupdatetime();
60		    $("#heading small").empty().append(gettext('[last update at ')+lu+']');
61            $('#my-spinner').hide();
62            ax_in_progress = false;
63            if($("#in-progress").is(':visible')){
64                $("#in-progress").hide();
65            }
66	    }
67    })
68    .ajaxError(function(event, request, settings){
69        if(request.status == 200){
70            location.href=settings.url;
71        }else{
72	        $(this).empty().append('<span class="ajax_error">'+gettext('Error connecting to server. check network!')+'</span>').show();
73	        ax_error = true;
74            ax_in_progress = false;
75            if($("#in-progress").is(':visible')){
76                $("#in-progress").hide();
77            }
78            auto_refresh = setTimeout(build_table_from_json,60000);
79        }
80    });
81    $('a').bind('click',prevent_interupt_refresh);
82}
83
84var auto_refresh = setInterval(build_table_from_json, 60000);
85$(document).ready(do_table_sort);