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", I18n.t("javascripts.map.layers.title"))
14 .html("<span class=\"icon layers\"></span>")
16 .appendTo($container);
19 .attr("class", "layers-ui");
22 .attr("class", "sidebar_heading")
25 $("<button type='button' class='btn-close float-end mt-1'>")
26 .attr("aria-label", I18n.t("javascripts.close"))
27 .bind("click", toggle))
30 .text(I18n.t("javascripts.map.layers.header")));
32 var baseSection = $("<div>")
33 .attr("class", "section base-layers")
36 var baseLayers = $("<ul class='list-unstyled'>")
37 .appendTo(baseSection);
39 layers.forEach(function (layer) {
41 .appendTo(baseLayers);
43 if (map.hasLayer(layer)) {
44 item.addClass("active");
50 map.whenReady(function () {
51 var miniMap = L.map(div[0], { attributionControl: false, zoomControl: false, keyboard: false })
52 .addLayer(new layer.constructor({ apikey: layer.options.apikey }));
54 miniMap.dragging.disable();
55 miniMap.touchZoom.disable();
56 miniMap.doubleClickZoom.disable();
57 miniMap.scrollWheelZoom.disable();
64 miniMap.invalidateSize();
65 setView({ animate: false });
66 map.on("moveend", moved);
70 map.off("moveend", moved);
77 function setView(options) {
78 miniMap.setView(map.getCenter(), Math.max(map.getZoom() - 2, 0), options);
82 var label = $("<label>")
85 var input = $("<input>")
86 .attr("type", "radio")
87 .prop("checked", map.hasLayer(layer))
90 label.append(layer.options.name);
92 item.on("click", function () {
93 layers.forEach(function (other) {
94 if (other === layer) {
97 map.removeLayer(other);
100 map.fire("baselayerchange", { layer: layer });
103 item.on("dblclick", toggle);
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 = $("<div>")
113 .attr("class", "section overlay-layers")
117 .text(I18n.t("javascripts.map.layers.overlays"))
118 .attr("class", "text-muted")
119 .appendTo(overlaySection);
121 var overlays = $("<ul class='list-unstyled form-check'>")
122 .appendTo(overlaySection);
124 var addOverlay = function (layer, name, maxArea) {
131 var label = $("<label>")
132 .attr("class", "form-check-label")
135 var checked = map.hasLayer(layer);
137 var input = $("<input>")
138 .attr("type", "checkbox")
139 .attr("class", "form-check-input")
140 .prop("checked", checked)
143 label.append(I18n.t("javascripts.map.layers." + name));
145 input.on("change", function () {
146 checked = input.is(":checked");
150 map.removeLayer(layer);
152 map.fire("overlaylayerchange", { layer: layer });
155 map.on("layeradd layerremove", function () {
156 input.prop("checked", map.hasLayer(layer));
159 map.on("zoomend", function () {
160 var disabled = map.getBounds().getSize() >= maxArea;
161 $(input).prop("disabled", disabled);
163 if (disabled && $(input).is(":checked")) {
164 $(input).prop("checked", false)
167 } else if (!disabled && !$(input).is(":checked") && checked) {
168 $(input).prop("checked", true)
172 $(item).attr("class", disabled ? "disabled" : "");
173 item.attr("data-bs-original-title", disabled ?
174 I18n.t("javascripts.site.map_" + name + "_zoom_in_tooltip") : "");
178 addOverlay(map.noteLayer, "notes", OSM.MAX_NOTE_REQUEST_AREA);
179 addOverlay(map.dataLayer, "data", OSM.MAX_REQUEST_AREA);
180 addOverlay(map.gpsLayer, "gps", Number.POSITIVE_INFINITY);
183 options.sidebar.addPane($ui);
188 options.sidebar.togglePane($ui, button);
189 $(".leaflet-control .control-button").tooltip("hide");
192 return $container[0];