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);
52 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
53 map.layersControl.addOverlay(noteLayer, I18n.t("browse.start_rjs.notes_layer_name"));
55 if (params.notes) map.addLayer(noteLayer);
59 url: "/api/" + OSM.API_VERSION + "/notes/" + params.note + ".json",
60 success: function (feature) {
61 var marker = updateMarker(notes[feature.properties.id], feature);
63 notes[feature.properties.id] = marker;
65 map.addLayer(noteLayer);
72 function updateMarker(marker, feature) {
73 var icon = noteIcons[feature.properties.status];
74 var popupContent = createPopupContent(marker, feature.properties);
78 marker.setIcon(noteIcons[feature.properties.status]);
79 marker._popup.setContent(popupContent);
83 marker = L.marker(feature.geometry.coordinates.reverse(), {
88 marker.addTo(noteLayer).bindPopup(popupContent, popupOptions());
96 function loadNotes() {
97 var bounds = map.getBounds();
98 var size = bounds.getSize();
100 if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
101 var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBOX();
103 if (noteLoader) noteLoader.abort();
105 noteLoader = $.ajax({
107 success: function (json) {
108 var oldNotes = notes;
112 json.features.forEach(function (feature) {
113 var marker = oldNotes[feature.properties.id];
115 delete oldNotes[feature.properties.id];
117 notes[feature.properties.id] = updateMarker(marker, feature);
120 for (id in oldNotes) {
121 noteLayer.removeLayer(oldNotes[id]);
130 function popupOptions() {
131 var mapSize = map.getSize();
135 maxWidth: mapSize.y * 1 / 3,
136 maxHeight: mapSize.y * 2 / 3,
137 offset: new L.Point(0, -3),
138 autoPanPadding: new L.Point(60, 40)
142 function createPopupContent(marker, properties) {
143 var content = $(JST["templates/notes/show"]({ note: properties }));
145 content.find("textarea").on("input", function (e) {
146 var form = e.target.form;
148 if ($(e.target).val() == "") {
149 $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
150 $(form.comment).prop("disabled", true);
152 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
153 $(form.comment).prop("disabled", false);
157 content.find("input[type=submit]").on("click", function (e) {
159 updateNote(marker, e.target.form, $(e.target).data("url"));
165 function createNote(marker, form, url) {
166 var location = marker.getLatLng();
168 $(form).find("input[type=submit]").prop("disabled", true);
176 text: $(form.text).val()
178 success: function (feature) {
179 notes[feature.properties.id] = updateMarker(marker, feature);
182 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
187 function updateNote(marker, form, url) {
188 $(form).find("input[type=submit]").prop("disabled", true);
194 text: $(form.text).val()
196 success: function (feature) {
197 var popupContent = createPopupContent(marker, feature.properties);
199 marker.setIcon(noteIcons[feature.properties.status]);
200 marker._popup.setContent(popupContent);
205 $("#createnoteanchor").click(function (e) {
208 if ($(e.target).hasClass("disabled")) return;
210 $(e.target).removeClass("geolink").addClass("disabled");
212 map.addLayer(noteLayer);
214 var mapSize = map.getSize();
219 markerPosition = [mapSize.x / 2, mapSize.y / 2];
221 else if (mapSize.y > 400)
223 markerPosition = [mapSize.x / 2, 400];
227 markerPosition = [mapSize.x / 2, mapSize.y];
230 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
231 icon: noteIcons["new"],
236 var popupContent = $(JST["templates/notes/new"]({ create_url: $(e.target).attr("href") }));
238 popupContent.find("textarea").on("input", function (e) {
239 var form = e.target.form;
241 if ($(e.target).val() == "") {
242 $(form.add).prop("disabled", true);
244 $(form.add).prop("disabled", false);
248 popupContent.find("input[type=submit]").on("click", function (e) {
250 createNote(newNote, e.target.form, $(e.target).data("url"));
253 newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
255 newNote.on("remove", function (e) {
256 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
259 newNote.on("dragstart", function (e) {
260 $(newNote).stopTime("removenote");
263 newNote.on("dragend", function (e) {
264 e.target.openPopup();