PageRenderTime 38ms CodeModel.GetById 1ms app.highlight 30ms RepoModel.GetById 2ms app.codeStats 0ms

/src/baruwa/static/js/domains.js

https://bitbucket.org/datopdog/baruwa
JavaScript | 212 lines | 184 code | 8 blank | 20 comment | 19 complexity | 9d3eeb72f501fc0b81d3593222b69e99 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 ajax_start(){
 22    $(this).append('&nbsp;Processing...').show();
 23    if ($('#in-progress').length) {
 24        $('#in-progress').remove();
 25    };
 26    $('#Footer_container').after(loading_msg);
 27}
 28
 29function ajax_stop(){
 30    $(this).empty().hide();
 31    $('#loading_message').remove();
 32}
 33
 34function ajax_error(event, request, settings){
 35    if(request.status == 200){
 36        location.href=settings.url;
 37    }else{
 38        $(this).empty().append('<span class="ajax_error">'+gettext('Error connecting to server. check network!')+'</span>').show();
 39        $('.Grid_heading').before('<div id="ajax-error-msg" class="ui-state-highlight">'+gettext('Server error')+'</div>');
 40        setTimeout(function() {
 41            $('#ajax-error-msg').empty().remove();
 42        }, 3900);
 43    }
 44    $('#loading_message').remove();
 45}
 46
 47function navigate(){
 48    window.scrollTo(0,0);
 49    url = $(this).attr('href').replace(/\//g, '-').replace(/^-/, '').replace(/-$/,'');
 50    $.address.value('?u='+url);
 51    $.address.history($.address.baseURL() + url);
 52    $.getJSON($(this).attr('href'),page_from_json);
 53    return false;
 54}
 55
 56function paginate(){
 57    fmt = gettext('Showing page %(page)s of %(pages)s pages.');
 58    data = {'page':rj.page, 'pages':rj.pages}
 59    tmp = interpolate(fmt, data, true);
 60    $('#heading').empty().append(tmp);
 61    $.address.title(tmp);
 62    count = 0;
 63    row = [];
 64    row[count++] = tmp;
 65    if(rj.show_first){
 66       row[count++] = '<span><a href="/'+rj.app+'/1/'+rj.direction+'/'+rj.order_by+'/">';
 67       row[count++] = '<img src="'+media_url+'imgs/first_pager.png" alt="First"/></a></span>';
 68       row[count++] = '<span>.....</span>';
 69    }
 70    if(rj.has_previous){
 71       row[count++] = '<span><a href="/'+rj.app+'/'+rj.previous+'/'+rj.direction+'/'+rj.order_by+'/">';
 72       row[count++] = '<img src="'+media_url+'imgs/previous_pager.png" alt="Previous"/></a></span>';
 73    }
 74    $.each(rj.page_numbers,function(itr,lnk){
 75       li = '/'+rj.app+'/'+lnk+'/'+rj.direction+'/'+rj.order_by+'/';
 76       if(rj.page == lnk){
 77          row[count++] = '<span><b>'+lnk+'</b>&nbsp;</span>';
 78       }else{
 79          row[count++] = '<span><a href="'+li+'">'+lnk+'</a>&nbsp;</span>'; 
 80       }
 81    });
 82    if(rj.has_next){
 83       row[count++] = '<span><a href="/'+rj.app+'/'+rj.next+'/'+rj.direction+'/'+rj.order_by+'/">';
 84       row[count++] = '<img src="'+media_url+'imgs/next_pager.png" alt="Next"/></a></span>';
 85    }
 86    if(rj.show_last){
 87       row[count++] = '<span>......</span>';
 88       row[count++] = '<a href="/'+rj.app+'/last/'+rj.direction+'/'+rj.order_by+'/">';
 89       row[count++] = '<img src="'+media_url+'imgs/last_pager.png" alt="Last"/></a></span>';
 90    }
 91    $('#paginator').html(row.join(''));
 92    $('#paginator span a').bind('click',navigate);
 93}
 94
 95function confirm_delete(event){
 96    event.preventDefault();
 97    $(this).blur();
 98    re = /\/accounts\/delete\/address\/([0-9]+)/
 99    l = $(this).attr('href');
100    m = l.match(re)
101    if (m) {
102        del_warning = [];
103        count = 0;
104        del_warning[count++] = '<div id="confirm-del-msg">';
105        del_warning[count++] = '<div id="confirm-del-info">';
106        del_warning[count++] = gettext('This will delete the domain ');
107        del_warning[count++] = gettext('and all associated data. This action is not reversible')+'</div>';
108        del_warning[count++] = '<div id="confirm-del-buttons">';
109        del_warning[count++] = gettext('Do you wish to continue ?');
110        del_warning[count++] = '&nbsp;<input type="button" value="Yes" id="yes_del" />&nbsp;';
111        del_warning[count++] = '<input type="button" value="No" id="no_del" />'
112        del_warning[count++] = '</div>';
113        del_warning[count++] = '</div>'
114        if ($('#confirm-del-msg').length) {
115            $('#confirm-del-msg').remove();
116        };
117        $('#domain-id-'+m[1]).after(del_warning.join(''));
118        $('#no_del').bind('click', function(event) {
119            event.preventDefault();
120            $('#confirm-del-msg').remove();
121        });
122        $('#yes_del').bind('click', function(event) {
123            event.preventDefault();
124            $('#confirm-del-msg').remove();
125            $.post(l, {id: m[1]}, function(response) {
126                if (response.success) {
127                    $('#domain-id-'+m[1]).remove();
128                    $('.Grid_content').before('<div id="in-progress">'+response.html+'</div>');
129                    $('#in-progress').append('<div id="dismiss"><a href="#">'+gettext('Dismiss')+'</a></div>')
130                    ip = setTimeout(function() {$('#in-progress').remove();}, 15050);
131                }else{
132                    $('.Grid_content').before('<div id="in-progress">'+response.html+'</div>');
133                    $('#in-progress').append('<div id="dismiss"><a href="#">'+gettext('Dismiss')+'</a></div>')
134                    ip = setTimeout(function() {$('#in-progress').remove();}, 15050);
135                };
136                $('#dismiss a').click(function(event){event.preventDefault();clearTimeout(ip);$('#in-progress').empty().remove();});
137            }, "json");
138        });
139    };
140}
141
142function page_from_json(data){
143    if (data) {
144        rj = data.paginator;
145        row = [];
146        count = 0;
147        css = 'DarkGray';
148        $.each(data.items, function(i, n) {
149            if(css == 'LightBlue'){
150                css = 'LightGray';
151            }else{
152                css = 'LightBlue';
153            }
154            if (n.enabled) {
155                img = '<img src="'+media_url+'imgs/active.png" alt="" />';
156                eimg = '<img src="'+media_url+'imgs/tick.png" alt="" />';
157            }else{
158                img = '<img src="'+media_url+'imgs/inactive.png" alt="" />';
159                eimg = '<img src="'+media_url+'imgs/minus.png" alt="" />';
160            };
161            row[count++] = '<div id="domain-id-'+n.id+'" class="'+css+'_div">';
162            row[count++] = '<div class="Domains_hash">'+img+'</div>';
163            row[count++] = '<div class="Domains_name"><a href="/settings/domains/'+n.id+'/">';
164            row[count++] = ' '+n.address+'</a></div>';
165            row[count++] = '<div class="Domains_owner"><a href="/accounts/user/'+n.user__id+'/">';
166            row[count++] = ' '+n.user__first_name+' '+n.user__last_name+' ('+n.user__username+')</a>';
167            row[count++] = '</div><div class="Domains_status">'+eimg+'</div>';
168            row[count++] = '<div class="Domains_action"><div class="Domains_action_edit">';
169            row[count++] = '<a href="/accounts/edit/address/'+n.id+'/">';
170            row[count++] = '<img src="'+media_url+'imgs/edit.png" alt="Edit" title="Edit" /></a></div>';
171            row[count++] = '<div class="Domains_action_delete">';
172            row[count++] = '<a href="/accounts/delete/address/'+n.id+'/">';
173            row[count++] = '<img src="'+media_url+'imgs/action_delete.png" alt="Delete" title="Delete" /></a>';
174            row[count++] = '</div></div></div>';
175        });
176        if(row.length){
177            $("div.Grid_heading").siblings('div').remove();
178            $("div.Grid_heading").after(row.join(''));
179        }else{
180            $("div.Grid_heading").siblings('div').remove();
181            $("div.Grid_heading").after('<div class="LightBlue_div"><div class="spanrow">'+gettext('No domains at the moment')+'</div></div>');
182        }
183        $('div.Domains_action_delete a').bind('click',confirm_delete);
184        paginate();
185    }
186}
187
188function handlextern(){
189    page = $.address.parameter("u");
190    if(page){
191        window.scrollTo(0,0);
192        page = $.trim(page);
193        re = /^settings\-[0-9]+\-dsc|asc\-id|address$/
194        if(re.test(page)){
195            page = page.replace(/-/g,'/');
196            url = '/'+ page + '/';
197            $.get(url, function(response){
198                page_from_json(response);
199            }, "json");   
200        }
201    }
202    return false;
203}
204
205function jsize_page(){
206    $('#my-spinner').ajaxStart(ajax_start).ajaxStop(ajax_stop).ajaxError(ajax_error);
207    $('#paginator span a').bind('click',navigate);
208    $.address.externalChange(handlextern);
209    $('div.Domains_action_delete a').bind('click',confirm_delete);
210}
211var loading_msg = '<div id="loading_message"><div id="loading"><img src="'+media_url+'imgs/ajax-loader.gif" alt="loading"/><br/><b>'+gettext('Processing')+'</b></div></div>';
212$(document).ready(jsize_page);