edit_many_association_script.html.twig 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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('#field_dialog_{{ admin.uniqid }}_{{ field_element.id }} 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 }}').trigger('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. jQuery('a', field_dialog_{{ admin.uniqid }}_{{ field_element.id }}).live('click', field_dialog_form_list_link_{{ admin.uniqid }}_{{ field_element.id }});
  53. jQuery('form', field_dialog_{{ admin.uniqid }}_{{ 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.uniqid }}_{{ field_element.id }}.html(html);
  62. }
  63. });
  64. });
  65. // open the dialog in modal mode
  66. field_dialog_{{ admin.uniqid }}_{{ field_element.id }}.dialog({
  67. height: 'auto',
  68. width: 980,
  69. modal: true,
  70. resizable: false,
  71. title: '{{ field_description.associationadmin.label }}',
  72. close: function(event, ui) {
  73. // make sure we have a clean state
  74. jQuery('a', field_dialog_{{ admin.uniqid }}_{{ field_element.id }}).die('click');
  75. jQuery('form', field_dialog_{{ admin.uniqid }}_{{ field_element.id }}).die('submit');
  76. }
  77. });
  78. }
  79. });
  80. };
  81. // handle the add link
  82. var field_dialog_form_add_{{ admin.uniqid }}_{{ field_element.id }} = function(event) {
  83. initialize_popup_{{ admin.uniqid }}_{{ field_element.id }}();
  84. event.preventDefault();
  85. var a = jQuery(this);
  86. field_dialog_{{ admin.uniqid }}_{{ field_element.id }}.html('');
  87. // retrieve the form element from the related admin generator
  88. jQuery.ajax({
  89. url: a.attr('href'),
  90. success: function(html) {
  91. // populate the popup container
  92. field_dialog_{{ admin.uniqid }}_{{ field_element.id }}.html(html);
  93. // capture the submit event to make an ajax call, ie : POST data to the
  94. // related create admin
  95. jQuery('a', field_dialog_{{ admin.uniqid }}_{{ field_element.id }}).live('click', field_dialog_form_action_{{ admin.uniqid }}_{{ field_element.id }});
  96. jQuery('form', field_dialog_{{ admin.uniqid }}_{{ field_element.id }}).live('submit', field_dialog_form_action_{{ admin.uniqid }}_{{ field_element.id }});
  97. // open the dialog in modal mode
  98. field_dialog_{{ admin.uniqid }}_{{ field_element.id }}.dialog({
  99. height: 'auto',
  100. width: 650,
  101. modal: true,
  102. resizable: false,
  103. title: '{{ field_description.associationadmin.label }}',
  104. close: function(event, ui) {
  105. // make sure we have a clean state
  106. jQuery('a', field_dialog_{{ admin.uniqid }}_{{ field_element.id }}).die('click');
  107. jQuery('form', field_dialog_{{ admin.uniqid }}_{{ field_element.id }}).die('submit');
  108. }
  109. });
  110. }
  111. });
  112. };
  113. // handle the post data
  114. var field_dialog_form_action_{{ admin.uniqid }}_{{ field_element.id }} = function(event) {
  115. event.preventDefault();
  116. initialize_popup_{{ admin.uniqid }}_{{ field_element.id }}();
  117. var element = jQuery(this);
  118. if (this.nodeName == 'FORM') {
  119. var url = element.attr('action');
  120. var type = element.attr('method');
  121. var data = element.serializeArray();
  122. } else if (this.nodeName == 'A') {
  123. var url = element.attr('href');
  124. var type = 'GET';
  125. var data = {};
  126. } else {
  127. alert('unexpected element : @' + this.nodeName + '@');
  128. return;
  129. }
  130. var form = jQuery(this);
  131. // the ajax post
  132. jQuery.ajax({
  133. url: url,
  134. type: type,
  135. data: data,
  136. success: function(data) {
  137. if(typeof data == 'string') {
  138. field_dialog_{{ admin.uniqid }}_{{ field_element.id }}.html(data);
  139. return;
  140. };
  141. // if the crud action return ok, then the element has been added
  142. // so the widget container must be refresh with the last option available
  143. if (data.result == 'ok') {
  144. field_dialog_{{ admin.uniqid }}_{{ field_element.id }}.dialog('close');
  145. {% if field_description.options.edit == 'list' %}
  146. {#
  147. in this case we update the hidden input, and call the change event to
  148. retrieve the post information
  149. #}
  150. console.log(jQuery('#{{ field_element.id }}'));
  151. jQuery('#{{ field_element.id }}').val(data.objectId);
  152. jQuery('#{{ field_element.id }}').change();
  153. {% else %}
  154. // reload the form element
  155. jQuery.ajax({
  156. url: '{{ url('sonata_base_application_retrieve_form_element', {
  157. 'elementId': field_element.id,
  158. 'objectId': admin.root.subject.id,
  159. 'uniqid': admin.root.uniqid,
  160. 'code': admin.root.code
  161. }) }}',
  162. data: jQuery('#field_widget_{{ admin.uniqid }}_{{ field_element.id }}').closest('form').serializeArray(),
  163. type: 'POST',
  164. success: function(html) {
  165. jQuery('#field_widget_{{ admin.uniqid }}_{{ field_element.id }}').html(html);
  166. }
  167. });
  168. {% endif %}
  169. return;
  170. }
  171. // otherwise, display form error
  172. field_dialog_{{ admin.uniqid }}_{{ field_element.id }}.html(html);
  173. BaseApplication.add_pretty_errors(field_dialog_{{ admin.uniqid }}_{{ field_element.id }});
  174. // reattach the event
  175. jQuery('form', field_dialog_{{ admin.uniqid }}_{{ field_element.id }}).submit(field_dialog_form_action_{{ admin.uniqid }}_{{ field_element.id }});
  176. }
  177. });
  178. return false;
  179. }
  180. var field_dialog_{{ admin.uniqid }}_{{ field_element.id }} = false;
  181. function initialize_popup_{{ admin.uniqid }}_{{ field_element.id }}() {
  182. // initialize component
  183. if(!field_dialog_{{ admin.uniqid }}_{{ field_element.id }}) {
  184. field_dialog_{{ admin.uniqid }}_{{ field_element.id }} = jQuery("#field_dialog_{{ admin.uniqid }}_{{ field_element.id }}");
  185. // move the dialog as a child of the root element, nested form breaks html ...
  186. jQuery(document).append(field_dialog_{{ admin.uniqid }}_{{ field_element.id }});
  187. }
  188. }
  189. {#
  190. This code is used to defined the "add" popup
  191. #}
  192. // this function initialize the popup
  193. // this can be only done this way has popup can be cascaded
  194. function start_field_dialog_form_add_{{ admin.uniqid }}_{{ field_element.id }}(event) {
  195. event.preventDefault();
  196. // remove the html event a
  197. var a = jQuery(event.target).closest('a');
  198. a.attr('onclick', '');
  199. initialize_popup_{{ admin.uniqid }}_{{ field_element.id }}();
  200. // add the jQuery event to the a element
  201. a.click(field_dialog_form_add_{{ admin.uniqid }}_{{ field_element.id }});
  202. // trigger the event
  203. a.trigger('click');
  204. return false;
  205. }
  206. BaseApplication.add_pretty_errors(field_dialog_{{ admin.uniqid }}_{{ field_element.id }});
  207. {% if field_description.options.edit == 'list' %}
  208. {#
  209. This code is used to defined the "list" popup
  210. #}
  211. // this function initialize the popup
  212. // this can be only done this way has popup can be cascaded
  213. function start_field_dialog_form_list_{{ admin.uniqid }}_{{ field_element.id }}(event) {
  214. event.preventDefault();
  215. // remove the html event a
  216. var a = jQuery(event.target).closest('a');
  217. a.attr('onclick', '');
  218. initialize_popup_{{ admin.uniqid }}_{{ field_element.id }}();
  219. // add the jQuery event to the a element
  220. a.click(field_dialog_form_list_{{ admin.uniqid }}_{{ field_element.id }});
  221. // trigger the event
  222. a.trigger('click');
  223. return false;
  224. }
  225. {#
  226. attach onchange event on the input
  227. #}
  228. // update the
  229. jQuery('#{{ field_element.id }}').live('change', function(event) {
  230. jQuery.ajax({
  231. type: 'GET',
  232. url: '{{ url('sonata_base_application_short_object_information', {
  233. 'objectId': 'OBJECT_ID',
  234. 'uniqid': field_description.associationadmin.uniqid,
  235. 'code': field_description.associationadmin.code
  236. })}}'.replace('OBJECT_ID', jQuery(this).val()),
  237. success: function(html) {
  238. jQuery('#field_widget_{{ admin.uniqid }}_{{ field_element.id }}').html(html);
  239. }
  240. });
  241. });
  242. {% endif %}
  243. <!-- / edit many association -->
  244. </script>
  245. {% endautoescape %}