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 offset: new L.Point(0, -3),
114 autoPanPadding: new L.Point(60, 40)
118 function createPopupContent(marker, properties) {
119 var content = $(JST["templates/notes/show"]({ note: properties }));
121 content.find("textarea").on("input", function (e) {
122 var form = e.target.form;
124 if ($(e.target).val() == "") {
125 $(form.close).val(I18n.t("javascripts.notes.show.close"));
126 $(form.comment).prop("disabled", true);
128 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_close"));
129 $(form.comment).prop("disabled", false);
133 content.find("input[type=submit]").on("click", function (e) {
135 updateNote(marker, e.target.form, $(e.target).data("url"));
141 function createNote(marker, form, url) {
142 var location = marker.getLatLng();
144 $(form).find("input[type=submit]").prop("disabled", true);
152 text: $(form.text).val()
154 success: function (feature) {
155 notes[feature.properties.id] = updateMarker(marker, feature);
157 $(".leaflet-popup-close-button").off("click.close");
162 function updateNote(marker, form, url) {
163 $(form).find("input[type=submit]").prop("disabled", true);
169 text: $(form.text).val()
171 success: function (feature) {
172 var popupContent = createPopupContent(marker, feature.properties);
174 marker.setIcon(noteIcons[feature.properties.status]);
175 marker._popup.setContent(popupContent);
180 $("#createnoteanchor").click(function (e) {
183 if ($(e.target).hasClass("disabled")) return;
185 map.addLayer(noteLayer);
187 var mapSize = map.getSize();
192 markerPosition = [mapSize.x / 2, mapSize.y / 2];
194 else if (mapSize.y > 400)
196 markerPosition = [mapSize.x / 2, 400];
200 markerPosition = [mapSize.x / 2, mapSize.y];
203 var marker = L.marker(map.containerPointToLatLng(markerPosition), {
204 icon: noteIcons["new"],
209 var popupContent = $(JST["templates/notes/new"]({ create_url: $(e.target).attr("href") }));
211 popupContent.find("input[type=submit]").on("click", function (e) {
213 createNote(marker, e.target.form, $(e.target).data("url"));
216 marker.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
218 $(".leaflet-popup-close-button").on("click.close", function (e) {
219 map.removeLayer(marker);
222 marker.on("dragend", function (e) {
223 e.target.openPopup();