1 //= require templates/notes/show
2 //= require templates/notes/new
4 function initializeNotes(map, params) {
5 var noteLayer = map.noteLayer,
11 iconUrl: "<%= image_path 'new_note_marker.png' %>",
16 iconUrl: "<%= image_path 'open_note_marker.png' %>",
21 iconUrl: "<%= image_path 'closed_note_marker.png' %>",
27 map.on("layeradd", function (e) {
28 if (e.layer == noteLayer) {
30 map.on("moveend", loadNotes);
32 }).on("layerremove", function (e) {
33 if (e.layer == noteLayer) {
34 map.off("moveend", loadNotes);
35 noteLayer.clearLayers();
38 }).on("popupclose", function (e) {
39 if (newNote && e.popup == newNote._popup) {
40 $(newNote).oneTime(10, "removenote", function () {
41 map.removeLayer(newNote);
45 }).on("popupopen", function (e) {
46 if (!('ontouchstart' in document.documentElement)) {
47 $(e.popup._container).find(".comment").focus();
51 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
52 if (params.layers.indexOf(noteLayer.options.code) >= 0) {
53 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 var addNoteButton = $(".control-note .control-button");
167 function createNote(marker, form, url) {
168 var location = marker.getLatLng();
170 marker.options.draggable = false;
171 marker.dragging.disable();
173 $(form).find("input[type=submit]").prop("disabled", true);
182 text: $(form.text).val()
187 function noteCreated(feature) {
188 $(marker._popup._content).find("textarea").val("");
190 notes[feature.properties.id] = updateMarker(marker, feature);
193 addNoteButton.removeClass("active");
197 function updateNote(marker, form, method, url) {
198 $(form).find("input[type=submit]").prop("disabled", true);
205 text: $(form.text).val()
207 success: function (feature) {
208 if (feature.properties.status == "hidden") {
209 noteLayer.removeLayer(marker);
211 delete notes[feature.properties.id];
213 var popupContent = createPopupContent(marker, feature.properties);
215 marker.setIcon(noteIcons[feature.properties.status]);
216 marker.setPopupContent(popupContent);
222 addNoteButton.on("click", function (e) {
226 if (addNoteButton.hasClass("disabled")) return;
227 if (addNoteButton.hasClass("active")) return;
229 addNoteButton.addClass("active");
231 map.addLayer(noteLayer);
233 var mapSize = map.getSize();
236 if (mapSize.y > 800) {
237 markerPosition = [mapSize.x / 2, mapSize.y / 2];
238 } else if (mapSize.y > 400) {
239 markerPosition = [mapSize.x / 2, 400];
241 markerPosition = [mapSize.x / 2, mapSize.y];
244 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
245 icon: noteIcons["new"],
250 var popupContent = $(JST["templates/notes/new"]());
252 popupContent.find("textarea").on("input", disableWhenBlank);
254 function disableWhenBlank(e) {
255 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
258 popupContent.find("input[type=submit]").on("click", function (e) {
260 createNote(newNote, e.target.form, '/api/0.6/notes.json');
263 newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
265 newNote.on("remove", function (e) {
266 addNoteButton.removeClass("active");
267 }).on("dragstart", function (e) {
268 $(newNote).stopTime("removenote");
269 }).on("dragend", function (e) {
270 e.target.openPopup();