[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/js/ -> custom-background.js (source)

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


Generated: Tue Mar 31 01:00:04 2020 Cross-referenced by PHPXref 0.7.1