1 //= require templates/notes/show
2 //= require templates/notes/new
4 $(document).ready(function () {
5 var params = OSM.mapParams();
9 iconUrl: "<%= image_path 'new_note_marker.png' %>",
14 iconUrl: "<%= image_path 'open_note_marker.png' %>",
19 iconUrl: "<%= image_path 'closed_note_marker.png' %>",
25 var noteLayer = new L.LayerGroup();
28 map.on("layeradd", function (e) {
29 if (e.layer == noteLayer) {
31 map.on("moveend", loadNotes);
35 map.on("layerremove", function (e) {
36 if (e.layer == noteLayer) {
37 map.off("moveend", loadNotes);
38 noteLayer.clearLayers();
42 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
43 map.layersControl.addOverlay(noteLayer, I18n.t("browse.start_rjs.notes_layer_name"));
45 if (params.notes) map.addLayer(noteLayer);
48 function updateMarker(marker, feature) {
49 var icon = noteIcons[feature.properties.status];
50 var popupContent = createPopupContent(marker, feature.properties);
54 marker.setIcon(noteIcons[feature.properties.status]);
55 marker._popup.setContent(popupContent);
59 marker = L.marker(feature.geometry.coordinates.reverse(), {
64 marker.addTo(noteLayer).bindPopup(popupContent, popupOptions());
72 function loadNotes() {
73 var bounds = map.getBounds();
74 var size = bounds.getSize();
76 if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
77 var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBOX();
79 if (noteLoader) noteLoader.abort();
83 success: function (json) {
88 json.features.forEach(function (feature) {
89 var marker = oldNotes[feature.properties.id];
91 delete oldNotes[feature.properties.id];
93 notes[feature.properties.id] = updateMarker(marker, feature);
96 for (id in oldNotes) {
97 noteLayer.removeLayer(oldNotes[id]);
106 function popupOptions() {
107 var mapSize = map.getSize();
109 return { maxHeight: mapSize.y * 2 / 3, autoPanPadding: new L.Point(60, 40) };
112 function createPopupContent(marker, properties) {
113 var content = $(JST["templates/notes/show"]({ note: properties }));
115 content.find("textarea").on("input", function (e) {
116 var form = e.target.form;
118 if ($(e.target).val() == "") {
119 $(form.close).val(I18n.t("javascripts.notes.show.close"));
120 $(form.comment).prop("disabled", true);
122 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_close"));
123 $(form.comment).prop("disabled", false);
127 content.find("input[type=submit]").on("click", function (e) {
129 updateNote(marker, e.target.form, $(e.target).data("url"));
135 function createNote(marker, form, url) {
136 var location = marker.getLatLng();
138 $(form).find("input[type=submit]").prop("disabled", true);
146 text: $(form.text).val()
148 success: function (feature) {
149 notes[feature.properties.id] = updateMarker(marker, feature);
151 $(".leaflet-popup-close-button").off("click.close");
156 function updateNote(marker, form, url) {
157 $(form).find("input[type=submit]").prop("disabled", true);
163 text: $(form.text).val()
165 success: function (feature) {
166 var popupContent = createPopupContent(marker, feature.properties);
168 marker.setIcon(noteIcons[feature.properties.status]);
169 marker._popup.setContent(popupContent);
174 $("#createnoteanchor").click(function (e) {
177 map.addLayer(noteLayer);
179 var marker = L.marker(map.getCenter(), {
180 icon: noteIcons["new"],
185 var popupContent = $(JST["templates/notes/new"]({ create_url: $(e.target).attr("href") }));
187 popupContent.find("input[type=submit]").on("click", function (e) {
189 createNote(marker, e.target.form, $(e.target).data("url"));
192 marker.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
194 $(".leaflet-popup-close-button").on("click.close", function (e) {
195 map.removeLayer(marker);
198 marker.on("dragend", function (e) {
199 e.target.openPopup();