custom-background.js 3.4 KB

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