]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/leaflet.layers.js
Add layers code to share link
[rails.git] / app / assets / javascripts / leaflet.layers.js
index 09cf1cf60f2bd3ba4ed6aef08f2eb1734f867b23..80613a5d937730353636a29e3f1b9d709f7985ef 100644 (file)
@@ -1,15 +1,10 @@
-L.OSM.Layers = L.Control.extend({
-  onAdd: function (map) {
-    this._map = map;
-    this._initLayout();
-    return this.$container[0];
-  },
-
-  _initLayout: function () {
-    var map = this._map,
-      layers = this.options.layers;
-
-    this.$container = $('<div>')
+L.OSM.layers = function(options) {
+  var control = L.control(options);
+
+  control.onAdd = function (map) {
+    var layers = options.layers;
+
+    var $container = $('<div>')
       .attr('class', 'control-layers');
 
     var link = $('<a>')
@@ -17,20 +12,22 @@ L.OSM.Layers = L.Control.extend({
       .attr('href', '#')
       .attr('title', 'Layers')
       .html('<span class="icon layers"></span>')
-      .appendTo(this.$container);
+      .on('click', toggle)
+      .appendTo($container);
 
-    if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
-      this.$ui = $('<div>')
-        .attr('class', 'layers-ui')
-        .appendTo(this.options.uiPane);
+    var $ui = $('<div>')
+      .attr('class', 'layers-ui');
 
-      $('<h2>')
-        .text(I18n.t('javascripts.map.layers.header'))
-        .appendTo(this.$ui);
+    $('<section>')
+       .appendTo($ui)
+       .append(
+        $('<h2>')
+          .text(I18n.t('javascripts.map.layers.header')));
 
+    if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
       var overlaySection = $('<section>')
         .addClass('overlay-layers')
-        .appendTo(this.$ui);
+        .appendTo($ui);
 
       $('<p>')
         .text(I18n.t('javascripts.map.layers.overlays'))
@@ -72,7 +69,7 @@ L.OSM.Layers = L.Control.extend({
 
     var baseSection = $('<section>')
       .addClass('base-layers')
-      .appendTo(this.$ui);
+      .appendTo($ui);
 
     $('<p>')
       .text(I18n.t('javascripts.map.layers.base'))
@@ -94,19 +91,30 @@ L.OSM.Layers = L.Control.extend({
 
       map.whenReady(function() {
         var miniMap = L.map(div[0], {attributionControl: false, zoomControl: false})
-          .setView(map.getCenter(), Math.max(map.getZoom() - 2, 0))
-          .addLayer(new layer.constructor);
+          .addLayer(new layer.constructor());
 
         miniMap.dragging.disable();
         miniMap.touchZoom.disable();
         miniMap.doubleClickZoom.disable();
         miniMap.scrollWheelZoom.disable();
 
-        map.on('moveend', function() {
-          miniMap.setView(map.getCenter(), Math.max(map.getZoom() - 2, 0));
-        });
+        $ui
+          .on('show', shown)
+          .on('hide', hide);
 
-        div.data('map', miniMap);
+        function shown() {
+          miniMap.invalidateSize();
+          setView();
+          map.on('moveend', setView);
+        }
+
+        function hide() {
+          map.off('moveend', setView);
+        }
+
+        function setView() {
+          miniMap.setView(map.getCenter(), Math.max(map.getZoom() - 2, 0));
+        }
       });
 
       var label = $('<label>')
@@ -128,29 +136,16 @@ L.OSM.Layers = L.Control.extend({
       });
     });
 
-    $(link).on('click', $.proxy(this.toggleLayers, this));
-  },
-
-  toggleLayers: function (e) {
-    e.stopPropagation();
-    e.preventDefault();
+    options.sidebar.addPane($ui);
 
-    var controlContainer = $('.leaflet-control-container .leaflet-top.leaflet-right');
-
-    if (this.$ui.is(':visible')) {
-      $(this.options.uiPane).hide();
-      controlContainer.css({paddingRight: '0'});
-    } else {
-      $(this.options.uiPane).show();
-      controlContainer.css({paddingRight: '230px'});
+    function toggle(e) {
+      e.stopPropagation();
+      e.preventDefault();
+      options.sidebar.togglePane($ui);
     }
 
-    this.$ui.find('.base-layers .leaflet-container').each(function() {
-      $(this).data('map').invalidateSize();
-    });
-  }
-});
+    return $container[0];
+  };
 
-L.OSM.layers = function(options) {
-  return new L.OSM.Layers(options);
+  return control;
 };