forked from trepmal/meta-box-media-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia.js
executable file
·66 lines (43 loc) · 1.49 KB
/
media.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
jQuery( function ( $ ) {
var file_frame = [],
$button = $( '.meta-box-upload-button' ),
$removebutton = $( '.meta-box-upload-button-remove' );
$button.on( 'click', function ( event ) {
event.preventDefault();
var $this = $( this ),
id = $this.attr( 'id' );
// If the media frame already exists, reopen it.
if ( file_frame[ id ] ) {
file_frame[ id ].open();
return;
}
// Create the media frame.
file_frame[ id ] = wp.media.frames.file_frame = wp.media( {
title : $this.data( 'uploader_title' ),
button : {
text : $this.data( 'uploader_button_text' )
},
multiple : false // Set to true to allow multiple files to be selected
} );
// When an image is selected, run a callback.
file_frame[ id ].on( 'select', function() {
// We set multiple to false so only get one image from the uploader
var attachment = file_frame[ id ].state().get( 'selection' ).first().toJSON();
// set input
$( '#' + id + '-value' ).val( attachment.id );
// set preview
var img = '<img src="' + attachment.url + '" style="max-width:100%;" />';
$this.next( 'input' ).next( '.image-preview' ).html( img );
} );
// Finally, open the modal
file_frame[ id ].open();
} );
$removebutton.on( 'click', function( event ) {
event.preventDefault();
var $this = $( this ),
id = $this.prev( 'input' ).attr( 'id' );
$this.next( '.image-preview' ).html( '' );
// $this.next( 'br' ).next( 'img' ).remove();
$( '#' + id + '-value' ).val( 0 );
} );
} );