wp/wp-admin/js/media.js
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     1
/* global ajaxurl, attachMediaBoxL10n, _wpMediaGridSettings */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
var findPosts;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     4
( function( $ ){
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
	findPosts = {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     6
		open: function( af_name, af_val ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     7
			var overlay = $( '.ui-find-overlay' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     9
			if ( overlay.length === 0 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
				$( 'body' ).append( '<div class="ui-find-overlay"></div>' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
				findPosts.overlay();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
			overlay.show();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
			if ( af_name && af_val ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    17
				$( '#affected' ).attr( 'name', af_name ).val( af_val );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
			}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    19
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    20
			$( '#find-posts' ).show();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    22
			$('#find-posts-input').focus().keyup( function( event ){
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    23
				if ( event.which == 27 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    24
					findPosts.close();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    25
				} // close on Escape
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
			// Pull some results up by default
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
			findPosts.send();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    34
		close: function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    35
			$('#find-posts-response').empty();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    36
			$('#find-posts').hide();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
			$( '.ui-find-overlay' ).hide();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    40
		overlay: function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    41
			$( '.ui-find-overlay' ).on( 'click', function () {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
				findPosts.close();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    46
		send: function() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
			var post = {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    48
					ps: $( '#find-posts-input' ).val(),
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
					action: 'find_posts',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
					_ajax_nonce: $('#_ajax_nonce').val()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
				},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
				spinner = $( '.find-box-search .spinner' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    54
			spinner.addClass( 'is-active' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    56
			$.ajax( ajaxurl, {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    57
				type: 'POST',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    58
				data: post,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    59
				dataType: 'json'
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    60
			}).always( function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    61
				spinner.removeClass( 'is-active' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    62
			}).done( function( x ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    63
				if ( ! x.success ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    64
					$( '#find-posts-response' ).text( attachMediaBoxL10n.error );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    65
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    67
				$( '#find-posts-response' ).html( x.data );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    68
			}).fail( function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    69
				$( '#find-posts-response' ).text( attachMediaBoxL10n.error );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
	};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    74
	$( document ).ready( function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    75
		var settings, $mediaGridWrap = $( '#wp-media-grid' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    76
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    77
		// Open up a manage media frame into the grid.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    78
		if ( $mediaGridWrap.length && window.wp && window.wp.media ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    79
			settings = _wpMediaGridSettings;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    80
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    81
			window.wp.media({
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    82
				frame: 'manage',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    83
				container: $mediaGridWrap,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    84
				library: settings.queryVars
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    85
			}).open();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    86
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    87
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    88
		$( '#find-posts-submit' ).click( function( event ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    89
			if ( ! $( '#find-posts-response input[type="radio"]:checked' ).length )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    90
				event.preventDefault();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
		});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
		$( '#find-posts .find-box-search :input' ).keypress( function( event ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
			if ( 13 == event.which ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
				findPosts.send();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
			}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    97
		});
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
		$( '#find-posts-search' ).click( findPosts.send );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
		$( '#find-posts-close' ).click( findPosts.close );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   100
		$( '#doaction, #doaction2' ).click( function( event ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   101
			$( 'select[name^="action"]' ).each( function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   102
				if ( $(this).val() === 'attach' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   103
					event.preventDefault();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
					findPosts.open();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
		});
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   108
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   109
		// Enable whole row to be clicked
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   110
		$( '.find-box-inside' ).on( 'click', 'tr', function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   111
			$( this ).find( '.found-radio input' ).prop( 'checked', true );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   112
		});
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
	});
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   114
})( jQuery );