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");
11 OSM.NewNote = function (map) {
12 const noteLayer = map.noteLayer,
13 content = $("#sidebar_content"),
15 control = $(".control-note"),
16 addNoteButton = control.find(".control-button");
20 function createNote(location, text, callback) {
21 fetch("/api/0.6/notes.json", {
23 headers: { ...OSM.oauth },
24 body: new URLSearchParams({
30 .then(response => response.json())
34 function addCreatedNoteMarker(feature) {
35 const marker = L.marker(feature.geometry.coordinates.reverse(), {
36 icon: OSM.noteMarkers[feature.properties.status],
40 marker.id = feature.properties.id;
41 marker.addTo(noteLayer);
44 function addHalo(latlng) {
45 if (halo) map.removeLayer(halo);
47 halo = L.circleMarker(latlng, {
57 function removeHalo() {
58 if (halo) map.removeLayer(halo);
62 function addNewNoteMarker(latlng) {
63 if (newNoteMarker) map.removeLayer(newNoteMarker);
65 newNoteMarker = L.marker(latlng, {
66 icon: OSM.noteMarkers.new,
71 newNoteMarker.on("dragstart dragend", function (a) {
73 if (a.type === "dragend") {
74 addHalo(newNoteMarker.getLatLng());
78 newNoteMarker.addTo(map);
79 addHalo(newNoteMarker.getLatLng());
81 newNoteMarker.on("dragend", function () {
82 content.find("textarea").trigger("focus");
86 function removeNewNoteMarker() {
88 if (newNoteMarker) map.removeLayer(newNoteMarker);
92 function moveNewNoteMarkerToClick(e) {
93 if (newNoteMarker) newNoteMarker.setLatLng(e.latlng);
94 if (halo) halo.setLatLng(e.latlng);
95 content.find("textarea").trigger("focus");
98 function updateControls() {
99 const zoomedOut = addNoteButton.hasClass("disabled");
100 const withoutText = content.find("textarea").val() === "";
102 content.find("#new-note-zoom-warning").prop("hidden", !zoomedOut);
103 content.find("input[type=submit]").prop("disabled", zoomedOut || withoutText);
104 if (newNoteMarker) newNoteMarker.setOpacity(zoomedOut ? 0.5 : 0.9);
107 page.pushstate = page.popstate = function (path) {
108 OSM.loadSidebarContent(path, function () {
113 page.load = function (path) {
114 control.addClass("active");
116 map.addLayer(noteLayer);
118 const params = new URLSearchParams(path.substring(path.indexOf("?")));
121 if (params.has("lat") && params.has("lon")) {
122 markerLatlng = L.latLng(params.get("lat"), params.get("lon"));
124 markerLatlng = map.getCenter();
127 map.panInside(markerLatlng, {
131 addNewNoteMarker(markerLatlng);
133 content.find("textarea")
134 .on("input", updateControls)
135 .attr("readonly", "readonly") // avoid virtual keyboard popping up on focus
137 .removeAttr("readonly");
139 content.find("input[type=submit]").on("click", function (e) {
140 const location = newNoteMarker.getLatLng().wrap();
141 const text = content.find("textarea").val();
144 $(this).prop("disabled", true);
145 newNoteMarker.options.draggable = false;
146 newNoteMarker.dragging.disable();
148 createNote(location, text, (feature) => {
149 if (typeof OSM.user === "undefined") {
150 const anonymousNotesCount = Number(OSM.cookies.get("_osm_anonymous_notes_count")) || 0;
151 OSM.cookies.set("_osm_anonymous_notes_count", anonymousNotesCount + 1, { expires: 30 });
153 content.find("textarea").val("");
154 addCreatedNoteMarker(feature);
155 OSM.router.route("/note/" + feature.properties.id);
159 map.on("click", moveNewNoteMarkerToClick);
160 addNoteButton.on("disabled enabled", updateControls);
163 return map.getState();
166 page.unload = function () {
167 map.off("click", moveNewNoteMarkerToClick);
168 addNoteButton.off("disabled enabled", updateControls);
169 removeNewNoteMarker();
170 control.removeClass("active");