1 OSM.initializations.push(function () {
2 $(".control-note .control-button").on("click", function (e) {
6 if ($(this).hasClass("disabled")) return;
8 OSM.router.route("/note/new");
12 OSM.NewNote = function (map) {
13 const noteLayer = map.noteLayer,
14 content = $("#sidebar_content"),
16 control = $(".control-note"),
17 addNoteButton = control.find(".control-button");
23 function createNote(location, text) {
24 return fetch("/api/0.6/notes.json", {
26 headers: { ...OSM.oauth },
27 body: new URLSearchParams({
34 if (resp.ok) return resp.json();
35 throw new Error(`Got response with status ${resp.status} ${resp.statusText}`);
39 function addCreatedNoteMarker(feature) {
40 const marker = L.marker(feature.geometry.coordinates.reverse(), {
41 icon: OSM.noteMarkers[feature.properties.status],
45 marker.id = feature.properties.id;
46 marker.addTo(noteLayer);
49 function addHalo(latlng) {
50 if (halo) map.removeLayer(halo);
52 halo = L.circleMarker(latlng, {
62 function removeHalo() {
63 if (halo) map.removeLayer(halo);
67 function addNewNoteMarker(latlng) {
68 if (newNoteMarker) map.removeLayer(newNoteMarker);
70 newNoteMarker = L.marker(latlng, {
71 icon: OSM.noteMarkers.new,
76 newNoteMarker.on("dragstart dragend", function (a) {
78 if (a.type === "dragend") {
79 addHalo(newNoteMarker.getLatLng());
83 newNoteMarker.addTo(map);
84 addHalo(newNoteMarker.getLatLng());
86 newNoteMarker.on("dragend", function () {
87 content.find("textarea").trigger("focus");
91 function removeNewNoteMarker() {
93 if (newNoteMarker) map.removeLayer(newNoteMarker);
97 function moveNewNoteMarkerToClick(e) {
98 if (newNoteMarker) newNoteMarker.setLatLng(e.latlng);
99 if (halo) halo.setLatLng(e.latlng);
100 content.find("textarea").trigger("focus");
103 function updateControls() {
104 const zoomedOut = addNoteButton.hasClass("disabled");
105 const withoutText = content.find("textarea").val() === "";
107 content.find("#new-note-zoom-warning").prop("hidden", !zoomedOut);
108 content.find("input[type=submit]").prop("disabled", zoomedOut || withoutText);
109 if (newNoteMarker) newNoteMarker.setOpacity(zoomedOut ? 0.5 : 0.9);
112 page.pushstate = page.popstate = function (path) {
113 OSM.loadSidebarContent(path, function () {
118 page.load = function (path) {
119 control.addClass("active");
121 map.addLayer(noteLayer);
123 const params = new URLSearchParams(path.substring(path.indexOf("?")));
126 if (params.has("lat") && params.has("lon")) {
127 markerLatlng = { lat: params.get("lat"), lng: params.get("lon") };
129 markerLatlng = map.getCenter();
132 map.panInside(markerLatlng, {
136 addNewNoteMarker(markerLatlng);
138 content.find("textarea")
139 .on("input", updateControls)
140 .attr("readonly", "readonly") // avoid virtual keyboard popping up on focus
142 .removeAttr("readonly");
144 content.find("input[type=submit]").on("click", function (e) {
145 const location = newNoteMarker.getLatLng().wrap();
146 const text = content.find("textarea").val();
148 errorPanel = content.find(".new-note-error");
149 errorPanel.addClass("d-none");
150 errorPanelDetail = errorPanel.find(".new-note-error-detail");
153 $(this).prop("disabled", true);
154 newNoteMarker.options.draggable = false;
155 newNoteMarker.dragging.disable();
157 createNote(location, text)
159 if (typeof OSM.user === "undefined") {
160 const anonymousNotesCount = Number(OSM.cookies.get("_osm_anonymous_notes_count")) || 0;
161 OSM.cookies.set("_osm_anonymous_notes_count", anonymousNotesCount + 1, { expires: 14 });
163 content.find("textarea").val("");
164 addCreatedNoteMarker(feature);
165 OSM.router.route("/note/" + feature.properties.id);
168 errorPanel.removeClass("d-none");
169 errorPanelDetail.text(err.message || err);
174 map.on("click", moveNewNoteMarkerToClick);
175 addNoteButton.on("disabled enabled", updateControls);
178 return map.getState();
181 page.unload = function () {
182 map.off("click", moveNewNoteMarkerToClick);
183 addNoteButton.off("disabled enabled", updateControls);
184 removeNewNoteMarker();
185 control.removeClass("active");