[ 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, _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( wp.i18n.__( 'An error has occurred. Please reload the page and try again.' ) );
 128                  }
 129  
 130                  $( '#find-posts-response' ).html( x.data );
 131              }).fail( function() {
 132                  $( '#find-posts-response' ).text( wp.i18n.__( 'An error has occurred. Please reload the page and try again.' ) );
 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              var frame = window.wp.media({
 151                  frame: 'manage',
 152                  container: $mediaGridWrap,
 153                  library: settings.queryVars
 154              }).open();
 155  
 156              // Fire a global ready event.
 157              $mediaGridWrap.trigger( 'wp-media-grid-ready', frame );
 158          }
 159  
 160          // Prevents form submission if no post has been selected.
 161          $( '#find-posts-submit' ).click( function( event ) {
 162              if ( ! $( '#find-posts-response input[type="radio"]:checked' ).length )
 163                  event.preventDefault();
 164          });
 165  
 166          // Submits the search query when hitting the enter key in the search input.
 167          $( '#find-posts .find-box-search :input' ).keypress( function( event ) {
 168              if ( 13 == event.which ) {
 169                  findPosts.send();
 170                  return false;
 171              }
 172          });
 173  
 174          // Binds the click event to the search button.
 175          $( '#find-posts-search' ).click( findPosts.send );
 176  
 177          // Binds the close dialog click event.
 178          $( '#find-posts-close' ).click( findPosts.close );
 179  
 180          // Binds the bulk action events to the submit buttons.
 181          $( '#doaction, #doaction2' ).click( function( event ) {
 182  
 183              /*
 184               * Retrieves all select elements for bulk actions that have a name starting with `action`
 185               * and handle its action based on its value.
 186               */
 187              $( 'select[name^="action"]' ).each( function() {
 188                  var optionValue = $( this ).val();
 189  
 190                  if ( 'attach' === optionValue ) {
 191                      event.preventDefault();
 192                      findPosts.open();
 193                  } else if ( 'delete' === optionValue ) {
 194                      if ( ! showNotice.warn() ) {
 195                          event.preventDefault();
 196                      }
 197                  }
 198              });
 199          });
 200  
 201          /**
 202           * Enables clicking on the entire table row.
 203           *
 204           * @return {void}
 205           */
 206          $( '.find-box-inside' ).on( 'click', 'tr', function() {
 207              $( this ).find( '.found-radio input' ).prop( 'checked', true );
 208          });
 209      });
 210  })( jQuery );


Generated: Sun Nov 29 01:00:04 2020 Cross-referenced by PHPXref 0.7.1