home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress2 / wp-admin / js / media.js < prev    next >
Encoding:
JavaScript  |  2017-06-15  |  5.1 KB  |  205 lines

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