base.js 8.2 KB

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