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("popupclose", function (e) {
45 if (newNote && e.popup == newNote._popup) {
46 $(newNote).oneTime(10, "removenote", function () {
47 map.removeLayer(newNote);
53 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
54 map.layersControl.addOverlay(noteLayer, I18n.t("browse.start_rjs.notes_layer_name"));
56 if (params.notes) map.addLayer(noteLayer);
60 url: "/api/" + OSM.API_VERSION + "/notes/" + params.note + ".json",
61 success: function (feature) {
62 var marker = updateMarker(notes[feature.properties.id], feature);
64 notes[feature.properties.id] = marker;
66 map.addLayer(noteLayer);
73 function updateMarker(marker, feature) {
74 var icon = noteIcons[feature.properties.status];
75 var popupContent = createPopupContent(marker, feature.properties);
79 marker.setIcon(noteIcons[feature.properties.status]);
80 marker._popup.setContent(popupContent);
84 marker = L.marker(feature.geometry.coordinates.reverse(), {
89 marker.addTo(noteLayer).bindPopup(popupContent, popupOptions());
97 function loadNotes() {
98 var bounds = map.getBounds();
99 var size = bounds.getSize();
101 if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
102 var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBOX();
104 if (noteLoader) noteLoader.abort();
106 noteLoader = $.ajax({
108 success: function (json) {
109 var oldNotes = notes;
113 json.features.forEach(function (feature) {
114 var marker = oldNotes[feature.properties.id];
116 delete oldNotes[feature.properties.id];
118 notes[feature.properties.id] = updateMarker(marker, feature);
121 for (id in oldNotes) {
122 noteLayer.removeLayer(oldNotes[id]);
131 function popupOptions() {
132 var mapSize = map.getSize();
136 maxWidth: mapSize.y * 1 / 3,
137 maxHeight: mapSize.y * 2 / 3,
138 offset: new L.Point(0, -3),
139 autoPanPadding: new L.Point(60, 40)
143 function createPopupContent(marker, properties) {
144 var content = $(JST["templates/notes/show"]({ note: properties }));
146 content.find("textarea").on("input", function (e) {
147 var form = e.target.form;
149 if ($(e.target).val() == "") {
150 $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
151 $(form.comment).prop("disabled", true);
153 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
154 $(form.comment).prop("disabled", false);
158 content.find("input[type=submit]").on("click", function (e) {
160 updateNote(marker, e.target.form, $(e.target).data("url"));
166 function createNote(marker, form, url) {
167 var location = marker.getLatLng();
169 $(form).find("input[type=submit]").prop("disabled", true);
178 text: $(form.text).val()
180 success: function (feature) {
181 notes[feature.properties.id] = updateMarker(marker, feature);
184 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
189 function updateNote(marker, form, url) {
190 $(form).find("input[type=submit]").prop("disabled", true);
197 text: $(form.text).val()
199 success: function (feature) {
200 var popupContent = createPopupContent(marker, feature.properties);
202 marker.setIcon(noteIcons[feature.properties.status]);
203 marker._popup.setContent(popupContent);
208 $("#createnoteanchor").click(function (e) {
211 if ($(e.target).hasClass("disabled")) return;
213 $(e.target).removeClass("geolink").addClass("disabled");
215 map.addLayer(noteLayer);
217 var mapSize = map.getSize();
222 markerPosition = [mapSize.x / 2, mapSize.y / 2];
224 else if (mapSize.y > 400)
226 markerPosition = [mapSize.x / 2, 400];
230 markerPosition = [mapSize.x / 2, mapSize.y];
233 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
234 icon: noteIcons["new"],
239 var popupContent = $(JST["templates/notes/new"]({ create_url: $(e.target).attr("href") }));
241 popupContent.find("textarea").on("input", function (e) {
242 var form = e.target.form;
244 if ($(e.target).val() == "") {
245 $(form.add).prop("disabled", true);
247 $(form.add).prop("disabled", false);
251 popupContent.find("input[type=submit]").on("click", function (e) {
253 createNote(newNote, e.target.form, $(e.target).data("url"));
256 newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
258 newNote.on("remove", function (e) {
259 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
262 newNote.on("dragstart", function (e) {
263 $(newNote).stopTime("removenote");
266 newNote.on("dragend", function (e) {
267 e.target.openPopup();