| 1 |
import { __wpupg } from 'Shared/Translations'; |
| 2 |
|
| 3 |
const Media = { |
| 4 |
selectImage( callback ) { |
| 5 |
this.select( 'image', callback ); |
| 6 |
}, |
| 7 |
selectVideo( callback ) { |
| 8 |
this.select( 'video', callback ); |
| 9 |
}, |
| 10 |
select( type, callback ) { |
| 11 |
let media_arguments = { |
| 12 |
title: __wpupg( 'Select Media' ), |
| 13 |
button: { |
| 14 |
text: __wpupg( 'Select' ), |
| 15 |
}, |
| 16 |
multiple: false, |
| 17 |
}; |
| 18 |
|
| 19 |
// Check what media type we're getting. |
| 20 |
if ( 'video' === type ) { |
| 21 |
media_arguments.frame = 'video'; |
| 22 |
media_arguments.state = 'video-details'; |
| 23 |
} else { |
| 24 |
// Default to image. |
| 25 |
media_arguments.library = { |
| 26 |
type: 'image', |
| 27 |
}; |
| 28 |
} |
| 29 |
|
| 30 |
// Create a new media frame (don't reuse because we have multiple different inputs) |
| 31 |
let frame = wp.media(media_arguments); |
| 32 |
|
| 33 |
// Handle image selection |
| 34 |
frame.on('select', function() { |
| 35 |
var attachment = frame.state().get('selection').first().toJSON(); |
| 36 |
callback( attachment ); |
| 37 |
}); |
| 38 |
|
| 39 |
// Handle video selection |
| 40 |
frame.on('update', function() { |
| 41 |
let attachment = frame.state().media.attachment; |
| 42 |
|
| 43 |
if ( attachment ) { |
| 44 |
callback( attachment ); |
| 45 |
} |
| 46 |
}); |
| 47 |
|
| 48 |
// Finally, open the modal on click |
| 49 |
frame.open(); |
| 50 |
} |
| 51 |
} |
| 52 |
export default Media; |