]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/browse.js
Make sure the object layer is kept in front of the data layer
[rails.git] / app / assets / javascripts / index / browse.js
index 752c84fecee841505b14bc402ce324c9f5cde7d6..1140515f5629b93ebd8b04079b806d69493fd5cb 100644 (file)
@@ -1,6 +1,5 @@
-function initializeBrowse(map) {
+OSM.initializeBrowse = function (map) {
   var browseBounds;
-  var selectedLayer;
   var dataLayer = map.dataLayer;
 
   dataLayer.setStyle({
@@ -26,51 +25,49 @@ function initializeBrowse(map) {
     onSelect(e.layer);
   });
 
-  map.on('layeradd', function (e) {
+  map.on("layeradd", function (e) {
     if (e.layer === dataLayer) {
       map.on("moveend", updateData);
       updateData();
     }
   });
 
-  map.on('layerremove', function (e) {
+  map.on("layerremove", function (e) {
     if (e.layer === dataLayer) {
       map.off("moveend", updateData);
-      $('#browse_status').empty();
+      $("#browse_status").empty();
     }
   });
 
   function updateData() {
     var bounds = map.getBounds();
-    getData(!browseBounds || !browseBounds.contains(bounds));
-    browseBounds = bounds;
+    if (!browseBounds || !browseBounds.contains(bounds)) {
+      getData();
+    }
   }
 
-  function displayFeatureWarning(count, limit, callback) {
-    $('#browse_status').html(
-      $("<p class='warning'></p>")
-        .text(I18n.t("browse.start_rjs.loaded_an_area_with_num_features", { num_features: count, max_features: limit }))
+  function displayFeatureWarning(count, limit, add, cancel) {
+    $("#browse_status").html(
+      $("<div>")
+        .append(
+          $("<h2>")
+            .text(I18n.t("browse.start_rjs.load_data"))
+            .prepend($("<span class='icon close'></span>").click(cancel)))
         .append(
-          $("<input type='submit'>")
-            .val(I18n.t('browse.start_rjs.load_data'))
-            .click(callback)));
+          $("<div>")
+            .append(
+              $("<p class='alert alert-warning clearfix'></p>")
+                .text(I18n.t("browse.start_rjs.feature_warning", { num_features: count, max_features: limit })))
+            .append(
+              $("<input type='submit' class='btn btn-primary'>")
+                .val(I18n.t("browse.start_rjs.load_data"))
+                .click(add))));
   }
 
   var dataLoader;
 
-  function getData(inPrevious) {
+  function getData() {
     var bounds = map.getBounds();
-    var size = bounds.getSize();
-
-    if (size > OSM.MAX_REQUEST_AREA) {
-      $('#browse_status').html(
-        $("<p class='warning'></p>")
-          .text(I18n.t("browse.start_rjs.unable_to_load_size", { max_bbox_size: OSM.MAX_REQUEST_AREA, bbox_size: size.toFixed(2) })));
-      return;
-    }
-
-    if (inPrevious) return;
-
     var url = "/api/" + OSM.API_VERSION + "/map?bbox=" + bounds.toBBoxString();
 
     /*
@@ -94,19 +91,27 @@ function initializeBrowse(map) {
       url: url,
       success: function (xml) {
         dataLayer.clearLayers();
-        selectedLayer = null;
 
         var features = dataLayer.buildFeatures(xml);
 
         function addFeatures() {
-          $('#browse_status').empty();
+          $("#browse_status").empty();
           dataLayer.addData(features);
+          browseBounds = bounds;
+        }
+
+        function cancelAddFeatures() {
+          $("#browse_status").empty();
         }
 
         if (features.length < maxFeatures) {
           addFeatures();
         } else {
-          displayFeatureWarning(features.length, maxFeatures, addFeatures);
+          displayFeatureWarning(features.length, maxFeatures, addFeatures, cancelAddFeatures);
+        }
+
+        if (map._objectLayer) {
+          map._objectLayer.bringToFront();
         }
 
         dataLoader = null;
@@ -115,18 +120,6 @@ function initializeBrowse(map) {
   }
 
   function onSelect(layer) {
-    // Unselect previously selected feature
-    if (selectedLayer) {
-      selectedLayer.setStyle(selectedLayer.originalStyle);
-    }
-
-    // Redraw in selected style
-    layer.originalStyle = layer.options;
-    layer.setStyle({color: '#0000ff', weight: 8});
-
-    OSM.route('/browse/' + layer.feature.type + '/' + layer.feature.id);
-
-    // Stash the currently drawn feature
-    selectedLayer = layer;
+    OSM.router.route("/" + layer.feature.type + "/" + layer.feature.id);
   }
-}
+};