1 OSM.NewNote = function(map) {
2 var noteLayer = map.noteLayer,
3 content = $('#sidebar_content'),
5 addNoteButton = $(".control-note .control-button"),
10 iconUrl: "<%= image_path 'new_note_marker.png' %>",
15 iconUrl: "<%= image_path 'open_note_marker.png' %>",
20 iconUrl: "<%= image_path 'closed_note_marker.png' %>",
26 addNoteButton.on("click", function (e) {
30 OSM.route('/new_note');
33 function createNote(marker, form, url) {
34 var location = marker.getLatLng();
36 marker.options.draggable = false;
37 marker.dragging.disable();
39 $(form).find("input[type=submit]").prop("disabled", true);
48 text: $(form.text).val()
50 success: function (feature) {
51 noteCreated(feature, marker);
55 function noteCreated(feature, marker) {
56 content.find("textarea").val("");
57 updateMarker(feature);
59 noteLayer.removeLayer(marker);
60 addNoteButton.removeClass("active");
61 OSM.route('/browse/note/' + feature.properties.id);
65 function updateMarker(feature) {
66 var marker = L.marker(feature.geometry.coordinates.reverse(), {
67 icon: noteIcons[feature.properties.status],
71 marker.id = feature.properties.id;
72 marker.addTo(noteLayer);
76 page.pushstate = page.popstate = function (path) {
77 OSM.loadSidebarContent(path, page.load);
80 page.load = function () {
81 if (addNoteButton.hasClass("disabled")) return;
82 if (addNoteButton.hasClass("active")) return;
84 addNoteButton.addClass("active");
86 map.addLayer(noteLayer);
88 var mapSize = map.getSize();
91 if (mapSize.y > 800) {
92 markerPosition = [mapSize.x / 2, mapSize.y / 2];
93 } else if (mapSize.y > 400) {
94 markerPosition = [mapSize.x / 2, 400];
96 markerPosition = [mapSize.x / 2, mapSize.y];
99 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
100 icon: noteIcons["new"],
105 newNote.addTo(noteLayer);
107 newNote.on("remove", function () {
108 addNoteButton.removeClass("active");
109 }).on("dragstart",function () {
110 $(newNote).stopTime("removenote");
111 }).on("dragend", function () {
112 content.find("textarea").focus();
115 content.find("textarea")
116 .on("input", disableWhenBlank)
119 function disableWhenBlank(e) {
120 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
123 content.find('input[type=submit]').on('click', function (e) {
125 createNote(newNote, e.target.form, '/api/0.6/notes.json');
129 page.unload = function () {
130 noteLayer.removeLayer(newNote);
131 addNoteButton.removeClass("active");