| 1 |
/** |
| 2 |
* Term featured-image picker for the plugin taxonomy term screens. |
| 3 |
* |
| 4 |
* Opens the native WordPress media modal, writes the chosen attachment id into |
| 5 |
* the hidden .mlsimport-term-image__id input, and shows a preview. "Remove" |
| 6 |
* clears the id and hides the preview. Works on both the Add and Edit term |
| 7 |
* screens (the Add form is cleared/rebuilt over AJAX, so bind by delegation). |
| 8 |
*/ |
| 9 |
( function ( $ ) { |
| 10 |
'use strict'; |
| 11 |
|
| 12 |
// Single reusable media-modal instance (recreated per open below). |
| 13 |
var frame = null; |
| 14 |
|
| 15 |
// "Set featured image" click — delegated so it works on the AJAX-rebuilt Add form too. |
| 16 |
$( document ).on( 'click', '.mlsimport-term-image__set', function ( e ) { |
| 17 |
// Don't let the button submit / follow a link. |
| 18 |
e.preventDefault(); |
| 19 |
|
| 20 |
// The wrapper holding this row's hidden id input, preview and remove button. |
| 21 |
var $wrap = $( this ).closest( '.mlsimport-term-image__wrap' ); |
| 22 |
|
| 23 |
// Open a fresh media frame limited to a single image selection. |
| 24 |
frame = wp.media( { |
| 25 |
title: 'Select featured image', |
| 26 |
button: { text: 'Use this image' }, |
| 27 |
library: { type: 'image' }, |
| 28 |
multiple: false |
| 29 |
} ); |
| 30 |
|
| 31 |
// When the user confirms a selection, copy the attachment into this row. |
| 32 |
frame.on( 'select', function () { |
| 33 |
// First (only) selected attachment as a plain object. |
| 34 |
var attachment = frame.state().get( 'selection' ).first().toJSON(); |
| 35 |
// Prefer the medium-size URL for the preview; fall back to the full URL. |
| 36 |
var src = attachment.sizes && attachment.sizes.medium ? attachment.sizes.medium.url : attachment.url; |
| 37 |
|
| 38 |
// Store the chosen attachment id in the hidden input that gets saved. |
| 39 |
$wrap.find( '.mlsimport-term-image__id' ).val( attachment.id ); |
| 40 |
// Show the preview image. |
| 41 |
$wrap.find( '.mlsimport-term-image__preview' ).attr( 'src', src ).css( 'display', 'block' ); |
| 42 |
// Reveal the remove button now that an image is set. |
| 43 |
$wrap.find( '.mlsimport-term-image__remove' ).css( 'display', 'inline-block' ); |
| 44 |
} ); |
| 45 |
|
| 46 |
// Show the media modal. |
| 47 |
frame.open(); |
| 48 |
} ); |
| 49 |
|
| 50 |
// "Remove image" click — clears the stored id and hides the preview. |
| 51 |
$( document ).on( 'click', '.mlsimport-term-image__remove', function ( e ) { |
| 52 |
// Don't let the button submit / follow a link. |
| 53 |
e.preventDefault(); |
| 54 |
// The wrapper for this row. |
| 55 |
var $wrap = $( this ).closest( '.mlsimport-term-image__wrap' ); |
| 56 |
// Clear the stored attachment id. |
| 57 |
$wrap.find( '.mlsimport-term-image__id' ).val( '' ); |
| 58 |
// Blank and hide the preview image. |
| 59 |
$wrap.find( '.mlsimport-term-image__preview' ).attr( 'src', '' ).css( 'display', 'none' ); |
| 60 |
// Hide the remove button itself. |
| 61 |
$( this ).css( 'display', 'none' ); |
| 62 |
} ); |
| 63 |
} )( jQuery ); |
| 64 |
|