123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- {#
- 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 manage the many-to-[one|many] association field popup
- #}
- <script>
- // handle the add link
- var field_dialog_form_add_{{ configuration.code }}_{{ field_element.id }} = function(event) {
- event.preventDefault();
- var a = jQuery(this);
- field_dialog_{{ configuration.code }}_{{ field_element.id }}.html('');
- // retrieve the form element from the related admin generator
- jQuery.ajax({
- url: a.attr('href'),
- success: function(html) {
- var html = jQuery(html);
- // populate the popup container
- field_dialog_{{ configuration.code }}_{{ field_element.id }}.html(html[0]);
- field_dialog_{{ configuration.code }}_{{ field_element.id }}.append(html[1]);
- // capture the submit event to make an ajax call, ie : POST data to the
- // related create admin
- jQuery('form', field_dialog_{{ configuration.code }}_{{ field_element.id }}).submit(field_dialog_form_action_{{ configuration.code }}_{{ field_element.id }});
- // open the dialog in modal mode
- field_dialog_{{ configuration.code }}_{{ field_element.id }}.dialog({
- height: 'auto',
- width: 650,
- modal: true,
- resizable: false,
- title: '{{ field_description.configuration.label }}'
- });
- }
- });
- };
-
- // handle the post data
- var field_dialog_form_action_{{ configuration.code }}_{{ field_element.id }} = function(event) {
- event.preventDefault();
- var form = jQuery(this);
- // the ajax post
- jQuery.ajax({
- url: form.attr('action'),
- type: "POST",
- data: form.serializeArray(),
- success: function(html) {
- // if the crud action return ok, then the element has been added
- // so the widget container must be refresh with the last option available
- if(html == 'ok') {
- field_dialog_{{ configuration.code }}_{{ field_element.id }}.dialog('close');
- // reload the form element
- jQuery.ajax({
- url: '{{ url('base_application_retrieve_form_element', {
- 'code': configuration.code,
- 'element_id': field_element.id,
- 'object_id': form.data.id,
- }) }}',
- data: jQuery('#field_widget_{{ configuration.code }}_{{ field_element.id}}').closest('form').serializeArray(),
- type: 'POST',
- success: function(html) {
- jQuery('#field_widget_{{ configuration.code }}_{{ field_element.id}}').html(html);
- }
- });
- return;
- }
- // otherwise, display form error
- field_dialog_{{ configuration.code }}_{{ field_element.id }}.html(html);
- BaseApplication.add_pretty_errors(field_dialog_{{ configuration.code }}_{{ field_element.id }});
- BaseApplication.add_collapsed_toggle(field_dialog_{{ configuration.code }}_{{ field_element.id }});
- // reattach the event
- jQuery('form', field_dialog_{{ configuration.code }}_{{ field_element.id }}).submit(field_dialog_form_action_{{ configuration.code }}_{{ field_element.id }});
- }
- });
- return false;
- }
- var field_dialog_{{ configuration.code }}_{{ field_element.id }} = false;
- // this function initialize the popup
- // this can be only done this way has popup can be cascaded
- function start_field_dialog_form_add_{{ configuration.code }}_{{ field_element.id }}(event) {
- event.preventDefault();
- // remove the html event a
- var a = jQuery(event.target).closest('a');
- a.attr('onclick', '');
-
- // initialize component
- field_dialog_{{ configuration.code }}_{{ field_element.id }} = jQuery("#field_dialog_{{ configuration.code }}_{{ field_element.id }}");
- // move the dialog as a child of the root element, nested form breaks html ...
- jQuery(document).append(field_dialog_{{ configuration.code }}_{{ field_element.id }});
- // add the jQuery event to the a element
- a.click(field_dialog_form_add_{{ configuration.code }}_{{ field_element.id }});
- // trigger the event
- a.trigger('click');
- return false;
- }
- BaseApplication.add_pretty_errors(field_dialog_{{ configuration.code }}_{{ field_element.id }});
- BaseApplication.add_collapsed_toggle(field_dialog_{{ configuration.code }}_{{ field_element.id }});
-
- </script>
|