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(marker, feature.properties));
94 marker = L.marker(feature.geometry.coordinates.reverse(), {
95 icon: noteIcons[feature.properties.status],
99 marker.addTo(noteLayer).bindPopup(
100 createPopupContent(marker, feature.properties),
110 function loadNotes() {
111 var bounds = map.getBounds();
112 var size = bounds.getSize();
114 if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
115 var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBOX();
117 if (noteLoader) noteLoader.abort();
119 noteLoader = $.ajax({
121 success: function (json) {
122 var oldNotes = notes;
126 json.features.forEach(function (feature) {
127 var marker = oldNotes[feature.properties.id];
129 delete oldNotes[feature.properties.id];
131 notes[feature.properties.id] = updateMarker(marker, feature);
134 for (id in oldNotes) {
135 noteLayer.removeLayer(oldNotes[id]);
144 function popupOptions() {
145 var mapSize = map.getSize();
149 maxWidth: mapSize.y * 1 / 3,
150 maxHeight: mapSize.y * 2 / 3,
151 offset: new L.Point(0, -3),
152 autoPanPadding: new L.Point(60, 40)
156 function createPopupContent(marker, properties) {
157 var content = $(JST["templates/notes/show"]({ note: properties }));
159 content.find("textarea").on("input", function (e) {
160 var form = e.target.form;
162 if ($(e.target).val() == "") {
163 $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
164 $(form.comment).prop("disabled", true);
166 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
167 $(form.comment).prop("disabled", false);
171 content.find("input[type=submit]").on("click", function (e) {
173 var data = $(e.target).data();
174 updateNote(marker, e.target.form, data.method, data.url);
180 function createNote(marker, form, url) {
181 var location = marker.getLatLng();
183 marker.options.draggable = false;
184 marker.dragging.disable();
186 $(form).find("input[type=submit]").prop("disabled", true);
195 text: $(form.text).val()
197 success: function (feature) {
198 notes[feature.properties.id] = updateMarker(marker, feature);
201 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
206 function updateNote(marker, form, method, url) {
207 $(form).find("input[type=submit]").prop("disabled", true);
214 text: $(form.text).val()
216 success: function (feature) {
217 if (feature.properties.status == "hidden") {
218 noteLayer.removeLayer(marker);
220 delete notes[feature.properties.id];
222 var popupContent = createPopupContent(marker, feature.properties);
224 marker.setIcon(noteIcons[feature.properties.status]);
225 marker._popup.setContent(popupContent);
231 $(".leaflet-control-attribution").on("click", "#createnoteanchor", function (e) {
234 if ($(e.target).hasClass("disabled")) return;
236 $(e.target).removeClass("geolink").addClass("disabled");
238 map.addLayer(noteLayer);
240 var mapSize = map.getSize();
245 markerPosition = [mapSize.x / 2, mapSize.y / 2];
247 else if (mapSize.y > 400)
249 markerPosition = [mapSize.x / 2, 400];
253 markerPosition = [mapSize.x / 2, mapSize.y];
256 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
257 icon: noteIcons["new"],
262 var popupContent = $(JST["templates/notes/new"]({ create_url: $(e.target).attr("href") }));
264 popupContent.find("textarea").on("input", function (e) {
265 var form = e.target.form;
267 if ($(e.target).val() == "") {
268 $(form.add).prop("disabled", true);
270 $(form.add).prop("disabled", false);
274 popupContent.find("input[type=submit]").on("click", function (e) {
276 createNote(newNote, e.target.form, $(e.target).data("url"));
279 newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
281 newNote.on("remove", function (e) {
282 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
285 newNote.on("dragstart", function (e) {
286 $(newNote).stopTime("removenote");
289 newNote.on("dragend", function (e) {
290 e.target.openPopup();