1 OSM.NewNote = function(map) {
2 var noteLayer = map.noteLayer,
3 content = $('#sidebar_content'),
5 addNoteButton = $(".control-note .control-button"),
11 iconUrl: "<%= image_path 'new_note_marker.png' %>",
16 iconUrl: "<%= image_path 'open_note_marker.png' %>",
21 iconUrl: "<%= image_path 'closed_note_marker.png' %>",
27 addNoteButton.on("click", function (e) {
31 if ($(this).hasClass('disabled')) return;
33 OSM.route('/new_note');
36 function createNote(marker, form, url) {
37 var location = marker.getLatLng();
39 marker.options.draggable = false;
40 marker.dragging.disable();
42 $(form).find("input[type=submit]").prop("disabled", true);
51 text: $(form.text).val()
53 success: function (feature) {
54 noteCreated(feature, marker);
58 function noteCreated(feature, marker) {
59 content.find("textarea").val("");
60 updateMarker(feature);
62 noteLayer.removeLayer(marker);
63 addNoteButton.removeClass("active");
64 OSM.route('/browse/note/' + feature.properties.id);
68 function updateMarker(feature) {
69 var marker = L.marker(feature.geometry.coordinates.reverse(), {
70 icon: noteIcons[feature.properties.status],
74 marker.id = feature.properties.id;
75 marker.addTo(noteLayer);
79 page.pushstate = page.popstate = function (path) {
80 OSM.loadSidebarContent(path, page.load);
83 page.load = function () {
84 if (addNoteButton.hasClass("disabled")) return;
85 if (addNoteButton.hasClass("active")) return;
87 addNoteButton.addClass("active");
89 map.addLayer(noteLayer);
91 var mapSize = map.getSize();
94 if (mapSize.y > 800) {
95 markerPosition = [mapSize.x / 2, mapSize.y / 2];
96 } else if (mapSize.y > 400) {
97 markerPosition = [mapSize.x / 2, 400];
99 markerPosition = [mapSize.x / 2, mapSize.y];
102 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
103 icon: noteIcons["new"],
108 newNote.addTo(noteLayer);
110 halo = L.circleMarker(map.containerPointToLatLng(markerPosition), {
119 newNote.on("remove", function () {
120 addNoteButton.removeClass("active");
121 }).on("dragstart",function () {
122 $(newNote).stopTime("removenote");
123 }).on("dragend", function () {
124 content.find("textarea").focus();
127 content.find("textarea")
128 .on("input", disableWhenBlank)
131 function disableWhenBlank(e) {
132 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
135 content.find('input[type=submit]').on('click', function (e) {
137 createNote(newNote, e.target.form, '/api/0.6/notes.json');
141 page.unload = function () {
142 noteLayer.removeLayer(newNote);
143 map.removeLayer(halo);
144 addNoteButton.removeClass("active");