[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/js/ -> media.js (source)

   1  /**
   2   * Creates a dialog containing posts that can have a particular media attached
   3   * to it.
   4   *
   5   * @since 2.7.0
   6   * @output wp-admin/js/media.js
   7   *
   8   * @namespace findPosts
   9   *
  10   * @requires jQuery
  11   */
  12  
  13  /* global ajaxurl, attachMediaBoxL10n, _wpMediaGridSettings, showNotice, findPosts */
  14  
  15  ( function( $ ){
  16      window.findPosts = {
  17          /**
  18           * Opens a dialog to attach media to a post.
  19           *
  20           * Adds an overlay prior to retrieving a list of posts to attach the media to.
  21           *
  22           * @since 2.7.0
  23           *
  24           * @memberOf findPosts
  25           *
  26           * @param {string} af_name The name of the affected element.
  27           * @param {string} af_val The value of the affected post element.
  28           *
  29           * @return {boolean} Always returns false.
  30           */
  31          open: function( af_name, af_val ) {
  32              var overlay = $( '.ui-find-overlay' );
  33  
  34              if ( overlay.length === 0 ) {
  35                  $( 'body' ).append( '<div class="ui-find-overlay"></div>' );
  36                  findPosts.overlay();
  37              }
  38  
  39              overlay.show();
  40  
  41              if ( af_name && af_val ) {
  42                  // #affected is a hidden input field in the dialog that keeps track of which media should be attached.
  43                  $( '#affected' ).attr( 'name', af_name ).val( af_val );
  44              }
  45  
  46              $( '#find-posts' ).show();
  47  
  48              // Close the dialog when the escape key is pressed.
  49              $('#find-posts-input').focus().keyup( function( event ){
  50                  if ( event.which == 27 ) {
  51                      findPosts.close();
  52                  }
  53              });
  54  
  55              // Retrieves a list of applicable posts for media attachment and shows them.
  56              findPosts.send();
  57  
  58              return false;
  59          },
  60  
  61          /**
  62           * Clears the found posts lists before hiding the attach media dialog.
  63           *
  64           * @since 2.7.0
  65           *
  66           * @memberOf findPosts
  67           *
  68           * @return {void}
  69           */
  70          close: function() {
  71              $('#find-posts-response').empty();
  72              $('#find-posts').hide();
  73              $( '.ui-find-overlay' ).hide();
  74          },
  75  
  76          /**
  77           * Binds a click event listener to the overlay which closes the attach media
  78           * dialog.
  79           *
  80           * @since 3.5.0
  81           *
  82           * @memberOf findPosts
  83           *
  84           * @return {void}
  85           */
  86          overlay: function() {
  87              $( '.ui-find-overlay' ).on( 'click', function () {
  88                  findPosts.close();
  89              });
  90          },
  91  
  92          /**
  93           * Retrieves and displays posts based on the search term.
  94           *
  95           * Sends a post request to the admin_ajax.php, requesting posts based on the
  96           * search term provided by the user. Defaults to all posts if no search term is
  97           * provided.
  98           *
  99           * @since 2.7.0
 100           *
 101           * @memberOf findPosts
 102           *
 103           * @return {void}
 104           */
 105          send: function() {
 106              var post = {
 107                      ps: $( '#find-posts-input' ).val(),
 108                      action: 'find_posts',
 109                      _ajax_nonce: $('#_ajax_nonce').val()
 110                  },
 111                  spinner = $( '.find-box-search .spinner' );
 112  
 113              spinner.addClass( 'is-active' );
 114  
 115              /**
 116               * Send a POST request to admin_ajax.php, hide the spinner and replace the list
 117               * of posts with the response data. If an error occurs, display it.
 118               */
 119              $.ajax( ajaxurl, {
 120                  type: 'POST',
 121                  data: post,
 122                  dataType: 'json'
 123              }).always( function() {
 124                  spinner.removeClass( 'is-active' );
 125              }).done( function( x ) {
 126                  if ( ! x.success ) {
 127                      $( '#find-posts-response' ).text( attachMediaBoxL10n.error );
 128                  }
 129  
 130                  $( '#find-posts-response' ).html( x.data );
 131              }).fail( function() {
 132                  $( '#find-posts-response' ).text( attachMediaBoxL10n.error );
 133              });
 134          }
 135      };
 136  
 137      /**
 138       * Initializes the file once the DOM is fully loaded and attaches events to the
 139       * various form elements.
 140       *
 141       * @return {void}
 142       */
 143      $( document ).ready( function() {
 144          var settings, $mediaGridWrap = $( '#wp-media-grid' );
 145  
 146          // Opens a manage media frame into the grid.
 147          if ( $mediaGridWrap.length && window.wp && window.wp.media ) {
 148              settings = _wpMediaGridSettings;
 149  
 150              window.wp.media({
 151                  frame: 'manage',
 152                  container: $mediaGridWrap,
 153                  library: settings.queryVars
 154              }).open();
 155          }
 156  
 157          // Prevents form submission if no post has been selected.
 158          $( '#find-posts-submit' ).click( function( event ) {
 159              if ( ! $( '#find-posts-response input[type="radio"]:checked' ).length )
 160                  event.preventDefault();
 161          });
 162  
 163          // Submits the search query when hitting the enter key in the search input.
 164          $( '#find-posts .find-box-search :input' ).keypress( function( event ) {
 165              if ( 13 == event.which ) {
 166                  findPosts.send();
 167                  return false;
 168              }
 169          });
 170  
 171          // Binds the click event to the search button.
 172          $( '#find-posts-search' ).click( findPosts.send );
 173  
 174          // Binds the close dialog click event.
 175          $( '#find-posts-close' ).click( findPosts.close );
 176  
 177          // Binds the bulk action events to the submit buttons.
 178          $( '#doaction, #doaction2' ).click( function( event ) {
 179  
 180              /*
 181               * Retrieves all select elements for bulk actions that have a name starting with `action`
 182               * and handle its action based on its value.
 183               */
 184              $( 'select[name^="action"]' ).each( function() {
 185                  var optionValue = $( this ).val();
 186  
 187                  if ( 'attach' === optionValue ) {
 188                      event.preventDefault();
 189                      findPosts.open();
 190                  } else if ( 'delete' === optionValue ) {
 191                      if ( ! showNotice.warn() ) {
 192                          event.preventDefault();
 193                      }
 194                  }
 195              });
 196          });
 197  
 198          /**
 199           * Enables clicking on the entire table row.
 200           *
 201           * @return {void}
 202           */
 203          $( '.find-box-inside' ).on( 'click', 'tr', function() {
 204              $( this ).find( '.found-radio input' ).prop( 'checked', true );
 205          });
 206      });
 207  })( jQuery );


Generated: Wed Jan 29 01:00:05 2020 Cross-referenced by PHPXref 0.7.1