Projet

Général

Profil

0001-gadjo.js-add-support-for-ajax-submission-of-form.patch

Benjamin Dauvergne, 18 juillet 2014 17:41

Télécharger (3,25 ko)

Voir les différences:

Subject: [PATCH] gadjo.js: add support for ajax submission of form

 gadjo/static/js/gadjo.js |   39 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 36 insertions(+), 3 deletions(-)
gadjo/static/js/gadjo.js
1 1
function displayPopup(event)
2 2
{
3 3
    var url = $(this).attr('href');
4

  
5
    function ajaxform_submit (data, status, xhr, form) {
6
        if ('location' in data) {
7
            var location = $.url(data.location);
8
            var href = $.url(window.location.href);
9
            if (location.attr('protocol') == href.attr('protocol') &&
10
                location.attr('host') == href.attr('host') &&
11
                location.attr('relative') == href.attr('relative')) {
12
                if (window.refresh_page != undefined) {
13
                  window.refresh_page(); // ajax refresh if available
14
                } else {
15
                  window.location.reload(true);
16
                }
17
            }
18
            // set anchor if it changed
19
            window.location = data.location;
20
        } else {
21
            var html = data.content;
22
            var new_form = $(html).find('#content form');
23
            $(form).html(new_form.html());
24
            $(form).find('.buttons').hide();
25
        }
26
    }
27

  
4 28
    $.ajax({
5 29
        url: url,
6 30
        success: function(html) {
31
            var is_json = typeof html != 'string';
32
            if (is_json) {
33
                var html = html.content;
34
            } else {
35
                var html = html;
36
            }
7 37
            var form = $(html).find('#content form');
8 38
            var title = $(html).find('#appbar h2').text();
9
            var dialog = $(form).dialog({modal: true, title: title, width: 'auto'});
39
            var dialog = $(form).dialog({modal: true, 'title': title, width: 'auto'});
10 40
            var buttons = Array();
11 41
            if (! form.prop('action')) {
12 42
                form.prop('action', url);
......
16 46
                var button = Object();
17 47
                button.text = $(elem).text();
18 48
                if ($(elem).hasClass('cancel')) {
19
                    button.click = function() { $(this).dialog('destroy'); return false; };
49
                    button.click = function() { dialog.dialog('destroy'); return false; };
20 50
                } else {
21
                    button.click = function() { (form).find('button').click(); return false; };
51
                    button.click = function() { form.find('button').click(); return false; };
22 52
                }
23 53
                if ($(elem).hasClass('submit-button')) {
24 54
                    button.class = 'submit-button';
......
32 62
            if ($(dialog).find('input:visible').length) {
33 63
                $(dialog).find('input:visible')[0].focus();
34 64
            }
65
            if (is_json && $.fn.ajaxForm != undefined) {
66
                $(form).ajaxForm({success: ajaxform_submit});
67
            }
35 68
            return false;
36 69
        }
37 70
    });
38
-