Skip to content

Commit

Permalink
Merge pull request #16 from UVLabs/development
Browse files Browse the repository at this point in the history
chore: syncing
  • Loading branch information
UVLabs authored Nov 17, 2021
2 parents 56412e0 + 62a3a90 commit 3ce4ae1
Show file tree
Hide file tree
Showing 16 changed files with 497 additions and 115 deletions.
99 changes: 99 additions & 0 deletions assets/admin/js/lpac-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,105 @@
}
});

function toggleAutoDetectOptions(){

const autoDetectLocation = $('#lpac_auto_detect_location');

if( ! autoDetectLocation ){
return;
}

const autoDetectLocationChecked = autoDetectLocation.is(":checked");
const forceMapUse = $('#lpac_force_map_use');

// Hide suboptions if feature disabled
if( autoDetectLocationChecked ){
forceMapUse.closest('tr').hide();
}

autoDetectLocation.on('click', () => {

if( autoDetectLocation.is(':checked') ){
forceMapUse.closest('tr').hide();
}else{
forceMapUse.closest('tr').show();
}

});

}

function toggleMapLinkOrderEmailOptions(){

const addToEmail = $('#lpac_enable_delivery_map_link_in_email');

if( ! addToEmail ){
return;
}

const addToEmailChecked = addToEmail.is(":checked");
const linkType = $('#lpac_email_delivery_map_link_type');
const linkLocation = $('#lpac_email_delivery_map_link_location');
const selectedEmails = $('#lpac_email_delivery_map_emails');

// Hide suboptions if feature disabled
if( ! addToEmailChecked ){
linkType.closest('tr').hide();
linkLocation.closest('tr').hide();
selectedEmails.closest('tr').hide();
}

addToEmail.on('click', () => {

if( addToEmail.is(':checked') ){
linkType.closest('tr').show();
linkLocation.closest('tr').show();
selectedEmails.closest('tr').show();
}else{
linkType.closest('tr').hide();
linkLocation.closest('tr').hide();
selectedEmails.closest('tr').hide();
}

});

}

function togglePlacesAutoCompleteOptions(){

const placesAutoComplete = $('#lpac_enable_places_autocomplete');

if( ! placesAutoComplete ){
return;
}

const placesAutoCompleteChecked = placesAutoComplete.is(":checked");
const placesAllowedFields = $('#lpac_places_autocomplete_fields');
const placesAutoCompleteHideMap = $('#lpac_places_autocomplete_hide_map');

if( ! placesAutoCompleteChecked ){
placesAllowedFields.closest('tr').hide();
placesAutoCompleteHideMap.closest('tr').hide();
}

placesAutoComplete.on('click', () => {

if( placesAutoComplete.is(':checked') ){
placesAllowedFields.closest('tr').show();
placesAutoCompleteHideMap.closest('tr').show();
}else{
placesAllowedFields.closest('tr').hide();
placesAutoCompleteHideMap.closest('tr').hide();
}

});

}

toggleAutoDetectOptions();
toggleMapLinkOrderEmailOptions();
togglePlacesAutoCompleteOptions();

});

})(jQuery);
Expand Down
172 changes: 156 additions & 16 deletions assets/public/js/maps/checkout-page-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const infowindow = window.lpac_infowindow;
const geocoder = new google.maps.Geocoder();

const find_location_btn = document.querySelector("#lpac-find-location-btn");
const places_autocomplete_used = document.querySelector('#lpac_places_autocomplete');

if (typeof (find_location_btn) !== 'undefined' && find_location_btn !== null) {
find_location_btn.addEventListener(
Expand Down Expand Up @@ -106,6 +107,8 @@ async function lpac_bootstrap_map_functionality(geocoder, map, infowindow) {
*/
lpac_fill_in_latlng(latlng);

places_autocomplete_used.value = 0;

}

/**
Expand Down Expand Up @@ -223,6 +226,7 @@ function lpac_map_listen_to_clicks() {
infowindow.setContent(detected_address);
infowindow.open(map, marker);

places_autocomplete_used.value = 0;
});

}
Expand Down Expand Up @@ -253,13 +257,14 @@ function lpac_marker_listen_to_drag() {
return;
}

let moved_to_address = results[ 0 ].formatted_address
let moved_to_address = results[ 0 ].formatted_address;

moved_to_address = lpacRemovePlusCode(moved_to_address);

infowindow.setContent(moved_to_address)
infowindow.setContent(moved_to_address);

lpac_fill_in_address_fields(results, moved_to_latlng)
lpac_fill_in_address_fields(results, moved_to_latlng);
places_autocomplete_used.value = 0;

}

Expand Down Expand Up @@ -301,11 +306,7 @@ function lpac_fill_in_address_fields(results, latLng = '') {

lpac_fill_in_latlng(latLng)

lpac_fill_in_shipping_country_region(results)
lpac_fill_in_shipping_full_address(results)
lpac_fill_in_shipping_town_city(results)
lpac_fill_in_shipping_state_county(results)
lpac_fill_in_shipping_zipcode(results)
lpac_fill_in_shipping_fields( results );

if (typeof (mapOptions) === 'undefined' || mapOptions === null) {
console.log('LPAC: mapOptions object not present, skipping...')
Expand All @@ -315,15 +316,37 @@ function lpac_fill_in_address_fields(results, latLng = '') {
const lpac_autofill_billing_fields = mapOptions.lpac_autofill_billing_fields

if (lpac_autofill_billing_fields) {
lpac_fill_in_billing_country_region(results)
lpac_fill_in_billing_full_address(results)
lpac_fill_in_billing_town_city(results)
lpac_fill_in_billing_state_county(results)
lpac_fill_in_billing_zipcode(results)
lpac_fill_in_billing_fields( results );
}

}

/**
* Fill in all shipping fields.
*
* @param {array} results
*/
function lpac_fill_in_shipping_fields( results ){
lpac_fill_in_shipping_country_region(results)
lpac_fill_in_shipping_full_address(results)
lpac_fill_in_shipping_town_city(results)
lpac_fill_in_shipping_state_county(results)
lpac_fill_in_shipping_zipcode(results)
}

/**
* Fill in all billing fields.
*
* @param {array} results
*/
function lpac_fill_in_billing_fields( results ){
lpac_fill_in_billing_country_region(results)
lpac_fill_in_billing_full_address(results)
lpac_fill_in_billing_town_city(results)
lpac_fill_in_billing_state_county(results)
lpac_fill_in_billing_zipcode(results)
}

/*
* Get country from map.
*/
Expand Down Expand Up @@ -660,7 +683,6 @@ function lpacHideShowMap(){

});


}

