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();
111 maxWidth: mapSize.y * 1 / 3,
112 maxHeight: mapSize.y * 2 / 3,
113 autoPanPadding: new L.Point(60, 40)
117 function createPopupContent(marker, properties) {
118 var content = $(JST["templates/notes/show"]({ note: properties }));
120 content.find("textarea").on("input", function (e) {
121 var form = e.target.form;
123 if ($(e.target).val() == "") {
124 $(form.close).val(I18n.t("javascripts.notes.show.close"));
125 $(form.comment).prop("disabled", true);
127 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_close"));
128 $(form.comment).prop("disabled", false);
132 content.find("input[type=submit]").on("click", function (e) {
134 updateNote(marker, e.target.form, $(e.target).data("url"));
140 function createNote(marker, form, url) {
141 var location = marker.getLatLng();
143 $(form).find("input[type=submit]").prop("disabled", true);
151 text: $(form.text).val()
153 success: function (feature) {
154 notes[feature.properties.id] = updateMarker(marker, feature);
156 $(".leaflet-popup-close-button").off("click.close");
161 function updateNote(marker, form, url) {
162 $(form).find("input[type=submit]").prop("disabled", true);
168 text: $(form.text).val()
170 success: function (feature) {
171 var popupContent = createPopupContent(marker, feature.properties);
173 marker.setIcon(noteIcons[feature.properties.status]);
174 marker._popup.setContent(popupContent);
179 $("#createnoteanchor").click(function (e) {
182 if ($(e.target).hasClass("disabled")) return;
184 map.addLayer(noteLayer);
186 var marker = L.marker(map.getCenter(), {
187 icon: noteIcons["new"],
192 var popupContent = $(JST["templates/notes/new"]({ create_url: $(e.target).attr("href") }));
194 popupContent.find("input[type=submit]").on("click", function (e) {
196 createNote(marker, e.target.form, $(e.target).data("url"));
199 marker.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
201 $(".leaflet-popup-close-button").on("click.close", function (e) {
202 map.removeLayer(marker);
205 marker.on("dragend", function (e) {
206 e.target.openPopup();