1 //= require templates/notes/show
2 //= require templates/notes/new
4 function initializeNotes(map) {
5 var params = OSM.mapParams(),
6 noteLayer = map.noteLayer,
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.on("layeradd", function (e) {
29 if (e.layer == noteLayer) {
31 map.on("moveend", loadNotes);
33 }).on("layerremove", function (e) {
34 if (e.layer == noteLayer) {
35 map.off("moveend", loadNotes);
36 noteLayer.clearLayers();
39 }).on("popupclose", function (e) {
40 if (newNote && e.popup == newNote._popup) {
41 $(newNote).oneTime(10, "removenote", function () {
42 map.removeLayer(newNote);
46 }).on("popupopen", function (e) {
47 if (!('ontouchstart' in document.documentElement)) {
48 $(e.popup._container).find(".comment").focus();
52 if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
53 if (params.notes || (params.layers && params.layers.indexOf('N')) >= 0) {
54 map.addLayer(noteLayer);
59 url: "/api/" + OSM.API_VERSION + "/notes/" + params.note + ".json",
60 success: function (feature) {
61 var marker = updateMarker(notes[feature.properties.id], feature);
62 notes[feature.properties.id] = marker;
63 map.addLayer(noteLayer);
70 function updateMarker(marker, feature) {
72 marker.setIcon(noteIcons[feature.properties.status]);
73 marker.setPopupContent(createPopupContent(
74 marker, feature.properties,
75 $(marker._popup._content).find("textarea").val()
78 marker = L.marker(feature.geometry.coordinates.reverse(), {
79 icon: noteIcons[feature.properties.status],
82 marker.addTo(noteLayer).bindPopup(
83 createPopupContent(marker, feature.properties),
92 function loadNotes() {
93 var bounds = map.getBounds();
94 var size = bounds.getSize();
96 if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
97 var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBoxString();
99 if (noteLoader) noteLoader.abort();
101 noteLoader = $.ajax({
107 function success(json) {
108 var oldNotes = notes;
110 json.features.forEach(updateMarkers);
112 function updateMarkers(feature) {
113 var marker = oldNotes[feature.properties.id];
114 delete oldNotes[feature.properties.id];
115 notes[feature.properties.id] = updateMarker(marker, feature);
118 for (id in oldNotes) {
119 noteLayer.removeLayer(oldNotes[id]);
126 function popupOptions() {
127 var mapSize = map.getSize();
131 maxWidth: mapSize.y * 1 / 3,
132 maxHeight: mapSize.y * 2 / 3,
133 offset: new L.Point(0, -40),
134 autoPanPadding: new L.Point(60, 40)
138 function createPopupContent(marker, properties, comment) {
139 var content = $(JST["templates/notes/show"]({ note: properties }));
141 content.find("textarea").on("input", function (e) {
142 var form = e.target.form;
144 if ($(e.target).val() == "") {
145 $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
146 $(form.comment).prop("disabled", true);
148 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
149 $(form.comment).prop("disabled", false);
153 content.find("input[type=submit]").on("click", function (e) {
155 var data = $(e.target).data();
156 updateNote(marker, e.target.form, data.method, data.url);
160 content.find("textarea").val(comment).trigger("input");
166 var addNoteButton = $(".control-note .control-button");
168 function createNote(marker, form, url) {
169 var location = marker.getLatLng();
171 marker.options.draggable = false;
172 marker.dragging.disable();
174 $(form).find("input[type=submit]").prop("disabled", true);
183 text: $(form.text).val()
188 function noteCreated(feature) {
189 $(marker._popup._content).find("textarea").val("");
191 notes[feature.properties.id] = updateMarker(marker, feature);
194 addNoteButton.removeClass("disabled");
198 function updateNote(marker, form, method, url) {
199 $(form).find("input[type=submit]").prop("disabled", true);
206 text: $(form.text).val()
208 success: function (feature) {
209 if (feature.properties.status == "hidden") {
210 noteLayer.removeLayer(marker);
212 delete notes[feature.properties.id];
214 var popupContent = createPopupContent(marker, feature.properties);
216 marker.setIcon(noteIcons[feature.properties.status]);
217 marker.setPopupContent(popupContent);
223 addNoteButton.on("click", function (e) {
227 if (addNoteButton.hasClass("disabled")) return;
229 addNoteButton.addClass("disabled");
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("disabled");
267 }).on("dragstart", function (e) {
268 $(newNote).stopTime("removenote");
269 }).on("dragend", function (e) {
270 e.target.openPopup();