1 export default function (map) {
2 const noteLayer = map.noteLayer,
3 content = $("#sidebar_content"),
5 control = $(".control-note"),
6 addNoteButton = control.find(".control-button");
12 function createNote(location, text) {
13 return fetch("/api/0.6/notes.json", {
15 headers: { ...OSM.oauth },
16 body: new URLSearchParams({
23 if (resp.ok) return resp.json();
24 throw new Error(`Got response with status ${resp.status} ${resp.statusText}`);
28 function addCreatedNoteMarker(feature) {
29 const marker = L.marker(feature.geometry.coordinates.reverse(), {
30 icon: OSM.noteMarkers[feature.properties.status],
34 marker.id = feature.properties.id;
35 marker.addTo(noteLayer);
38 function addHalo(latlng) {
39 if (halo) map.removeLayer(halo);
41 halo = L.circleMarker(latlng, {
51 function removeHalo() {
52 if (halo) map.removeLayer(halo);
56 function addNewNoteMarker(latlng) {
57 if (newNoteMarker) map.removeLayer(newNoteMarker);
59 newNoteMarker = L.marker(latlng, {
60 icon: OSM.noteMarkers.new,
65 newNoteMarker.on("dragstart", removeHalo);
66 newNoteMarker.on("dragend", function () {
68 addHalo(newNoteMarker.getLatLng());
71 newNoteMarker.addTo(map);
72 addHalo(newNoteMarker.getLatLng());
74 newNoteMarker.on("dragend", function () {
75 content.find("textarea").trigger("focus");
79 function removeNewNoteMarker() {
81 if (newNoteMarker) map.removeLayer(newNoteMarker);
85 function moveNewNoteMarkerToClick(e) {
86 if (newNoteMarker) newNoteMarker.setLatLng(e.latlng);
87 if (halo) halo.setLatLng(e.latlng);
88 content.find("textarea").trigger("focus");
91 function updateControls() {
92 const zoomedOut = addNoteButton.hasClass("disabled");
93 const withoutText = content.find("textarea").val() === "";
95 content.find("#new-note-zoom-warning").prop("hidden", !zoomedOut);
96 content.find("input[type=submit]").prop("disabled", zoomedOut || withoutText);
97 if (newNoteMarker) newNoteMarker.setOpacity(zoomedOut ? 0.5 : 0.9);
100 page.pushstate = page.popstate = function (path) {
101 OSM.loadSidebarContent(path, function () {
106 page.load = function (path) {
107 control.addClass("active");
109 map.addLayer(noteLayer);
111 const params = new URLSearchParams(path.substring(path.indexOf("?")));
114 if (params.has("lat") && params.has("lon")) {
115 markerLatlng = { lat: params.get("lat"), lng: params.get("lon") };
117 markerLatlng = map.getCenter();
120 map.panInside(markerLatlng, {
124 addNewNoteMarker(markerLatlng);
126 content.find("textarea")
127 .on("input", updateControls)
128 .attr("readonly", "readonly") // avoid virtual keyboard popping up on focus
130 .removeAttr("readonly");
132 content.find("input[type=submit]").on("click", function (e) {
133 const location = newNoteMarker.getLatLng().wrap();
134 const text = content.find("textarea").val();
136 errorPanel = content.find(".new-note-error");
137 errorPanel.addClass("d-none");
138 errorPanelDetail = errorPanel.find(".new-note-error-detail");
141 $(this).prop("disabled", true);
142 newNoteMarker.options.draggable = false;
143 newNoteMarker.dragging.disable();
145 createNote(location, text)
147 if (typeof OSM.user === "undefined") {
148 const anonymousNotesCount = Number(OSM.cookies.get("_osm_anonymous_notes_count")) || 0;
149 OSM.cookies.set("_osm_anonymous_notes_count", anonymousNotesCount + 1, { expires: 14 });
151 content.find("textarea").val("");
152 addCreatedNoteMarker(feature);
153 OSM.router.route("/note/" + feature.properties.id);
156 errorPanel.removeClass("d-none");
157 errorPanelDetail.text(err.message || err);
162 map.on("click", moveNewNoteMarkerToClick);
163 addNoteButton.on("disabled enabled", updateControls);
166 return map.getState();
169 page.unload = function () {
170 map.off("click", moveNewNoteMarkerToClick);
171 addNoteButton.off("disabled enabled", updateControls);
172 removeNewNoteMarker();
173 control.removeClass("active");