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 page.pushstate = page.popstate = function (path) {
111 OSM.loadSidebarContent(path, function () {
116 page.load = function (path) {
117 if (addNoteButton.hasClass("disabled")) return;
118 if (addNoteButton.hasClass("active")) return;
120 addNoteButton.addClass("active");
122 map.addLayer(noteLayer);
124 var params = Qs.parse(path.substring(path.indexOf("?") + 1));
127 if (params.lat && params.lon) {
128 markerLatlng = L.latLng(params.lat, params.lon);
130 markerLatlng = map.getCenter();
133 map.panInside(markerLatlng, {
137 addNewNoteMarker(markerLatlng);
139 content.find("textarea")
140 .on("input", disableWhenBlank)
143 function disableWhenBlank(e) {
144 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
147 content.find("input[type=submit]").on("click", function (e) {
148 const location = newNoteMarker.getLatLng().wrap();
149 const text = content.find("textarea").val();
152 $(this).prop("disabled", true);
153 newNoteMarker.options.draggable = false;
154 newNoteMarker.dragging.disable();
156 createNote(location, text, (feature) => {
157 content.find("textarea").val("");
158 addCreatedNoteMarker(feature);
159 OSM.router.route("/note/" + feature.properties.id);
163 return map.getState();
166 page.unload = function () {
167 removeNewNoteMarker();
168 addNoteButton.removeClass("active");