PluginProbe
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings / 7.1.1
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings v7.1.1
7.2.1 7.2 7.1.2 7.1.1 7.1 7.0.4 7.0.6 7.0.7 6.3.8 6.3.7 6.3.6 6.3.5 6.3.4 6.3.3 6.3.1 trunk 5.7.3 5.7.5 5.8.1 5.8.2 5.8.3 5.8.4 5.8.6 6.0.4 6.0.5 All 36 releases
mlsimport / admin / js / mlsimport-term-meta.js

mlsimport-term-meta.js in MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings 7.1.1, at admin/js/mlsimport-term-meta.js

64 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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