Skip to content

Commit

Permalink
Merge pull request #53 from sherlockode/fix/geolocation
Browse files Browse the repository at this point in the history
Fix/geolocation
  • Loading branch information
Vowow authored Mar 2, 2023
2 parents 2077eaf + 7110be5 commit 5719062
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
32 changes: 31 additions & 1 deletion src/Resources/public/js/mondial-relay-fancy-list-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,37 @@ class MondialRelayFancyListAdapter
}

setMapCenter(coordinates) {
this.map.setCenter(coordinates);
let geocoder = new google.maps.Geocoder(),
map = this.map;

return new Promise(function (resolve, reject) {
geocoder.geocode({location: coordinates}).then(
function (response) {
let zipCode = null;

for (let i = 0; i < response.results.length; i++) {
let item = response.results[i];
for (let j = 0; j < item.address_components.length; j++) {
if (-1 !== item.address_components[j].types.indexOf('postal_code')) {
zipCode = item.address_components[j].short_name;

break;
}
}

if (zipCode) {
break;
}
}

if (zipCode) {
map.setCenter(coordinates);

return resolve(zipCode);
}
}
).catch(reject);
});
}
}

Expand Down
26 changes: 23 additions & 3 deletions src/Resources/public/js/mondial-relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ class MondialRelay
this.onHideSearchPanel();
}

if (-1 !== [].indexOf.call(document.querySelectorAll(this.selectors.geolocationBtn), event.target)) {
if (
-1 !== [].indexOf.call(document.querySelectorAll(this.selectors.geolocationBtn), event.target) ||
-1 !== [].indexOf.call(document.querySelectorAll(this.selectors.geolocationBtn), event.target.parentNode)
) {
this.processGeolocation();
}
}.bind(this), false);
Expand Down Expand Up @@ -136,17 +139,34 @@ class MondialRelay

processGeolocation() {
if ("undefined" !== typeof(navigator.geolocation)) {
let btn = document.querySelector(this.selectors.geolocationBtn);
btn.setAttribute('disabled', 'disabled');

navigator.geolocation.getCurrentPosition(
function (position) {
this.adapter.setMapCenter({
lat: position.coords.latitude,
lng: position.coords.longitude,
});
}.bind(this)
}).then(function (zipCode) {
btn.removeAttribute('disabled');
this.changeLocation(zipCode);
}.bind(this));
}.bind(this),
function () {
btn.removeAttribute('disabled');
}
)
}
}

changeLocation(location) {
let searchForm = this.getSearchForm(),
locationField = searchForm.querySelector('input[name$="\[zipCode\]"]');

locationField.value = location;
this.onSearch();
}

onSelectMondialRelayShipping() {
this.adapter.onSelectShippingMethod();
}
Expand Down

0 comments on commit 5719062

Please sign in to comment.