edit_orm_many_association_script.html.twig 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. {% set associationadmin = sonata_admin.field_description.associationadmin %}
  12. <!-- edit many association -->
  13. <script type="text/javascript">
  14. {#
  15. handle link click in a list :
  16. - if the parent has an objectId defined then the related input get updated
  17. - if the parent has NO object then an ajax request is made to refresh the popup
  18. #}
  19. var field_dialog_form_list_link_{{ id }} = function(event) {
  20. initialize_popup_{{ id }}();
  21. event.preventDefault();
  22. event.stopPropagation();
  23. Admin.log('[{{ id }}|field_dialog_form_list_link] handle link click in a list');
  24. var element = jQuery(this).parents('#field_dialog_{{ id }} .sonata-ba-list-field');
  25. // the user does click on a row column
  26. if (element.length == 0) {
  27. // make a recursive call (ie: reset the filter)
  28. jQuery.ajax({
  29. type: 'GET',
  30. url: jQuery(this).attr('href'),
  31. success: function(html) {
  32. field_dialog_{{ id }}.html(html);
  33. }
  34. });
  35. return;
  36. }
  37. jQuery('#{{ id }}').val(element.attr('objectId'));
  38. jQuery('#{{ id }}').trigger('change');
  39. field_dialog_{{ id }}.dialog('close');
  40. }
  41. // handle the add link
  42. var field_dialog_form_list_{{ id }} = function(event) {
  43. initialize_popup_{{ id }}();
  44. event.preventDefault();
  45. event.stopPropagation();
  46. Admin.log('[{{ id }}|field_dialog_form_list] open the list modal');
  47. var a = jQuery(this);
  48. field_dialog_{{ id }}.html('');
  49. // retrieve the form element from the related admin generator
  50. jQuery.ajax({
  51. url: a.attr('href'),
  52. success: function(html) {
  53. Admin.log('[{{ id }}|field_dialog_form_list] retrieving the list content');
  54. // populate the popup container
  55. field_dialog_{{ id }}.html(html);
  56. Admin.add_filters(field_dialog_{{ id }});
  57. // capture the submit event to make an ajax call, ie : POST data to the
  58. // related create admin
  59. jQuery('a', field_dialog_{{ id }}).live('click', field_dialog_form_list_link_{{ id }});
  60. jQuery('form', field_dialog_{{ id }}).live('submit', function(event) {
  61. event.preventDefault();
  62. var form = jQuery(this);
  63. jQuery(form).ajaxSubmit({
  64. type: form.attr('method'),
  65. url: form.attr('action'),
  66. dataType: 'html',
  67. data: {_xml_http_request: true},
  68. success: function(html) {
  69. field_dialog_{{ id }}.html(html);
  70. }
  71. });
  72. });
  73. // open the dialog in modal mode
  74. field_dialog_{{ id }}.dialog({
  75. height: 'auto',
  76. width: 980,
  77. modal: true,
  78. resizable: false,
  79. title: '{{ associationadmin.label|trans({}, associationadmin.translationdomain) }}',
  80. close: function(event, ui) {
  81. // make sure we have a clean state
  82. jQuery('a', field_dialog_{{ id }}).die('click');
  83. jQuery('form', field_dialog_{{ id }}).die('submit');
  84. },
  85. zIndex: 9998,
  86. });
  87. }
  88. });
  89. };
  90. // handle the add link
  91. var field_dialog_form_add_{{ id }} = function(event) {
  92. initialize_popup_{{ id }}();
  93. event.preventDefault();
  94. event.stopPropagation();
  95. var a = jQuery(this);
  96. field_dialog_{{ id }}.html('');
  97. Admin.log('[{{ id }}|field_dialog_form_add] add link action');
  98. // retrieve the form element from the related admin generator
  99. jQuery.ajax({
  100. url: a.attr('href'),
  101. dataType: 'html',
  102. success: function(html) {
  103. Admin.log('[{{ id }}|field_dialog_form_add] ajax success', field_dialog_{{ id }});
  104. // populate the popup container
  105. field_dialog_{{ id }}.html(html);
  106. // capture the submit event to make an ajax call, ie : POST data to the
  107. // related create admin
  108. jQuery('a', field_dialog_{{ id }}).live('click', field_dialog_form_action_{{ id }});
  109. jQuery('form', field_dialog_{{ id }}).live('submit', field_dialog_form_action_{{ id }});
  110. // open the dialog in modal mode
  111. field_dialog_{{ id }}.dialog({
  112. height: 'auto',
  113. width: 650,
  114. modal: true,
  115. autoOpen: true,
  116. resizable: false,
  117. title: '{{ associationadmin.label|trans({}, associationadmin.translationdomain) }}',
  118. close: function(event, ui) {
  119. Admin.log('[{{ id }}|field_dialog_form_add] dialog closed - removing `live` events');
  120. // make sure we have a clean state
  121. jQuery('a', field_dialog_{{ id }}).die('click');
  122. jQuery('form', field_dialog_{{ id }}).die('submit');
  123. },
  124. zIndex: 9998,
  125. });
  126. }
  127. });
  128. };
  129. // handle the post data
  130. var field_dialog_form_action_{{ id }} = function(event) {
  131. event.preventDefault();
  132. event.stopPropagation();
  133. Admin.log('[{{ id }}|field_dialog_form_action] action catch', this);
  134. initialize_popup_{{ id }}();
  135. var element = jQuery(this);
  136. if (this.nodeName == 'FORM') {
  137. var url = element.attr('action');
  138. var type = element.attr('method');
  139. } else if (this.nodeName == 'A') {
  140. var url = element.attr('href');
  141. var type = 'GET';
  142. } else {
  143. alert('unexpected element : @' + this.nodeName + '@');
  144. return;
  145. }
  146. if (element.hasClass('sonata-ba-action')) {
  147. Admin.log('[{{ id }}|field_dialog_form_action] reserved action stop catch all events');
  148. return;
  149. }
  150. var data = {
  151. _xml_http_request: true
  152. }
  153. var form = jQuery(this);
  154. Admin.log('[{{ id }}|field_dialog_form_action] execute ajax call');
  155. // the ajax post
  156. jQuery(form).ajaxSubmit({
  157. url: url,
  158. type: type,
  159. data: data,
  160. success: function(data) {
  161. Admin.log('[{{ id }}|field_dialog_form_action] ajax success');
  162. if (typeof data == 'string') {
  163. field_dialog_{{ id }}.html(data);
  164. return;
  165. };
  166. // if the crud action return ok, then the element has been added
  167. // so the widget container must be refresh with the last option available
  168. if (data.result == 'ok') {
  169. field_dialog_{{ id }}.dialog('close');
  170. {% if sonata_admin.field_description.options.edit == 'list' %}
  171. {#
  172. in this case we update the hidden input, and call the change event to
  173. retrieve the post information
  174. #}
  175. jQuery('#{{ id }}').val(data.objectId);
  176. jQuery('#{{ id }}').change();
  177. {% else %}
  178. // reload the form element
  179. jQuery('#field_widget_{{ id }}').closest('form').ajaxSubmit({
  180. url: '{{ url('sonata_admin_retrieve_form_element', {
  181. 'elementId': id,
  182. 'objectId': sonata_admin.admin.root.id(sonata_admin.admin.root.subject),
  183. 'uniqid': sonata_admin.admin.root.uniqid,
  184. 'code': sonata_admin.admin.root.code
  185. }) }}',
  186. data: {_xml_http_request: true },
  187. type: 'POST',
  188. success: function(html) {
  189. jQuery('#field_container_{{ id }}').replaceWith(html);
  190. }
  191. });
  192. {% endif %}
  193. return;
  194. }
  195. // otherwise, display form error
  196. field_dialog_{{ id }}.html(html);
  197. Admin.add_pretty_errors(field_dialog_{{ id }});
  198. // reattach the event
  199. jQuery('form', field_dialog_{{ id }}).submit(field_dialog_form_action_{{ id }});
  200. }
  201. });
  202. return false;
  203. }
  204. var field_dialog_{{ id }} = false;
  205. function initialize_popup_{{ id }}() {
  206. // initialize component
  207. if (!field_dialog_{{ id }}) {
  208. field_dialog_{{ id }} = jQuery("#field_dialog_{{ id }}");
  209. // move the dialog as a child of the root element, nested form breaks html ...
  210. jQuery(document.body).append(field_dialog_{{ id }});
  211. Admin.log('[{{ id }}|field_dialog] move dialog container as a document child');
  212. }
  213. }
  214. {#
  215. This code is used to defined the "add" popup
  216. #}
  217. // this function initialize the popup
  218. // this can be only done this way has popup can be cascaded
  219. function start_field_dialog_form_add_{{ id }}(link) {
  220. // remove the html event
  221. link.onclick = null;
  222. initialize_popup_{{ id }}();
  223. // add the jQuery event to the a element
  224. jQuery(link)
  225. .click(field_dialog_form_add_{{ id }})
  226. .trigger('click')
  227. ;
  228. return false;
  229. }
  230. Admin.add_pretty_errors(field_dialog_{{ id }});
  231. {% if sonata_admin.field_description.options.edit == 'list' %}
  232. {#
  233. This code is used to defined the "list" popup
  234. #}
  235. // this function initialize the popup
  236. // this can be only done this way has popup can be cascaded
  237. function start_field_dialog_form_list_{{ id }}(link) {
  238. link.onclick = null;
  239. initialize_popup_{{ id }}();
  240. // add the jQuery event to the a element
  241. jQuery(link)
  242. .click(field_dialog_form_list_{{ id }})
  243. .trigger('click')
  244. ;
  245. return false;
  246. }
  247. function remove_selected_element_{{ id }}(link) {
  248. link.onclick = null;
  249. jQuery(link)
  250. .click(field_remove_element_{{ id}})
  251. .trigger('click')
  252. ;
  253. return false;
  254. }
  255. function field_remove_element_{{ id }}(event) {
  256. event.preventDefault();
  257. if (jQuery('#{{ id }} option').get(0)) {
  258. jQuery('#{{ id }}').attr('selectedIndex', '-1').children("option:selected").attr("selected", false);
  259. }
  260. jQuery('#{{ id }}').val('');
  261. jQuery('#{{ id }}').trigger('change');
  262. return false;
  263. }
  264. {#
  265. attach onchange event on the input
  266. #}
  267. // update the label
  268. jQuery('#{{ id }}').live('change', function(event) {
  269. Admin.log('[{{ id }}] update the label');
  270. jQuery('#field_widget_{{ id }}').html("<span><img src=\"{{ asset('bundles/sonataadmin/ajax-loader.gif') }}\" style=\"vertical-align: middle; margin-right: 10px\"/>{{ 'loading_information'|trans([], 'SonataAdminBundle') }}</span>");
  271. jQuery.ajax({
  272. type: 'GET',
  273. url: '{{ url('sonata_admin_short_object_information', {
  274. 'objectId': 'OBJECT_ID',
  275. 'uniqid': associationadmin.uniqid,
  276. 'code': associationadmin.code
  277. })}}'.replace('OBJECT_ID', jQuery(this).val()),
  278. success: function(html) {
  279. jQuery('#field_widget_{{ id }}').html(html);
  280. }
  281. });
  282. });
  283. {% endif %}
  284. </script>
  285. <!-- / edit many association -->
  286. {% endautoescape %}