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

feat(html): add explicit submit/reset buttons for feature filters ins… #212

Merged
merged 1 commit into from
Jul 5, 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
77 changes: 56 additions & 21 deletions internal/ogc/features/templates/features.go.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

<script>
{{- /* generic function to update query string parameters */ -}}
function updateQueryString(name, value, skipReload) {
const url = new URL(window.location.href)
function updateQueryString(name, value, existingUrl) {
const url = existingUrl ? new URL(existingUrl) : new URL(window.location.href)
url.searchParams.delete('cursor') // when filters change, we can't continue pagination.
if (value) {
if (name === 'datetime') {
Expand All @@ -18,14 +18,49 @@
} else {
url.searchParams.delete(name)
}
return url.toString()
}

function callUrl(url, skipReload) {
if (skipReload) {
window.history.pushState({}, '', url.toString()) // only change the url but don't reload the page
window.history.pushState({}, '', url) // only change the url but don't reload the page
} else {
window.location.href = url.toString()
window.location.href = url
}
return url.toString()
}

function submitForm(event) {
event.preventDefault()

// Gather form values
const fd = new FormData(event.target)
const srsValue = fd.get('srs')
const datetimeValue = fd.get('datetime')
const limitValue = fd.get('limit')
const propertyFilters = {}
{{ range $pfName, $pf := .Params.ConfiguredPropertyFilters }}
propertyFilters["{{ $pfName }}"] = fd.get("{{ $pfName }}")
{{ end }}

// Build url with form values
let url = updateQueryString('crs', srsValue)
url = updateQueryString('datetime', datetimeValue, url)
url = updateQueryString('limit', limitValue, url)
for (const [key, value] of Object.entries(propertyFilters)) {
url = updateQueryString(key, value, url)
}

callUrl(url)
}

function resetForm() {
callUrl("{{ $baseUrl }}/collections/{{ .Params.CollectionID }}/items")
}

window.addEventListener("load", function() {
document.getElementById('form').addEventListener('submit', submitForm)
document.getElementById('form').addEventListener('reset', resetForm)
});
</script>

<hgroup>
Expand All @@ -51,12 +86,12 @@ <h2 class="card-header h5">
</div>

<!-- filters -->
<form class="col-12 mt-4">
<form id="form" class="col-12 mt-4">
<!-- projection -->
<div class="row mb-2">
<label for="srs-select" class="col-sm-3 col-form-label fw-bold">{{ i18n "Projection" }}</label>
<div class="col-sm">
<select id="srs-select" class="form-select" onchange="updateQueryString('crs', this.value)">
<select id="srs-select" name="srs" class="form-select">
<option value="http://www.opengis.net/def/crs/OGC/1.3/CRS84" selected>
http://www.opengis.net/def/crs/OGC/1.3/CRS84
</option>
Expand All @@ -71,18 +106,16 @@ <h2 class="card-header h5">
<div class="row mb-2">
<label for="referencedate-input" class="col-sm-3 col-form-label fw-bold">{{ i18n "ReferenceDate" }}</label>
<div class="col-sm">
<input id="referencedate-input" type="date" class="form-control"
value="{{ if .Params.ReferenceDate }}{{ .Params.ReferenceDate | date "2006-01-02" }}{{ end }}"
onchange="updateQueryString('datetime', this.value)">
<input id="referencedate-input" name="datetime" type="date" class="form-control"
value="{{ if .Params.ReferenceDate }}{{ .Params.ReferenceDate | date "2006-01-02" }}{{ end }}">
</div>
</div>
{{ end }}
<!-- limit -->
<div class="row mb-2">
<label for="limit-select" class="col-sm-3 col-form-label fw-bold">{{ i18n "Limit" }}</label>
<div class="col-sm">
<select id="limit-select" class="form-select" aria-label="Limit selection"
onchange="updateQueryString('limit', this.value)">
<select id="limit-select" name="limit" class="form-select" aria-label="Limit selection">
<option value="10" {{if eq .Params.Limit 10 }}selected{{end}}>10 {{ i18n "Items" }}</option>
<option value="100" {{if eq .Params.Limit 100 }}selected{{end}}>100 {{ i18n "Items" }}</option>
{{/* see Limit.Max in config.go, can't be smaller than 100 */}}
Expand All @@ -100,24 +133,24 @@ <h2 class="card-header h5">
title="{{ $pfName | title }}">{{ $pfName | title }}</label>
<div class="col-sm">
{{ if and $pf $pf.AllowedValues }}
<select id="{{ $pfName }}-input" class="form-select"
onchange="updateQueryString('{{ $pfName }}', this.value)">
<select id="{{ $pfName }}-input" name="{{ $pfName }}" class="form-select">
<!-- default to empty value -->
<option></option>
{{ range $value := $pf.AllowedValues }}
<option value="{{ $value }}" {{if eq (index $.Params.PropertyFilters $pfName) $value }}selected{{end}}>{{ $value }}</option>
{{ end }}
</select>
{{ else }}
<input id="{{ $pfName }}-input" type="text" class="form-control"
<input id="{{ $pfName }}-input" name="{{ $pfName }}" type="text" class="form-control"
{{/* get the actual value as supplied by the user from the PropertyFilters,
not the ConfiguredPropertyFilters (which doesn't have this info) */}}
value="{{ index $.Params.PropertyFilters $pfName }}"
onchange="updateQueryString('{{ $pfName }}', this.value)">
value="{{ index $.Params.PropertyFilters $pfName }}">
{{ end }}
</div>
</div>
{{ end }}
<button type="submit" class="btn btn-primary mt-2"> {{ i18n "ApplyFilters" }}</button>
<button type="reset" class="btn btn-secondary mt-2">{{ i18n "ResetFilter" }}</button>
</form>
</div>

Expand Down Expand Up @@ -150,11 +183,13 @@ <h2 class="card-header h5">

{{ if $mapSheetProperties }}
viewer.addEventListener('box', selectBox => {
let newUrl = new URL(updateQueryString('bbox', selectBox.detail, true))
newUrl.searchParams.set('f', 'json')
let newUrl = new URL(updateQueryString('bbox', selectBox.detail))
callUrl(newUrl.toString(), true)

// when moving the map to load additional sheets we don't want to do a full page reload (like we
// do when one draws a bbox). Therefor we update the browser URL + link references (like GeoJSON/JSON-FG)
// on the page manually.
newUrl.searchParams.set('f', 'json')
viewer.setAttribute('items-url', newUrl.toString())
{{ range $formatKey, $formatName := .AvailableFormats }}
if (document.getElementById("format-{{ $formatKey }}")) {
Expand All @@ -164,11 +199,11 @@ <h2 class="card-header h5">
{{ end }}
})
viewer.addEventListener('activeFeature', activeFeature => {
updateQueryString("{{ $mapSheetProperties.MapSheetID }}", activeFeature.detail.get('{{ $mapSheetProperties.MapSheetID }}'))
callUrl(updateQueryString("{{ $mapSheetProperties.MapSheetID }}", activeFeature.detail.get('{{ $mapSheetProperties.MapSheetID }}')))
})
{{ else }}
viewer.addEventListener('box', selectBox => {
updateQueryString('bbox', selectBox.detail)
callUrl(updateQueryString('bbox', selectBox.detail))
})
{{ end }}
</script>
Expand Down
16 changes: 6 additions & 10 deletions internal/ogc/features/testdata/expected_straatnaam_silodam.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
<div class="row mb-2">
<label for="postcode-input" class="col-sm-3 col-form-label fw-bold text-truncate"
title="Postcode">Postcode</label>
<label for="postcode-input" class="col-sm-3 col-form-label fw-bold text-truncate" title="Postcode">Postcode</label>
<div class="col-sm">

<input id="postcode-input" type="text" class="form-control"
value=""
onchange="updateQueryString('postcode', this.value)">
<input id="postcode-input" name="postcode" type="text" class="form-control"
value="">

</div>
</div>
<div class="row mb-2">
<label for="straatnaam-input" class="col-sm-3 col-form-label fw-bold text-truncate"
title="Straatnaam">Straatnaam</label>
<label for="straatnaam-input" class="col-sm-3 col-form-label fw-bold text-truncate" title="Straatnaam">Straatnaam</label>
<div class="col-sm">

<input id="straatnaam-input" type="text" class="form-control"
value="Silodam"
onchange="updateQueryString('straatnaam', this.value)">
<input id="straatnaam-input" name="straatnaam" type="text" class="form-control"
value="Silodam">

</div>
</div>
Loading