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');
22 .attr('class', 'sidebar_heading')
26 .text(I18n.t('javascripts.close'))
27 .attr('class', 'sidebar_close')
29 .bind('click', toggle))
32 .text(I18n.t('javascripts.map.layers.header')));
34 var baseSection = $('<section>')
35 .addClass('base-layers')
39 .text(I18n.t('javascripts.map.layers.base'))
40 .appendTo(baseSection);
43 .appendTo(baseSection);
45 layers.forEach(function(layer) {
49 if (map.hasLayer(layer)) {
50 item.addClass('active');
56 map.whenReady(function() {
57 var miniMap = L.map(div[0], {attributionControl: false, zoomControl: false})
58 .addLayer(new layer.constructor());
60 miniMap.dragging.disable();
61 miniMap.touchZoom.disable();
62 miniMap.doubleClickZoom.disable();
63 miniMap.scrollWheelZoom.disable();
70 miniMap.invalidateSize();
72 map.on('moveend', setView);
76 map.off('moveend', setView);
80 miniMap.setView(map.getCenter(), Math.max(map.getZoom() - 2, 0));
84 var label = $('<label>')
87 var input = $('<input>')
88 .attr('type', 'radio')
89 .prop('checked', map.hasLayer(layer))
92 label.append(layer.options.name);
94 item.on('click', function() {
95 layers.forEach(function(other) {
96 if (other === layer) {
99 map.removeLayer(other);
102 map.fire('baselayerchange', {layer: layer});
105 map.on('layeradd layerremove', function() {
106 item.toggleClass('active', map.hasLayer(layer));
107 input.prop('checked', map.hasLayer(layer));
111 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
112 var overlaySection = $('<section>')
113 .addClass('overlay-layers')
117 .text(I18n.t('javascripts.map.layers.overlays'))
118 .appendTo(overlaySection);
121 .appendTo(overlaySection);
123 function addOverlay(layer, name) {
127 var label = $('<label>')
130 var input = $('<input>')
131 .attr('type', 'checkbox')
132 .prop('checked', map.hasLayer(layer))
137 input.on('change', function() {
138 if (input.is(':checked')) {
141 map.removeLayer(layer);
145 map.on('layeradd layerremove', function() {
146 input.prop('checked', map.hasLayer(layer));
150 addOverlay(map.noteLayer, I18n.t('javascripts.map.layers.notes'));
151 addOverlay(map.dataLayer, I18n.t('javascripts.map.layers.data'));
154 options.sidebar.addPane($ui);
159 options.sidebar.togglePane($ui);
162 return $container[0];