1 export default function (map) {
2 const content = $("#sidebar_content"),
5 page.load = function (path, id) {
6 OSM.loadSidebarContent(path).then(function () {
7 const data = $(".details").data();
9 const [lat, lng] = data.coordinates.split(",").map(parseFloat);
10 initialize(path, id, map.getBounds().contains({ lat, lng }));
14 page.init = function (path, id) {
18 function initialize(path, id, skipMoveToNote) {
19 content.find("button[name]").on("click", function (e) {
21 const { url, method } = $(e.target).data(),
22 name = $(e.target).attr("name"),
23 data = new URLSearchParams();
24 content.find("button[name]").prop("disabled", true);
26 if (name !== "subscribe" && name !== "unsubscribe") {
27 const textarea = content.find("textarea");
28 if (textarea.length) {
29 data.set("text", textarea.val());
35 headers: { ...OSM.oauth },
39 if (response.ok) return response;
40 return response.text().then(text => {
41 throw new Error(text || `HTTP Error ${response.status} ${response.statusText}`);
44 .then(() => OSM.loadSidebarContent(path))
45 .then(() => initialize(path, id, false))
47 content.find("#comment-error")
49 .prop("hidden", false)
50 .get(0).scrollIntoView({ block: "nearest" });
55 content.find("textarea").on("input", function (e) {
56 updateButtons(e.target.form);
59 content.find("textarea").val("").trigger("input");
61 const data = $(".details").data();
64 const hashParams = OSM.parseHash();
68 latLng: L.latLng(data.coordinates.split(",")),
69 icon: OSM.noteMarkers[data.status]
71 if (!hashParams.center && !skipMoveToNote) {
72 const latLng = L.latLng(data.coordinates.split(","));
73 OSM.router.withoutMoveListener(function () {
74 map.setView(latLng, 15, { reset: true });
81 function updateButtons() {
82 const resolveButton = content.find("button[name='close']");
83 const commentButton = content.find("button[name='comment']");
85 content.find("button[name]").prop("disabled", false);
86 if (content.find("textarea").val() === "") {
87 resolveButton.text(resolveButton.data("defaultActionText"));
88 commentButton.prop("disabled", true);
90 resolveButton.text(resolveButton.data("commentActionText"));
94 page.unload = function () {