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 dragend", function (a) {
67 if (a.type === "dragend") {
68 addHalo(newNoteMarker.getLatLng());
72 newNoteMarker.addTo(map);
73 addHalo(newNoteMarker.getLatLng());
75 newNoteMarker.on("dragend", function () {
76 content.find("textarea").trigger("focus");
80 function removeNewNoteMarker() {
82 if (newNoteMarker) map.removeLayer(newNoteMarker);
86 function moveNewNoteMarkerToClick(e) {
87 if (newNoteMarker) newNoteMarker.setLatLng(e.latlng);
88 if (halo) halo.setLatLng(e.latlng);
89 content.find("textarea").trigger("focus");
92 function updateControls() {
93 const zoomedOut = addNoteButton.hasClass("disabled");
94 const withoutText = content.find("textarea").val() === "";
96 content.find("#new-note-zoom-warning").prop("hidden", !zoomedOut);
97 content.find("input[type=submit]").prop("disabled", zoomedOut || withoutText);
98 if (newNoteMarker) newNoteMarker.setOpacity(zoomedOut ? 0.5 : 0.9);
101 page.pushstate = page.popstate = function (path) {
102 OSM.loadSidebarContent(path, function () {
107 page.load = function (path) {
108 control.addClass("active");
110 map.addLayer(noteLayer);
112 const params = new URLSearchParams(path.substring(path.indexOf("?")));
115 if (params.has("lat") && params.has("lon")) {
116 markerLatlng = { lat: params.get("lat"), lng: params.get("lon") };
118 markerLatlng = map.getCenter();
121 map.panInside(markerLatlng, {
125 addNewNoteMarker(markerLatlng);
127 content.find("textarea")
128 .on("input", updateControls)
129 .attr("readonly", "readonly") // avoid virtual keyboard popping up on focus
131 .removeAttr("readonly");
133 content.find("input[type=submit]").on("click", function (e) {
134 const location = newNoteMarker.getLatLng().wrap();
135 const text = content.find("textarea").val();
137 errorPanel = content.find(".new-note-error");
138 errorPanel.addClass("d-none");
139 errorPanelDetail = errorPanel.find(".new-note-error-detail");
142 $(this).prop("disabled", true);
143 newNoteMarker.options.draggable = false;
144 newNoteMarker.dragging.disable();
146 createNote(location, text)
148 if (typeof OSM.user === "undefined") {
149 const anonymousNotesCount = Number(OSM.cookies.get("_osm_anonymous_notes_count")) || 0;
150 OSM.cookies.set("_osm_anonymous_notes_count", anonymousNotesCount + 1, { expires: 14 });
152 content.find("textarea").val("");
153 addCreatedNoteMarker(feature);
154 OSM.router.route("/note/" + feature.properties.id);
157 errorPanel.removeClass("d-none");
158 errorPanelDetail.text(err.message || err);
163 map.on("click", moveNewNoteMarkerToClick);
164 addNoteButton.on("disabled enabled", updateControls);
167 return map.getState();
170 page.unload = function () {
171 map.off("click", moveNewNoteMarkerToClick);
172 addNoteButton.off("disabled enabled", updateControls);
173 removeNewNoteMarker();
174 control.removeClass("active");