You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

media.js 2.9 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* global ajaxurl, attachMediaBoxL10n, _wpMediaGridSettings, showNotice */
  2. var findPosts;
  3. ( function( $ ){
  4. findPosts = {
  5. open: function( af_name, af_val ) {
  6. var overlay = $( '.ui-find-overlay' );
  7. if ( overlay.length === 0 ) {
  8. $( 'body' ).append( '<div class="ui-find-overlay"></div>' );
  9. findPosts.overlay();
  10. }
  11. overlay.show();
  12. if ( af_name && af_val ) {
  13. $( '#affected' ).attr( 'name', af_name ).val( af_val );
  14. }
  15. $( '#find-posts' ).show();
  16. $('#find-posts-input').focus().keyup( function( event ){
  17. if ( event.which == 27 ) {
  18. findPosts.close();
  19. } // close on Escape
  20. });
  21. // Pull some results up by default
  22. findPosts.send();
  23. return false;
  24. },
  25. close: function() {
  26. $('#find-posts-response').empty();
  27. $('#find-posts').hide();
  28. $( '.ui-find-overlay' ).hide();
  29. },
  30. overlay: function() {
  31. $( '.ui-find-overlay' ).on( 'click', function () {
  32. findPosts.close();
  33. });
  34. },
  35. send: function() {
  36. var post = {
  37. ps: $( '#find-posts-input' ).val(),
  38. action: 'find_posts',
  39. _ajax_nonce: $('#_ajax_nonce').val()
  40. },
  41. spinner = $( '.find-box-search .spinner' );
  42. spinner.addClass( 'is-active' );
  43. $.ajax( ajaxurl, {
  44. type: 'POST',
  45. data: post,
  46. dataType: 'json'
  47. }).always( function() {
  48. spinner.removeClass( 'is-active' );
  49. }).done( function( x ) {
  50. if ( ! x.success ) {
  51. $( '#find-posts-response' ).text( attachMediaBoxL10n.error );
  52. }
  53. $( '#find-posts-response' ).html( x.data );
  54. }).fail( function() {
  55. $( '#find-posts-response' ).text( attachMediaBoxL10n.error );
  56. });
  57. }
  58. };
  59. $( document ).ready( function() {
  60. var settings, $mediaGridWrap = $( '#wp-media-grid' );
  61. // Open up a manage media frame into the grid.
  62. if ( $mediaGridWrap.length && window.wp && window.wp.media ) {
  63. settings = _wpMediaGridSettings;
  64. window.wp.media({
  65. frame: 'manage',
  66. container: $mediaGridWrap,
  67. library: settings.queryVars
  68. }).open();
  69. }
  70. $( '#find-posts-submit' ).click( function( event ) {
  71. if ( ! $( '#find-posts-response input[type="radio"]:checked' ).length )
  72. event.preventDefault();
  73. });
  74. $( '#find-posts .find-box-search :input' ).keypress( function( event ) {
  75. if ( 13 == event.which ) {
  76. findPosts.send();
  77. return false;
  78. }
  79. });
  80. $( '#find-posts-search' ).click( findPosts.send );
  81. $( '#find-posts-close' ).click( findPosts.close );
  82. $( '#doaction, #doaction2' ).click( function( event ) {
  83. $( 'select[name^="action"]' ).each( function() {
  84. var optionValue = $( this ).val();
  85. if ( 'attach' === optionValue ) {
  86. event.preventDefault();
  87. findPosts.open();
  88. } else if ( 'delete' === optionValue ) {
  89. if ( ! showNotice.warn() ) {
  90. event.preventDefault();
  91. }
  92. }
  93. });
  94. });
  95. // Enable whole row to be clicked
  96. $( '.find-box-inside' ).on( 'click', 'tr', function() {
  97. $( this ).find( '.found-radio input' ).prop( 'checked', true );
  98. });
  99. });
  100. })( jQuery );