contact-info-admin.js
55 lines
| 1 | /* global ajaxurl, contact_info_api_key_ajax_obj */ |
| 2 | |
| 3 | ( function ( $ ) { |
| 4 | $( document ).on( 'change', '.jp-contact-info-showmap', function () { |
| 5 | var $checkbox = $( this ), |
| 6 | isChecked = $checkbox.is( ':checked' ); |
| 7 | |
| 8 | $checkbox.closest( '.widget' ).find( '.jp-contact-info-admin-map' ).toggle( isChecked ); |
| 9 | } ); |
| 10 | |
| 11 | $( document ).on( 'widget-synced', function ( event, widgetContainer ) { |
| 12 | // This event fires for all widgets, so restrict this to Contact Info widgets and the API key input. |
| 13 | if ( |
| 14 | ! widgetContainer.is( '[id*="widget_contact_info"]' ) || |
| 15 | ! $( document.activeElement ).is( 'input[id*="apikey"]' ) |
| 16 | ) { |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | event.preventDefault(); |
| 21 | |
| 22 | var $apikey_input = widgetContainer.find( 'input[id*="apikey"]' ); |
| 23 | |
| 24 | $.post( |
| 25 | ajaxurl, |
| 26 | { |
| 27 | _ajax_nonce: contact_info_api_key_ajax_obj.nonce, |
| 28 | action: 'customize-contact-info-api-key', |
| 29 | apikey: $apikey_input.val(), |
| 30 | }, |
| 31 | function ( data ) { |
| 32 | var $map_element = $apikey_input |
| 33 | .closest( '.jp-contact-info-admin-map' ) |
| 34 | .parent() |
| 35 | .find( '.jp-contact-info-embed-map' ); |
| 36 | var $warning_span = $map_element.find( '[class*="notice"]' ); |
| 37 | |
| 38 | if ( '1' !== data.result ) { |
| 39 | if ( $warning_span.length === 0 ) { |
| 40 | $map_element.append( |
| 41 | '<span class="notice notice-warning" style="display: block;">' + |
| 42 | data.result + |
| 43 | '</span>' |
| 44 | ); |
| 45 | } else if ( $warning_span.text() !== data.result ) { |
| 46 | $warning_span.text( data.result ); |
| 47 | } |
| 48 | } else { |
| 49 | $map_element.empty(); |
| 50 | } |
| 51 | } |
| 52 | ); |
| 53 | } ); |
| 54 | } )( window.jQuery ); |
| 55 |