From f4413a8a7451b3ad779bbb05244074c1d01fa1c7 Mon Sep 17 00:00:00 2001 From: flammermann Date: Sun, 3 Mar 2024 13:28:03 +0000 Subject: [PATCH] translation capability and small cleanups --- umap/static/umap/js/umap.controls.js | 25 +++++++++++++++---------- umap/static/umap/js/umap.core.js | 7 +++++++ umap/static/umap/js/umap.features.js | 2 +- umap/static/umap/js/umap.forms.js | 15 +++++---------- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/umap/static/umap/js/umap.controls.js b/umap/static/umap/js/umap.controls.js index 5d77ffff..f8d6e4e5 100644 --- a/umap/static/umap/js/umap.controls.js +++ b/umap/static/umap/js/umap.controls.js @@ -710,7 +710,7 @@ const ControlsMixin = { } } else { value = String(value) - value = (value.length ? value : "empty string") + value = (value.length ? value : L._("empty string")) if (!!value && !facetCriteria[key]["choices"].includes(value)) { facetCriteria[key]["choices"].push(value) } @@ -729,15 +729,20 @@ const ControlsMixin = { if (!found) this.ui.alert({ content: L._('No results for these facets'), level: 'info' }) } - const fields = keys.map((key) => [ - `facets.${key}`, - { - handler: ["date", "datetime-local", "number"].includes(facetCriteria[key]["inputType"]) ? 'FacetSearchMinMax' : 'FacetSearchChoices', - criteria: facetCriteria[key], - label: facetKeys[key]["label"] - }, - ]) - const builder = new U.FormBuilder(this, fields, { + const fields = keys.map((key) => { + let criteria = facetCriteria[key] + let handler = ["date", "datetime-local", "number"].includes(criteria["inputType"]) ? 'FacetSearchMinMax' : 'FacetSearchChoices' + let label = facetKeys[key]["label"] + return [ + `facets.${key}`, + { + criteria: criteria, + handler: handler, + label: label + }, + ] + }) + const builder = new L.U.FormBuilder(this, fields, { makeDirty: false, callback: filterFeatures, callbackContext: this, diff --git a/umap/static/umap/js/umap.core.js b/umap/static/umap/js/umap.core.js index 06313859..b9b7f659 100644 --- a/umap/static/umap/js/umap.core.js +++ b/umap/static/umap/js/umap.core.js @@ -67,6 +67,13 @@ L.Util.setNullableBooleanFromQueryString = function (options, name) { } } +L.Util.calculateStepFromNumber = function (n) { + // calculate step for number input field from significant digits of number + let step = String(n).replace(/^\d+?(0*)((\.)(\d*?)0*|)$/, "1$1$3$4").split('.') + step = parseFloat((step[1] || "").replace(/\d/g, "0").replace(/^0/, "0.0").replace(/0$/, "1") || (step[0] || "").replace(/0$/, "") || "1") + return step +} + L.DomUtil.add = (tagName, className, container, content) => { const el = L.DomUtil.create(tagName, className, container) if (content) { diff --git a/umap/static/umap/js/umap.features.js b/umap/static/umap/js/umap.features.js index 96688a04..5ec54115 100644 --- a/umap/static/umap/js/umap.features.js +++ b/umap/static/umap/js/umap.features.js @@ -516,7 +516,7 @@ U.FeatureMixin = { } else { const choices = criteria["choices"] value = String(value) - value = (value.length ? value : "empty string") + value = (value.length ? value : L._("empty string")) if (choices.length && (!value || !choices.includes(value))) return false } } diff --git a/umap/static/umap/js/umap.forms.js b/umap/static/umap/js/umap.forms.js index 63a36592..cb9ce2c7 100644 --- a/umap/static/umap/js/umap.forms.js +++ b/umap/static/umap/js/umap.forms.js @@ -796,7 +796,7 @@ L.FormBuilder.FacetSearchMinMax = L.FormBuilder.Element.extend({ this.minTdLabel = L.DomUtil.create('td', '', this.minTr) this.minLabel = L.DomUtil.create('label', '', this.minTdLabel) - this.minLabel.innerHTML = 'Min' + this.minLabel.innerHTML = L._('Min') this.minLabel.htmlFor = `${this.inputType}_${this.name}_min` this.minTdInput = L.DomUtil.create('td', '', this.minTr) @@ -813,7 +813,7 @@ L.FormBuilder.FacetSearchMinMax = L.FormBuilder.Element.extend({ this.maxTdLabel = L.DomUtil.create('td', '', this.maxTr) this.maxLabel = L.DomUtil.create('label', '', this.maxTdLabel) - this.maxLabel.innerHTML = 'Max' + this.maxLabel.innerHTML = L._('Max') this.maxLabel.htmlFor = `${this.inputType}_${this.name}_max` this.maxTdInput = L.DomUtil.create('td', '', this.maxTr) @@ -827,8 +827,8 @@ L.FormBuilder.FacetSearchMinMax = L.FormBuilder.Element.extend({ } if (["date", "datetime-local"].includes(this.inputType)) { - this.minLabel.innerHTML = 'From' - this.maxLabel.innerHTML = 'Until' + this.minLabel.innerHTML = L._('From') + this.maxLabel.innerHTML = L._('Until') if (min != null) { this.minInput.valueAsNumber = (min.valueOf() - min.getTimezoneOffset() * 60000) } @@ -845,12 +845,7 @@ L.FormBuilder.FacetSearchMinMax = L.FormBuilder.Element.extend({ if (["number"].includes(this.inputType)) { if (min != null && max != null) { // calculate step from significant digits of min and max values - let minStep = String(min).replace(/^\d+?(0*)((\.)(\d*?)0*|)$/, "1$1$3$4").split('.') - let maxStep = String(max).replace(/^\d+?(0*)((\.)(\d*?)0*|)$/, "1$1$3$4").split('.') - minStep = parseFloat((minStep[1] || "").replace(/\d/g, "0").replace(/^0/, "0.0").replace(/0$/, "1") || (minStep[0] || "").replace(/0$/, "") || "1") - maxStep = parseFloat((maxStep[1] || "").replace(/\d/g, "0").replace(/^0/, "0.0").replace(/0$/, "1") || (maxStep[0] || "").replace(/0$/, "") || "1") - - const step = Math.min(minStep, maxStep) + const step = Math.min(L.Util.calculateStepFromNumber(min), L.Util.calculateStepFromNumber(max)) this.minInput.step = String(step) this.maxInput.step = String(step) }