diff --git a/app/javascript/controllers/autocompleter_controller.js b/app/javascript/controllers/autocompleter_controller.js index ab1476779e..8402522114 100644 --- a/app/javascript/controllers/autocompleter_controller.js +++ b/app/javascript/controllers/autocompleter_controller.js @@ -215,6 +215,13 @@ export default class extends Controller { } if (type == undefined) { return; } + let location = false; + if (detail.hasOwnProperty("request_params") && + detail.request_params.hasOwnProperty("lat") && + detail.request_params.hasOwnProperty("lng")) { + location = detail.request_params; + } + if (!AUTOCOMPLETER_TYPES.hasOwnProperty(type)) { alert("MOAutocompleter: Invalid type: \"" + type + "\""); } else { @@ -235,11 +242,13 @@ export default class extends Controller { !this.keepBtnTarget?.classList?.contains('active')) { this.clearHiddenId(); } - this.constrainedSelectionUI(); + this.constrainedSelectionUI(location); } } - constrainedSelectionUI() { + // Depending on the type of autocompleter, the UI may need to change. + // detail may also contain request_params for lat/lng. + constrainedSelectionUI(location = false) { if (this.TYPE === "location_google") { this.verbose("autocompleter: swapped to location_google"); this.element.classList.add('create'); @@ -250,7 +259,7 @@ export default class extends Controller { } else { this.verbose("autocompleter: no map wrap"); } - this.activateMapOutlet(); + this.activateMapOutlet(location); } else if (this.ACT_LIKE_SELECT) { this.verbose("autocompleter: swapped to ACT_LIKE_SELECT"); this.deactivateMapOutlet(); @@ -271,8 +280,8 @@ export default class extends Controller { this.swap({ detail: { type: "location_google" } }); } - // Connects the location_google autocompleter to call map controller methods - activateMapOutlet() { + // Connects autocompleter to map controller to call its methods + activateMapOutlet(location = false) { if (!this.hasMapOutlet) { this.verbose("autocompleter: no map outlet"); return; @@ -286,10 +295,15 @@ export default class extends Controller { } // set the map type so box is editable this.mapOutlet.map_type = "hybrid"; // only if location_google - - let location - if (location = this.mapOutlet.validateLatLngInputs(false)) { - this.mapOutlet.geocodeLatLng(location); + // set the map to stop ignoring place input + this.mapOutlet.ignorePlaceInput = false; + + // Often, this swap to location_google is for geolocating place_names and + // should pay attention to text only. But in some cases the swap (e.g., from + // form-exif) sends request_params lat/lng, so geocode when switching. + if (location) { + // this.mapOutlet.geocodeLatLng(location); + this.mapOutlet.tryToGeocode(); } else if (this.mapOutlet.hasLockBoxBtnTarget) { this.mapOutlet.lockBoxBtnTarget.classList.remove("d-none"); } diff --git a/app/javascript/controllers/geocode_controller.js b/app/javascript/controllers/geocode_controller.js index fba70c52c8..1284151180 100644 --- a/app/javascript/controllers/geocode_controller.js +++ b/app/javascript/controllers/geocode_controller.js @@ -45,9 +45,9 @@ export default class extends Controller { } tryToGeocode() { - let location + const location = this.validateLatLngInputs(false) - if (location = this.validateLatLngInputs(false) && + if (location && JSON.stringify(location) !== JSON.stringify(this.lastGeocodedLatLng)) { this.geocodeLatLng(location) } @@ -75,10 +75,10 @@ export default class extends Controller { } tryToGeolocate() { - let address = this.placeInputTarget.value + const address = this.placeInputTarget.value - if (this.ignorePlaceInput === false && address !== "" - && address !== this.lastGeolocatedAddress) { + if (this.ignorePlaceInput === false && + address !== "" && address !== this.lastGeolocatedAddress) { this.geolocatePlaceName(address) } } @@ -449,9 +449,9 @@ export default class extends Controller { // ------------------------------- DEBUGGING ------------------------------ - helpDebug() { - debugger - } + // helpDebug() { + // debugger + // } verbose(str) { // console.log(str); diff --git a/app/javascript/controllers/map_controller.js b/app/javascript/controllers/map_controller.js index 770769439f..2a115b765b 100644 --- a/app/javascript/controllers/map_controller.js +++ b/app/javascript/controllers/map_controller.js @@ -398,11 +398,11 @@ export default class extends GeocodeController { let id if (this.hasLocationIdTarget && (id = this.locationIdTarget.value)) { this.mapLocationIdData() - // Only geocode lat/lng if we have no location_id and not ignoring place } else if (["location", "hybrid"].includes(this.map_type)) { + // Only geocode lat/lng if we have no location_id and not ignoring place + // ...and only geolocate placeName if we have no lat/lng if (this.ignorePlaceInput !== false) { this.tryToGeocode() // multiple possible results - // ...and only geolocate placeName if we have no lat/lng } else { this.tryToGeolocate() } @@ -499,7 +499,7 @@ export default class extends GeocodeController { // Action called by the "Open Map" button only. // open/close handled by BS collapse toggleMap() { - // this.verbose("map:toggleMap") + this.verbose("map:toggleMap") if (this.opened) { this.opened = false this.controlWrapTarget.classList.remove("map-open") @@ -627,9 +627,9 @@ export default class extends GeocodeController { // ------------------------------- DEBUGGING ------------------------------ - helpDebug() { - debugger - } + // helpDebug() { + // debugger + // } verbose(str) { // console.log(str); diff --git a/test/system/observation_form_system_test.rb b/test/system/observation_form_system_test.rb index f9b73bde5f..14113ff5a0 100644 --- a/test/system/observation_form_system_test.rb +++ b/test/system/observation_form_system_test.rb @@ -385,6 +385,8 @@ def test_post_edit_and_destroy_with_details_and_location all('[id^="project_id_"]', visible: :all).each do |project_checkbox| project_checkbox.trigger("click") if project_checkbox.checked? end + step_nav_3 = find("#step-nav-3") + within(step_nav_3) { click_on(:BACK.l) } # submit_observation_form_with_errors within("#observation_form") { click_commit }