The callback that the slippy map calls when an area is selected is
modified so that it gets as argument 'bounds', which represents the
bounding box of the selected area.

Using this, we get the center of the bounding box, and using an Ajax
request, ask the reversegeo service to find the corresponding
entries. When the reply is received, we extract the country code from
the received reply.

This will be used in a later commit to pre-filter the list of
languages according to the selected geographic area.

Signed-off-by: Thomas Petazzoni <[email protected]>
---
 www/media/map_rendering_form.js |   35 ++++++++++++++++++++++++++---------
 www/media/osm_map.js            |    4 ++--
 2 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/www/media/map_rendering_form.js b/www/media/map_rendering_form.js
index efc0502..c8cb406 100644
--- a/www/media/map_rendering_form.js
+++ b/www/media/map_rendering_form.js
@@ -167,6 +167,15 @@ var selectedCountry;
  * <option> elements. */
 var savedLanguageList;
 
+function setSelectedCountryCallback(geoResults)
+{
+  $.each(geoResults, function(i, item) {
+    if (typeof item.country_code != 'undefined') {
+      selectedCountry = item.country_code;
+    }
+  });
+}
+
 /* Filter the set of available languages according to the country in
  * which the administrative boundary is. There is no filtering done
  * when the area is given by bounding box. */
@@ -535,18 +544,26 @@ function getCurrentMode() {
      return 'admin';
 }
 
+var bboxReverseGeoAjaxQuery = null;
+
 /** Callback that the slippy map calls when a new area is defined. The
  * boolean tells whether the area is valid (not too big) or not valid
  * (too large to be rendered) */
-function mapAreaSelectionNotifier(isvalid) {
-    if (isvalid) {
-        allowNextStep();
-        $("#toobigareaerror").hide();
-    }
-    else {
-        disallowNextStep();
-        $("#toobigareaerror").show();
-    }
+function mapAreaSelectionNotifier(isvalid, bounds) {
+  if (isvalid) {
+    center = bounds.getCenterLonLat();
+    if (bboxReverseGeoAjaxQuery != null)
+      bboxReverseGeoAjaxQuery.abort();
+    bboxReverseGeoAjaxQuery =
+      $.getJSON("/apis/reversegeo/" + center.lat + "/" + center.lon + "/",
+                { }, setSelectedCountryCallback);
+    allowNextStep();
+    $("#toobigareaerror").hide();
+  }
+  else {
+    disallowNextStep();
+    $("#toobigareaerror").show();
+  }
 }
 
 /** Page initialization. */
diff --git a/www/media/osm_map.js b/www/media/osm_map.js
index 9b24b49..53f8b15 100644
--- a/www/media/osm_map.js
+++ b/www/media/osm_map.js
@@ -68,9 +68,9 @@ function updateFormBbox(bounds, areaSelectionNotifier)
 
     if (bbox_width > BBOX_MAXIMUM_LENGTH_IN_KM ||
         bbox_height > BBOX_MAXIMUM_LENGTH_IN_KM)
-       areaSelectionNotifier(false);
+       areaSelectionNotifier(false, bounds);
     else
-       areaSelectionNotifier(true);
+       areaSelectionNotifier(true, bounds);
 }
 
 /* Update the map on form field modification. */
-- 
1.7.0.4


Reply via email to