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();
29 map.on("layeradd", function (e) {
30 if (e.layer == noteLayer) {
32 map.on("moveend", loadNotes);
36 map.on("layerremove", function (e) {
37 if (e.layer == noteLayer) {
38 map.off("moveend", loadNotes);
39 noteLayer.clearLayers();
43 map.on("popupclose", function (e) {
44 if (newNote && e.popup == newNote._popup) {
45 $(newNote).oneTime(10, "removenote", function () {
46 map.removeLayer(newNote);
51 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
52 map.layersControl.addOverlay(noteLayer, I18n.t("browse.start_rjs.notes_layer_name"));
54 if (params.notes) map.addLayer(noteLayer);
58 url: "/api/" + OSM.API_VERSION + "/notes/" + params.note + ".json",
59 success: function (feature) {
60 var marker = updateMarker(notes[feature.properties.id], feature);
62 notes[feature.properties.id] = marker;
64 map.addLayer(noteLayer);
71 function updateMarker(marker, feature) {
72 var icon = noteIcons[feature.properties.status];
73 var popupContent = createPopupContent(marker, feature.properties);
77 marker.setIcon(noteIcons[feature.properties.status]);
78 marker._popup.setContent(popupContent);
82 marker = L.marker(feature.geometry.coordinates.reverse(), {
87 marker.addTo(noteLayer).bindPopup(popupContent, popupOptions());
95 function loadNotes() {
96 var bounds = map.getBounds();
97 var size = bounds.getSize();
99 if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
100 var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBOX();
102 if (noteLoader) noteLoader.abort();
104 noteLoader = $.ajax({
106 success: function (json) {
107 var oldNotes = notes;
111 json.features.forEach(function (feature) {
112 var marker = oldNotes[feature.properties.id];
114 delete oldNotes[feature.properties.id];
116 notes[feature.properties.id] = updateMarker(marker, feature);
119 for (id in oldNotes) {
120 noteLayer.removeLayer(oldNotes[id]);
129 function popupOptions() {
130 var mapSize = map.getSize();
134 maxWidth: mapSize.y * 1 / 3,
135 maxHeight: mapSize.y * 2 / 3,
136 offset: new L.Point(0, -3),
137 autoPanPadding: new L.Point(60, 40)
141 function createPopupContent(marker, properties) {
142 var content = $(JST["templates/notes/show"]({ note: properties }));
144 content.find("textarea").on("input", function (e) {
145 var form = e.target.form;
147 if ($(e.target).val() == "") {
148 $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
149 $(form.comment).prop("disabled", true);
151 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
152 $(form.comment).prop("disabled", false);
156 content.find("input[type=submit]").on("click", function (e) {
158 updateNote(marker, e.target.form, $(e.target).data("url"));
164 function createNote(marker, form, url) {
165 var location = marker.getLatLng();
167 $(form).find("input[type=submit]").prop("disabled", true);
175 text: $(form.text).val()
177 success: function (feature) {
178 notes[feature.properties.id] = updateMarker(marker, feature);
180 $(".leaflet-popup-close-button").off("click.close");
181 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
186 function updateNote(marker, form, url) {
187 $(form).find("input[type=submit]").prop("disabled", true);
193 text: $(form.text).val()
195 success: function (feature) {
196 var popupContent = createPopupContent(marker, feature.properties);
198 marker.setIcon(noteIcons[feature.properties.status]);
199 marker._popup.setContent(popupContent);
204 $("#createnoteanchor").click(function (e) {
207 if ($(e.target).hasClass("disabled")) return;
209 $(e.target).removeClass("geolink").addClass("disabled");
211 map.addLayer(noteLayer);
213 var mapSize = map.getSize();
218 markerPosition = [mapSize.x / 2, mapSize.y / 2];
220 else if (mapSize.y > 400)
222 markerPosition = [mapSize.x / 2, 400];
226 markerPosition = [mapSize.x / 2, mapSize.y];
229 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
230 icon: noteIcons["new"],
235 var popupContent = $(JST["templates/notes/new"]({ create_url: $(e.target).attr("href") }));
237 popupContent.find("textarea").on("input", function (e) {
238 var form = e.target.form;
240 if ($(e.target).val() == "") {
241 $(form.add).prop("disabled", true);
243 $(form.add).prop("disabled", false);
247 popupContent.find("input[type=submit]").on("click", function (e) {
249 createNote(newNote, e.target.form, $(e.target).data("url"));
252 newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
254 newNote.on("remove", function (e) {
255 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
258 newNote.on("dragstart", function (e) {
259 $(newNote).stopTime("removenote");
262 newNote.on("dragend", function (e) {
263 e.target.openPopup();