1 //= require querystring
3 OSM.NewNote = function (map) {
4 var querystring = require("querystring-component");
6 var noteLayer = map.noteLayer,
7 content = $("#sidebar_content"),
9 addNoteButton = $(".control-note .control-button"),
15 iconUrl: OSM.NEW_NOTE_MARKER,
20 iconUrl: OSM.OPEN_NOTE_MARKER,
25 iconUrl: OSM.CLOSED_NOTE_MARKER,
31 addNoteButton.on("click", function (e) {
35 if ($(this).hasClass("disabled")) return;
37 OSM.router.route("/note/new");
40 function createNote(marker, form, url) {
41 var location = marker.getLatLng().wrap();
43 marker.options.draggable = false;
44 marker.dragging.disable();
46 $(form).find("input[type=submit]").prop("disabled", true);
55 text: $(form.text).val()
57 success: function (feature) {
58 noteCreated(feature, marker);
62 function noteCreated(feature, marker) {
63 content.find("textarea").val("");
64 updateMarker(feature);
66 noteLayer.removeLayer(marker);
67 addNoteButton.removeClass("active");
68 OSM.router.route("/note/" + feature.properties.id);
72 function updateMarker(feature) {
73 var marker = L.marker(feature.geometry.coordinates.reverse(), {
74 icon: noteIcons[feature.properties.status],
78 marker.id = feature.properties.id;
79 marker.addTo(noteLayer);
83 page.pushstate = page.popstate = function (path) {
84 OSM.loadSidebarContent(path, function () {
89 function newHalo(loc, a) {
90 if (a === "dragstart" && map.hasLayer(halo)) {
91 map.removeLayer(halo);
93 if (map.hasLayer(halo)) 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 = querystring.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("dragstart", function () {
143 $(newNote).stopTime("removenote");
144 }).on("dragend", function () {
145 content.find("textarea").focus();
148 content.find("textarea")
149 .on("input", disableWhenBlank)
152 function disableWhenBlank(e) {
153 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
156 content.find("input[type=submit]").on("click", function (e) {
158 createNote(newNote, e.target.form, "/api/0.6/notes.json");
161 return map.getState();
164 page.unload = function () {
165 noteLayer.removeLayer(newNote);
166 map.removeLayer(halo);
167 addNoteButton.removeClass("active");