base.js 7.0 KB

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