]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/leaflet.share.js
Bump eslint from 8.53.0 to 8.54.0
[rails.git] / app / assets / javascripts / leaflet.share.js
1 L.OSM.share = function (options) {
2   var control = L.OSM.sidebarPane(options, "share", "javascripts.share.title", "javascripts.share.title"),
3       marker = L.marker([0, 0], { draggable: true }),
4       locationFilter = new L.LocationFilter({
5         enableButton: false,
6         adjustButton: false
7       });
8
9   control.onAddPane = function (map, button, $ui) {
10     // Link / Embed
11
12     var $linkSection = $("<div>")
13       .attr("class", "section share-link")
14       .appendTo($ui);
15
16     $("<h4>")
17       .text(I18n.t("javascripts.share.link"))
18       .appendTo($linkSection);
19
20     var $form = $("<form>")
21       .appendTo($linkSection);
22
23     $("<div>")
24       .attr("class", "form-check mb-3")
25       .appendTo($form)
26       .append($("<label>")
27         .attr("for", "link_marker")
28         .attr("class", "form-check-label")
29         .text(I18n.t("javascripts.share.include_marker")))
30       .append($("<input>")
31         .attr("id", "link_marker")
32         .attr("type", "checkbox")
33         .attr("class", "form-check-input")
34         .bind("change", toggleMarker));
35
36     $("<div class='btn-group btn-group-sm mb-2'>")
37       .appendTo($form)
38       .append($("<a class='btn btn-primary'>")
39         .addClass("active")
40         .attr("for", "long_input")
41         .attr("id", "long_link")
42         .text(I18n.t("javascripts.share.long_link")))
43       .append($("<a class='btn btn-primary'>")
44         .attr("for", "short_input")
45         .attr("id", "short_link")
46         .text(I18n.t("javascripts.share.short_link")))
47       .append($("<a class='btn btn-primary'>")
48         .attr("for", "embed_html")
49         .attr("href", "#")
50         .text(I18n.t("javascripts.share.embed")))
51       .on("click", "a", function (e) {
52         e.preventDefault();
53         var id = "#" + $(this).attr("for");
54         $(this).siblings("a")
55           .removeClass("active");
56         $(this).addClass("active");
57         $linkSection.find(".share-tab")
58           .hide();
59         $linkSection.find(".share-tab:has(" + id + ")")
60           .show()
61           .find("input, textarea")
62           .select();
63       });
64
65     $("<div>")
66       .attr("class", "share-tab")
67       .appendTo($form)
68       .append($("<input>")
69         .attr("id", "long_input")
70         .attr("type", "text")
71         .attr("class", "form-control form-control-sm font-monospace")
72         .attr("readonly", true)
73         .on("click", select));
74
75     $("<div>")
76       .attr("class", "share-tab")
77       .hide()
78       .appendTo($form)
79       .append($("<input>")
80         .attr("id", "short_input")
81         .attr("type", "text")
82         .attr("class", "form-control form-control-sm font-monospace")
83         .attr("readonly", true)
84         .on("click", select));
85
86     $("<div>")
87       .attr("class", "share-tab")
88       .hide()
89       .appendTo($form)
90       .append(
91         $("<textarea>")
92           .attr("id", "embed_html")
93           .attr("class", "form-control form-control-sm font-monospace")
94           .attr("readonly", true)
95           .on("click", select))
96       .append(
97         $("<p>")
98           .attr("class", "text-muted")
99           .text(I18n.t("javascripts.share.paste_html")));
100
101     // Geo URI
102
103     var $geoUriSection = $("<div>")
104       .attr("class", "section share-geo-uri")
105       .appendTo($ui);
106
107     $("<h4>")
108       .text(I18n.t("javascripts.share.geo_uri"))
109       .appendTo($geoUriSection);
110
111     $("<div>")
112       .appendTo($geoUriSection)
113       .append($("<a>")
114         .attr("id", "geo_uri"));
115
116     // Image
117
118     var $imageSection = $("<div>")
119       .attr("class", "section share-image")
120       .appendTo($ui);
121
122     $("<h4>")
123       .text(I18n.t("javascripts.share.image"))
124       .appendTo($imageSection);
125
126     $("<div>")
127       .attr("id", "export-warning")
128       .attr("class", "text-muted")
129       .text(I18n.t("javascripts.share.only_standard_layer"))
130       .appendTo($imageSection);
131
132     $form = $("<form>")
133       .attr("id", "export-image")
134       .attr("action", "/export/finish")
135       .attr("method", "post")
136       .appendTo($imageSection);
137
138     $("<div>")
139       .appendTo($form)
140       .attr("class", "row mb-3")
141       .append($("<label>")
142         .attr("for", "mapnik_format")
143         .attr("class", "col-auto col-form-label")
144         .text(I18n.t("javascripts.share.format")))
145       .append($("<div>")
146         .attr("class", "col-auto")
147         .append($("<select>")
148           .attr("name", "mapnik_format")
149           .attr("id", "mapnik_format")
150           .attr("class", "form-select w-auto")
151           .append($("<option>").val("png").text("PNG").prop("selected", true))
152           .append($("<option>").val("jpeg").text("JPEG"))
153           .append($("<option>").val("svg").text("SVG"))
154           .append($("<option>").val("pdf").text("PDF"))));
155
156     $("<div>")
157       .appendTo($form)
158       .attr("class", "row mb-3")
159       .append($("<label>")
160         .attr("for", "mapnik_scale")
161         .attr("class", "col-auto col-form-label")
162         .text(I18n.t("javascripts.share.scale")))
163       .append($("<div>")
164         .attr("class", "col-auto")
165         .append($("<div>")
166           .attr("class", "input-group flex-nowrap")
167           .append($("<span>")
168             .attr("class", "input-group-text")
169             .text("1 : "))
170           .append($("<input>")
171             .attr("name", "mapnik_scale")
172             .attr("id", "mapnik_scale")
173             .attr("type", "text")
174             .attr("class", "form-control")
175             .on("change", update))));
176
177     $("<div>")
178       .attr("class", "row mb-3")
179       .appendTo($form)
180       .append($("<div>")
181         .attr("class", "col-auto")
182         .append($("<div>")
183           .attr("class", "form-check")
184           .append($("<label>")
185             .attr("for", "image_filter")
186             .attr("class", "form-check-label")
187             .text(I18n.t("javascripts.share.custom_dimensions")))
188           .append($("<input>")
189             .attr("id", "image_filter")
190             .attr("type", "checkbox")
191             .attr("class", "form-check-input")
192             .bind("change", toggleFilter))));
193
194     ["minlon", "minlat", "maxlon", "maxlat"].forEach(function (name) {
195       $("<input>")
196         .attr("id", "mapnik_" + name)
197         .attr("name", name)
198         .attr("type", "hidden")
199         .appendTo($form);
200     });
201
202     $("<input>")
203       .attr("name", "format")
204       .attr("value", "mapnik")
205       .attr("type", "hidden")
206       .appendTo($form);
207
208     var csrf_param = $("meta[name=csrf-param]").attr("content"),
209         csrf_token = $("meta[name=csrf-token]").attr("content");
210
211     $("<input>")
212       .attr("name", csrf_param)
213       .attr("value", csrf_token)
214       .attr("type", "hidden")
215       .appendTo($form);
216
217     var args = {
218       width: "<span id=\"mapnik_image_width\"></span>",
219       height: "<span id=\"mapnik_image_height\"></span>"
220     };
221
222     $("<p>")
223       .attr("class", "text-muted")
224       .html(I18n.t("javascripts.share.image_dimensions", args))
225       .appendTo($form);
226
227     $("<input>")
228       .attr("type", "submit")
229       .attr("class", "btn btn-primary")
230       .attr("value", I18n.t("javascripts.share.download"))
231       .appendTo($form);
232
233     locationFilter
234       .on("change", update)
235       .addTo(map);
236
237     marker.on("dragend", movedMarker);
238     map.on("move", movedMap);
239     map.on("moveend layeradd layerremove", update);
240
241     $ui
242       .on("show", shown)
243       .on("hide", hidden);
244
245     function shown() {
246       $("#mapnik_scale").val(getScale());
247       update();
248     }
249
250     function hidden() {
251       map.removeLayer(marker);
252       map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
253       locationFilter.disable();
254       update();
255     }
256
257     function toggleMarker() {
258       if ($(this).is(":checked")) {
259         marker.setLatLng(map.getCenter());
260         map.addLayer(marker);
261         map.options.scrollWheelZoom = map.options.doubleClickZoom = "center";
262       } else {
263         map.removeLayer(marker);
264         map.options.scrollWheelZoom = map.options.doubleClickZoom = true;
265       }
266       update();
267     }
268
269     function toggleFilter() {
270       if ($(this).is(":checked")) {
271         locationFilter.setBounds(map.getBounds().pad(-0.2));
272         locationFilter.enable();
273       } else {
274         locationFilter.disable();
275       }
276       update();
277     }
278
279     function movedMap() {
280       marker.setLatLng(map.getCenter());
281       update();
282     }
283
284     function movedMarker() {
285       if (map.hasLayer(marker)) {
286         map.off("move", movedMap);
287         map.on("moveend", updateOnce);
288         map.panTo(marker.getLatLng());
289       }
290     }
291
292     function updateOnce() {
293       map.off("moveend", updateOnce);
294       map.on("move", movedMap);
295       update();
296     }
297
298     function escapeHTML(string) {
299       var htmlEscapes = {
300         "&": "&amp;",
301         "<": "&lt;",
302         ">": "&gt;",
303         "\"": "&quot;",
304         "'": "&#x27;"
305       };
306       return string === null ? "" : String(string).replace(/[&<>"']/g, function (match) {
307         return htmlEscapes[match];
308       });
309     }
310
311     function update() {
312       var bounds = map.getBounds();
313
314       $("#link_marker")
315         .prop("checked", map.hasLayer(marker));
316
317       $("#image_filter")
318         .prop("checked", locationFilter.isEnabled());
319
320       // Link / Embed
321
322       $("#short_input").val(map.getShortUrl(marker));
323       $("#long_input").val(map.getUrl(marker));
324       $("#short_link").attr("href", map.getShortUrl(marker));
325       $("#long_link").attr("href", map.getUrl(marker));
326
327       var params = {
328         bbox: bounds.toBBoxString(),
329         layer: map.getMapBaseLayerId()
330       };
331
332       if (map.hasLayer(marker)) {
333         var latLng = marker.getLatLng().wrap();
334         params.marker = latLng.lat + "," + latLng.lng;
335       }
336
337       $("#embed_html").val(
338         "<iframe width=\"425\" height=\"350\" src=\"" +
339           escapeHTML(OSM.SERVER_PROTOCOL + "://" + OSM.SERVER_URL + "/export/embed.html?" + $.param(params)) +
340           "\" style=\"border: 1px solid black\"></iframe><br/>" +
341           "<small><a href=\"" + escapeHTML(map.getUrl(marker)) + "\">" +
342           escapeHTML(I18n.t("javascripts.share.view_larger_map")) + "</a></small>");
343
344       // Geo URI
345
346       $("#geo_uri")
347         .attr("href", map.getGeoUri(marker))
348         .html(map.getGeoUri(marker));
349
350       // Image
351
352       if (locationFilter.isEnabled()) {
353         bounds = locationFilter.getBounds();
354       }
355
356       var scale = $("#mapnik_scale").val(),
357           size = L.bounds(L.CRS.EPSG3857.project(bounds.getSouthWest()),
358                           L.CRS.EPSG3857.project(bounds.getNorthEast())).getSize(),
359           maxScale = Math.floor(Math.sqrt(size.x * size.y / 0.3136));
360
361       $("#mapnik_minlon").val(bounds.getWest());
362       $("#mapnik_minlat").val(bounds.getSouth());
363       $("#mapnik_maxlon").val(bounds.getEast());
364       $("#mapnik_maxlat").val(bounds.getNorth());
365
366       if (scale < maxScale) {
367         scale = roundScale(maxScale);
368         $("#mapnik_scale").val(scale);
369       }
370
371       $("#mapnik_image_width").text(Math.round(size.x / scale / 0.00028));
372       $("#mapnik_image_height").text(Math.round(size.y / scale / 0.00028));
373
374       if (map.getMapBaseLayerId() === "mapnik") {
375         $("#export-image").show();
376         $("#export-warning").hide();
377       } else {
378         $("#export-image").hide();
379         $("#export-warning").show();
380       }
381     }
382
383     function select() {
384       $(this).select();
385     }
386
387     function getScale() {
388       var bounds = map.getBounds(),
389           centerLat = bounds.getCenter().lat,
390           halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
391           meters = halfWorldMeters * (bounds.getEast() - bounds.getWest()) / 180,
392           pixelsPerMeter = map.getSize().x / meters,
393           metersPerPixel = 1 / (92 * 39.3701);
394       return Math.round(1 / (pixelsPerMeter * metersPerPixel));
395     }
396
397     function roundScale(scale) {
398       var precision = 5 * Math.pow(10, Math.floor(Math.LOG10E * Math.log(scale)) - 2);
399       return precision * Math.ceil(scale / precision);
400     }
401   };
402
403   return control;
404 };