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.router.route('/note/new');
36 function createNote(marker, form, url) {
37 var location = marker.getLatLng().wrap();
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('/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 function newHalo(loc, a) {
84 if (a == 'dragstart' && map.hasLayer(halo)) {
85 map.removeLayer(halo);
87 if (map.hasLayer(halo)) map.removeLayer(halo);
89 halo = L.circleMarker(loc, {
100 page.load = function () {
101 if (addNoteButton.hasClass("disabled")) return;
102 if (addNoteButton.hasClass("active")) return;
104 addNoteButton.addClass("active");
106 map.addLayer(noteLayer);
108 var mapSize = map.getSize();
111 if (mapSize.y > 800) {
112 markerPosition = [mapSize.x / 2, mapSize.y / 2];
113 } else if (mapSize.y > 400) {
114 markerPosition = [mapSize.x / 2, 400];
116 markerPosition = [mapSize.x / 2, mapSize.y];
119 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
120 icon: noteIcons["new"],
125 newNote.on("dragstart dragend", function(a) {
126 newHalo(newNote.getLatLng(), a.type);
129 newNote.addTo(noteLayer);
130 newHalo(newNote.getLatLng());
132 newNote.on("remove", function () {
133 addNoteButton.removeClass("active");
134 }).on("dragstart",function () {
135 $(newNote).stopTime("removenote");
136 }).on("dragend", function () {
137 content.find("textarea").focus();
140 content.find("textarea")
141 .on("input", disableWhenBlank)
144 function disableWhenBlank(e) {
145 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
148 content.find('input[type=submit]').on('click', function (e) {
150 createNote(newNote, e.target.form, '/api/0.6/notes.json');
153 return map.getState();
156 page.unload = function () {
157 noteLayer.removeLayer(newNote);
158 map.removeLayer(halo);
159 addNoteButton.removeClass("active");