Projet

Général

Profil

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

v3 - Benjamin Dauvergne, 18 juillet 2014 18:42

Télécharger (3,39 ko)

Voir les différences:

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

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

  
7
    function ajaxform_submit (data, status, xhr, form) {
8
        if ('location' in data) {
9
            var location = $.url(data.location);
10
            var href = $.url(window.location.href);
11
            if (location.attr('protocol') == href.attr('protocol') &&
12
                location.attr('host') == href.attr('host') &&
13
                location.attr('relative') == href.attr('relative')) {
14
                var e = $.Event('popup-success');
15
                $anchor.trigger(e);
16
                if (! e.isDefaultPrevented()) {
17
                  window.location.reload(true);
18
                }
19
            }
20
            // set anchor if it changed
21
            window.location = data.location;
22
        } else {
23
            var html = data.content;
24
            $(form).empty().append($(html).find(selector).children());
25
            $(form).find('.buttons').hide();
26
        }
27
    }
28

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