/**
Expand All @@ -675,6 +697,11 @@ function lpacSetLastOrderMarker(){
return;
}

// If no coordinates exist don't try to plot the location on the map
if( ! lpacLastOrder.latitude || ! lpacLastOrder.longitude ){
return;
}

const latlng = {
lat: parseFloat(lpacLastOrder.latitude),
lng: parseFloat(lpacLastOrder.longitude),
Expand Down Expand Up @@ -713,6 +740,110 @@ function lpacSetLastOrderMarker(){

}

/**
* Places AutoComplete feature.
*
* https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete
*
* @returns
*/
function addPlacesAutoComplete(){

// Return if feature not enabled
if( ! mapOptions.lpac_enable_places_autocomplete ){
return;
}

// Hide map if option is turned on.
if( mapOptions.lpac_places_autocomplete_hide_map ){
changeMapVisibility(false);
}

const fields = mapOptions.lpac_places_autocomplete_fields;

fields.forEach(fieldID => {

const field = document.querySelector('#' + fieldID);

/*
* If field doesn't exist bail.
* This might happen if user sets shipping destination to "Force shipping to the customer billing address" so the shipping fields wouldn't exist.
*/
if( !field ){
return;
}

const options = {
// componentRestrictions: { country: ["us", "ca"] }, // TODO let users control this
fields: ["address_components", "formatted_address", "geometry"],
types: ["address"],
}

const autoComplete = new google.maps.places.Autocomplete(field, options);

/* Bind the map's bounds (viewport) property to the autocomplete object,
so that the autocomplete requests use the current map bounds for the
bounds option in the request. */
autoComplete.bindTo("bounds", map);

autoComplete.addListener("place_changed", () => {

const results = [autoComplete.getPlace()];

const latlng = {
lat: parseFloat( results[0].geometry.location.lat() ),
lng: parseFloat( results[0].geometry.location.lng() ),
}

if( fieldID.includes('shipping') ){

if( mapOptions.lpac_places_fill_shipping_fields ){
lpac_fill_in_shipping_fields(results);
}

lpac_fill_in_latlng(latlng);

map.setCenter(latlng);
marker.setPosition(latlng);
map.setZoom(16);
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
places_autocomplete_used.value = 1;
// Add event listeners to map
lpac_marker_listen_to_drag();
lpac_map_listen_to_clicks();
}else{

if( mapOptions.lpac_places_fill_shipping_fields ){
lpac_fill_in_billing_fields(results);
}

/*
* When Shipping destination is set as "Force shipping to the customer billing address" in WooCommerce->Shipping->Shipping Options
* We would want to adjust the map as needed.
*/
if( mapOptions.lpac_wc_shipping_destination_setting === 'billing_only'){
lpac_fill_in_latlng(latlng);
map.setCenter(latlng);
marker.setPosition(latlng);
map.setZoom(16);
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
places_autocomplete_used.value = 1;
// Add event listeners to map
lpac_marker_listen_to_drag();
lpac_map_listen_to_clicks();
}

}

})

});

}
addPlacesAutoComplete();

/**
* Detect when shipping methods are changed based on WC custom updated_checkout event.
* This event can't be accessed via vanilla JS because it's triggered by jQuery.
Expand All @@ -722,9 +853,18 @@ function lpacSetLastOrderMarker(){

$(document).ready(
function () {
// Prevents ajax call in lpacHideShowMap from overriding our lpac_places_autocomplete_hide_map option.
if( ! mapOptions.lpac_places_autocomplete_hide_map ){
$(document.body).on('updated_checkout', lpacHideShowMap);
}

// If the auto detect location feature is turned on, then detect the location but don't output the last order details.
if( mapOptions.lpac_auto_detect_location ){
lpac_bootstrap_map_functionality(geocoder, map, infowindow)
}else{
lpacSetLastOrderMarker();
}

$(document.body).on('updated_checkout', lpacHideShowMap);
lpacSetLastOrderMarker();
}
);

Expand Down
3 changes: 3 additions & 0 deletions class-lpac-uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public static function remove_plugin_settings() {
'lpac_admin_view_order_map_id',
'lpac_installed_at_version',
'lpac_remove_address_plus_code',
'lpac_enable_places_autocomplete',
'lpac_places_autocomplete_fields',
'lpac_auto_detect_location',
);

foreach ( $option_keys as $key ) {
Expand Down
Loading

0 comments on commit 3ce4ae1

Please sign in to comment.