Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Location: Improve Riparius handling #2300

Merged
merged 3 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions app/javascript/controllers/autocompleter_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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');
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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");
}
Expand Down
16 changes: 8 additions & 8 deletions app/javascript/controllers/geocode_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -449,9 +449,9 @@ export default class extends Controller {

// ------------------------------- DEBUGGING ------------------------------

helpDebug() {
debugger
}
// helpDebug() {
// debugger
// }

verbose(str) {
// console.log(str);
Expand Down
12 changes: 6 additions & 6 deletions app/javascript/controllers/map_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -627,9 +627,9 @@ export default class extends GeocodeController {

// ------------------------------- DEBUGGING ------------------------------

helpDebug() {
debugger
}
// helpDebug() {
// debugger
// }

verbose(str) {
// console.log(str);
Expand Down
2 changes: 2 additions & 0 deletions test/system/observation_form_system_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down