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);
68 notes[feature.properties.id] = marker;
70 map.addLayer(noteLayer);
77 function updateMarker(marker, feature) {
79 marker.setIcon(noteIcons[feature.properties.status]);
80 marker.setPopupContent(createPopupContent(
81 marker, feature.properties,
82 $(marker._popup._content).find("textarea").val()
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.toBBoxString();
108 if (noteLoader) noteLoader.abort();
110 noteLoader = $.ajax({
116 function success(json) {
117 var oldNotes = notes;
121 json.features.forEach(updateMarkers);
123 function updateMarkers(feature) {
124 var marker = oldNotes[feature.properties.id];
125 delete oldNotes[feature.properties.id];
126 notes[feature.properties.id] = updateMarker(marker, feature);
129 for (id in oldNotes) {
130 noteLayer.removeLayer(oldNotes[id]);
137 function popupOptions() {
138 var mapSize = map.getSize();
142 maxWidth: mapSize.y * 1 / 3,
143 maxHeight: mapSize.y * 2 / 3,
144 offset: new L.Point(0, -40),
145 autoPanPadding: new L.Point(60, 40)
149 function createPopupContent(marker, properties, comment) {
150 var content = $(JST["templates/notes/show"]({ note: properties }));
152 content.find("textarea").on("input", function (e) {
153 var form = e.target.form;
155 if ($(e.target).val() == "") {
156 $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
157 $(form.comment).prop("disabled", true);
159 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
160 $(form.comment).prop("disabled", false);
164 content.find("input[type=submit]").on("click", function (e) {
166 var data = $(e.target).data();
167 updateNote(marker, e.target.form, data.method, data.url);
171 content.find("textarea").val(comment).trigger("input");
177 function createNote(marker, form, url) {
178 var location = marker.getLatLng();
180 marker.options.draggable = false;
181 marker.dragging.disable();
183 $(form).find("input[type=submit]").prop("disabled", true);
192 text: $(form.text).val()
197 function noteCreated(feature) {
198 $(marker._popup._content).find("textarea").val("");
200 notes[feature.properties.id] = updateMarker(marker, feature);
203 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
207 function updateNote(marker, form, method, url) {
208 $(form).find("input[type=submit]").prop("disabled", true);
215 text: $(form.text).val()
217 success: function (feature) {
218 if (feature.properties.status == "hidden") {
219 noteLayer.removeLayer(marker);
221 delete notes[feature.properties.id];
223 var popupContent = createPopupContent(marker, feature.properties);
225 marker.setIcon(noteIcons[feature.properties.status]);
226 marker.setPopupContent(popupContent);
232 $(".leaflet-control-attribution").on("click", "#createnoteanchor", function (e) {
235 if ($(e.target).hasClass("disabled")) return;
237 $(e.target).removeClass("geolink").addClass("disabled");
239 map.addLayer(noteLayer);
241 var mapSize = map.getSize();
244 if (mapSize.y > 800) {
245 markerPosition = [mapSize.x / 2, mapSize.y / 2];
246 } else if (mapSize.y > 400) {
247 markerPosition = [mapSize.x / 2, 400];
249 markerPosition = [mapSize.x / 2, mapSize.y];
252 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
253 icon: noteIcons["new"],
258 var popupContent = $(JST["templates/notes/new"]({
259 create_url: $(e.target).attr("href")
262 popupContent.find("textarea").on("input", disableWhenBlank);
264 function disableWhenBlank(e) {
265 $(e.target.form).prop("disabled", $(e.target).val() === "");
268 popupContent.find("input[type=submit]").on("click", function (e) {
270 createNote(newNote, e.target.form, $(e.target).data("url"));
273 newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
275 newNote.on("remove", function (e) {
276 $("#createnoteanchor").removeClass("disabled").addClass("geolink");
279 newNote.on("dragstart", function (e) {
280 $(newNote).stopTime("removenote");
283 newNote.on("dragend", function (e) {
284 e.target.openPopup();