edit_many_association_script.twig.html 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. {#
  12. handle link click in a list :
  13. - if the parent has an object_id defined then the related input get updated
  14. - if the parent has NO object then an ajax request is made to refresh the popup
  15. #}
  16. var field_dialog_form_list_link_{{ admin.code }}_{{ field_element.id }} = function(event) {
  17. event.preventDefault();
  18. var element = jQuery(this).parents('td.sonata-ba-list-field');
  19. // the user does does click on a row column
  20. if(element.length == 0) {
  21. // make a recurive call (ie: reset the filter)
  22. jQuery.ajax({
  23. type: 'GET',
  24. url: jQuery(this).attr('href'),
  25. success: function(html) {
  26. field_dialog_{{ admin.code }}_{{ field_element.id }}.html(html);
  27. }
  28. });
  29. return;
  30. }
  31. jQuery('#{{ field_element.id}}').val(element.attr('object_id'));
  32. jQuery('#{{ field_element.id}}').change();
  33. field_dialog_{{ admin.code }}_{{ field_element.id }}.dialog('close');
  34. }
  35. // handle the add link
  36. var field_dialog_form_list_{{ admin.code }}_{{ field_element.id }} = function(event) {
  37. event.preventDefault();
  38. var a = jQuery(this);
  39. field_dialog_{{ admin.code }}_{{ field_element.id }}.html('');
  40. // retrieve the form element from the related admin generator
  41. jQuery.ajax({
  42. url: a.attr('href'),
  43. success: function(html) {
  44. // populate the popup container
  45. field_dialog_{{ admin.code }}_{{ field_element.id }}.html(html);
  46. // capture the submit event to make an ajax call, ie : POST data to the
  47. // related create admin
  48. // make sure we have a clean state
  49. jQuery('a', field_dialog_{{ admin.code }}_{{ field_element.id }}).die('click');
  50. jQuery('form', field_dialog_{{ admin.code }}_{{ field_element.id }}).die('submit');
  51. // add live event
  52. jQuery('a', field_dialog_{{ admin.code }}_{{ field_element.id }}).live('click', field_dialog_form_list_link_{{ admin.code }}_{{ field_element.id }});
  53. jQuery('form', field_dialog_{{ admin.code }}_{{ field_element.id }}).live('submit', function(event) {
  54. event.preventDefault();
  55. var form = jQuery(this);
  56. jQuery.ajax({
  57. type: form.attr('method'),
  58. url: form.attr('action'),
  59. data: form.serializeArray(),
  60. success: function(html) {
  61. field_dialog_{{ admin.code }}_{{ field_element.id }}.html(html);
  62. }
  63. });
  64. });
  65. // open the dialog in modal mode
  66. field_dialog_{{ admin.code }}_{{ field_element.id }}.dialog({
  67. height: 'auto',
  68. width: 980,
  69. modal: true,
  70. resizable: false,
  71. title: '{{ field_description.associationadmin.label }}'
  72. });
  73. }
  74. });
  75. };
  76. // handle the add link
  77. var field_dialog_form_add_{{ admin.code }}_{{ field_element.id }} = function(event) {
  78. event.preventDefault();
  79. var a = jQuery(this);
  80. field_dialog_{{ admin.code }}_{{ field_element.id }}.html('');
  81. // retrieve the form element from the related admin generator
  82. jQuery.ajax({
  83. url: a.attr('href'),
  84. success: function(html) {
  85. // populate the popup container
  86. field_dialog_{{ admin.code }}_{{ field_element.id }}.html(html);
  87. // capture the submit event to make an ajax call, ie : POST data to the
  88. // related create admin
  89. jQuery('form', field_dialog_{{ admin.code }}_{{ field_element.id }}).submit(field_dialog_form_action_{{ admin.code }}_{{ field_element.id }});
  90. // open the dialog in modal mode
  91. field_dialog_{{ admin.code }}_{{ field_element.id }}.dialog({
  92. height: 'auto',
  93. width: 650,
  94. modal: true,
  95. resizable: false,
  96. title: '{{ field_description.associationadmin.label }}'
  97. });
  98. }
  99. });
  100. };
  101. // handle the post data
  102. var field_dialog_form_action_{{ admin.code }}_{{ field_element.id }} = function(event) {
  103. event.preventDefault();
  104. var form = jQuery(this);
  105. // the ajax post
  106. jQuery.ajax({
  107. url: form.attr('action'),
  108. type: "POST",
  109. data: form.serializeArray(),
  110. success: function(html) {
  111. // if the crud action return ok, then the element has been added
  112. // so the widget container must be refresh with the last option available
  113. if (html == 'ok') {
  114. field_dialog_{{ admin.code }}_{{ field_element.id }}.dialog('close');
  115. // reload the form element
  116. jQuery.ajax({
  117. url: '{{ url('sonata_base_application_retrieve_form_element', {
  118. 'code': admin.root.code,
  119. 'element_id': field_element.id,
  120. 'object_id': admin.root.subject.id,
  121. }) }}',
  122. data: jQuery('#field_widget_{{ admin.code }}_{{ field_element.id}}').closest('form').serializeArray(),
  123. type: 'POST',
  124. success: function(html) {
  125. jQuery('#field_widget_{{ admin.code }}_{{ field_element.id}}').html(html);
  126. }
  127. });
  128. return;
  129. }
  130. // otherwise, display form error
  131. field_dialog_{{ admin.code }}_{{ field_element.id }}.html(html);
  132. BaseApplication.add_pretty_errors(field_dialog_{{ admin.code }}_{{ field_element.id }});
  133. // reattach the event
  134. jQuery('form', field_dialog_{{ admin.code }}_{{ field_element.id }}).submit(field_dialog_form_action_{{ admin.code }}_{{ field_element.id }});
  135. }
  136. });
  137. return false;
  138. }
  139. var field_dialog_{{ admin.code }}_{{ field_element.id }} = false;
  140. function initialize_popup_{{ admin.code }}_{{ field_element.id }}() {
  141. // initialize component
  142. if(!field_dialog_{{ admin.code }}_{{ field_element.id }}) {
  143. field_dialog_{{ admin.code }}_{{ field_element.id }} = jQuery("#field_dialog_{{ admin.code }}_{{ field_element.id }}");
  144. // move the dialog as a child of the root element, nested form breaks html ...
  145. jQuery(document).append(field_dialog_{{ admin.code }}_{{ field_element.id }});
  146. }
  147. }
  148. {#
  149. This code is used to defined the "add" popup
  150. #}
  151. // this function initialize the popup
  152. // this can be only done this way has popup can be cascaded
  153. function start_field_dialog_form_add_{{ admin.code }}_{{ field_element.id }}(event) {
  154. event.preventDefault();
  155. // remove the html event a
  156. var a = jQuery(event.target).closest('a');
  157. a.attr('onclick', '');
  158. initialize_popup_{{ admin.code }}_{{ field_element.id }}();
  159. // add the jQuery event to the a element
  160. a.click(field_dialog_form_add_{{ admin.code }}_{{ field_element.id }});
  161. // trigger the event
  162. a.trigger('click');
  163. return false;
  164. }
  165. BaseApplication.add_pretty_errors(field_dialog_{{ admin.code }}_{{ field_element.id }});
  166. {% if field_description.options.edit == 'list' %}
  167. {#
  168. This code is used to defined the "list" popup
  169. #}
  170. // this function initialize the popup
  171. // this can be only done this way has popup can be cascaded
  172. function start_field_dialog_form_list_{{ admin.code }}_{{ field_element.id }}(event) {
  173. event.preventDefault();
  174. // remove the html event a
  175. var a = jQuery(event.target).closest('a');
  176. a.attr('onclick', '');
  177. initialize_popup_{{ admin.code }}_{{ field_element.id }}();
  178. // add the jQuery event to the a element
  179. a.click(field_dialog_form_list_{{ admin.code }}_{{ field_element.id }});
  180. // trigger the event
  181. a.trigger('click');
  182. return false;
  183. }
  184. {#
  185. attach onchange event on the input
  186. #}
  187. // update the
  188. jQuery('#{{ field_element.id}}').change(function(event) {
  189. jQuery.ajax({
  190. type: 'GET',
  191. url: '{{ url('sonata_base_application_short_object_information', {
  192. 'code': field_description.associationadmin.code,
  193. 'object_id': 'OBJECT_ID'
  194. })}}'.replace('OBJECT_ID', jQuery(this).val()),
  195. success: function(html) {
  196. jQuery('#field_widget_{{ admin.code }}_{{ field_element.id}}').html(html);
  197. }
  198. });
  199. });
  200. {% endif %}
  201. </script>