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 page.pushstate = page.popstate = function () {
30 addNoteButton.on("click", function (e) {
34 OSM.route('/new_note');
37 function createNote(marker, form, url) {
38 var location = marker.getLatLng();
40 marker.options.draggable = false;
41 marker.dragging.disable();
43 $(form).find("input[type=submit]").prop("disabled", true);
52 text: $(form.text).val()
54 success: function (feature) {
55 noteCreated(feature, marker);
59 function noteCreated(feature, marker) {
60 content.find("textarea").val("");
61 updateMarker(feature);
63 noteLayer.removeLayer(marker);
64 addNoteButton.removeClass("active");
65 OSM.route('/browse/note/' + feature.properties.id);
69 function updateMarker(feature) {
70 marker = L.marker(feature.geometry.coordinates.reverse(), {
71 icon: noteIcons[feature.properties.status],
75 marker.id = feature.properties.id;
76 marker.addTo(noteLayer);
80 function initialize() {
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.load = function () {
130 content.load(window.location.pathname + "?xhr=1", function (a, b, xhr) {
131 if (xhr.getResponseHeader('X-Page-Title')) {
132 document.title = xhr.getResponseHeader('X-Page-Title');
138 page.unload = function () {
139 noteLayer.removeLayer(newNote);
140 addNoteButton.removeClass("active");