templates/bundles/SonataAdminBundle/CRUD/Association/edit_one_script.html.twig line 1

Open in your IDE?
  1. {#
  2. This file is part of the Sonata package.
  3. (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  4. For the full copyright and license information, please view the LICENSE
  5. file that was distributed with this source code.
  6. #}
  7. {#
  8. This code manages the one-to-many association field popup
  9. #}
  10. {% autoescape false %}
  11. <!-- edit one association -->
  12. <script type="text/javascript">
  13.     // handle the add link
  14.     var field_add_{{ id }} = function(event) {
  15.         event.preventDefault();
  16.         event.stopPropagation();
  17.         var form = jQuery(this).closest('form');
  18.         form.find("input.numeric").getNumericValues();
  19.         // the ajax post
  20.         jQuery(form).ajaxSubmit({
  21.                 url: '{{ path('sonata_admin_append_form_element', {
  22.                 '_sonata_admin': sonata_admin.admin.root.baseCodeRoute,
  23.                 'elementId': id,
  24.                 'objectId': sonata_admin.admin.root.id(sonata_admin.admin.root.subject),
  25.                 'uniqid': sonata_admin.admin.root.uniqid,
  26.             } + sonata_admin.field_description.getOption('link_parameters', {})
  27.             + (
  28.                 sonata_admin.admin.root.hasRequest()
  29.                     ? sonata_admin.admin.root.request.attributes.get('_route_params', {})
  30.                     : {}
  31.             ) + app.request.query.all|default({})
  32.     ) }}',
  33.         type: "POST",
  34.         dataType: 'html',
  35.         data: { _xml_http_request: true },
  36.     success: function(html) {
  37.         if (!html.length) {
  38.             return;
  39.         }
  40.         var $newForm = jQuery(html);
  41.         var $oldForm = jQuery('#field_container_{{ id }}');
  42.         // Maintain state of file inputs
  43.         $oldForm.find('input[type="file"]').each(function(){
  44.             var id = '#'+$(this).attr('id');
  45.             $newForm.find(id).replaceWith($(this));
  46.         });
  47.         $oldForm.replaceWith($newForm); // replace the html
  48.         Admin.shared_setup(jQuery('#field_container_{{ id }}'));
  49.         if(jQuery('input[type="file"]', form).length > 0) {
  50.             jQuery(form).attr('enctype', 'multipart/form-data');
  51.             jQuery(form).attr('encoding', 'multipart/form-data');
  52.         }
  53.         jQuery('#sonata-ba-field-container-{{ id }}').trigger('sonata.add_element');
  54.         jQuery('#field_container_{{ id }}').trigger('sonata.add_element');
  55.     },
  56.             complete:function()
  57.             {
  58.                 $(form).find("input.percents").setPercent();
  59.                 $(form).find("input.numerics").setNumeric();
  60.                 $(form).find("input.currencies").setCurrency();
  61.                 $(form).find("input.grams").setGram();
  62.             }
  63.     });
  64.     return false;
  65.     };
  66.     var field_widget_{{ id }} = false;
  67.     // this function initializes the popup
  68.     // this can be only done this way as popup can be cascaded
  69.     function start_field_retrieve_{{ id }}(link) {
  70.         link.onclick = null;
  71.         // initialize component
  72.         field_widget_{{ id }} = jQuery("#field_widget_{{ id }}");
  73.         // add the jQuery event to the a element
  74.         jQuery(link)
  75.             .click(field_add_{{ id }})
  76.     .trigger('click')
  77.         ;
  78.         return false;
  79.     }
  80. </script>
  81. <!-- / edit one association -->
  82. {% endautoescape %}