{#

This file is part of the Sonata package.

(c) Thomas Rabaix <thomas.rabaix@sonata-project.org>

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

#}


{#

This code manages the one-to-many association field popup

#}

{% autoescape false %}

<!-- edit one association -->

<script type="text/javascript">

    // handle the add link
    var field_add_{{ id }} = function(event) {

        event.preventDefault();
        event.stopPropagation();

        var form = jQuery(this).closest('form');

        // the ajax post
        jQuery(form).ajaxSubmit({
            url: '{{ path('sonata_admin_append_form_element', {
                '_sonata_admin': sonata_admin.admin.root.baseCodeRoute,
                'elementId': id,
                'objectId': sonata_admin.admin.root.id(sonata_admin.admin.root.subject),
                'uniqid': sonata_admin.admin.root.uniqid,
            } + sonata_admin.field_description.getOption('link_parameters', {})
            + (
                sonata_admin.admin.root.hasRequest()
                ? sonata_admin.admin.root.request.attributes.get('_route_params', {})
                : {}
            ) + app.request.query.all|default({})
            ) }}',
            type: "POST",
            dataType: 'html',
            data: { _xml_http_request: true },
            success: function(html) {
                if (!html.length) {
                    return;
                }

                var $newForm = jQuery(html);
                var $oldForm = jQuery('#field_container_{{ id }}');

                // Maintain state of file inputs
                $oldForm.find('input[type="file"]').each(function(){
                    var id = '#'+$(this).attr('id');
                    $newForm.find(id).replaceWith($(this));
                });

                $oldForm.replaceWith($newForm); // replace the html

                Admin.shared_setup(jQuery('#field_container_{{ id }}'));

                if(jQuery('input[type="file"]', form).length > 0) {
                    jQuery(form).attr('enctype', 'multipart/form-data');
                    jQuery(form).attr('encoding', 'multipart/form-data');
                }
                jQuery('#sonata-ba-field-container-{{ id }}').trigger('sonata.add_element');
                jQuery('#field_container_{{ id }}').trigger('sonata.add_element');
            }
        });

        return false;
    };

    var field_widget_{{ id }} = false;

    // this function initializes the popup
    // this can be only done this way as popup can be cascaded
    function start_field_retrieve_{{ id }}(link) {

        link.onclick = null;

        // initialize component
        field_widget_{{ id }} = jQuery("#field_widget_{{ id }}");

        // add the jQuery event to the a element
        jQuery(link)
            .click(field_add_{{ id }})
            .trigger('click')
        ;

        return false;
    }
</script>

<!-- / edit one association -->

{% endautoescape %}
