1 export default function (map) {
2 const content = $("#sidebar_content");
4 function initialize(path, id, skipMoveToNote) {
5 content.find("button[name]").on("click", function (e) {
7 const { url, method } = $(e.target).data(),
8 name = $(e.target).attr("name"),
9 data = new URLSearchParams();
10 content.find("button[name]").prop("disabled", true);
12 if (name !== "subscribe" && name !== "unsubscribe") {
13 const textarea = content.find("textarea");
14 if (textarea.length) {
15 data.set("text", textarea.val());
21 headers: { ...OSM.oauth },
25 if (response.ok) return response;
26 return response.text().then(text => {
27 throw new Error(text || `HTTP Error ${response.status} ${response.statusText}`);
30 .then(() => OSM.loadSidebarContent(path))
31 .then(() => initialize(path, id, false))
33 content.find("#comment-error")
35 .prop("hidden", false)
36 .get(0).scrollIntoView({ block: "nearest" });
41 content.find("textarea").on("input", function (e) {
42 updateButtons(e.target.form);
45 content.find("textarea").val("").trigger("input");
47 const data = $(".details").data();
50 const hashParams = OSM.parseHash();
54 latLng: L.latLng(data.coordinates.split(",")),
55 icon: OSM.noteMarkers[data.status]
57 if (!hashParams.center && !skipMoveToNote) {
58 const latLng = L.latLng(data.coordinates.split(","));
59 OSM.router.withoutMoveListener(function () {
60 map.setView(latLng, 15, { reset: true });
67 function updateButtons() {
68 const resolveButton = content.find("button[name='close']");
69 const commentButton = content.find("button[name='comment']");
71 content.find("button[name]").prop("disabled", false);
72 if (content.find("textarea").val() === "") {
73 resolveButton.text(resolveButton.data("defaultActionText"));
74 commentButton.prop("disabled", true);
76 resolveButton.text(resolveButton.data("commentActionText"));
82 OSM.loadSidebarContent(path).then(function () {
83 const data = $(".details").data();
85 const [lat, lng] = data.coordinates.split(",").map(parseFloat);
86 initialize(path, id, map.getBounds().contains({ lat, lng }));