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("remove", function () {
100 addNoteButton.removeClass("active");
101 }).on("dragend", function () {
102 content.find("textarea").focus();
106 function removeNewNoteMarker() {
108 if (newNoteMarker) map.removeLayer(newNoteMarker);
109 newNoteMarker = null;
112 page.pushstate = page.popstate = function (path) {
113 OSM.loadSidebarContent(path, function () {
118 page.load = function (path) {
119 if (addNoteButton.hasClass("disabled")) return;
120 if (addNoteButton.hasClass("active")) return;
122 addNoteButton.addClass("active");
124 map.addLayer(noteLayer);
126 var params = Qs.parse(path.substring(path.indexOf("?") + 1));
129 if (params.lat && params.lon) {
130 markerLatlng = L.latLng(params.lat, params.lon);
132 markerLatlng = map.getCenter();
135 map.panInside(markerLatlng, {
139 addNewNoteMarker(markerLatlng);
141 content.find("textarea")
142 .on("input", disableWhenBlank)
145 function disableWhenBlank(e) {
146 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
149 content.find("input[type=submit]").on("click", function (e) {
150 const location = newNoteMarker.getLatLng().wrap();
151 const text = content.find("textarea").val();
154 $(this).prop("disabled", true);
155 newNoteMarker.options.draggable = false;
156 newNoteMarker.dragging.disable();
158 createNote(location, text, (feature) => {
159 content.find("textarea").val("");
160 addCreatedNoteMarker(feature);
161 removeNewNoteMarker();
162 addNoteButton.removeClass("active");
163 OSM.router.route("/note/" + feature.properties.id);
167 return map.getState();
170 page.unload = function () {
171 removeNewNoteMarker();
172 addNoteButton.removeClass("active");