modal_api.html 2.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title" id="myModalLabel">
        <span class="glyphicon glyphicon-list-alt" style="margin-right: 10px;" ></span>
        {% block modal_title %}{% endblock %}    
    </h4>
  </div>
  <div class="modal-body">
  {% block modal_body %}{% endblock %}
  </div>
12
  {% block modal_footer %}
13 14 15 16
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    {% if object.id != 0 %}
    <input type="hidden" id="modal_object_id" value="{{ object.id }}">
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
17
    <input type="button" class="btn btn-success" id="save_button" value="Enregistrer les changements">
18 19 20 21 22 23
    <input type="button" class="btn btn-danger" id="delete_button" value="Supprimer">
    {% else %}
    <input type="hidden" id="modal_object_id" value="0">
    <input type="button" class="btn btn-success" id="save_button" value="Créer">
    {% endif %}
  </div>
24
  {% endblock %}
25 26 27 28 29
</div>

<script>
// buttons events
$('#save_button').click(function(){
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
30
    {% block return_url %}{% endblock %}
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    {% block javascript_fields %}{% endblock %}
    var post_data = { o: $('#o').val() };
    
    if ($('#modal_object_id').val() == 0) {
        post_data['a'] = 'create';
    } else {
        post_data['a'] = 'update';
        post_data['i'] = $('#modal_object_id').val();
    }
    $.each(fields, function (key, val) {
        post_data[val] = $('#'+val).val();
        // and clean the error messages
        $('#'+val).parent().removeClass('has-error');
        $('#help-'+val).html('').hide();
    });
46

Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
47 48 49
    $.post('api.php', post_data)
        .done(function( data ) {
            $('#created_ok').show();
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
50
            if(return_url != '') {
51
                window.setTimeout(1000, 
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
52 53
                    location.href = return_url);
            }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
54 55 56 57 58 59 60 61 62 63
        })
        .fail(function(data) {
            $.each(data.responseJSON, function(key, val) {
                // add the class error to the field
                $('#' + key).parent().addClass('has-error');
                // display reasons
                var helptext = '<ul>';
                // format val
                $.each(val, function(key2, val2) {
                    helptext += '<li>' + val2 + '</li>';
64
                });
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
65
                $('#help-' + key).html(helptext + '</ul>').show();
66
            });
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
67 68
            return false;
        });
69 70 71 72 73 74 75 76 77 78
    return true;
});
$('#delete_button').click(function(){
    {% block javascript_msg %}{% endblock %}
    if(confirm(msg)) {
        $('#a').val('delete');
        document.defaultform.submit();
    }
});
</script>