base.js 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. jQuery(document).ready(function() {
  2. Admin.add_pretty_errors(document);
  3. Admin.add_collapsed_toggle();
  4. });
  5. var Admin = {
  6. /**
  7. * render log message
  8. * @param mixed
  9. */
  10. log: function() {
  11. var msg = '[Sonata.Admin] ' + Array.prototype.join.call(arguments,', ');
  12. if (window.console && window.console.log) {
  13. window.console.log(msg);
  14. }
  15. else if (window.opera && window.opera.postError) {
  16. window.opera.postError(msg);
  17. }
  18. },
  19. /**
  20. * display related errors messages
  21. *
  22. * @param subject
  23. */
  24. add_pretty_errors: function(subject) {
  25. jQuery('div.sonata-ba-field-error', subject).each(function(index, element) {
  26. var input = jQuery('input, textarea', element);
  27. var message = jQuery('div.sonata-ba-field-error-messages', element).html();
  28. jQuery('div.sonata-ba-field-error-messages', element).html('');
  29. if (!message) {
  30. message = '';
  31. }
  32. if (message.length == 0) {
  33. return;
  34. }
  35. input.qtip({
  36. content: message,
  37. show: 'focusin',
  38. hide: 'focusout',
  39. position: {
  40. corner: {
  41. target: 'rightMiddle',
  42. tooltip: 'leftMiddle'
  43. }
  44. },
  45. style: {
  46. name: 'red',
  47. border: {
  48. radius: 2
  49. },
  50. tip: 'leftMiddle'
  51. }
  52. })
  53. });
  54. },
  55. /**
  56. * Add the collapsed toggle option to the admin
  57. *
  58. * @param subject
  59. */
  60. add_collapsed_toggle: function(subject) {
  61. jQuery('fieldset legend a.sonata-ba-collapsed', subject).live('click', function(event) {
  62. event.preventDefault();
  63. var fieldset = jQuery(this).closest('fieldset');
  64. jQuery('div.sonata-ba-collapsed-fields', fieldset).toggle();
  65. fieldset.toggleClass('sonata-ba-collapsed-fields-close');
  66. }).click();
  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. }