Skip to content

Commit

Permalink
Merge pull request #33997 from dimagi/riese/bbox
Browse files Browse the repository at this point in the history
Save the projects default location bounding box and use in on search results
  • Loading branch information
MartinRiese committed Jan 23, 2024
2 parents 332ff2b + 571f70c commit ca79c0b
Show file tree
Hide file tree
Showing 6 changed files with 368 additions and 118 deletions.
11 changes: 7 additions & 4 deletions corehq/apps/cloudcare/static/cloudcare/js/form_entry/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ hqDefine("cloudcare/js/form_entry/utils", function () {
* @param {function|undefined} inputOnKeyDown - inputOnKeyDown function (optional)
* @param {boolean} showGeolocationButton - show geolocation button. Defaults to false. (optional)
* @param {boolean} geolocateOnLoad - geolocate the user's location on load. Defaults to false. (optional)
* @param {boolean} setProximity - set proximity to user's location. Defaults to false. (optional)
* @param {boolean} useBoundingBox - use default locations bbox to filter results. Defaults to false. (optional)
* @param {string} responseDataTypes - set Mapbox's data type response https://docs.mapbox.com/api/search/geocoding/#data-types (optional)
*/
module.renderMapboxInput = function ({
Expand All @@ -86,12 +86,12 @@ hqDefine("cloudcare/js/form_entry/utils", function () {
inputOnKeyDown,
showGeolocationButton = false,
geolocateOnLoad = false,
setProximity = false,
responseDataTypes = 'address'
useBoundingBox = false,
responseDataTypes = 'address',
}) {
showGeolocationButton = showGeolocationButton || toggles.toggleEnabled('GEOCODER_MY_LOCATION_BUTTON');
geolocateOnLoad = geolocateOnLoad || toggles.toggleEnabled('GEOCODER_AUTOLOAD_USER_LOCATION');
setProximity = setProximity || toggles.toggleEnabled('GEOCODER_USER_PROXIMITY');
var setProximity = toggles.toggleEnabled('GEOCODER_USER_PROXIMITY');
var defaultGeocoderLocation = initialPageData.get('default_geocoder_location') || {};
var geocoder = new MapboxGeocoder({
accessToken: initialPageData.get("mapbox_access_token"),
Expand All @@ -106,6 +106,9 @@ hqDefine("cloudcare/js/form_entry/utils", function () {
} else if (defaultGeocoderLocation.coordinates) {
geocoder.setProximity(defaultGeocoderLocation.coordinates);
}
if (setProximity && useBoundingBox && defaultGeocoderLocation.bbox) {
geocoder.setBbox(defaultGeocoderLocation.bbox);
}
geocoder.on('clear', clearCallBack);
geocoder.on('result', (item) => itemCallback(item.result));
geocoder.addTo('#' + divId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ hqDefine("cloudcare/js/formplayer/menus/views/query", function () {
itemCallback: geocoderItemCallback(id, model),
clearCallBack: geocoderOnClearCallback(id),
responseDataTypes: 'address,region,place,postcode',
useBoundingBox: true,
});
var divEl = $field.find('.mapboxgl-ctrl-geocoder');
divEl.css("max-width", "none");
Expand Down
214 changes: 107 additions & 107 deletions corehq/apps/hqwebapp/static/hqwebapp/js/bootstrap3/widgets.js
Original file line number Diff line number Diff line change
@@ -1,125 +1,125 @@
hqDefine("hqwebapp/js/bootstrap3/widgets",[
'jquery',
'underscore',
'@mapbox/mapbox-gl-geocoder/dist/mapbox-gl-geocoder.min',
'hqwebapp/js/initial_page_data',
'select2/dist/js/select2.full.min',
'jquery-ui/ui/widgets/datepicker',
], function ($, _, MapboxGeocoder, initialPageData) {
var init = function () {
var MAPBOX_ACCESS_TOKEN = initialPageData.get(
"mapbox_access_token"
);
// .hqwebapp-select2 is a basic select2-based dropdown or multiselect
_.each($(".hqwebapp-select2"), function (element) {
$(element).select2({
width: '100%',
});
if (window.USE_BOOTSTRAP5 && $(element).hasClass('is-invalid')) {
$(element).data('select2').$container.addClass('is-invalid');
}
if (window.USE_BOOTSTRAP5 && $(element).hasClass('is-valid')) {
$(element).data('select2').$container.addClass('is-valid');
}
'jquery',
'underscore',
'@mapbox/mapbox-gl-geocoder/dist/mapbox-gl-geocoder.min',
'hqwebapp/js/initial_page_data',
'select2/dist/js/select2.full.min',
'jquery-ui/ui/widgets/datepicker',
], function ($, _, MapboxGeocoder, initialPageData) {
var init = function () {
var MAPBOX_ACCESS_TOKEN = initialPageData.get(
"mapbox_access_token"
);
// .hqwebapp-select2 is a basic select2-based dropdown or multiselect
_.each($(".hqwebapp-select2"), function (element) {
$(element).select2({
width: '100%',
});
if (window.USE_BOOTSTRAP5 && $(element).hasClass('is-invalid')) {
$(element).data('select2').$container.addClass('is-invalid');
}
if (window.USE_BOOTSTRAP5 && $(element).hasClass('is-valid')) {
$(element).data('select2').$container.addClass('is-valid');
}
});

// .hqwebapp-autocomplete also allows for free text entry
_.each($(".hqwebapp-autocomplete"), function (input) {
var $input = $(input);
$input.select2({
multiple: true,
tags: true,
width: '100%',
});
// .hqwebapp-autocomplete also allows for free text entry
_.each($(".hqwebapp-autocomplete"), function (input) {
var $input = $(input);
$input.select2({
multiple: true,
tags: true,
width: '100%',
});
});

_.each($(".hqwebapp-autocomplete-email"), function (input) {
var $input = $(input);
$input.select2({
multiple: true,
placeholder: ' ',
tags: true,
tokenSeparators: [','],
width: '100%',
createTag: function (params) {
// Support pasting in comma-separated values
var terms = parseEmails(params.term);
if (terms.length === 1) {
return {
id: terms[0],
text: terms[0],
};
}
$input.select2('close');
var values = $input.val() || [];
if (!_.isArray(values)) {
values = [values];
_.each($(".hqwebapp-autocomplete-email"), function (input) {
var $input = $(input);
$input.select2({
multiple: true,
placeholder: ' ',
tags: true,
tokenSeparators: [','],
width: '100%',
createTag: function (params) {
// Support pasting in comma-separated values
var terms = parseEmails(params.term);
if (terms.length === 1) {
return {
id: terms[0],
text: terms[0],
};
}
$input.select2('close');
var values = $input.val() || [];
if (!_.isArray(values)) {
values = [values];
}
_.each(terms, function (term) {
if (!_.contains(values, term)) {
$input.append(new Option(term, term));
values.push(term);
}
_.each(terms, function (term) {
if (!_.contains(values, term)) {
$input.append(new Option(term, term));
values.push(term);
}
});
$input.val(values).trigger("change");
});
$input.val(values).trigger("change");

return null;
},
});
return null;
},
});
});

_.each($(".geocoder-proximity"), function (input) {
var $input = $(input).find('input');

function getGeocoderItem(item) {
var inputEl = $input;
var geoObj = {};
geoObj.place_name = item.place_name;
geoObj.coordinates = {
longitude: item.geometry.coordinates[0],
latitude: item.geometry.coordinates[1],
};
inputEl.attr("value", JSON.stringify(geoObj));
return item.place_name;
}

function getGeocoderValue() {
var geocoderValue = $input.val();
if (geocoderValue) {
geocoderValue = JSON.parse(geocoderValue);
return geocoderValue.place_name;
}
return null;
}
_.each($(".geocoder-proximity"), function (input) {
var $input = $(input).find('input');

var geocoder = new MapboxGeocoder({
accessToken: MAPBOX_ACCESS_TOKEN,
types:
"country,region,place,postcode,locality,neighborhood",
getItemValue: getGeocoderItem,
});
function getGeocoderItem(item) {
var inputEl = $input;
var geoObj = {};
geoObj.place_name = item.place_name;
geoObj.coordinates = {
longitude: item.geometry.coordinates[0],
latitude: item.geometry.coordinates[1],
};
geoObj.bbox = item.bbox;
inputEl.attr("value", JSON.stringify(geoObj));
return item.place_name;
}

geocoder.addTo(".geocoder-proximity");
var geocoderValue = getGeocoderValue();
function getGeocoderValue() {
var geocoderValue = $input.val();
if (geocoderValue) {
geocoder.setInput(getGeocoderValue());
geocoderValue = JSON.parse(geocoderValue);
return geocoderValue.place_name;
}
return null;
}

var geocoder = new MapboxGeocoder({
accessToken: MAPBOX_ACCESS_TOKEN,
types:
"country,region,place,postcode,locality,neighborhood",
getItemValue: getGeocoderItem,
});

$('.date-picker').datepicker({ dateFormat: "yy-mm-dd" });
};
geocoder.addTo(".geocoder-proximity");
var geocoderValue = getGeocoderValue();
if (geocoderValue) {
geocoder.setInput(getGeocoderValue());
}
});

$('.date-picker').datepicker({ dateFormat: "yy-mm-dd" });
};

var parseEmails = function (input) {
return $.trim(input).split(/[, ]\s*/);
};
var parseEmails = function (input) {
return $.trim(input).split(/[, ]\s*/);
};

$(function () {
init();
});
$(function () {
init();
});

return {
init: init,
parseEmails: parseEmails, // export for testing
};
}
);
return {
init: init,
parseEmails: parseEmails, // export for testing
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ hqDefine("hqwebapp/js/bootstrap5/widgets",[
longitude: item.geometry.coordinates[0],
latitude: item.geometry.coordinates[1],
};
geoObj.bbox = item.bbox;
inputEl.attr("value", JSON.stringify(geoObj));
return item.place_name;
}
Expand Down

0 comments on commit ca79c0b

Please sign in to comment.