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, form, url) {
39 var location = marker.getLatLng().wrap();
41 marker.options.draggable = false;
42 marker.dragging.disable();
44 $(form).find("input[type=submit]").prop("disabled", true);
53 text: $(form.text).val()
55 success: function (feature) {
56 noteCreated(feature, marker);
60 function noteCreated(feature, marker) {
61 content.find("textarea").val("");
62 updateMarker(feature);
64 noteLayer.removeLayer(marker);
65 addNoteButton.removeClass("active");
66 OSM.router.route("/note/" + feature.properties.id);
70 function updateMarker(feature) {
71 var marker = L.marker(feature.geometry.coordinates.reverse(), {
72 icon: noteIcons[feature.properties.status],
76 marker.id = feature.properties.id;
77 marker.addTo(noteLayer);
81 page.pushstate = page.popstate = function (path) {
82 OSM.loadSidebarContent(path, function () {
87 function newHalo(loc, a) {
88 var hasHalo = halo && map.hasLayer(halo);
90 if (a === "dragstart" && hasHalo) {
91 map.removeLayer(halo);
93 if (hasHalo) map.removeLayer(halo);
95 halo = L.circleMarker(loc, {
106 page.load = function (path) {
107 if (addNoteButton.hasClass("disabled")) return;
108 if (addNoteButton.hasClass("active")) return;
110 addNoteButton.addClass("active");
112 map.addLayer(noteLayer);
114 var params = Qs.parse(path.substring(path.indexOf("?") + 1));
117 if (params.lat && params.lon) {
118 markerLatlng = L.latLng(params.lat, params.lon);
120 markerLatlng = map.getCenter();
123 map.panInside(markerLatlng, {
127 newNote = L.marker(markerLatlng, {
133 newNote.on("dragstart dragend", function (a) {
134 newHalo(newNote.getLatLng(), a.type);
137 newNote.addTo(noteLayer);
138 newHalo(newNote.getLatLng());
140 newNote.on("remove", function () {
141 addNoteButton.removeClass("active");
142 }).on("dragend", function () {
143 content.find("textarea").focus();
146 content.find("textarea")
147 .on("input", disableWhenBlank)
150 function disableWhenBlank(e) {
151 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
154 content.find("input[type=submit]").on("click", function (e) {
156 createNote(newNote, e.target.form, "/api/0.6/notes.json");
159 return map.getState();
162 page.unload = function () {
163 if (newNote) noteLayer.removeLayer(newNote);
164 if (halo) map.removeLayer(halo);
165 addNoteButton.removeClass("active");