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 if ($(this).hasClass('disabled')) return;
32 OSM.route('/new_note');
35 function createNote(marker, form, url) {
36 var location = marker.getLatLng();
38 marker.options.draggable = false;
39 marker.dragging.disable();
41 $(form).find("input[type=submit]").prop("disabled", true);
50 text: $(form.text).val()
52 success: function (feature) {
53 noteCreated(feature, marker);
57 function noteCreated(feature, marker) {
58 content.find("textarea").val("");
59 updateMarker(feature);
61 noteLayer.removeLayer(marker);
62 addNoteButton.removeClass("active");
63 OSM.route('/browse/note/' + feature.properties.id);
67 function updateMarker(feature) {
68 var marker = L.marker(feature.geometry.coordinates.reverse(), {
69 icon: noteIcons[feature.properties.status],
73 marker.id = feature.properties.id;
74 marker.addTo(noteLayer);
78 page.pushstate = page.popstate = function (path) {
79 OSM.loadSidebarContent(path, page.load);
82 page.load = function () {
83 if (addNoteButton.hasClass("disabled")) return;
84 if (addNoteButton.hasClass("active")) return;
86 addNoteButton.addClass("active");
88 map.addLayer(noteLayer);
90 var mapSize = map.getSize();
93 if (mapSize.y > 800) {
94 markerPosition = [mapSize.x / 2, mapSize.y / 2];
95 } else if (mapSize.y > 400) {
96 markerPosition = [mapSize.x / 2, 400];
98 markerPosition = [mapSize.x / 2, mapSize.y];
101 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
102 icon: noteIcons["new"],
107 newNote.addTo(noteLayer);
109 newNote.on("remove", function () {
110 addNoteButton.removeClass("active");
111 }).on("dragstart",function () {
112 $(newNote).stopTime("removenote");
113 }).on("dragend", function () {
114 content.find("textarea").focus();
117 content.find("textarea")
118 .on("input", disableWhenBlank)
121 function disableWhenBlank(e) {
122 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
125 content.find('input[type=submit]').on('click', function (e) {
127 createNote(newNote, e.target.form, '/api/0.6/notes.json');
131 page.unload = function () {
132 noteLayer.removeLayer(newNote);
133 addNoteButton.removeClass("active");