edit_orm_many_association_script.html.twig 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 = sassociationadmin %}
  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 }} td.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. });
  86. }
  87. });
  88. };
  89. // handle the add link
  90. var field_dialog_form_add_{{ id }} = function(event) {
  91. initialize_popup_{{ id }}();
  92. event.preventDefault();
  93. event.stopPropagation();
  94. var a = jQuery(this);
  95. field_dialog_{{ id }}.html('');
  96. Admin.log('[{{ id }}|field_dialog_form_add] add link action');
  97. // retrieve the form element from the related admin generator
  98. jQuery.ajax({
  99. url: a.attr('href'),
  100. dataType: 'html',
  101. success: function(html) {
  102. Admin.log('[{{ id }}|field_dialog_form_add] ajax success', field_dialog_{{ id }});
  103. // populate the popup container
  104. field_dialog_{{ id }}.html(html);
  105. // capture the submit event to make an ajax call, ie : POST data to the
  106. // related create admin
  107. jQuery('a', field_dialog_{{ id }}).live('click', field_dialog_form_action_{{ id }});
  108. jQuery('form', field_dialog_{{ id }}).live('submit', field_dialog_form_action_{{ id }});
  109. // open the dialog in modal mode
  110. field_dialog_{{ id }}.dialog({
  111. height: 'auto',
  112. width: 650,
  113. modal: true,
  114. autoOpen: true,
  115. resizable: false,
  116. title: '{{ associationadmin.label|trans({}, associationadmin.translationdomain) }}',
  117. close: function(event, ui) {
  118. Admin.log('[{{ id }}|field_dialog_form_add] dialog closed - removing `live` events');
  119. // make sure we have a clean state
  120. jQuery('a', field_dialog_{{ id }}).die('click');
  121. jQuery('form', field_dialog_{{ id }}).die('submit');
  122. }
  123. });
  124. }
  125. });
  126. };
  127. // handle the post data
  128. var field_dialog_form_action_{{ id }} = function(event) {
  129. event.preventDefault();
  130. event.stopPropagation();
  131. Admin.log('[{{ id }}|field_dialog_form_action] action catch', this);
  132. initialize_popup_{{ id }}();
  133. var element = jQuery(this);
  134. if (this.nodeName == 'FORM') {
  135. var url = element.attr('action');
  136. var type = element.attr('method');
  137. } else if (this.nodeName == 'A') {
  138. var url = element.attr('href');
  139. var type = 'GET';
  140. } else {
  141. alert('unexpected element : @' + this.nodeName + '@');
  142. return;
  143. }
  144. if (element.hasClass('sonata-ba-action')) {
  145. Admin.log('[{{ id }}|field_dialog_form_action] reserved action stop catch all events');
  146. return;
  147. }
  148. var data = {
  149. _xml_http_request: true
  150. }
  151. var form = jQuery(this);
  152. Admin.log('[{{ id }}|field_dialog_form_action] execute ajax call');
  153. // the ajax post
  154. jQuery(form).ajaxSubmit({
  155. url: url,
  156. type: type,
  157. data: data,
  158. success: function(data) {
  159. Admin.log('[{{ id }}|field_dialog_form_action] ajax success');
  160. if (typeof data == 'string') {
  161. field_dialog_{{ id }}.html(data);
  162. return;
  163. };
  164. // if the crud action return ok, then the element has been added
  165. // so the widget container must be refresh with the last option available
  166. if (data.result == 'ok') {
  167. field_dialog_{{ id }}.dialog('close');
  168. {% if sonata_admin.field_description.options.edit == 'list' %}
  169. {#
  170. in this case we update the hidden input, and call the change event to
  171. retrieve the post information
  172. #}
  173. jQuery('#{{ id }}').val(data.objectId);
  174. jQuery('#{{ id }}').change();
  175. {% else %}
  176. // reload the form element
  177. jQuery('#field_widget_{{ id }}').closest('form').ajaxSubmit({
  178. url: '{{ url('sonata_admin_retrieve_form_element', {
  179. 'elementId': id,
  180. 'objectId': sonata_admin.admin.root.id(sonata_admin.admin.root.subject),
  181. 'uniqid': sonata_admin.admin.root.uniqid,
  182. 'code': sonata_admin.admin.root.code
  183. }) }}',
  184. data: {_xml_http_request: true },
  185. type: 'POST',
  186. success: function(html) {
  187. jQuery('#field_container_{{ id }}').replaceWith(html);
  188. }
  189. });
  190. {% endif %}
  191. return;
  192. }
  193. // otherwise, display form error
  194. field_dialog_{{ id }}.html(html);
  195. Admin.add_pretty_errors(field_dialog_{{ id }});
  196. // reattach the event
  197. jQuery('form', field_dialog_{{ id }}).submit(field_dialog_form_action_{{ id }});
  198. }
  199. });
  200. return false;
  201. }
  202. var field_dialog_{{ id }} = false;
  203. function initialize_popup_{{ id }}() {
  204. // initialize component
  205. if (!field_dialog_{{ id }}) {
  206. field_dialog_{{ id }} = jQuery("#field_dialog_{{ id }}");
  207. // move the dialog as a child of the root element, nested form breaks html ...
  208. jQuery(document.body).append(field_dialog_{{ id }});
  209. Admin.log('[{{ id }}|field_dialog] move dialog container as a document child');
  210. }
  211. }
  212. {#
  213. This code is used to defined the "add" popup
  214. #}
  215. // this function initialize the popup
  216. // this can be only done this way has popup can be cascaded
  217. function start_field_dialog_form_add_{{ id }}(link) {
  218. // remove the html event
  219. link.onclick = null;
  220. initialize_popup_{{ id }}();
  221. // add the jQuery event to the a element
  222. jQuery(link)
  223. .click(field_dialog_form_add_{{ id }})
  224. .trigger('click')
  225. ;
  226. return false;
  227. }
  228. Admin.add_pretty_errors(field_dialog_{{ id }});
  229. {% if sonata_admin.field_description.options.edit == 'list' %}
  230. {#
  231. This code is used to defined the "list" popup
  232. #}
  233. // this function initialize the popup
  234. // this can be only done this way has popup can be cascaded
  235. function start_field_dialog_form_list_{{ id }}(link) {
  236. link.onclick = null;
  237. initialize_popup_{{ id }}();
  238. // add the jQuery event to the a element
  239. jQuery(link)
  240. .click(field_dialog_form_list_{{ id }})
  241. .trigger('click')
  242. ;
  243. return false;
  244. }
  245. function remove_selected_element_{{ id }}(link) {
  246. link.onclick = null;
  247. jQuery(link)
  248. .click(field_remove_element_{{ id}})
  249. .trigger('click')
  250. ;
  251. return false;
  252. }
  253. function field_remove_element_{{ id }}(event) {
  254. event.preventDefault();
  255. if (jQuery('#{{ id }} option').get(0)) {
  256. jQuery('#{{ id }}').attr('selectedIndex', '-1').children("option:selected").attr("selected", false);
  257. }
  258. jQuery('#{{ id }}').val('');
  259. jQuery('#{{ id }}').trigger('change');
  260. return false;
  261. }
  262. {#
  263. attach onchange event on the input
  264. #}
  265. // update the label
  266. jQuery('#{{ id }}').live('change', function(event) {
  267. Admin.log('[{{ id }}] update the label');
  268. 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>");
  269. jQuery.ajax({
  270. type: 'GET',
  271. url: '{{ url('sonata_admin_short_object_information', {
  272. 'objectId': 'OBJECT_ID',
  273. 'uniqid': associationadmin.uniqid,
  274. 'code': associationadmin.code
  275. })}}'.replace('OBJECT_ID', jQuery(this).val()),
  276. success: function(html) {
  277. jQuery('#field_widget_{{ id }}').html(html);
  278. }
  279. });
  280. });
  281. {% endif %}
  282. </script>
  283. <!-- / edit many association -->
  284. {% endautoescape %}