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(marker, text, callback) {
39 var location = marker.getLatLng().wrap();
41 marker.options.draggable = false;
42 marker.dragging.disable();
45 url: "/api/0.6/notes.json",
57 function updateMarker(feature) {
58 var marker = L.marker(feature.geometry.coordinates.reverse(), {
59 icon: noteIcons[feature.properties.status],
63 marker.id = feature.properties.id;
64 marker.addTo(noteLayer);
68 page.pushstate = page.popstate = function (path) {
69 OSM.loadSidebarContent(path, function () {
74 function newHalo(loc, a) {
75 var hasHalo = halo && map.hasLayer(halo);
77 if (a === "dragstart" && hasHalo) {
78 map.removeLayer(halo);
80 if (hasHalo) map.removeLayer(halo);
82 halo = L.circleMarker(loc, {
93 page.load = function (path) {
94 if (addNoteButton.hasClass("disabled")) return;
95 if (addNoteButton.hasClass("active")) return;
97 addNoteButton.addClass("active");
99 map.addLayer(noteLayer);
101 var params = Qs.parse(path.substring(path.indexOf("?") + 1));
104 if (params.lat && params.lon) {
105 markerLatlng = L.latLng(params.lat, params.lon);
107 markerLatlng = map.getCenter();
110 map.panInside(markerLatlng, {
114 newNoteMarker = L.marker(markerLatlng, {
120 newNoteMarker.on("dragstart dragend", function (a) {
121 newHalo(newNoteMarker.getLatLng(), a.type);
124 newNoteMarker.addTo(noteLayer);
125 newHalo(newNoteMarker.getLatLng());
127 newNoteMarker.on("remove", function () {
128 addNoteButton.removeClass("active");
129 }).on("dragend", function () {
130 content.find("textarea").focus();
133 content.find("textarea")
134 .on("input", disableWhenBlank)
137 function disableWhenBlank(e) {
138 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
141 content.find("input[type=submit]").on("click", function (e) {
142 const text = content.find("textarea").val();
145 $(this).prop("disabled", true);
146 createNote(newNoteMarker, text, (feature) => {
147 content.find("textarea").val("");
148 updateMarker(feature);
149 noteLayer.removeLayer(newNoteMarker);
150 newNoteMarker = null;
151 addNoteButton.removeClass("active");
152 OSM.router.route("/note/" + feature.properties.id);
156 return map.getState();
159 page.unload = function () {
160 if (newNoteMarker) noteLayer.removeLayer(newNoteMarker);
161 if (halo) map.removeLayer(halo);
162 addNoteButton.removeClass("active");