address.js
4 years ago
admin-settings.js
4 years ago
count.js
4 years ago
file_upload.js
4 years ago
image.js
4 years ago
infinite_table.js
4 years ago
opening-hours.js
4 years ago
ordering.js
4 years ago
scheduler.js
4 years ago
spectrum.js
4 years ago
address.js
117 lines
| 1 | /** |
| 2 | * Javascript functions for Address component |
| 3 | * |
| 4 | * @package Simple Admin Pages |
| 5 | */ |
| 6 | |
| 7 | jQuery(document).ready(function ($) { |
| 8 | |
| 9 | /** |
| 10 | * Set coordinate that have been received |
| 11 | */ |
| 12 | function sap_address_set_coords( control, lat, lon ) { |
| 13 | control.find( '.sap-coords-result' ).remove(); |
| 14 | control.find( 'input.lat' ).val( lat ); |
| 15 | control.find( 'input.lon' ).val( lon ); |
| 16 | |
| 17 | if ( lat == '' && lon == '' ) { |
| 18 | control.find( '.sap-map-coords' ).text( lat + sap_address.strings['no-setting'] + lon ).attr( 'style', '' ); |
| 19 | } else { |
| 20 | control.find( '.sap-map-coords' ).text( lat + sap_address.strings['sep-lat-lon'] + lon ).attr( 'style', '' ); |
| 21 | } |
| 22 | |
| 23 | var url = 'https://maps.google.com/maps?q=' + lat + ',' + lon; |
| 24 | if ( control.find( '.sap-view-coords' ).length ) { |
| 25 | control.find( '.sap-view-coords' ).attr( 'href', url ); |
| 26 | } else { |
| 27 | control.find( '.sap-map-coords-wrapper' ).append( '<a class="sap-view-coords" href="' + url + '" target="_blank">' + sap_address.strings.view + '</a>' ); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Retrieve coordinates |
| 33 | */ |
| 34 | $('.sap-get-coords').click( function(e) { |
| 35 | |
| 36 | e.stopPropagation(); |
| 37 | e.preventDefault(); |
| 38 | |
| 39 | var control = $(this).parent().parent(); |
| 40 | var address = control.find( 'textarea' ).val(); |
| 41 | var params = { |
| 42 | sensor: false, |
| 43 | address: address, |
| 44 | }; |
| 45 | if ( sap_address.api_key_selector ) { |
| 46 | var $input = $( sap_address.api_key_selector ); |
| 47 | if ( $input.length && $input.val() ) { |
| 48 | params.key = $input.val(); |
| 49 | } |
| 50 | } else if ( sap_address.api_key ) { |
| 51 | params.key = sap_address.api_key; |
| 52 | } |
| 53 | |
| 54 | // Reset messages |
| 55 | control.find( '.sap-coords-result' ).remove(); |
| 56 | control.find( '.error' ).remove(); |
| 57 | control.find( '.sap-view-coords' ).remove(); |
| 58 | control.find( '.sap-map-coords' ).text( sap_address.strings.retrieving ).attr( 'style', 'opacity: 0.3' ); |
| 59 | |
| 60 | // Call Google Maps geocoding API |
| 61 | // See: https://developers.google.com/maps/documentation/geocoding/ |
| 62 | var req = $.get( |
| 63 | 'https://maps.googleapis.com/maps/api/geocode/json', |
| 64 | params, |
| 65 | function( data ) { |
| 66 | |
| 67 | if ( data.status == 'OK' ) { |
| 68 | if ( data.results.length == 1 ) { |
| 69 | sap_address_set_coords( control, data.results[0].geometry.location.lat, data.results[0].geometry.location.lng ); |
| 70 | |
| 71 | } else { |
| 72 | for ( var key in data.results ) { |
| 73 | control.append( '<p class="sap-coords-result">' + data.results[key].formatted_address + ' <span class="dashicons dashicons-arrow-right"></span> <a href="#" data-lat="' + data.results[key].geometry.location.lat + '" data-lon="' + data.results[key].geometry.location.lng + '">Set</a></p>' ); |
| 74 | } |
| 75 | control.find( '.sap-map-coords' ).text( sap_address.strings.select ).attr( 'style', '' ); |
| 76 | |
| 77 | control.find( '.sap-coords-result a' ).click( function() { |
| 78 | sap_address_set_coords( control, $(this).data( 'lat' ), $(this).data( 'lon' ) ); |
| 79 | }); |
| 80 | |
| 81 | } |
| 82 | |
| 83 | } else { |
| 84 | sap_address_set_coords( control, control.find( 'input.lat' ).val(), control.find( 'input.lon' ).val() ); |
| 85 | |
| 86 | if ( data.status == 'UNKNOWN_ERROR' ) { |
| 87 | control.find( '.sap-coords-action-wrapper' ).prepend( '<div class="error">' + sap_address.strings.result_error + '</div>' ); |
| 88 | } else if ( data.status == 'INVALID_REQUEST' ) { |
| 89 | control.find( '.sap-coords-action-wrapper' ).prepend( '<div class="error">' + sap_address.strings.result_invalid + '</div>' ); |
| 90 | } else if ( data.status == 'INVALID_REQUEST' ) { |
| 91 | control.find( '.sap-coords-action-wrapper' ).prepend( '<div class="error">' + sap_address.strings.result_error + '</div>' ); |
| 92 | } else if ( data.status == 'REQUEST_DENIED' ) { |
| 93 | control.find( '.sap-coords-action-wrapper' ).prepend( '<div class="error">' + sap_address.strings.result_denied + '</div>' ); |
| 94 | } else if ( data.status == 'OVER_QUERY_LIMIT' ) { |
| 95 | control.find( '.sap-coords-action-wrapper' ).prepend( '<div class="error">' + sap_address.strings.result_limit + '</div>' ); |
| 96 | } else if ( data.status == 'ZERO_RESULTS' ) { |
| 97 | control.find( '.sap-coords-action-wrapper' ).prepend( '<div class="error">' + sap_address.strings.result_empty + '</div>' ); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | ) |
| 102 | }); |
| 103 | |
| 104 | /** |
| 105 | * Remove coordinates from settings |
| 106 | */ |
| 107 | $('.sap-remove-coords').click( function(e) { |
| 108 | |
| 109 | e.stopPropagation(); |
| 110 | e.preventDefault(); |
| 111 | |
| 112 | var control = $(this).parent().parent(); |
| 113 | sap_address_set_coords( control, '', '' ); |
| 114 | }); |
| 115 | |
| 116 | }); |
| 117 |