1 L.OSM.layers = function(options) {
2 var control = L.control(options);
4 control.onAdd = function (map) {
5 var layers = options.layers;
7 var $container = $('<div>')
8 .attr('class', 'control-layers');
11 .attr('class', 'control-button')
13 .attr('title', 'Layers')
14 .html('<span class="icon layers"></span>')
16 .appendTo($container);
19 .attr('class', 'layers-ui')
20 .appendTo(options.uiPane);
23 .text(I18n.t('javascripts.map.layers.header'))
26 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
27 var overlaySection = $('<section>')
28 .addClass('overlay-layers')
32 .text(I18n.t('javascripts.map.layers.overlays'))
33 .appendTo(overlaySection);
36 .appendTo(overlaySection);
38 function addOverlay(layer, name) {
42 var label = $('<label>')
45 var input = $('<input>')
46 .attr('type', 'checkbox')
47 .prop('checked', map.hasLayer(layer))
52 input.on('change', function() {
53 if (input.is(':checked')) {
56 map.removeLayer(layer);
60 map.on('layeradd layerremove', function() {
61 input.prop('checked', map.hasLayer(layer));
65 addOverlay(map.noteLayer, I18n.t('javascripts.map.layers.notes'));
66 addOverlay(map.dataLayer, I18n.t('javascripts.map.layers.data'));
69 var baseSection = $('<section>')
70 .addClass('base-layers')
74 .text(I18n.t('javascripts.map.layers.base'))
75 .appendTo(baseSection);
78 .appendTo(baseSection);
80 layers.forEach(function(layer) {
84 if (map.hasLayer(layer)) {
85 item.addClass('active');
91 map.whenReady(function() {
92 var miniMap = L.map(div[0], {attributionControl: false, zoomControl: false})
93 .setView(map.getCenter(), Math.max(map.getZoom() - 2, 0))
94 .addLayer(new layer.constructor);
96 miniMap.dragging.disable();
97 miniMap.touchZoom.disable();
98 miniMap.doubleClickZoom.disable();
99 miniMap.scrollWheelZoom.disable();
101 map.on('moveend', function() {
102 miniMap.setView(map.getCenter(), Math.max(map.getZoom() - 2, 0));
105 div.data('map', miniMap);
108 var label = $('<label>')
109 .text(layer.options.name)
112 item.on('click', function() {
113 layers.forEach(function(other) {
114 if (other === layer) {
117 map.removeLayer(other);
122 map.on('layeradd layerremove', function() {
123 item.toggleClass('active', map.hasLayer(layer));
131 var controlContainer = $('.leaflet-control-container .leaflet-top.leaflet-right');
133 if ($ui.is(':visible')) {
134 $(control.options.uiPane).hide();
135 controlContainer.css({paddingRight: '0'});
137 $(control.options.uiPane).show();
138 controlContainer.css({paddingRight: '230px'});
141 $ui.find('.base-layers .leaflet-container').each(function() {
142 $(this).data('map').invalidateSize();
146 return $container[0];