PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/bundles/Sonata/DoctrineORMAdminBundle/Resources/views/CRUD/edit_orm_many_association_script.html.twig

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