edit_many_association_script.twig 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 manage the many-to-[one|many] association field popup
  9. #}
  10. <script>
  11. // handle the add link
  12. var field_dialog_form_add_{{ configuration.code }}_{{ field_element.id }} = function(event) {
  13. event.preventDefault();
  14. var a = jQuery(this);
  15. field_dialog_{{ configuration.code }}_{{ field_element.id }}.html('');
  16. // retrieve the form element from the related admin generator
  17. jQuery.ajax({
  18. url: a.attr('href'),
  19. success: function(html) {
  20. var html = jQuery(html);
  21. // populate the popup container
  22. field_dialog_{{ configuration.code }}_{{ field_element.id }}.html(html[0]);
  23. field_dialog_{{ configuration.code }}_{{ field_element.id }}.append(html[1]);
  24. // capture the submit event to make an ajax call, ie : POST data to the
  25. // related create admin
  26. jQuery('form', field_dialog_{{ configuration.code }}_{{ field_element.id }}).submit(field_dialog_form_action_{{ configuration.code }}_{{ field_element.id }});
  27. // open the dialog in modal mode
  28. field_dialog_{{ configuration.code }}_{{ field_element.id }}.dialog({
  29. height: 'auto',
  30. width: 650,
  31. modal: true,
  32. resizable: false,
  33. title: '{{ field_description.configuration.label }}'
  34. });
  35. }
  36. });
  37. };
  38. // handle the post data
  39. var field_dialog_form_action_{{ configuration.code }}_{{ field_element.id }} = function(event) {
  40. event.preventDefault();
  41. var form = jQuery(this);
  42. // the ajax post
  43. jQuery.ajax({
  44. url: form.attr('action'),
  45. type: "POST",
  46. data: form.serializeArray(),
  47. success: function(html) {
  48. // if the crud action return ok, then the element has been added
  49. // so the widget container must be refresh with the last option available
  50. if(html == 'ok') {
  51. field_dialog_{{ configuration.code }}_{{ field_element.id }}.dialog('close');
  52. // reload the form element
  53. jQuery.ajax({
  54. url: '{{ url('base_application_retrieve_form_element', {
  55. 'code': configuration.code,
  56. 'element_id': field_element.id,
  57. 'object_id': form.data.id,
  58. }) }}',
  59. data: jQuery('#field_widget_{{ configuration.code }}_{{ field_element.id}}').closest('form').serializeArray(),
  60. type: 'POST',
  61. success: function(html) {
  62. jQuery('#field_widget_{{ configuration.code }}_{{ field_element.id}}').html(html);
  63. }
  64. });
  65. return;
  66. }
  67. // otherwise, display form error
  68. field_dialog_{{ configuration.code }}_{{ field_element.id }}.html(html);
  69. BaseApplication.add_pretty_errors(field_dialog_{{ configuration.code }}_{{ field_element.id }});
  70. BaseApplication.add_collapsed_toggle(field_dialog_{{ configuration.code }}_{{ field_element.id }});
  71. // reattach the event
  72. jQuery('form', field_dialog_{{ configuration.code }}_{{ field_element.id }}).submit(field_dialog_form_action_{{ configuration.code }}_{{ field_element.id }});
  73. }
  74. });
  75. return false;
  76. }
  77. var field_dialog_{{ configuration.code }}_{{ field_element.id }} = false;
  78. // this function initialize the popup
  79. // this can be only done this way has popup can be cascaded
  80. function start_field_dialog_form_add_{{ configuration.code }}_{{ field_element.id }}(event) {
  81. event.preventDefault();
  82. // remove the html event a
  83. var a = jQuery(event.target).closest('a');
  84. a.attr('onclick', '');
  85. // initialize component
  86. field_dialog_{{ configuration.code }}_{{ field_element.id }} = jQuery("#field_dialog_{{ configuration.code }}_{{ field_element.id }}");
  87. // move the dialog as a child of the root element, nested form breaks html ...
  88. jQuery(document).append(field_dialog_{{ configuration.code }}_{{ field_element.id }});
  89. // add the jQuery event to the a element
  90. a.click(field_dialog_form_add_{{ configuration.code }}_{{ field_element.id }});
  91. // trigger the event
  92. a.trigger('click');
  93. return false;
  94. }
  95. BaseApplication.add_pretty_errors(field_dialog_{{ configuration.code }}_{{ field_element.id }});
  96. BaseApplication.add_collapsed_toggle(field_dialog_{{ configuration.code }}_{{ field_element.id }});
  97. </script>