3 OSM.NewNote = function (map) {
4 var noteLayer = map.noteLayer,
5 content = $("#sidebar_content"),
7 addNoteButton = $(".control-note .control-button"),
13 iconUrl: OSM.NEW_NOTE_MARKER,
18 iconUrl: OSM.OPEN_NOTE_MARKER,
23 iconUrl: OSM.CLOSED_NOTE_MARKER,
29 addNoteButton.on("click", function (e) {
33 if ($(this).hasClass("disabled")) return;
35 OSM.router.route("/note/new");
38 function createNote(location, text, callback) {
40 url: "/api/0.6/notes.json",
52 function addCreatedNoteMarker(feature) {
53 var marker = L.marker(feature.geometry.coordinates.reverse(), {
54 icon: noteIcons[feature.properties.status],
58 marker.id = feature.properties.id;
59 marker.addTo(noteLayer);
62 function addHalo(latlng) {
63 if (halo) map.removeLayer(halo);
65 halo = L.circleMarker(latlng, {
75 function removeHalo() {
76 if (halo) map.removeLayer(halo);
80 function addNewNoteMarker(latlng) {
81 if (newNoteMarker) map.removeLayer(newNoteMarker);
83 newNoteMarker = L.marker(latlng, {
89 newNoteMarker.on("dragstart dragend", function (a) {
91 if (a.type === "dragend") {
92 addHalo(newNoteMarker.getLatLng());
96 newNoteMarker.addTo(map);
97 addHalo(newNoteMarker.getLatLng());
99 newNoteMarker.on("dragend", function () {
100 content.find("textarea").focus();
104 function removeNewNoteMarker() {
106 if (newNoteMarker) map.removeLayer(newNoteMarker);
107 newNoteMarker = null;
110 function moveNewNotMarkerToClick(e) {
111 if (newNoteMarker) newNoteMarker.setLatLng(e.latlng);
112 if (halo) halo.setLatLng(e.latlng);
113 content.find("textarea").focus();
116 function updateControls() {
117 const zoomedOut = addNoteButton.hasClass("disabled");
118 const withoutText = content.find("textarea").val() === "";
120 content.find("#new-note-zoom-warning").prop("hidden", !zoomedOut);
121 content.find("input[type=submit]").prop("disabled", zoomedOut || withoutText);
122 if (newNoteMarker) newNoteMarker.setOpacity(zoomedOut ? 0.5 : 0.9);
125 page.pushstate = page.popstate = function (path) {
126 OSM.loadSidebarContent(path, function () {
131 page.load = function (path) {
132 addNoteButton.addClass("active");
134 map.addLayer(noteLayer);
136 var params = Qs.parse(path.substring(path.indexOf("?") + 1));
139 if (params.lat && params.lon) {
140 markerLatlng = L.latLng(params.lat, params.lon);
142 markerLatlng = map.getCenter();
145 map.panInside(markerLatlng, {
149 addNewNoteMarker(markerLatlng);
151 content.find("textarea")
152 .on("input", updateControls)
155 content.find("input[type=submit]").on("click", function (e) {
156 const location = newNoteMarker.getLatLng().wrap();
157 const text = content.find("textarea").val();
160 $(this).prop("disabled", true);
161 newNoteMarker.options.draggable = false;
162 newNoteMarker.dragging.disable();
164 createNote(location, text, (feature) => {
165 content.find("textarea").val("");
166 addCreatedNoteMarker(feature);
167 OSM.router.route("/note/" + feature.properties.id);
171 map.on("click", moveNewNotMarkerToClick);
172 addNoteButton.on("disabled enabled", updateControls);
175 return map.getState();
178 page.unload = function () {
179 map.off("click", moveNewNotMarkerToClick);
180 addNoteButton.off("disabled enabled", updateControls);
181 removeNewNoteMarker();
182 addNoteButton.removeClass("active");