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")
26 .text(I18n.t("javascripts.close"))
27 .attr("class", "icon close")
28 .bind("click", toggle))
31 .text(I18n.t("javascripts.map.layers.header")));
33 var baseSection = $("<div>")
34 .attr("class", "section base-layers")
37 var baseLayers = $("<ul class='list-unstyled'>")
38 .appendTo(baseSection);
40 layers.forEach(function (layer) {
42 .appendTo(baseLayers);
44 if (map.hasLayer(layer)) {
45 item.addClass("active");
51 map.whenReady(function () {
52 var miniMap = L.map(div[0], { attributionControl: false, zoomControl: false, keyboard: false })
53 .addLayer(new layer.constructor({ apikey: layer.options.apikey }));
55 miniMap.dragging.disable();
56 miniMap.touchZoom.disable();
57 miniMap.doubleClickZoom.disable();
58 miniMap.scrollWheelZoom.disable();
65 miniMap.invalidateSize();
66 setView({ animate: false });
67 map.on("moveend", moved);
71 map.off("moveend", moved);
78 function setView(options) {
79 miniMap.setView(map.getCenter(), Math.max(map.getZoom() - 2, 0), options);
83 var label = $("<label>")
86 var input = $("<input>")
87 .attr("type", "radio")
88 .prop("checked", map.hasLayer(layer))
91 label.append(layer.options.name);
93 item.on("click", function () {
94 layers.forEach(function (other) {
95 if (other === layer) {
98 map.removeLayer(other);
101 map.fire("baselayerchange", { layer: layer });
104 item.on("dblclick", toggle);
106 map.on("layeradd layerremove", function () {
107 item.toggleClass("active", map.hasLayer(layer));
108 input.prop("checked", map.hasLayer(layer));
112 if (OSM.STATUS !== "api_offline" && OSM.STATUS !== "database_offline") {
113 var overlaySection = $("<div>")
114 .attr("class", "section overlay-layers")
118 .text(I18n.t("javascripts.map.layers.overlays"))
119 .attr("class", "text-muted")
120 .appendTo(overlaySection);
122 var overlays = $("<ul class='list-unstyled form-check'>")
123 .appendTo(overlaySection);
125 var addOverlay = function (layer, name, maxArea) {
132 var label = $("<label>")
133 .attr("class", "form-check-label")
136 var checked = map.hasLayer(layer);
138 var input = $("<input>")
139 .attr("type", "checkbox")
140 .attr("class", "form-check-input")
141 .prop("checked", checked)
144 label.append(I18n.t("javascripts.map.layers." + name));
146 input.on("change", function () {
147 checked = input.is(":checked");
151 map.removeLayer(layer);
153 map.fire("overlaylayerchange", { layer: layer });
156 map.on("layeradd layerremove", function () {
157 input.prop("checked", map.hasLayer(layer));
160 map.on("zoomend", function () {
161 var disabled = map.getBounds().getSize() >= maxArea;
162 $(input).prop("disabled", disabled);
164 if (disabled && $(input).is(":checked")) {
165 $(input).prop("checked", false)
168 } else if (!disabled && !$(input).is(":checked") && checked) {
169 $(input).prop("checked", true)
173 $(item).attr("class", disabled ? "disabled" : "");
174 item.attr("data-original-title", disabled ?
175 I18n.t("javascripts.site.map_" + name + "_zoom_in_tooltip") : "");
179 addOverlay(map.noteLayer, "notes", OSM.MAX_NOTE_REQUEST_AREA);
180 addOverlay(map.dataLayer, "data", OSM.MAX_REQUEST_AREA);
181 addOverlay(map.gpsLayer, "gps", Number.POSITIVE_INFINITY);
184 options.sidebar.addPane($ui);
189 options.sidebar.togglePane($ui, button);
190 $(".leaflet-control .control-button").tooltip("hide");
193 return $container[0];