custom-background.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* global ajaxurl */
  2. /**
  3. * @summary Registers all events for customizing the background.
  4. *
  5. * @since 3.0.0
  6. *
  7. * @requires jQuery
  8. */
  9. (function($) {
  10. $(document).ready(function() {
  11. var frame,
  12. bgImage = $( '#custom-background-image' );
  13. /**
  14. * @summary Instantiates the WordPress color picker and binds the change and clear events.
  15. *
  16. * @since 3.5.0
  17. *
  18. * @returns {void}
  19. */
  20. $('#background-color').wpColorPicker({
  21. change: function( event, ui ) {
  22. bgImage.css('background-color', ui.color.toString());
  23. },
  24. clear: function() {
  25. bgImage.css('background-color', '');
  26. }
  27. });
  28. /**
  29. * @summary Alters the background size CSS property whenever the background size input has changed.
  30. *
  31. * @since 4.7.0
  32. *
  33. * @returns {void}
  34. */
  35. $( 'select[name="background-size"]' ).change( function() {
  36. bgImage.css( 'background-size', $( this ).val() );
  37. });
  38. /**
  39. * @summary Alters the background position CSS property whenever the background position input has changed.
  40. *
  41. * @since 4.7.0
  42. *
  43. * @returns {void}
  44. */
  45. $( 'input[name="background-position"]' ).change( function() {
  46. bgImage.css( 'background-position', $( this ).val() );
  47. });
  48. /**
  49. * @summary Alters the background repeat CSS property whenever the background repeat input has changed.
  50. *
  51. * @since 3.0.0
  52. *
  53. * @returns {void}
  54. */
  55. $( 'input[name="background-repeat"]' ).change( function() {
  56. bgImage.css( 'background-repeat', $( this ).is( ':checked' ) ? 'repeat' : 'no-repeat' );
  57. });
  58. /**
  59. * @summary Alters the background attachment CSS property whenever the background attachment input has changed.
  60. *
  61. * @since 4.7.0
  62. *
  63. * @returns {void}
  64. */
  65. $( 'input[name="background-attachment"]' ).change( function() {
  66. bgImage.css( 'background-attachment', $( this ).is( ':checked' ) ? 'scroll' : 'fixed' );
  67. });
  68. /**
  69. * @summary Binds the event for opening the WP Media dialog.
  70. *
  71. * @since 3.5.0
  72. *
  73. * @returns {void}
  74. */
  75. $('#choose-from-library-link').click( function( event ) {
  76. var $el = $(this);
  77. event.preventDefault();
  78. // If the media frame already exists, reopen it.
  79. if ( frame ) {
  80. frame.open();
  81. return;
  82. }
  83. // Create the media frame.
  84. frame = wp.media.frames.customBackground = wp.media({
  85. // Set the title of the modal.
  86. title: $el.data('choose'),
  87. // Tell the modal to show only images.
  88. library: {
  89. type: 'image'
  90. },
  91. // Customize the submit button.
  92. button: {
  93. // Set the text of the button.
  94. text: $el.data('update'),
  95. /*
  96. * Tell the button not to close the modal, since we're
  97. * going to refresh the page when the image is selected.
  98. */
  99. close: false
  100. }
  101. });
  102. /**
  103. * @summary When an image is selected, run a callback.
  104. *
  105. * @since 3.5.0
  106. *
  107. * @returns {void}
  108. */
  109. frame.on( 'select', function() {
  110. // Grab the selected attachment.
  111. var attachment = frame.state().get('selection').first();
  112. // Run an AJAX request to set the background image.
  113. $.post( ajaxurl, {
  114. action: 'set-background-image',
  115. attachment_id: attachment.id,
  116. size: 'full'
  117. }).done( function() {
  118. // When the request completes, reload the window.
  119. window.location.reload();
  120. });
  121. });
  122. // Finally, open the modal.
  123. frame.open();
  124. });
  125. });
  126. })(jQuery);