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);
49 url: "/api/" + OSM.API_VERSION + "/notes/" + params.note + ".json",
50 success: function (feature) {
51 var marker = updateMarker(notes[feature.properties.id], feature);
53 notes[feature.properties.id] = marker;
55 map.addLayer(noteLayer);
62 function updateMarker(marker, feature) {
63 var icon = noteIcons[feature.properties.status];
64 var popupContent = createPopupContent(marker, feature.properties);
68 marker.setIcon(noteIcons[feature.properties.status]);
69 marker._popup.setContent(popupContent);
73 marker = L.marker(feature.geometry.coordinates.reverse(), {
78 marker.addTo(noteLayer).bindPopup(popupContent, popupOptions());
86 function loadNotes() {
87 var bounds = map.getBounds();
88 var size = bounds.getSize();
90 if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
91 var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBOX();
93 if (noteLoader) noteLoader.abort();
97 success: function (json) {
102 json.features.forEach(function (feature) {
103 var marker = oldNotes[feature.properties.id];
105 delete oldNotes[feature.properties.id];
107 notes[feature.properties.id] = updateMarker(marker, feature);
110 for (id in oldNotes) {
111 noteLayer.removeLayer(oldNotes[id]);
120 function popupOptions() {
121 var mapSize = map.getSize();
125 maxWidth: mapSize.y * 1 / 3,
126 maxHeight: mapSize.y * 2 / 3,
127 offset: new L.Point(0, -3),
128 autoPanPadding: new L.Point(60, 40)
132 function createPopupContent(marker, properties) {
133 var content = $(JST["templates/notes/show"]({ note: properties }));
135 content.find("textarea").on("input", function (e) {
136 var form = e.target.form;
138 if ($(e.target).val() == "") {
139 $(form.close).val(I18n.t("javascripts.notes.show.close"));
140 $(form.comment).prop("disabled", true);
142 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_close"));
143 $(form.comment).prop("disabled", false);
147 content.find("input[type=submit]").on("click", function (e) {
149 updateNote(marker, e.target.form, $(e.target).data("url"));
155 function createNote(marker, form, url) {
156 var location = marker.getLatLng();
158 $(form).find("input[type=submit]").prop("disabled", true);
166 text: $(form.text).val()
168 success: function (feature) {
169 notes[feature.properties.id] = updateMarker(marker, feature);
171 $(".leaflet-popup-close-button").off("click.close");
176 function updateNote(marker, form, url) {
177 $(form).find("input[type=submit]").prop("disabled", true);
183 text: $(form.text).val()
185 success: function (feature) {
186 var popupContent = createPopupContent(marker, feature.properties);
188 marker.setIcon(noteIcons[feature.properties.status]);
189 marker._popup.setContent(popupContent);
194 $("#createnoteanchor").click(function (e) {
197 if ($(e.target).hasClass("disabled")) return;
199 $(e.target).removeClass("geolink").addClass("disabled");
201 map.addLayer(noteLayer);
203 var mapSize = map.getSize();
208 markerPosition = [mapSize.x / 2, mapSize.y / 2];
210 else if (mapSize.y > 400)
212 markerPosition = [mapSize.x / 2, 400];
216 markerPosition = [mapSize.x / 2, mapSize.y];
219 var marker = L.marker(map.containerPointToLatLng(markerPosition), {
220 icon: noteIcons["new"],
225 var popupContent = $(JST["templates/notes/new"]({ create_url: $(e.target).attr("href") }));
227 popupContent.find("input[type=submit]").on("click", function (e) {
229 createNote(marker, e.target.form, $(e.target).data("url"));
232 marker.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
234 $(".leaflet-popup-close-button").on("click.close", function (e) {
235 map.removeLayer(marker);
237 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
240 marker.on("dragend", function (e) {
241 e.target.openPopup();