1 OSM.NewNote = function (map) {
2 var noteLayer = map.noteLayer,
3 content = $("#sidebar_content"),
5 addNoteButton = $(".control-note .control-button"),
11 iconUrl: OSM.NEW_NOTE_MARKER,
16 iconUrl: OSM.OPEN_NOTE_MARKER,
21 iconUrl: OSM.CLOSED_NOTE_MARKER,
27 addNoteButton.on("click", function (e) {
31 if ($(this).hasClass("disabled")) return;
33 OSM.router.route("/note/new");
36 function createNote(location, text, callback) {
38 url: "/api/0.6/notes.json",
50 function addCreatedNoteMarker(feature) {
51 var marker = L.marker(feature.geometry.coordinates.reverse(), {
52 icon: noteIcons[feature.properties.status],
56 marker.id = feature.properties.id;
57 marker.addTo(noteLayer);
60 function addHalo(latlng) {
61 if (halo) map.removeLayer(halo);
63 halo = L.circleMarker(latlng, {
73 function removeHalo() {
74 if (halo) map.removeLayer(halo);
78 function addNewNoteMarker(latlng) {
79 if (newNoteMarker) map.removeLayer(newNoteMarker);
81 newNoteMarker = L.marker(latlng, {
87 newNoteMarker.on("dragstart dragend", function (a) {
89 if (a.type === "dragend") {
90 addHalo(newNoteMarker.getLatLng());
94 newNoteMarker.addTo(map);
95 addHalo(newNoteMarker.getLatLng());
97 newNoteMarker.on("dragend", function () {
98 content.find("textarea").focus();
102 function removeNewNoteMarker() {
104 if (newNoteMarker) map.removeLayer(newNoteMarker);
105 newNoteMarker = null;
108 function moveNewNotMarkerToClick(e) {
109 if (newNoteMarker) newNoteMarker.setLatLng(e.latlng);
110 if (halo) halo.setLatLng(e.latlng);
111 content.find("textarea").focus();
114 function updateControls() {
115 const zoomedOut = addNoteButton.hasClass("disabled");
116 const withoutText = content.find("textarea").val() === "";
118 content.find("#new-note-zoom-warning").prop("hidden", !zoomedOut);
119 content.find("input[type=submit]").prop("disabled", zoomedOut || withoutText);
120 if (newNoteMarker) newNoteMarker.setOpacity(zoomedOut ? 0.5 : 0.9);
123 page.pushstate = page.popstate = function (path) {
124 OSM.loadSidebarContent(path, function () {
129 page.load = function (path) {
130 addNoteButton.addClass("active");
132 map.addLayer(noteLayer);
134 const params = new URLSearchParams(path.substring(path.indexOf("?")));
137 if (params.has("lat") && params.has("lon")) {
138 markerLatlng = L.latLng(params.get("lat"), params.get("lon"));
140 markerLatlng = map.getCenter();
143 map.panInside(markerLatlng, {
147 addNewNoteMarker(markerLatlng);
149 content.find("textarea")
150 .on("input", updateControls)
153 content.find("input[type=submit]").on("click", function (e) {
154 const location = newNoteMarker.getLatLng().wrap();
155 const text = content.find("textarea").val();
158 $(this).prop("disabled", true);
159 newNoteMarker.options.draggable = false;
160 newNoteMarker.dragging.disable();
162 createNote(location, text, (feature) => {
163 if (typeof OSM.user === "undefined") {
164 var anonymousNotesCount = Number(Cookies.get("_osm_anonymous_notes_count")) || 0;
165 Cookies.set("_osm_anonymous_notes_count", anonymousNotesCount + 1, { secure: true, expires: 30, path: "/", samesite: "lax" });
167 content.find("textarea").val("");
168 addCreatedNoteMarker(feature);
169 OSM.router.route("/note/" + feature.properties.id);
173 map.on("click", moveNewNotMarkerToClick);
174 addNoteButton.on("disabled enabled", updateControls);
177 return map.getState();
180 page.unload = function () {
181 map.off("click", moveNewNotMarkerToClick);
182 addNoteButton.off("disabled enabled", updateControls);
183 removeNewNoteMarker();
184 addNoteButton.removeClass("active");