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 $(form).find("input[type=submit]").prop("disabled", true);
183 text: $(form.text).val()
185 success: function (feature) {
186 notes[feature.properties.id] = updateMarker(marker, feature);
189 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
194 function updateNote(marker, form, method, url) {
195 $(form).find("input[type=submit]").prop("disabled", true);
202 text: $(form.text).val()
204 success: function (feature) {
205 if (feature.properties.status == "hidden") {
206 noteLayer.removeLayer(marker);
208 delete notes[feature.properties.id];
210 var popupContent = createPopupContent(marker, feature.properties);
212 marker.setIcon(noteIcons[feature.properties.status]);
213 marker._popup.setContent(popupContent);
219 $("#createnoteanchor").click(function (e) {
222 if ($(e.target).hasClass("disabled")) return;
224 $(e.target).removeClass("geolink").addClass("disabled");
226 map.addLayer(noteLayer);
228 var mapSize = map.getSize();
233 markerPosition = [mapSize.x / 2, mapSize.y / 2];
235 else if (mapSize.y > 400)
237 markerPosition = [mapSize.x / 2, 400];
241 markerPosition = [mapSize.x / 2, mapSize.y];
244 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
245 icon: noteIcons["new"],
250 var popupContent = $(JST["templates/notes/new"]({ create_url: $(e.target).attr("href") }));
252 popupContent.find("textarea").on("input", function (e) {
253 var form = e.target.form;
255 if ($(e.target).val() == "") {
256 $(form.add).prop("disabled", true);
258 $(form.add).prop("disabled", false);
262 popupContent.find("input[type=submit]").on("click", function (e) {
264 createNote(newNote, e.target.form, $(e.target).data("url"));
267 newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
269 newNote.on("remove", function (e) {
270 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
273 newNote.on("dragstart", function (e) {
274 $(newNote).stopTime("removenote");
277 newNote.on("dragend", function (e) {
278 e.target.openPopup();