]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index.js
Zoom out when switching to a layer with a lower maxZoom
[rails.git] / app / assets / javascripts / index.js
index bd366b60906a892181a218921d26baaa08f15522..7f4887469c6c8f13ef8702430792446ced01d954 100644 (file)
@@ -22,40 +22,63 @@ $(document).ready(function () {
 
   map.hash = L.hash(map);
 
+  var copyright = I18n.t('javascripts.map.copyright', {copyright_url: '/copyright'});
+
   var layers = [
     new L.OSM.Mapnik({
-      attribution: '',
+      attribution: copyright,
       code: "M",
       keyid: "mapnik",
       name: I18n.t("javascripts.map.base.standard")
     }),
     new L.OSM.CycleMap({
-      attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
+      attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
       code: "C",
       keyid: "cyclemap",
       name: I18n.t("javascripts.map.base.cycle_map")
     }),
     new L.OSM.TransportMap({
-      attribution: "Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
+      attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
       code: "T",
       keyid: "transportmap",
       name: I18n.t("javascripts.map.base.transport_map")
     }),
     new L.OSM.MapQuestOpen({
-      attribution: "Tiles courtesy of <a href='http://www.mapquest.com/' target='_blank'>MapQuest</a> <img src='http://developer.mapquest.com/content/osm/mq_logo.png'>",
+      attribution: copyright + ". Tiles courtesy of <a href='http://www.mapquest.com/' target='_blank'>MapQuest</a> <img src='http://developer.mapquest.com/content/osm/mq_logo.png'>",
       code: "Q",
       keyid: "mapquest",
       name: I18n.t("javascripts.map.base.mapquest")
     })
   ];
 
-  for (var i = layers.length - 1; i >= 0; i--) {
-    if (i === 0 || params.layers.indexOf(layers[i].options.code) >= 0) {
-      map.addLayer(layers[i]);
-      break;
+  function updateLayers(params) {
+    var layerParam = params.layers || "M";
+    var layersAdded = "";
+
+    for (var i = layers.length - 1; i >= 0; i--) {
+      if (layerParam.indexOf(layers[i].options.code) >= 0) {
+        map.addLayer(layers[i]);
+        layersAdded = layersAdded + layers[i].options.code;
+      } else if (i == 0 && layersAdded == "") {
+        map.addLayer(layers[i]);
+      } else {
+        map.removeLayer(layers[i]);
+      }
     }
   }
 
+  updateLayers(params);
+
+  $(window).on("hashchange", function () {
+    updateLayers(OSM.mapParams());
+  });
+
+  map.on("baselayerchange", function (e) {
+    if (map.getZoom() > e.layer.options.maxZoom) {
+      map.setView(map.getCenter(), e.layer.options.maxZoom, { reset: true });
+    }
+  });
+
   map.noteLayer = new L.LayerGroup();
   map.noteLayer.options = {code: 'N'};
 
@@ -73,7 +96,10 @@ $(document).ready(function () {
 
   L.control.locate({
     position: position,
-    title: I18n.t('javascripts.map.locate.title')
+    strings: {
+      title: I18n.t('javascripts.map.locate.title'),
+      popup: I18n.t('javascripts.map.locate.popup')
+    }
   }).addTo(map);
 
   var sidebar = L.OSM.sidebar('#map-ui')
@@ -108,6 +134,18 @@ $(document).ready(function () {
 
   map.on('moveend layeradd layerremove', updateLocation);
 
+  if (OSM.PIWIK) {
+    map.on('layeradd', function (e) {
+      if (e.layer.options) {
+        var goal = OSM.PIWIK.goals[e.layer.options.keyid];
+
+        if (goal) {
+          $('body').trigger('piwikgoal', goal);
+        }
+      }
+    });
+  }
+
   var marker = L.marker([0, 0], {icon: getUserIcon()});
 
   if (!params.object_zoom) {
@@ -153,6 +191,20 @@ $(document).ready(function () {
     remoteEditHandler(map.getBounds());
   }
 
+  if (OSM.params().edit_help) {
+    $('#editanchor')
+      .removeAttr('title')
+      .tooltip({
+        placement: 'bottom',
+        title: I18n.t('javascripts.edit_help')
+      })
+      .tooltip('show');
+
+    $('body').one('click', function() {
+      $('#editanchor').tooltip('hide');
+    });
+  }
+
   initializeSearch(map);
   initializeExport(map);
   initializeBrowse(map, params);