]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/query.js
Allow configuration of storage server URL for security policy
[rails.git] / app / assets / javascripts / index / query.js
index 333fce5494866c9c9ff4503a61288b658ede9641..6869e19f63835f44366e1a68d6ba4a18487b2d7a 100644 (file)
@@ -1,10 +1,9 @@
 //= require jquery.simulate
 
-OSM.Query = function(map) {
-  var protocol = document.location.protocol === "https:" ? "https:" : "http:",
-    url = protocol + OSM.OVERPASS_URL,
+OSM.Query = function (map) {
+  var url = OSM.OVERPASS_URL,
     queryButton = $(".control-query .control-button"),
-    uninterestingTags = ['source', 'source_ref', 'source:ref', 'history', 'attribution', 'created_by', 'tiger:county', 'tiger:tlid', 'tiger:upload_uuid', 'KSJ2:curve_id', 'KSJ2:lat', 'KSJ2:lon', 'KSJ2:coordinate', 'KSJ2:filename', 'note:ja'],
+    uninterestingTags = ["source", "source_ref", "source:ref", "history", "attribution", "created_by", "tiger:county", "tiger:tlid", "tiger:upload_uuid", "KSJ2:curve_id", "KSJ2:lat", "KSJ2:lon", "KSJ2:coordinate", "KSJ2:filename", "note:ja"],
     marker;
 
   var featureStyle = {
@@ -12,7 +11,7 @@ OSM.Query = function(map) {
     weight: 4,
     opacity: 1,
     fillOpacity: 0.5,
-    clickable: false
+    interactive: false
   };
 
   queryButton.on("click", function (e) {
@@ -56,7 +55,7 @@ OSM.Query = function(map) {
           var geometry = $(this).data("geometry");
           if (geometry) map.removeLayer(geometry);
 
-          if (!$(e.target).is('a')) {
+          if (!$(e.target).is("a")) {
             $(this).find("a").simulate("click", e);
           }
         }
@@ -146,7 +145,7 @@ OSM.Query = function(map) {
 
     if (feature.type === "node" && feature.lat && feature.lon) {
       geometry = L.circleMarker([feature.lat, feature.lon], featureStyle);
-    } else if (feature.type === "way" && feature.geometry) {
+    } else if (feature.type === "way" && feature.geometry && feature.geometry.length > 0) {
       geometry = L.polyline(feature.geometry.filter(function (point) {
         return point !== null;
       }).map(function (point) {
@@ -161,7 +160,7 @@ OSM.Query = function(map) {
     return geometry;
   }
 
-  function runQuery(latlng, radius, query, $section, compare) {
+  function runQuery(latlng, radius, query, $section, merge, compare) {
     var $ul = $section.find("ul");
 
     $ul.empty();
@@ -179,19 +178,34 @@ OSM.Query = function(map) {
       url: url,
       method: "POST",
       data: {
-        data: "[timeout:5][out:json];" + query,
+        data: "[timeout:10][out:json];" + query
       },
-      success: function(results) {
+      success: function (results) {
         var elements;
 
         $section.find(".loader").stopTime("loading").hide();
 
-        if (compare) {
-          elements = results.elements.sort(compare);
+        if (merge) {
+          elements = results.elements.reduce(function (hash, element) {
+            var key = element.type + element.id;
+            if ("geometry" in element) {
+              delete element.bounds;
+            }
+            hash[key] = $.extend({}, hash[key], element);
+            return hash;
+          }, {});
+
+          elements = Object.keys(elements).map(function (key) {
+            return elements[key];
+          });
         } else {
           elements = results.elements;
         }
 
+        if (compare) {
+          elements = elements.sort(compare);
+        }
+
         for (var i = 0; i < elements.length; i++) {
           var element = elements[i];
 
@@ -211,13 +225,19 @@ OSM.Query = function(map) {
           }
         }
 
+        if (results.remark) {
+          $("<li>")
+            .text(I18n.t("javascripts.query.error", { server: url, error: results.remark }))
+            .appendTo($ul);
+        }
+
         if ($ul.find("li").length === 0) {
           $("<li>")
             .text(I18n.t("javascripts.query.nothing_found"))
             .appendTo($ul);
         }
       },
-      error: function(xhr, status, error) {
+      error: function (xhr, status, error) {
         $section.find(".loader").stopTime("loading").hide();
 
         $("<li>")
@@ -267,8 +287,8 @@ OSM.Query = function(map) {
       nodes = "node(" + around + ")",
       ways = "way(" + around + ")",
       relations = "relation(" + around + ")",
-      nearby = "(" + nodes + ";" + ways + ");out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");",
-      isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags geom(" + bbox + ");relation(pivot.a);out tags bb;";
+      nearby = "(" + nodes + ";" + ways + ";);out tags geom(" + bbox + ");" + relations + ";out geom(" + bbox + ");",
+      isin = "is_in(" + lat + "," + lng + ")->.a;way(pivot.a);out tags bb;out ids geom(" + bbox + ");relation(pivot.a);out tags bb;";
 
     $("#sidebar_content .query-intro")
       .hide();
@@ -281,14 +301,14 @@ OSM.Query = function(map) {
         map.removeLayer(marker);
       } else {
         marker.setStyle({
-          opacity: 1 - i * 0.1,
-          fillOpacity: 0.5 - i * 0.05
+          opacity: 1 - (i * 0.1),
+          fillOpacity: 0.5 - (i * 0.05)
         });
       }
     }, 10);
 
-    runQuery(latlng, radius, nearby, $("#query-nearby"));
-    runQuery(latlng, radius, isin, $("#query-isin"), compareSize);
+    runQuery(latlng, radius, nearby, $("#query-nearby"), false);
+    runQuery(latlng, radius, isin, $("#query-isin"), true, compareSize);
   }
 
   function clickHandler(e) {
@@ -315,14 +335,14 @@ OSM.Query = function(map) {
 
   var page = {};
 
-  page.pushstate = page.popstate = function(path) {
+  page.pushstate = page.popstate = function (path) {
     OSM.loadSidebarContent(path, function () {
       page.load(path, true);
     });
   };
 
-  page.load = function(path, noCentre) {
-    var params = querystring.parse(path.substring(path.indexOf('?') + 1)),
+  page.load = function (path, noCentre) {
+    var params = querystring.parse(path.substring(path.indexOf("?") + 1)),
       latlng = L.latLng(params.lat, params.lon);
 
     if (!window.location.hash && !noCentre && !map.getBounds().contains(latlng)) {
@@ -334,7 +354,7 @@ OSM.Query = function(map) {
     queryOverpass(params.lat, params.lon);
   };
 
-  page.unload = function(sameController) {
+  page.unload = function (sameController) {
     if (!sameController) {
       disableQueryMode();
     }