| 1 |
|
| 2 |
jQuery( document ).bind( 'sucom_init_metabox', function( event, container_id, doing_ajax ) { |
| 3 |
|
| 4 |
sucomInitAdminMedia( container_id, doing_ajax ); |
| 5 |
} ); |
| 6 |
|
| 7 |
function sucomInitAdminMedia( container_id, doing_ajax ) { |
| 8 |
|
| 9 |
var table_id = 'table.sucom-settings'; |
| 10 |
|
| 11 |
if ( typeof container_id !== 'undefined' && container_id ) { |
| 12 |
|
| 13 |
table_id = container_id + ' ' + table_id; |
| 14 |
} |
| 15 |
|
| 16 |
jQuery( table_id + ' .sucom_image_upload_lib' ).each( function( event ) { |
| 17 |
|
| 18 |
sucomMaybeShowUpload( this, event ); // Also executes sucomShowLibraryImage(). |
| 19 |
} ); |
| 20 |
|
| 21 |
jQuery( document ).on( 'change', table_id + ' .sucom_image_upload_lib', function( event ) { |
| 22 |
|
| 23 |
sucomMaybeShowUpload( this, event ); // Also executes sucomShowLibraryImage(). |
| 24 |
} ); |
| 25 |
|
| 26 |
jQuery( document ).on( 'change', table_id + ' .sucom_image_upload_id', function( event ) { |
| 27 |
|
| 28 |
sucomShowLibraryImage( this, event ); |
| 29 |
} ); |
| 30 |
|
| 31 |
jQuery( document ).on( 'click', table_id + ' .sucom_image_upload_button', function( event ) { |
| 32 |
|
| 33 |
sucomSelectLibraryImage( this, event ); |
| 34 |
} ); |
| 35 |
} |
| 36 |
|
| 37 |
function sucomMaybeShowUpload( t, e ) { |
| 38 |
|
| 39 |
var img_lib_value = jQuery( t ).val(); |
| 40 |
var img_id_css_id = jQuery( t ).attr( 'data-img-id-css-id' ); |
| 41 |
var upload_css_id = jQuery( t ).attr( 'data-upload-css-id' ); |
| 42 |
|
| 43 |
if ( 'wp' === img_lib_value ) { |
| 44 |
|
| 45 |
jQuery( '#' + upload_css_id ).show(); |
| 46 |
|
| 47 |
} else { |
| 48 |
|
| 49 |
jQuery( '#' + upload_css_id ).hide(); |
| 50 |
} |
| 51 |
|
| 52 |
if ( img_id_css_id ) { |
| 53 |
|
| 54 |
var img_id_container = jQuery( '#' + img_id_css_id ); |
| 55 |
|
| 56 |
sucomShowLibraryImage( img_id_container, e ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
function sucomShowLibraryImage( t, e ) { |
| 61 |
|
| 62 |
var img_lib_css_id = jQuery( t ).attr( 'data-img-lib-css-id' ); |
| 63 |
var img_url_css_id = jQuery( t ).attr( 'data-img-url-css-id' ); |
| 64 |
var preview_css_id = jQuery( t ).attr( 'data-preview-css-id' ); |
| 65 |
var img_id = jQuery( t ).val(); |
| 66 |
var img_id_thumb = img_id; |
| 67 |
|
| 68 |
if ( ! img_id_thumb ) { // Fallback to the placeholder image ID if we don't have a value. |
| 69 |
|
| 70 |
img_id_thumb = jQuery( t ).attr( 'placeholder' ); |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! preview_css_id ) { // A thumbnail preview container ID is required. |
| 74 |
|
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
var preview_container = jQuery( '#' + preview_css_id ); |
| 79 |
|
| 80 |
preview_container.empty(); // Remove any old image preview. |
| 81 |
|
| 82 |
if ( ! img_id_thumb || ! img_lib_css_id ) { // A thumbnail image ID and the library type container ID is required. |
| 83 |
|
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
var img_lib_value = jQuery( '#' + img_lib_css_id ).val(); |
| 88 |
|
| 89 |
if ( 'wp' === img_lib_value && jQuery.isNumeric( img_id_thumb ) ) { |
| 90 |
|
| 91 |
if ( img_url_css_id ) { |
| 92 |
|
| 93 |
if ( img_id ) { // Disable if we have a value (not just a placeholder). |
| 94 |
|
| 95 |
jQuery( '#' + img_url_css_id ).val( '' ); // Remove the value (and reset to placeholder). |
| 96 |
jQuery( '#' + img_url_css_id ).prop( 'disabled', true ); |
| 97 |
|
| 98 |
} else { // Re-enable if we don't have a value. |
| 99 |
|
| 100 |
jQuery( '#' + img_url_css_id ).prop( 'disabled', false ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
var q = new wp.media.model.Attachment.get( img_id_thumb ); |
| 105 |
|
| 106 |
q.fetch( { success:function( ret ) { |
| 107 |
|
| 108 |
if ( typeof ret.attributes.sizes.thumbnail.url !== 'undefined' ) { |
| 109 |
|
| 110 |
var thumbnail = ret.attributes.sizes.thumbnail; |
| 111 |
|
| 112 |
if ( preview_container ) { |
| 113 |
|
| 114 |
var img_html = '<img src="' + thumbnail.url + '"'; |
| 115 |
|
| 116 |
if ( thumbnail.width ) { |
| 117 |
|
| 118 |
img_html += ' width="' + thumbnail.width + '"'; |
| 119 |
} |
| 120 |
|
| 121 |
if ( thumbnail.height ) { |
| 122 |
|
| 123 |
img_html += ' height="' + thumbnail.height + '"'; |
| 124 |
} |
| 125 |
|
| 126 |
img_html += '/>'; |
| 127 |
|
| 128 |
preview_container.html( img_html ); |
| 129 |
} |
| 130 |
} |
| 131 |
} } ); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
function sucomSelectLibraryImage( t, e ) { |
| 136 |
|
| 137 |
e.preventDefault(); |
| 138 |
|
| 139 |
if ( typeof window.sucom_image_upload_media !== 'undefined' ) { |
| 140 |
|
| 141 |
window.sucom_image_upload_media.close(); |
| 142 |
} |
| 143 |
|
| 144 |
window.sucom_image_upload_media = wp.media( { |
| 145 |
title: sucomAdminMediaL10n._select_image, |
| 146 |
button: { text: sucomAdminMediaL10n._select_image }, |
| 147 |
multiple: false, |
| 148 |
library: { |
| 149 |
type: 'image', |
| 150 |
}, |
| 151 |
} ); |
| 152 |
|
| 153 |
window.sucom_image_upload_media.on( 'open', function() { |
| 154 |
|
| 155 |
var img_id = jQuery( t ).attr( 'data-wp-img-id' ); |
| 156 |
|
| 157 |
if ( img_id && jQuery.isNumeric( img_id ) ) { |
| 158 |
|
| 159 |
var selection = window.sucom_image_upload_media.state().get( 'selection' ); |
| 160 |
var attachment = wp.media.attachment( img_id ); |
| 161 |
|
| 162 |
selection.add( attachment ? [ attachment ] : [] ); |
| 163 |
} |
| 164 |
|
| 165 |
jQuery( '.media-modal', t.el ).find( '#media-search-input' ).focus(); |
| 166 |
} ); |
| 167 |
|
| 168 |
window.sucom_image_upload_media.on( 'select', function() { |
| 169 |
|
| 170 |
var attachment = window.sucom_image_upload_media.state().get( 'selection' ).first().toJSON(); |
| 171 |
var img_id_css_id = jQuery( t ).attr( 'data-img-id-css-id' ); |
| 172 |
var img_lib_css_id = jQuery( t ).attr( 'data-img-lib-css-id' ); |
| 173 |
|
| 174 |
jQuery( t ).attr( 'data-wp-img-id', attachment.id ); |
| 175 |
|
| 176 |
/* |
| 177 |
* Update the media library before the image id, but do not trigger a change yet. |
| 178 |
*/ |
| 179 |
if ( img_lib_css_id ) { |
| 180 |
|
| 181 |
jQuery( '#' + img_lib_css_id ).val( 'wp' ); |
| 182 |
} |
| 183 |
|
| 184 |
/* |
| 185 |
* Update the image id and trigger a change event. |
| 186 |
*/ |
| 187 |
if ( img_id_css_id ) { |
| 188 |
|
| 189 |
jQuery( '#' + img_id_css_id ).val( attachment.id ).change(); |
| 190 |
} |
| 191 |
} ); |
| 192 |
|
| 193 |
window.sucom_image_upload_media.open(); |
| 194 |
}; |
| 195 |
|