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 newHalo(loc, a) {
63 var hasHalo = halo && map.hasLayer(halo);
65 if (a === "dragstart" && hasHalo) {
66 map.removeLayer(halo);
68 if (hasHalo) map.removeLayer(halo);
70 halo = L.circleMarker(loc, {
81 page.pushstate = page.popstate = function (path) {
82 OSM.loadSidebarContent(path, function () {
87 page.load = function (path) {
88 if (addNoteButton.hasClass("disabled")) return;
89 if (addNoteButton.hasClass("active")) return;
91 addNoteButton.addClass("active");
93 map.addLayer(noteLayer);
95 var params = Qs.parse(path.substring(path.indexOf("?") + 1));
98 if (params.lat && params.lon) {
99 markerLatlng = L.latLng(params.lat, params.lon);
101 markerLatlng = map.getCenter();
104 map.panInside(markerLatlng, {
108 newNoteMarker = L.marker(markerLatlng, {
114 newNoteMarker.on("dragstart dragend", function (a) {
115 newHalo(newNoteMarker.getLatLng(), a.type);
118 newNoteMarker.addTo(map);
119 newHalo(newNoteMarker.getLatLng());
121 newNoteMarker.on("remove", function () {
122 addNoteButton.removeClass("active");
123 }).on("dragend", function () {
124 content.find("textarea").focus();
127 content.find("textarea")
128 .on("input", disableWhenBlank)
131 function disableWhenBlank(e) {
132 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
135 content.find("input[type=submit]").on("click", function (e) {
136 const location = newNoteMarker.getLatLng().wrap();
137 const text = content.find("textarea").val();
140 $(this).prop("disabled", true);
141 newNoteMarker.options.draggable = false;
142 newNoteMarker.dragging.disable();
144 createNote(location, text, (feature) => {
145 content.find("textarea").val("");
146 addCreatedNoteMarker(feature);
147 map.removeLayer(newNoteMarker);
148 newNoteMarker = null;
149 addNoteButton.removeClass("active");
150 OSM.router.route("/note/" + feature.properties.id);
154 return map.getState();
157 page.unload = function () {
158 if (newNoteMarker) map.removeLayer(newNoteMarker);
159 if (halo) map.removeLayer(halo);
160 addNoteButton.removeClass("active");