Skip to content

Commit

Permalink
Merge branch 'main' into herbarium-form-modal
Browse files Browse the repository at this point in the history
  • Loading branch information
nimmolo committed Aug 20, 2024
2 parents fc29d9a + a12bb66 commit 2d98814
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 28 deletions.
41 changes: 29 additions & 12 deletions app/javascript/controllers/autocompleter_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ export default class extends Controller {
this.swap({ detail: { type: "location_google" } });
}

leaveCreate() {
if (!(['location_google'].includes(this.TYPE) && this.hasMapOutlet)) return;

this.verbose("autocompleter: leaveCreate()");
const location = this.mapOutlet.validateLatLngInputs(false);
// Will swap to location, or location_containing if lat/lngs are present
if (this.mapOutlet.ignorePlaceInput !== true) {
this.mapOutlet.sendPointChanged(location);
}
}

// Connects autocompleter to map controller to call its methods
activateMapOutlet(location = false) {
if (!this.hasMapOutlet) {
Expand All @@ -304,17 +315,16 @@ export default class extends Controller {
if (location) {
// this.mapOutlet.geocodeLatLng(location);
this.mapOutlet.tryToGeocode();
} else if (this.mapOutlet.hasLockBoxBtnTarget) {
this.mapOutlet.lockBoxBtnTarget.classList.remove("d-none");
}
}

deactivateMapOutlet() {
if (!this.hasMapOutlet) return;

this.verbose("autocompleter: deactivateMapOutlet()");
if (this.mapOutlet.rectangle) this.mapOutlet.clearRectangle();
this.mapOutlet.map_type = "observation";
if (this.mapOutlet.rectangle) this.mapOutlet.rectangle.setEditable(false);
// if (this.mapOutlet.rectangle) this.mapOutlet.rectangle.setEditable(false);

this.mapOutlet.northInputTarget.value = '';
this.mapOutlet.southInputTarget.value = '';
Expand Down Expand Up @@ -479,16 +489,17 @@ export default class extends Controller {
const old_val = this.old_value;
const new_val = this.inputTarget.value;
// this.debug("ourChange(" + this.inputTarget.value + ")");
if (new_val.length > 0) {
this.cssUncollapseFields();
} else {
if (new_val.length == 0) {
this.cssCollapseFields();
}
if (new_val != old_val) {
this.old_value = new_val;
if (do_refresh) {
this.verbose("autocompleter:ourChange()");
this.scheduleRefresh();
this.leaveCreate();
} else {
this.cssUncollapseFields();
if (new_val != old_val) {
this.old_value = new_val;
if (do_refresh) {
this.verbose("autocompleter:ourChange()");
this.scheduleRefresh();
}
}
}
}
Expand Down Expand Up @@ -1013,12 +1024,18 @@ export default class extends Controller {
if (hidden_id && hidden_id !== NaN && hidden_id != 0) {
this.wrapTarget.classList.add('has-id');
this.wrapTarget.classList.remove('offer-create');
if (this.hasKeepBtnTarget) {
this.keepBtnTarget.classList.remove("d-none");
}
} else {
this.wrapTarget.classList.remove('has-id');
if (this.inputTarget.value &&
!this.wrapTarget.classList.contains('create')) {
this.wrapTarget.classList.add('offer-create');
}
if (this.hasKeepBtnTarget) {
this.keepBtnTarget.classList.add("d-none");
}
}
// On forms where a map may not be relevant, we also show/hide the map.
// Only show if we're in "create" mode.
Expand Down
30 changes: 21 additions & 9 deletions app/javascript/controllers/map_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ export default class extends GeocodeController {

// Action called from the "Clear Map" button
clearMap() {
this.verbose("map:clearMap")
const inputTargets = [
this.placeInputTarget, this.northInputTarget, this.southInputTarget,
this.eastInputTarget, this.westInputTarget, this.highInputTarget,
Expand All @@ -547,19 +548,30 @@ export default class extends GeocodeController {
inputTargets.forEach((element) => { element.value = '' })
this.ignorePlaceInput = false // turn string geolocation back on

if (this.marker) {
this.marker.setMap(null)
this.marker = null
}
if (this.rectangle) {
this.rectangle.setMap(null)
this.rectangle = null
// this.showBoxBtnTarget.disabled = false
}
this.clearMarker()
this.clearRectangle()
this.dispatch("reenableBtns")
this.sendPointChanged({ lat: null, lng: null })
}

clearMarker() {
if (!this.marker) return false

this.verbose("map:clearMarker")
this.marker.setMap(null)
this.marker = null
}

clearRectangle() {
if (!this.rectangle) return false

this.verbose("map:clearRectangle")
this.rectangle.setVisible(false)
this.rectangle.setMap(null)
this.rectangle = null
return true
}

//
// COORDINATES
//
Expand Down
4 changes: 2 additions & 2 deletions app/views/controllers/observations/namings/_form.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%# locals: (local:, show_reasons: true, form_locals: {}) -%>
<%# locals: (local: false, form_locals: {}) -%>

<%
# Fields must be separate because they're included in the obs form too.
Expand Down Expand Up @@ -35,7 +35,7 @@ end
# `naming_locals`: modal forms can accept a `form_locals` local. The controller
# may send `context` (i.e. where the form appears), which defaults to "blank".
# `show_reasons` is false on the obs form, true on the naming form.
naming_locals = { create:, button_name:, show_reasons: }.merge(form_locals)
naming_locals = { create:, button_name:, show_reasons: true, context: "blank" }.merge(form_locals)
%>

<%= form_with(**form_args) do |f| %>
Expand Down
3 changes: 2 additions & 1 deletion app/views/controllers/observations/namings/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ add_tab_set(naming_form_edit_tabs(obs: @observation))

<div class="mt-3">
<%= render(partial: "observations/namings/form",
locals: { show_reasons: true, local: true }) %>
locals: { local: true,
form_locals: { show_reasons: true } }) %>
</div>
</div><!--.col-->

Expand Down
3 changes: 2 additions & 1 deletion app/views/controllers/observations/namings/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ add_tab_set(naming_form_new_tabs(obs: @observation))

<div class="mt-3">
<%= render(partial: "observations/namings/form",
locals: { show_reasons: true, local: true }) %>
locals: { local: true,
form_locals: { show_reasons: true } }) %>
</div>
</div><!--.col-->

Expand Down
7 changes: 4 additions & 3 deletions app/views/controllers/shared/_modal_form_reload.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<%# locals: (identifier:, form:, form_locals: {}) -%>
<%
# What to do if a form submit doesn't succeed
# Takes locals: { identifier:, form:, form_locals: }
form_locals ||= {}
# Takes locals: { identifier:, form:, form_locals: {} }
# form_locals ||= {}
%>

<%= turbo_stream.update("modal_#{identifier}_flash") do
flash_notices_html
end %>

<%= turbo_stream.replace("#{identifier}_form") do
render(partial: form, locals: { format: :turbo_stream }.merge(form_locals))
render(partial: form, locals: { local: false, form_locals: })
end %>

0 comments on commit 2d98814

Please sign in to comment.