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();
44 map.on("popupopen", function (e) {
45 $(e.popup._container).find(".comment").focus();
48 map.on("popupclose", function (e) {
49 if (newNote && e.popup == newNote._popup) {
50 $(newNote).oneTime(10, "removenote", function () {
51 map.removeLayer(newNote);
57 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
58 map.layersControl.addOverlay(noteLayer, I18n.t("browse.start_rjs.notes_layer_name"));
60 if (params.notes) map.addLayer(noteLayer);
64 url: "/api/" + OSM.API_VERSION + "/notes/" + params.note + ".json",
65 success: function (feature) {
66 var marker = updateMarker(notes[feature.properties.id], feature);
68 notes[feature.properties.id] = marker;
70 map.addLayer(noteLayer);
77 function updateMarker(marker, feature) {
80 marker.setIcon(noteIcons[feature.properties.status]);
81 marker._popup.setContent(createPopupContent(marker, feature.properties));
85 marker = L.marker(feature.geometry.coordinates.reverse(), {
86 icon: noteIcons[feature.properties.status],
90 marker.addTo(noteLayer).bindPopup(
91 createPopupContent(marker, feature.properties),
101 function loadNotes() {
102 var bounds = map.getBounds();
103 var size = bounds.getSize();
105 if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
106 var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBOX();
108 if (noteLoader) noteLoader.abort();
110 noteLoader = $.ajax({
112 success: function (json) {
113 var oldNotes = notes;
117 json.features.forEach(function (feature) {
118 var marker = oldNotes[feature.properties.id];
120 delete oldNotes[feature.properties.id];
122 notes[feature.properties.id] = updateMarker(marker, feature);
125 for (id in oldNotes) {
126 noteLayer.removeLayer(oldNotes[id]);
135 function popupOptions() {
136 var mapSize = map.getSize();
140 maxWidth: mapSize.y * 1 / 3,
141 maxHeight: mapSize.y * 2 / 3,
142 offset: new L.Point(0, -3),
143 autoPanPadding: new L.Point(60, 40)
147 function createPopupContent(marker, properties) {
148 var content = $(JST["templates/notes/show"]({ note: properties }));
150 content.find("textarea").on("input", function (e) {
151 var form = e.target.form;
153 if ($(e.target).val() == "") {
154 $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
155 $(form.comment).prop("disabled", true);
157 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
158 $(form.comment).prop("disabled", false);
162 content.find("input[type=submit]").on("click", function (e) {
164 var data = $(e.target).data();
165 updateNote(marker, e.target.form, data.method, data.url);
171 function createNote(marker, form, url) {
172 var location = marker.getLatLng();
174 marker.options.draggable = false;
175 marker.dragging.disable();
177 $(form).find("input[type=submit]").prop("disabled", true);
186 text: $(form.text).val()
188 success: function (feature) {
189 notes[feature.properties.id] = updateMarker(marker, feature);
192 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
197 function updateNote(marker, form, method, url) {
198 $(form).find("input[type=submit]").prop("disabled", true);
205 text: $(form.text).val()
207 success: function (feature) {
208 if (feature.properties.status == "hidden") {
209 noteLayer.removeLayer(marker);
211 delete notes[feature.properties.id];
213 var popupContent = createPopupContent(marker, feature.properties);
215 marker.setIcon(noteIcons[feature.properties.status]);
216 marker._popup.setContent(popupContent);
222 $(".leaflet-control-attribution").on("click", "#createnoteanchor", function (e) {
225 if ($(e.target).hasClass("disabled")) return;
227 $(e.target).removeClass("geolink").addClass("disabled");
229 map.addLayer(noteLayer);
231 var mapSize = map.getSize();
236 markerPosition = [mapSize.x / 2, mapSize.y / 2];
238 else if (mapSize.y > 400)
240 markerPosition = [mapSize.x / 2, 400];
244 markerPosition = [mapSize.x / 2, mapSize.y];
247 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
248 icon: noteIcons["new"],
253 var popupContent = $(JST["templates/notes/new"]({ create_url: $(e.target).attr("href") }));
255 popupContent.find("textarea").on("input", function (e) {
256 var form = e.target.form;
258 if ($(e.target).val() == "") {
259 $(form.add).prop("disabled", true);
261 $(form.add).prop("disabled", false);
265 popupContent.find("input[type=submit]").on("click", function (e) {
267 createNote(newNote, e.target.form, $(e.target).data("url"));
270 newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
272 newNote.on("remove", function (e) {
273 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
276 newNote.on("dragstart", function (e) {
277 $(newNote).stopTime("removenote");
280 newNote.on("dragend", function (e) {
281 e.target.openPopup();