From 24dc746421820a95a98205f5882aa96cac25f1e9 Mon Sep 17 00:00:00 2001 From: Aristeides Stathopoulos Date: Mon, 17 Jul 2017 13:44:15 +0300 Subject: [PATCH] fixes #1469 --- controls/image/image.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/controls/image/image.js b/controls/image/image.js index 8b27ae837..c35d239ba 100644 --- a/controls/image/image.js +++ b/controls/image/image.js @@ -17,11 +17,13 @@ wp.customize.controlConstructor['kirki-image'] = wp.customize.Control.extend({ initKirkiControl: function() { - var control = this, - value = control.getValue(), - saveAs = ( ! _.isUndefined( control.params.choices ) && ! _.isUndefined( control.params.choices.save_as ) ) ? control.params.choices.save_as : 'url', - preview = control.container.find( '.placeholder, .thumbnail' ), - previewImage = ( 'array' === saveAs ) ? value.url : value; + var control = this, + value = control.getValue(), + saveAs = ( ! _.isUndefined( control.params.choices ) && ! _.isUndefined( control.params.choices.save_as ) ) ? control.params.choices.save_as : 'url', + preview = control.container.find( '.placeholder, .thumbnail' ), + previewImage = ( 'array' === saveAs ) ? value.url : value, + removeButton = control.container.find( '.image-upload-remove-button' ), + defaultButton = control.container.find( '.image-default-button' ); control.container.find( '.kirki-controls-loading-spinner' ).hide(); @@ -30,6 +32,11 @@ wp.customize.controlConstructor['kirki-image'] = wp.customize.Control.extend({ control.container.find( 'image-default-button' ).hide(); } + // If value is empty, hide the "remove" button. + if ( ( 'url' === saveAs && '' === value ) || ( 'array' === saveAs && ( _.isUndefined( value.url ) || '' === value.url ) ) ) { + removeButton.hide(); + } + // If value is default, hide the default button. if ( value === control.params['default'] ) { control.container.find( 'image-default-button' ).hide(); @@ -44,9 +51,7 @@ wp.customize.controlConstructor['kirki-image'] = wp.customize.Control.extend({ // This will return the selected image from the Media Uploader, the result is an object. var uploadedImage = image.state().get( 'selection' ).first(), - previewImage = uploadedImage.toJSON().sizes.full.url, - removeButton, - defaultButton; + previewImage = uploadedImage.toJSON().sizes.full.url; if ( ! _.isUndefined( uploadedImage.toJSON().sizes.medium ) ) { previewImage = uploadedImage.toJSON().sizes.medium.url; @@ -65,9 +70,6 @@ wp.customize.controlConstructor['kirki-image'] = wp.customize.Control.extend({ control.saveValue( 'url', uploadedImage.toJSON().sizes.full.url ); } - removeButton = control.container.find( '.image-upload-remove-button' ); - defaultButton = control.container.find( '.image-default-button' ); - if ( preview.length ) { preview.removeClass().addClass( 'thumbnail thumbnail-image' ).html( '' ); }