edit_many_association_script.html.twig 11 KB

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