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, url) {
39 var location = marker.getLatLng().wrap();
41 marker.options.draggable = false;
42 marker.dragging.disable();
53 success: function (feature) {
54 noteCreated(feature, marker);
58 function noteCreated(feature, marker) {
59 content.find("textarea").val("");
60 updateMarker(feature);
62 noteLayer.removeLayer(marker);
63 addNoteButton.removeClass("active");
64 OSM.router.route("/note/" + feature.properties.id);
68 function updateMarker(feature) {
69 var marker = L.marker(feature.geometry.coordinates.reverse(), {
70 icon: noteIcons[feature.properties.status],
74 marker.id = feature.properties.id;
75 marker.addTo(noteLayer);
79 page.pushstate = page.popstate = function (path) {
80 OSM.loadSidebarContent(path, function () {
85 function newHalo(loc, a) {
86 var hasHalo = halo && map.hasLayer(halo);
88 if (a === "dragstart" && hasHalo) {
89 map.removeLayer(halo);
91 if (hasHalo) map.removeLayer(halo);
93 halo = L.circleMarker(loc, {
104 page.load = function (path) {
105 if (addNoteButton.hasClass("disabled")) return;
106 if (addNoteButton.hasClass("active")) return;
108 addNoteButton.addClass("active");
110 map.addLayer(noteLayer);
112 var params = Qs.parse(path.substring(path.indexOf("?") + 1));
115 if (params.lat && params.lon) {
116 markerLatlng = L.latLng(params.lat, params.lon);
118 markerLatlng = map.getCenter();
121 map.panInside(markerLatlng, {
125 newNoteMarker = L.marker(markerLatlng, {
131 newNoteMarker.on("dragstart dragend", function (a) {
132 newHalo(newNoteMarker.getLatLng(), a.type);
135 newNoteMarker.addTo(noteLayer);
136 newHalo(newNoteMarker.getLatLng());
138 newNoteMarker.on("remove", function () {
139 addNoteButton.removeClass("active");
140 }).on("dragend", function () {
141 content.find("textarea").focus();
144 content.find("textarea")
145 .on("input", disableWhenBlank)
148 function disableWhenBlank(e) {
149 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
152 content.find("input[type=submit]").on("click", function (e) {
153 const text = content.find("textarea").val();
156 $(this).prop("disabled", true);
157 createNote(newNoteMarker, text, "/api/0.6/notes.json");
160 return map.getState();
163 page.unload = function () {
164 if (newNoteMarker) noteLayer.removeLayer(newNoteMarker);
165 if (halo) map.removeLayer(halo);
166 addNoteButton.removeClass("active");