1 //= require templates/notes/show
2 //= require templates/notes/new
4 $(document).ready(function () {
5 var params = OSM.mapParams(),
6 noteLayer = new L.LayerGroup(),
12 iconUrl: "<%= image_path 'new_note_marker.png' %>",
17 iconUrl: "<%= image_path 'open_note_marker.png' %>",
22 iconUrl: "<%= image_path 'closed_note_marker.png' %>",
33 map.noteLayer = noteLayer;
35 map.on("layeradd", function (e) {
36 if (e.layer == noteLayer) {
38 map.on("moveend", loadNotes);
40 }).on("layerremove", function (e) {
41 if (e.layer == noteLayer) {
42 map.off("moveend", loadNotes);
43 noteLayer.clearLayers();
46 }).on("popupclose", function (e) {
47 if (newNote && e.popup == newNote._popup) {
48 $(newNote).oneTime(10, "removenote", function () {
49 map.removeLayer(newNote);
53 }).on("popupopen", function (e) {
54 if (!('ontouchstart' in document.documentElement)) {
55 $(e.popup._container).find(".comment").focus();
59 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
60 if (params.layers) setMapLayers(params.layers);
61 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);
67 notes[feature.properties.id] = marker;
68 map.addLayer(noteLayer);
75 function updateMarker(marker, feature) {
77 marker.setIcon(noteIcons[feature.properties.status]);
78 marker.setPopupContent(createPopupContent(
79 marker, feature.properties,
80 $(marker._popup._content).find("textarea").val()
83 marker = L.marker(feature.geometry.coordinates.reverse(), {
84 icon: noteIcons[feature.properties.status],
87 marker.addTo(noteLayer).bindPopup(
88 createPopupContent(marker, feature.properties),
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.toBBoxString();
104 if (noteLoader) noteLoader.abort();
106 noteLoader = $.ajax({
112 function success(json) {
113 var oldNotes = notes;
115 json.features.forEach(updateMarkers);
117 function updateMarkers(feature) {
118 var marker = oldNotes[feature.properties.id];
119 delete oldNotes[feature.properties.id];
120 notes[feature.properties.id] = updateMarker(marker, feature);
123 for (id in oldNotes) {
124 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, -40),
139 autoPanPadding: new L.Point(60, 40)
143 function createPopupContent(marker, properties, comment) {
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 var data = $(e.target).data();
161 updateNote(marker, e.target.form, data.method, data.url);
165 content.find("textarea").val(comment).trigger("input");
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()
191 function noteCreated(feature) {
192 $(marker._popup._content).find("textarea").val("");
194 notes[feature.properties.id] = updateMarker(marker, feature);
197 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
201 function updateNote(marker, form, method, url) {
202 $(form).find("input[type=submit]").prop("disabled", true);
209 text: $(form.text).val()
211 success: function (feature) {
212 if (feature.properties.status == "hidden") {
213 noteLayer.removeLayer(marker);
215 delete notes[feature.properties.id];
217 var popupContent = createPopupContent(marker, feature.properties);
219 marker.setIcon(noteIcons[feature.properties.status]);
220 marker.setPopupContent(popupContent);
226 $(".leaflet-control-attribution").on("click", "#createnoteanchor", function (e) {
229 if ($(e.target).hasClass("disabled")) return;
231 $(e.target).removeClass("geolink").addClass("disabled");
233 map.addLayer(noteLayer);
235 var mapSize = map.getSize();
238 if (mapSize.y > 800) {
239 markerPosition = [mapSize.x / 2, mapSize.y / 2];
240 } else if (mapSize.y > 400) {
241 markerPosition = [mapSize.x / 2, 400];
243 markerPosition = [mapSize.x / 2, mapSize.y];
246 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
247 icon: noteIcons["new"],
252 var popupContent = $(JST["templates/notes/new"]({
253 create_url: $(e.target).attr("href")
256 popupContent.find("textarea").on("input", disableWhenBlank);
258 function disableWhenBlank(e) {
259 $(e.target.form).prop("disabled", $(e.target).val() === "");
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");
271 }).on("dragstart", function (e) {
272 $(newNote).stopTime("removenote");
273 }).on("dragend", function (e) {
274 e.target.openPopup();