119c10bd2aee55ac2e2e56d242ef926c032720a6
[autoinstalls/wordpress.git] / wp-admin / js / media.js
1 /* global ajaxurl, attachMediaBoxL10n */
2
3 var findPosts;
4 ( function( $ ){
5         findPosts = {
6                 open: function( af_name, af_val ) {
7                         var overlay = $( '.ui-find-overlay' );
8
9                         if ( overlay.length === 0 ) {
10                                 $( 'body' ).append( '<div class="ui-find-overlay"></div>' );
11                                 findPosts.overlay();
12                         }
13
14                         overlay.show();
15
16                         if ( af_name && af_val ) {
17                                 $( '#affected' ).attr( 'name', af_name ).val( af_val );
18                         }
19
20                         $( '#find-posts' ).show();
21
22                         $('#find-posts-input').focus().keyup( function( event ){
23                                 if ( event.which == 27 ) {
24                                         findPosts.close();
25                                 } // close on Escape
26                         });
27
28                         // Pull some results up by default
29                         findPosts.send();
30
31                         return false;
32                 },
33
34                 close: function() {
35                         $('#find-posts-response').html('');
36                         $('#find-posts').hide();
37                         $( '.ui-find-overlay' ).hide();
38                 },
39
40                 overlay: function() {
41                         $( '.ui-find-overlay' ).on( 'click', function () {
42                                 findPosts.close();
43                         });
44                 },
45
46                 send: function() {
47                         var post = {
48                                         ps: $( '#find-posts-input' ).val(),
49                                         action: 'find_posts',
50                                         _ajax_nonce: $('#_ajax_nonce').val()
51                                 },
52                                 spinner = $( '.find-box-search .spinner' );
53
54                         spinner.show();
55
56                         $.ajax( ajaxurl, {
57                                 type: 'POST',
58                                 data: post,
59                                 dataType: 'json'
60                         }).always( function() {
61                                 spinner.hide();
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 );
70                         });
71                 }
72         };
73
74         $( document ).ready( function() {
75                 // Open up a manage media frame into the grid.
76                 wp.media && wp.media({
77                         frame: 'manage',
78                         container: $('#wpbody-content > .wrap')
79                 }).open();
80
81                 $( '#find-posts-submit' ).click( function( event ) {
82                         if ( ! $( '#find-posts-response input[type="radio"]:checked' ).length )
83                                 event.preventDefault();
84                 });
85                 $( '#find-posts .find-box-search :input' ).keypress( function( event ) {
86                         if ( 13 == event.which ) {
87                                 findPosts.send();
88                                 return false;
89                         }
90                 });
91                 $( '#find-posts-search' ).click( findPosts.send );
92                 $( '#find-posts-close' ).click( findPosts.close );
93                 $( '#doaction, #doaction2' ).click( function( event ) {
94                         $( 'select[name^="action"]' ).each( function() {
95                                 if ( $(this).val() === 'attach' ) {
96                                         event.preventDefault();
97                                         findPosts.open();
98                                 }
99                         });
100                 });
101
102                 // Enable whole row to be clicked
103                 $( '.find-box-inside' ).on( 'click', 'tr', function() {
104                         $( this ).find( '.found-radio input' ).prop( 'checked', true );
105                 });
106         });
107 })( jQuery );