Admin.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. jQuery(document).ready(function() {
  2. jQuery('html').removeClass('no-js');
  3. if (window.SONATA_CONFIG && window.SONATA_CONFIG.CONFIRM_EXIT) {
  4. jQuery('.sonata-ba-form form').confirmExit();
  5. }
  6. Admin.setup_select2(document);
  7. Admin.setup_xeditable(document);
  8. Admin.add_pretty_errors(document);
  9. Admin.add_filters(document);
  10. Admin.set_object_field_value(document);
  11. Admin.setup_collection_buttons(document);
  12. Admin.setup_per_page_switcher(document);
  13. Admin.setup_form_tabs_for_errors(document);
  14. Admin.setup_inline_form_errors(document);
  15. });
  16. jQuery(document).on('sonata-admin-append-form-element', function(e) {
  17. Admin.setup_select2(e.target);
  18. });
  19. var Admin = {
  20. setup_select2: function(subject) {
  21. if (window.SONATA_CONFIG && window.SONATA_CONFIG.USE_SELECT2 && window.Select2) {
  22. jQuery('select:not([data-sonata-select2="false"])', subject).each(function() {
  23. var select = $(this);
  24. var allowClearEnabled = false;
  25. if (select.find('option[value=""]').length) {
  26. allowClearEnabled = true;
  27. }
  28. if (select.attr('data-sonata-select2-allow-clear')==='true') {
  29. allowClearEnabled = true;
  30. } else if (select.attr('data-sonata-select2-allow-clear')==='false') {
  31. allowClearEnabled = false;
  32. }
  33. select.select2({
  34. width: 'resolve',
  35. minimumResultsForSearch: 10,
  36. allowClear: allowClearEnabled
  37. });
  38. var popover = select.data('popover');
  39. if (undefined !== popover) {
  40. select
  41. .select2('container')
  42. .popover(popover.options)
  43. ;
  44. }
  45. });
  46. }
  47. },
  48. setup_xeditable: function(subject) {
  49. jQuery('.x-editable', subject).editable({
  50. emptyclass: 'editable-empty btn btn-sm',
  51. emptytext: '<i class="glyphicon glyphicon-edit"></i>',
  52. success: function(response) {
  53. if('KO' === response.status) {
  54. return response.message;
  55. }
  56. var html = jQuery(response.content);
  57. Admin.setup_xeditable(html);
  58. jQuery(this)
  59. .closest('td')
  60. .replaceWith(html)
  61. ;
  62. }
  63. });
  64. },
  65. /**
  66. * render log message
  67. * @param mixed
  68. */
  69. log: function() {
  70. var msg = '[Sonata.Admin] ' + Array.prototype.join.call(arguments,', ');
  71. if (window.console && window.console.log) {
  72. window.console.log(msg);
  73. } else if (window.opera && window.opera.postError) {
  74. window.opera.postError(msg);
  75. }
  76. },
  77. /**
  78. * display related errors messages
  79. *
  80. * @param subject
  81. */
  82. add_pretty_errors: function(subject) {
  83. Admin.setup_select2(subject);
  84. jQuery('div.sonata-ba-field-error', subject).each(function(index, element) {
  85. var input = jQuery(':input', element);
  86. if (!input.length) {
  87. return;
  88. }
  89. var message = jQuery('div.sonata-ba-field-error-messages', element).html();
  90. jQuery('div.sonata-ba-field-error-messages', element).remove();
  91. if (!message || message.length == 0) {
  92. return;
  93. }
  94. var target = input,
  95. fieldShortDescription = input.closest('.field-container').find('.field-short-description'),
  96. select2 = input.closest('.select2-container')
  97. ;
  98. if (fieldShortDescription.length) {
  99. target = fieldShortDescription;
  100. } else if (select2.length) {
  101. target= select2;
  102. }
  103. target.popover({
  104. content: message,
  105. trigger: 'hover',
  106. html: true,
  107. placement: 'right',
  108. template: '<div class="popover"><div class="arrow"></div><div class="popover-inner"><div class="popover-content alert-error"><p></p></div></div></div>'
  109. });
  110. });
  111. },
  112. stopEvent: function(event) {
  113. // https://github.com/sonata-project/SonataAdminBundle/issues/151
  114. //if it is a standard browser use preventDefault otherwise it is IE then return false
  115. if(event.preventDefault) {
  116. event.preventDefault();
  117. } else {
  118. event.returnValue = false;
  119. }
  120. //if it is a standard browser get target otherwise it is IE then adapt syntax and get target
  121. if (typeof event.target != 'undefined') {
  122. targetElement = event.target;
  123. } else {
  124. targetElement = event.srcElement;
  125. }
  126. return targetElement;
  127. },
  128. add_filters: function(subject) {
  129. jQuery('div.filter_container .sonata-filter-option', subject).hide();
  130. jQuery('h4.filter_legend', subject).click(function(event) {
  131. jQuery('div.filter_container .sonata-filter-option').toggle();
  132. });
  133. },
  134. /**
  135. * Change object field value
  136. * @param subject
  137. */
  138. set_object_field_value: function(subject) {
  139. this.log(jQuery('a.sonata-ba-edit-inline', subject));
  140. jQuery('a.sonata-ba-edit-inline', subject).click(function(event) {
  141. Admin.stopEvent(event);
  142. var subject = jQuery(this);
  143. jQuery.ajax({
  144. url: subject.attr('href'),
  145. type: 'POST',
  146. success: function(json) {
  147. if(json.status === "OK") {
  148. var elm = jQuery(subject).parent();
  149. elm.children().remove();
  150. // fix issue with html comment ...
  151. elm.html(jQuery(json.content.replace(/<!--[\s\S]*?-->/g, "")).html());
  152. elm.effect("highlight", {'color' : '#57A957'}, 2000);
  153. Admin.set_object_field_value(elm);
  154. } else {
  155. jQuery(subject).parent().effect("highlight", {'color' : '#C43C35'}, 2000);
  156. }
  157. }
  158. });
  159. });
  160. },
  161. setup_collection_buttons: function(subject) {
  162. jQuery(subject).on('click', '.sonata-collection-add', function(event) {
  163. Admin.stopEvent(event);
  164. var container = jQuery(this).closest('[data-prototype]');
  165. var proto = container.attr('data-prototype');
  166. var protoName = container.attr('data-prototype-name') || '__name__';
  167. // Set field id
  168. var idRegexp = new RegExp(container.attr('id')+'_'+protoName,'g');
  169. proto = proto.replace(idRegexp, container.attr('id')+'_'+(container.children().length - 1));
  170. // Set field name
  171. var parts = container.attr('id').split('_');
  172. var nameRegexp = new RegExp(parts[parts.length-1]+'\\]\\['+protoName,'g');
  173. proto = proto.replace(nameRegexp, parts[parts.length-1]+']['+(container.children().length - 1));
  174. jQuery(proto)
  175. .insertBefore(jQuery(this).parent())
  176. .trigger('sonata-admin-append-form-element')
  177. ;
  178. jQuery(this).trigger('sonata-collection-item-added');
  179. });
  180. jQuery(subject).on('click', '.sonata-collection-delete', function(event) {
  181. Admin.stopEvent(event);
  182. jQuery(this).trigger('sonata-collection-item-deleted');
  183. jQuery(this).closest('.sonata-collection-row').remove();
  184. });
  185. },
  186. setup_per_page_switcher: function(subject) {
  187. jQuery('select.per-page').change(function(event) {
  188. jQuery('input[type=submit]').hide();
  189. window.top.location.href=this.options[this.selectedIndex].value;
  190. });
  191. },
  192. setup_form_tabs_for_errors: function(subject) {
  193. // Switch to first tab with server side validation errors on page load
  194. jQuery('form', subject).each(function() {
  195. Admin.show_form_first_tab_with_errors(jQuery(this), '.sonata-ba-field-error');
  196. });
  197. // Switch to first tab with HTML5 errors on form submit
  198. jQuery(subject)
  199. .on('click', 'form [type="submit"]', function() {
  200. Admin.show_form_first_tab_with_errors(jQuery(this).closest('form'), ':invalid');
  201. })
  202. .on('keypress', 'form [type="text"]', function(e) {
  203. if (13 === e.which) {
  204. Admin.show_form_first_tab_with_errors(jQuery(this), ':invalid');
  205. }
  206. })
  207. ;
  208. },
  209. show_form_first_tab_with_errors: function(form, errorSelector) {
  210. var tabs = form.find('.nav-tabs a'),
  211. firstTabWithErrors;
  212. tabs.each(function() {
  213. var id = jQuery(this).attr('href'),
  214. tab = jQuery(this),
  215. icon = tab.find('.has-errors');
  216. if (jQuery(id).find(errorSelector).length > 0) {
  217. // Only show first tab with errors
  218. if (!firstTabWithErrors) {
  219. tab.tab('show');
  220. firstTabWithErrors = tab;
  221. }
  222. icon.removeClass('hide');
  223. } else {
  224. icon.addClass('hide');
  225. }
  226. });
  227. },
  228. setup_inline_form_errors: function(subject) {
  229. var deleteCheckboxSelector = '.sonata-ba-field-inline-table [id$="_delete"][type="checkbox"]';
  230. jQuery(deleteCheckboxSelector, subject).each(function() {
  231. Admin.switch_inline_form_errors(jQuery(this));
  232. });
  233. $(subject).on('change', deleteCheckboxSelector, function() {
  234. Admin.switch_inline_form_errors(jQuery(this));
  235. });
  236. },
  237. /**
  238. * Disable inline form errors when the row is marked for deletion
  239. */
  240. switch_inline_form_errors: function(deleteCheckbox) {
  241. var row = deleteCheckbox.closest('.sonata-ba-field-inline-table'),
  242. errors = row.find('.sonata-ba-field-error-messages')
  243. ;
  244. if (deleteCheckbox.is(':checked')) {
  245. row
  246. .find('[required]')
  247. .removeAttr('required')
  248. .attr('data-required', 'required')
  249. ;
  250. errors.hide();
  251. } else {
  252. row
  253. .find('[data-required]')
  254. .attr('required', 'required')
  255. ;
  256. errors.show();
  257. }
  258. }
  259. };