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();
34 map.on("layeradd", function (e) {
35 if (e.layer == noteLayer) {
37 map.on("moveend", loadNotes);
41 map.on("layerremove", function (e) {
42 if (e.layer == noteLayer) {
43 map.off("moveend", loadNotes);
44 noteLayer.clearLayers();
49 // Don't focus the text area on touch devices to avoid flashing the keyboard
50 if (!('ontouchstart' in document.documentElement)) {
51 map.on("popupopen", function (e) {
52 $(e.popup._container).find(".comment").focus();
56 map.on("popupclose", function (e) {
57 if (newNote && e.popup == newNote._popup) {
58 $(newNote).oneTime(10, "removenote", function () {
59 map.removeLayer(newNote);
65 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
66 map.layersControl.addOverlay(noteLayer, I18n.t("browse.start_rjs.notes_layer_name"));
68 if (params.layers) setMapLayers(params.layers);
69 if (params.notes) map.addLayer(noteLayer);
73 url: "/api/" + OSM.API_VERSION + "/notes/" + params.note + ".json",
74 success: function (feature) {
75 var marker = updateMarker(notes[feature.properties.id], feature);
77 notes[feature.properties.id] = marker;
79 map.addLayer(noteLayer);
86 function updateMarker(marker, feature) {
89 marker.setIcon(noteIcons[feature.properties.status]);
90 marker._popup.setContent(createPopupContent(
91 marker, feature.properties,
92 $(marker._popup._content).find("textarea").val()
97 marker = L.marker(feature.geometry.coordinates.reverse(), {
98 icon: noteIcons[feature.properties.status],
102 marker.addTo(noteLayer).bindPopup(
103 createPopupContent(marker, feature.properties),
113 function loadNotes() {
114 var bounds = map.getBounds();
115 var size = bounds.getSize();
117 if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
118 var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBOX();
120 if (noteLoader) noteLoader.abort();
122 noteLoader = $.ajax({
124 success: function (json) {
125 var oldNotes = notes;
129 json.features.forEach(function (feature) {
130 var marker = oldNotes[feature.properties.id];
132 delete oldNotes[feature.properties.id];
134 notes[feature.properties.id] = updateMarker(marker, feature);
137 for (id in oldNotes) {
138 noteLayer.removeLayer(oldNotes[id]);
147 function popupOptions() {
148 var mapSize = map.getSize();
152 maxWidth: mapSize.y * 1 / 3,
153 maxHeight: mapSize.y * 2 / 3,
154 offset: new L.Point(0, -3),
155 autoPanPadding: new L.Point(60, 40)
159 function createPopupContent(marker, properties, comment) {
160 var content = $(JST["templates/notes/show"]({ note: properties }));
162 content.find("textarea").on("input", function (e) {
163 var form = e.target.form;
165 if ($(e.target).val() == "") {
166 $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
167 $(form.comment).prop("disabled", true);
169 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
170 $(form.comment).prop("disabled", false);
174 content.find("input[type=submit]").on("click", function (e) {
176 var data = $(e.target).data();
177 updateNote(marker, e.target.form, data.method, data.url);
181 content.find("textarea").val(comment).trigger("input");
187 function createNote(marker, form, url) {
188 var location = marker.getLatLng();
190 marker.options.draggable = false;
191 marker.dragging.disable();
193 $(form).find("input[type=submit]").prop("disabled", true);
202 text: $(form.text).val()
204 success: function (feature) {
205 $(marker._popup._content).find("textarea").val("");
207 notes[feature.properties.id] = updateMarker(marker, feature);
210 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
215 function updateNote(marker, form, method, url) {
216 $(form).find("input[type=submit]").prop("disabled", true);
223 text: $(form.text).val()
225 success: function (feature) {
226 if (feature.properties.status == "hidden") {
227 noteLayer.removeLayer(marker);
229 delete notes[feature.properties.id];
231 var popupContent = createPopupContent(marker, feature.properties);
233 marker.setIcon(noteIcons[feature.properties.status]);
234 marker._popup.setContent(popupContent);
240 $(".leaflet-control-attribution").on("click", "#createnoteanchor", function (e) {
243 if ($(e.target).hasClass("disabled")) return;
245 $(e.target).removeClass("geolink").addClass("disabled");
247 map.addLayer(noteLayer);
249 var mapSize = map.getSize();
254 markerPosition = [mapSize.x / 2, mapSize.y / 2];
256 else if (mapSize.y > 400)
258 markerPosition = [mapSize.x / 2, 400];
262 markerPosition = [mapSize.x / 2, mapSize.y];
265 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
266 icon: noteIcons["new"],
271 var popupContent = $(JST["templates/notes/new"]({ create_url: $(e.target).attr("href") }));
273 popupContent.find("textarea").on("input", function (e) {
274 var form = e.target.form;
276 if ($(e.target).val() == "") {
277 $(form.add).prop("disabled", true);
279 $(form.add).prop("disabled", false);
283 popupContent.find("input[type=submit]").on("click", function (e) {
285 createNote(newNote, e.target.form, $(e.target).data("url"));
288 newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
290 newNote.on("remove", function (e) {
291 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
294 newNote.on("dragstart", function (e) {
295 $(newNote).stopTime("removenote");
298 newNote.on("dragend", function (e) {
299 e.target.openPopup();