flexible-shipping
/
src
/
WPDesk
/
FS
/
TableRate
/
ShippingMethod
/
views
/
method-logo-settings-field-script.php
method-logo-settings-field-script.php
63 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @var array $view_data |
| 4 | */ |
| 5 | |
| 6 | ?> |
| 7 | <script type="text/javascript"> |
| 8 | jQuery( function( $ ) { |
| 9 | const chooseLabel = <?php echo wp_json_encode( $view_data['choose_label'] ); ?>; |
| 10 | const changeLabel = <?php echo wp_json_encode( $view_data['change_label'] ); ?>; |
| 11 | |
| 12 | $( document.body ).on( 'click', '.fs-method-logo-field__select', function( event ) { |
| 13 | event.preventDefault(); |
| 14 | |
| 15 | const field = $( this ).closest( '.fs-method-logo-field' ); |
| 16 | const input = field.find( 'input[type="hidden"]' ); |
| 17 | const preview = field.find( '.fs-method-logo-field__preview' ); |
| 18 | const image = field.find( '.fs-method-logo-field__image' ); |
| 19 | const removeButton = field.find( '.fs-method-logo-field__remove' ); |
| 20 | const selectButton = $( this ); |
| 21 | const frame = wp.media( { |
| 22 | title: changeLabel, |
| 23 | library: { |
| 24 | type: 'image' |
| 25 | }, |
| 26 | button: { |
| 27 | text: changeLabel |
| 28 | }, |
| 29 | multiple: false |
| 30 | } ); |
| 31 | |
| 32 | frame.on( 'select', function() { |
| 33 | const attachment = frame.state().get( 'selection' ).first().toJSON(); |
| 34 | const imageUrl = attachment?.sizes?.thumbnail?.url || attachment?.url || ''; |
| 35 | |
| 36 | if ( ! imageUrl ) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | input.val( attachment.id || '' ); |
| 41 | image.attr( 'src', imageUrl ); |
| 42 | image.attr( 'alt', attachment.alt || attachment.title || '' ); |
| 43 | preview.prop( 'hidden', false ); |
| 44 | removeButton.prop( 'hidden', false ); |
| 45 | selectButton.text( changeLabel ); |
| 46 | } ); |
| 47 | |
| 48 | frame.open(); |
| 49 | } ); |
| 50 | |
| 51 | $( document.body ).on( 'click', '.fs-method-logo-field__remove', function( event ) { |
| 52 | event.preventDefault(); |
| 53 | |
| 54 | const field = $( this ).closest( '.fs-method-logo-field' ); |
| 55 | field.find( 'input[type="hidden"]' ).val( '' ); |
| 56 | field.find( '.fs-method-logo-field__image' ).attr( 'src', '' ).attr( 'alt', '' ); |
| 57 | field.find( '.fs-method-logo-field__preview' ).prop( 'hidden', true ); |
| 58 | field.find( '.fs-method-logo-field__select' ).text( chooseLabel ); |
| 59 | $( this ).prop( 'hidden', true ); |
| 60 | } ); |
| 61 | } ); |
| 62 | </script> |
| 63 |