X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/10c9ca81c788c5aa3cd7a66d9cdbaedc14def1bc..606e66beb32d2b683651cf9ac7ae575d90df7ad6:/app/assets/javascripts/leaflet.layers.js diff --git a/app/assets/javascripts/leaflet.layers.js b/app/assets/javascripts/leaflet.layers.js index ce0fcd9f3..edcbfbd4d 100644 --- a/app/assets/javascripts/leaflet.layers.js +++ b/app/assets/javascripts/leaflet.layers.js @@ -1,68 +1,166 @@ -//= require templates/map/layers - -L.OSM.Layers = L.Control.extend({ - onAdd: function (map) { - this._map = map; - this._initLayout(map); - return this._container; - }, - - _initLayout: function (map) { - var className = 'leaflet-control-map-ui', - container = this._container = L.DomUtil.create('div', className); - - var link = this._layersLink = L.DomUtil.create('a', 'leaflet-map-ui-layers', container); - link.href = '#'; - link.title = 'Layers'; - - this._uiPane = this.options.uiPane; - - $(link).on('click', $.proxy(this.toggleLayers, this)); - }, - - toggleLayers: function (e) { - e.stopPropagation(); - e.preventDefault(); +L.OSM.layers = function(options) { + var control = L.control(options); + + control.onAdd = function (map) { + var layers = options.layers; + + var $container = $('
') + .attr('class', 'control-layers'); + + var link = $('') + .attr('class', 'control-button') + .attr('href', '#') + .attr('title', 'Layers') + .html('') + .on('click', toggle) + .appendTo($container); + + var $ui = $('
') + .attr('class', 'layers-ui'); + + $('
') + .attr('class', 'sidebar_heading') + .appendTo($ui) + .append( + $('') + .text(I18n.t('javascripts.close')) + .attr('class', 'sidebar_close') + .attr('href', '#') + .bind('click', toggle)) + .append( + $('

') + .text(I18n.t('javascripts.map.layers.header'))); + + var baseSection = $('
') + .attr('class', 'section base-layers') + .appendTo($ui); + + list = $('
    ') + .appendTo(baseSection); + + layers.forEach(function(layer) { + var item = $('
  • ') + .appendTo(list); + + if (map.hasLayer(layer)) { + item.addClass('active'); + } - var controlContainer = $('.leaflet-control-container .leaflet-top.leaflet-right'); + var div = $('
    ') + .appendTo(item); + + map.whenReady(function() { + var miniMap = L.map(div[0], {attributionControl: false, zoomControl: false}) + .addLayer(new layer.constructor()); + + miniMap.dragging.disable(); + miniMap.touchZoom.disable(); + miniMap.doubleClickZoom.disable(); + miniMap.scrollWheelZoom.disable(); + + $ui + .on('show', shown) + .on('hide', hide); + + function shown() { + miniMap.invalidateSize(); + setView({animate: false}); + map.on('moveend', moved); + } + + function hide() { + map.off('moveend', moved); + } + + function moved() { + setView(); + } + + function setView(options) { + miniMap.setView(map.getCenter(), Math.max(map.getZoom() - 2, 0), options); + } + }); + + var label = $('