33 lines
1.2 KiB
JavaScript
33 lines
1.2 KiB
JavaScript
jQuery( document ).ready( function( $ ) {
|
||
var frame;
|
||
// Quand on clique sur "Choisir une image"
|
||
$( '.ccgw-upload-image' ).on( 'click', function( e ) {
|
||
e.preventDefault();
|
||
// Si le frame existe déjà, on l’ouvre
|
||
if ( frame ) {
|
||
frame.open();
|
||
return;
|
||
}
|
||
// Créer le frame
|
||
frame = wp.media({
|
||
title: ccgw_data.title || 'Choisir une image de catégorie',
|
||
button: { text: ccgw_data.button || 'Sélectionner' },
|
||
multiple: false
|
||
});
|
||
// Quand on sélectionne une image
|
||
frame.on( 'select', function() {
|
||
var attachment = frame.state().get('selection').first().toJSON();
|
||
$( '#category-image-id' ).val( attachment.id );
|
||
$( '#category-image-wrapper' ).html( '<img src="' + attachment.sizes.thumbnail.url + '" style="max-width:100px;height:auto;" />' );
|
||
});
|
||
frame.open();
|
||
});
|
||
|
||
// Quand on clique sur "Supprimer l’image"
|
||
$( '.ccgw-remove-image' ).on( 'click', function( e ) {
|
||
e.preventDefault();
|
||
$( '#category-image-id' ).val('');
|
||
$( '#category-image-wrapper' ).html('');
|
||
});
|
||
});
|