From 3bcc504bd896d87b7b72e2215da390850a84ab15 Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Fri, 2 May 2025 18:06:13 +0200 Subject: [PATCH] Geocoding utils: Support more flexible set of input fields (#5025) --- src/pretix/static/pretixcontrol/js/ui/geo.js | 46 ++++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/src/pretix/static/pretixcontrol/js/ui/geo.js b/src/pretix/static/pretixcontrol/js/ui/geo.js index bdc6446ee2..d86d1d2842 100644 --- a/src/pretix/static/pretixcontrol/js/ui/geo.js +++ b/src/pretix/static/pretixcontrol/js/ui/geo.js @@ -4,6 +4,26 @@ $(document).on("pretix:bind-forms", function () { function cleanup(l) { return $.trim(l.replace(/\n/g, ", ")); } + function combine($sel) { + var parts = [ + $sel.filter("[name*=street]").val(), + $sel.filter("[name*=zipcode]").val(), + $sel.filter("[name*=city]").val(), + $sel.filter("[name*=state]").val(), + $sel.filter("[name*=country]").find("option:selected").text(), + $sel.filter("[name*=location]").val(), + ] + var res = ""; + for (var val of parts) { + if (val) { + if (res) { + res += ", " + } + res += val + } + } + return cleanup(res) + } $(".geodata-section").each(function () { // Geocoding // detach notifications and append them to first label (should be from location) @@ -13,8 +33,26 @@ $(document).on("pretix:bind-forms", function () { var lat; var lon; var $updateButton = $("[data-action=update]", this); - var $location = $("textarea[lang=en], input[lang=en]", this).first(); - if (!$location.length) $location = $("textarea, input[type=text]", this).first(); + + var $location; + // The .geodata-section is expected to include either... + // ... an English "location" field + if ($("textarea[lang=en], input[lang=en]", this).length) { + $location = $("textarea[lang=en], input[lang=en], select", this).not("[name*=geo_]"); + } + + // ... a "location" field in any other language + if (!$location || !$location.length) { + var lang = $("textarea, input[type=text]", this).not("[name*=geo_]").first().attr("lang"); + if (lang) { + $location = $("textarea[lang=" + lang + "], input[lang=" + lang + "], select", this); + } + } + + // ... or a set of fields like a full address form + if (!$location || !$location.length) { + $location = $("textarea, input, select", this).not("[name*=geo_]"); + } if (!$lat.length || !$lon.length || !$location.length) { return; @@ -23,7 +61,7 @@ $(document).on("pretix:bind-forms", function () { var debounceLoad, debounceLatLonChange, delayUpdateDismissal; var touched = $lat.val() !== ""; var xhr; - var lastLocation = cleanup($location.val()); + var lastLocation = combine($location); function load() { window.clearTimeout(debounceLoad); @@ -32,7 +70,7 @@ $(document).on("pretix:bind-forms", function () { xhr = null; } - var q = cleanup($location.val()); + var q = combine($location); if (q === "" || q === lastLocation) return; lastLocation = q;