wp/wp-admin/js/media.js
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
       
     1 /* global ajaxurl, attachMediaBoxL10n, _wpMediaGridSettings */
     1 
     2 
     2 var findPosts;
     3 var findPosts;
     3 (function($){
     4 ( function( $ ){
     4 	findPosts = {
     5 	findPosts = {
     5 		open : function(af_name, af_val) {
     6 		open: function( af_name, af_val ) {
     6 			var st = document.documentElement.scrollTop || $(document).scrollTop(),
     7 			var overlay = $( '.ui-find-overlay' );
     7 				overlay = $( '.ui-find-overlay' );
       
     8 
     8 
     9 			if ( overlay.length == 0 ) {
     9 			if ( overlay.length === 0 ) {
    10 				$( 'body' ).append( '<div class="ui-find-overlay"></div>' );
    10 				$( 'body' ).append( '<div class="ui-find-overlay"></div>' );
    11 				findPosts.overlay();
    11 				findPosts.overlay();
    12 			}
    12 			}
    13 
    13 
    14 			overlay.show();
    14 			overlay.show();
    15 
    15 
    16 			if ( af_name && af_val ) {
    16 			if ( af_name && af_val ) {
    17 				$('#affected').attr('name', af_name).val(af_val);
    17 				$( '#affected' ).attr( 'name', af_name ).val( af_val );
    18 			}
    18 			}
    19 			$('#find-posts').show().draggable({
       
    20 				handle: '#find-posts-head'
       
    21 			}).css({'top':st + 50 + 'px','left':'50%','marginLeft':'-328px'});
       
    22 
    19 
    23 			$('#find-posts-input').focus().keyup(function(e){
    20 			$( '#find-posts' ).show();
    24 				if (e.which == 27) { findPosts.close(); } // close on Escape
    21 
       
    22 			$('#find-posts-input').focus().keyup( function( event ){
       
    23 				if ( event.which == 27 ) {
       
    24 					findPosts.close();
       
    25 				} // close on Escape
    25 			});
    26 			});
    26 
    27 
    27 			// Pull some results up by default
    28 			// Pull some results up by default
    28 			findPosts.send();
    29 			findPosts.send();
    29 
    30 
    30 			return false;
    31 			return false;
    31 		},
    32 		},
    32 
    33 
    33 		close : function() {
    34 		close: function() {
    34 			$('#find-posts-response').html('');
    35 			$('#find-posts-response').empty();
    35 			$('#find-posts').draggable('destroy').hide();
    36 			$('#find-posts').hide();
    36 			$( '.ui-find-overlay' ).hide();
    37 			$( '.ui-find-overlay' ).hide();
    37 		},
    38 		},
    38 
    39 
    39 		overlay : function() {
    40 		overlay: function() {
    40 			$( '.ui-find-overlay' ).css(
    41 			$( '.ui-find-overlay' ).on( 'click', function () {
    41 				{ 'z-index': '999', 'width': $( document ).width() + 'px', 'height': $( document ).height() + 'px' }
       
    42 			).on('click', function () {
       
    43 				findPosts.close();
    42 				findPosts.close();
    44 			});
    43 			});
    45 		},
    44 		},
    46 
    45 
    47 		send : function() {
    46 		send: function() {
    48 			var post = {
    47 			var post = {
    49 					ps: $('#find-posts-input').val(),
    48 					ps: $( '#find-posts-input' ).val(),
    50 					action: 'find_posts',
    49 					action: 'find_posts',
    51 					_ajax_nonce: $('#_ajax_nonce').val()
    50 					_ajax_nonce: $('#_ajax_nonce').val()
    52 				},
    51 				},
    53 				spinner = $( '.find-box-search .spinner' );
    52 				spinner = $( '.find-box-search .spinner' );
    54 
    53 
    55 			spinner.show();
    54 			spinner.addClass( 'is-active' );
    56 
    55 
    57 			$.ajax({
    56 			$.ajax( ajaxurl, {
    58 				type : 'POST',
    57 				type: 'POST',
    59 				url : ajaxurl,
    58 				data: post,
    60 				data : post,
    59 				dataType: 'json'
    61 				success : function(x) { findPosts.show(x); spinner.hide(); },
    60 			}).always( function() {
    62 				error : function(r) { findPosts.error(r); spinner.hide(); }
    61 				spinner.removeClass( 'is-active' );
       
    62 			}).done( function( x ) {
       
    63 				if ( ! x.success ) {
       
    64 					$( '#find-posts-response' ).text( attachMediaBoxL10n.error );
       
    65 				}
       
    66 
       
    67 				$( '#find-posts-response' ).html( x.data );
       
    68 			}).fail( function() {
       
    69 				$( '#find-posts-response' ).text( attachMediaBoxL10n.error );
    63 			});
    70 			});
    64 		},
       
    65 
       
    66 		show : function(x) {
       
    67 
       
    68 			if ( typeof(x) == 'string' ) {
       
    69 				this.error({'responseText': x});
       
    70 				return;
       
    71 			}
       
    72 
       
    73 			var r = wpAjax.parseAjaxResponse(x);
       
    74 
       
    75 			if ( r.errors ) {
       
    76 				this.error({'responseText': wpAjax.broken});
       
    77 			}
       
    78 			r = r.responses[0];
       
    79 			$('#find-posts-response').html(r.data);
       
    80 
       
    81 			// Enable whole row to be clicked
       
    82 			$( '.found-posts td' ).on( 'click', function () {
       
    83 				$( this ).parent().find( '.found-radio input' ).prop( 'checked', true );
       
    84 			});
       
    85 		},
       
    86 
       
    87 		error : function(r) {
       
    88 			var er = r.statusText;
       
    89 
       
    90 			if ( r.responseText ) {
       
    91 				er = r.responseText.replace( /<.[^<>]*?>/g, '' );
       
    92 			}
       
    93 			if ( er ) {
       
    94 				$('#find-posts-response').html(er);
       
    95 			}
       
    96 		}
    71 		}
    97 	};
    72 	};
    98 
    73 
    99 	$(document).ready(function() {
    74 	$( document ).ready( function() {
   100 		$('#find-posts-submit').click(function(e) {
    75 		var settings, $mediaGridWrap = $( '#wp-media-grid' );
   101 			if ( '' == $('#find-posts-response').html() )
    76 
   102 				e.preventDefault();
    77 		// Open up a manage media frame into the grid.
       
    78 		if ( $mediaGridWrap.length && window.wp && window.wp.media ) {
       
    79 			settings = _wpMediaGridSettings;
       
    80 
       
    81 			window.wp.media({
       
    82 				frame: 'manage',
       
    83 				container: $mediaGridWrap,
       
    84 				library: settings.queryVars
       
    85 			}).open();
       
    86 		}
       
    87 
       
    88 		$( '#find-posts-submit' ).click( function( event ) {
       
    89 			if ( ! $( '#find-posts-response input[type="radio"]:checked' ).length )
       
    90 				event.preventDefault();
   103 		});
    91 		});
   104 		$( '#find-posts .find-box-search :input' ).keypress( function( event ) {
    92 		$( '#find-posts .find-box-search :input' ).keypress( function( event ) {
   105 			if ( 13 == event.which ) {
    93 			if ( 13 == event.which ) {
   106 				findPosts.send();
    94 				findPosts.send();
   107 				return false;
    95 				return false;
   108 			}
    96 			}
   109 		} );
    97 		});
   110 		$( '#find-posts-search' ).click( findPosts.send );
    98 		$( '#find-posts-search' ).click( findPosts.send );
   111 		$( '#find-posts-close' ).click( findPosts.close );
    99 		$( '#find-posts-close' ).click( findPosts.close );
   112 		$('#doaction, #doaction2').click(function(e){
   100 		$( '#doaction, #doaction2' ).click( function( event ) {
   113 			$('select[name^="action"]').each(function(){
   101 			$( 'select[name^="action"]' ).each( function() {
   114 				if ( $(this).val() == 'attach' ) {
   102 				if ( $(this).val() === 'attach' ) {
   115 					e.preventDefault();
   103 					event.preventDefault();
   116 					findPosts.open();
   104 					findPosts.open();
   117 				}
   105 				}
   118 			});
   106 			});
   119 		});
   107 		});
       
   108 
       
   109 		// Enable whole row to be clicked
       
   110 		$( '.find-box-inside' ).on( 'click', 'tr', function() {
       
   111 			$( this ).find( '.found-radio input' ).prop( 'checked', true );
       
   112 		});
   120 	});
   113 	});
   121 	$(window).resize(function() {
   114 })( jQuery );
   122 		findPosts.overlay();
       
   123 	});
       
   124 })(jQuery);