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");
21 function createNote(location, text, callback) {
22 fetch("/api/0.6/notes.json", {
24 headers: { ...OSM.oauth },
25 body: new URLSearchParams({
31 .then(response => response.json())
35 function addCreatedNoteMarker(feature) {
36 const marker = L.marker(feature.geometry.coordinates.reverse(), {
37 icon: OSM.noteMarkers[feature.properties.status],
41 marker.id = feature.properties.id;
42 marker.addTo(noteLayer);
45 function addHalo(latlng) {
46 if (halo) map.removeLayer(halo);
48 halo = L.circleMarker(latlng, {
58 function removeHalo() {
59 if (halo) map.removeLayer(halo);
63 function addNewNoteMarker(latlng) {
64 if (newNoteMarker) map.removeLayer(newNoteMarker);
66 newNoteMarker = L.marker(latlng, {
67 icon: OSM.noteMarkers.new,
72 newNoteMarker.on("dragstart dragend", function (a) {
74 if (a.type === "dragend") {
75 addHalo(newNoteMarker.getLatLng());
79 newNoteMarker.addTo(map);
80 addHalo(newNoteMarker.getLatLng());
82 newNoteMarker.on("dragend", function () {
83 content.find("textarea").trigger("focus");
87 function removeNewNoteMarker() {
89 if (newNoteMarker) map.removeLayer(newNoteMarker);
93 function moveNewNoteMarkerToClick(e) {
94 if (newNoteMarker) newNoteMarker.setLatLng(e.latlng);
95 if (halo) halo.setLatLng(e.latlng);
96 content.find("textarea").trigger("focus");
99 function updateControls() {
100 const zoomedOut = addNoteButton.hasClass("disabled");
101 const withoutText = content.find("textarea").val() === "";
103 content.find("#new-note-zoom-warning").prop("hidden", !zoomedOut);
104 content.find("input[type=submit]").prop("disabled", zoomedOut || withoutText);
105 if (newNoteMarker) newNoteMarker.setOpacity(zoomedOut ? 0.5 : 0.9);
108 page.pushstate = page.popstate = function (path) {
109 OSM.loadSidebarContent(path, function () {
114 page.load = function (path) {
115 control.addClass("active");
117 map.addLayer(noteLayer);
119 const params = new URLSearchParams(path.substring(path.indexOf("?")));
122 if (params.has("lat") && params.has("lon")) {
123 markerLatlng = L.latLng(params.get("lat"), params.get("lon"));
125 markerLatlng = map.getCenter();
128 map.panInside(markerLatlng, {
132 addNewNoteMarker(markerLatlng);
134 content.find("textarea")
135 .on("input", updateControls)
136 .attr("readonly", "readonly") // avoid virtual keyboard popping up on focus
138 .removeAttr("readonly");
140 content.find("input[type=submit]").on("click", function (e) {
141 const location = newNoteMarker.getLatLng().wrap();
142 const text = content.find("textarea").val();
145 $(this).prop("disabled", true);
146 newNoteMarker.options.draggable = false;
147 newNoteMarker.dragging.disable();
149 createNote(location, text, (feature) => {
150 if (typeof OSM.user === "undefined") {
151 const anonymousNotesCount = Number(OSM.cookies.get("_osm_anonymous_notes_count")) || 0;
152 OSM.cookies.set("_osm_anonymous_notes_count", anonymousNotesCount + 1, { expires: 30 });
154 content.find("textarea").val("");
155 addCreatedNoteMarker(feature);
156 OSM.router.route("/note/" + feature.properties.id);
160 map.on("click", moveNewNoteMarkerToClick);
161 addNoteButton.on("disabled enabled", updateControls);
164 return map.getState();
167 page.unload = function () {
168 map.off("click", moveNewNoteMarkerToClick);
169 addNoteButton.off("disabled enabled", updateControls);
170 removeNewNoteMarker();
171 control.removeClass("active");