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 updateMarker(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);
63 page.pushstate = page.popstate = function (path) {
64 OSM.loadSidebarContent(path, function () {
69 function newHalo(loc, a) {
70 var hasHalo = halo && map.hasLayer(halo);
72 if (a === "dragstart" && hasHalo) {
73 map.removeLayer(halo);
75 if (hasHalo) map.removeLayer(halo);
77 halo = L.circleMarker(loc, {
88 page.load = function (path) {
89 if (addNoteButton.hasClass("disabled")) return;
90 if (addNoteButton.hasClass("active")) return;
92 addNoteButton.addClass("active");
94 map.addLayer(noteLayer);
96 var params = Qs.parse(path.substring(path.indexOf("?") + 1));
99 if (params.lat && params.lon) {
100 markerLatlng = L.latLng(params.lat, params.lon);
102 markerLatlng = map.getCenter();
105 map.panInside(markerLatlng, {
109 newNoteMarker = L.marker(markerLatlng, {
115 newNoteMarker.on("dragstart dragend", function (a) {
116 newHalo(newNoteMarker.getLatLng(), a.type);
119 newNoteMarker.addTo(noteLayer);
120 newHalo(newNoteMarker.getLatLng());
122 newNoteMarker.on("remove", function () {
123 addNoteButton.removeClass("active");
124 }).on("dragend", function () {
125 content.find("textarea").focus();
128 content.find("textarea")
129 .on("input", disableWhenBlank)
132 function disableWhenBlank(e) {
133 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
136 content.find("input[type=submit]").on("click", function (e) {
137 const location = newNoteMarker.getLatLng().wrap();
138 const text = content.find("textarea").val();
141 $(this).prop("disabled", true);
142 newNoteMarker.options.draggable = false;
143 newNoteMarker.dragging.disable();
145 createNote(location, text, (feature) => {
146 content.find("textarea").val("");
147 updateMarker(feature);
148 noteLayer.removeLayer(newNoteMarker);
149 newNoteMarker = null;
150 addNoteButton.removeClass("active");
151 OSM.router.route("/note/" + feature.properties.id);
155 return map.getState();
158 page.unload = function () {
159 if (newNoteMarker) noteLayer.removeLayer(newNoteMarker);
160 if (halo) map.removeLayer(halo);
161 addNoteButton.removeClass("active");