1 //= require templates/notes/show
2 //= require templates/notes/new
4 $(document).ready(function () {
5 var params = OSM.mapParams(),
6 noteLayer = new L.LayerGroup({code: 'N'}),
12 iconUrl: "<%= image_path 'new_note_marker.png' %>",
17 iconUrl: "<%= image_path 'open_note_marker.png' %>",
22 iconUrl: "<%= image_path 'closed_note_marker.png' %>",
28 map.noteLayer = noteLayer;
30 map.on("layeradd", function (e) {
31 if (e.layer == noteLayer) {
33 map.on("moveend", loadNotes);
35 }).on("layerremove", function (e) {
36 if (e.layer == noteLayer) {
37 map.off("moveend", loadNotes);
38 noteLayer.clearLayers();
41 }).on("popupclose", function (e) {
42 if (newNote && e.popup == newNote._popup) {
43 $(newNote).oneTime(10, "removenote", function () {
44 map.removeLayer(newNote);
48 }).on("popupopen", function (e) {
49 if (!('ontouchstart' in document.documentElement)) {
50 $(e.popup._container).find(".comment").focus();
54 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
55 if (params.notes || params.layers.indexOf('N') >= 0) map.addLayer(noteLayer);
58 url: "/api/" + OSM.API_VERSION + "/notes/" + params.note + ".json",
59 success: function (feature) {
60 var marker = updateMarker(notes[feature.properties.id], feature);
61 notes[feature.properties.id] = marker;
62 map.addLayer(noteLayer);
69 function updateMarker(marker, feature) {
71 marker.setIcon(noteIcons[feature.properties.status]);
72 marker.setPopupContent(createPopupContent(
73 marker, feature.properties,
74 $(marker._popup._content).find("textarea").val()
77 marker = L.marker(feature.geometry.coordinates.reverse(), {
78 icon: noteIcons[feature.properties.status],
81 marker.addTo(noteLayer).bindPopup(
82 createPopupContent(marker, feature.properties),
91 function loadNotes() {
92 var bounds = map.getBounds();
93 var size = bounds.getSize();
95 if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
96 var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBoxString();
98 if (noteLoader) noteLoader.abort();
100 noteLoader = $.ajax({
106 function success(json) {
107 var oldNotes = notes;
109 json.features.forEach(updateMarkers);
111 function updateMarkers(feature) {
112 var marker = oldNotes[feature.properties.id];
113 delete oldNotes[feature.properties.id];
114 notes[feature.properties.id] = updateMarker(marker, feature);
117 for (id in oldNotes) {
118 noteLayer.removeLayer(oldNotes[id]);
125 function popupOptions() {
126 var mapSize = map.getSize();
130 maxWidth: mapSize.y * 1 / 3,
131 maxHeight: mapSize.y * 2 / 3,
132 offset: new L.Point(0, -40),
133 autoPanPadding: new L.Point(60, 40)
137 function createPopupContent(marker, properties, comment) {
138 var content = $(JST["templates/notes/show"]({ note: properties }));
140 content.find("textarea").on("input", function (e) {
141 var form = e.target.form;
143 if ($(e.target).val() == "") {
144 $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
145 $(form.comment).prop("disabled", true);
147 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
148 $(form.comment).prop("disabled", false);
152 content.find("input[type=submit]").on("click", function (e) {
154 var data = $(e.target).data();
155 updateNote(marker, e.target.form, data.method, data.url);
159 content.find("textarea").val(comment).trigger("input");
165 function createNote(marker, form, url) {
166 var location = marker.getLatLng();
168 marker.options.draggable = false;
169 marker.dragging.disable();
171 $(form).find("input[type=submit]").prop("disabled", true);
180 text: $(form.text).val()
185 function noteCreated(feature) {
186 $(marker._popup._content).find("textarea").val("");
188 notes[feature.properties.id] = updateMarker(marker, feature);
191 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
195 function updateNote(marker, form, method, url) {
196 $(form).find("input[type=submit]").prop("disabled", true);
203 text: $(form.text).val()
205 success: function (feature) {
206 if (feature.properties.status == "hidden") {
207 noteLayer.removeLayer(marker);
209 delete notes[feature.properties.id];
211 var popupContent = createPopupContent(marker, feature.properties);
213 marker.setIcon(noteIcons[feature.properties.status]);
214 marker.setPopupContent(popupContent);
220 $(".leaflet-control-attribution").on("click", "#createnoteanchor", function (e) {
223 if ($(e.target).hasClass("disabled")) return;
225 $(e.target).removeClass("geolink").addClass("disabled");
227 map.addLayer(noteLayer);
229 var mapSize = map.getSize();
232 if (mapSize.y > 800) {
233 markerPosition = [mapSize.x / 2, mapSize.y / 2];
234 } else if (mapSize.y > 400) {
235 markerPosition = [mapSize.x / 2, 400];
237 markerPosition = [mapSize.x / 2, mapSize.y];
240 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
241 icon: noteIcons["new"],
246 var popupContent = $(JST["templates/notes/new"]({
247 create_url: $(e.target).attr("href")
250 popupContent.find("textarea").on("input", disableWhenBlank);
252 function disableWhenBlank(e) {
253 $(e.target.form).prop("disabled", $(e.target).val() === "");
256 popupContent.find("input[type=submit]").on("click", function (e) {
258 createNote(newNote, e.target.form, $(e.target).data("url"));
261 newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
263 newNote.on("remove", function (e) {
264 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
265 }).on("dragstart", function (e) {
266 $(newNote).stopTime("removenote");
267 }).on("dragend", function (e) {
268 e.target.openPopup();