1 //= require templates/notes/show
2 //= require templates/notes/new
4 function initializeNotes(map) {
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 noteLayer.showNote = function(id) {
53 url: "/api/" + OSM.API_VERSION + "/notes/" + id + ".json",
54 success: function (feature) {
55 var marker = updateMarker(notes[feature.properties.id], feature);
56 notes[feature.properties.id] = marker;
57 map.addLayer(noteLayer);
63 function updateMarker(marker, feature) {
65 marker.setIcon(noteIcons[feature.properties.status]);
66 marker.setPopupContent(createPopupContent(
67 marker, feature.properties,
68 $(marker._popup._content).find("textarea").val()
71 marker = L.marker(feature.geometry.coordinates.reverse(), {
72 icon: noteIcons[feature.properties.status],
75 marker.id = feature.properties.id;
76 marker.addTo(noteLayer).bindPopup(
77 createPopupContent(marker, feature.properties),
84 noteLayer.getLayerId = function(marker) {
90 function loadNotes() {
91 var bounds = map.getBounds();
92 var size = bounds.getSize();
94 if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
95 var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBoxString();
97 if (noteLoader) noteLoader.abort();
105 function success(json) {
106 var oldNotes = notes;
108 json.features.forEach(updateMarkers);
110 function updateMarkers(feature) {
111 var marker = oldNotes[feature.properties.id];
112 delete oldNotes[feature.properties.id];
113 notes[feature.properties.id] = updateMarker(marker, feature);
116 for (id in oldNotes) {
117 noteLayer.removeLayer(oldNotes[id]);
124 function popupOptions() {
125 var mapSize = map.getSize();
129 maxWidth: mapSize.y * 1 / 3,
130 maxHeight: mapSize.y * 2 / 3,
131 offset: new L.Point(0, -40),
132 autoPanPadding: new L.Point(60, 40)
136 function createPopupContent(marker, properties, comment) {
137 var content = $(JST["templates/notes/show"]({ note: properties }));
139 content.find("textarea").on("input", function (e) {
140 var form = e.target.form;
142 if ($(e.target).val() == "") {
143 $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
144 $(form.comment).prop("disabled", true);
146 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
147 $(form.comment).prop("disabled", false);
151 content.find("input[type=submit]").on("click", function (e) {
153 var data = $(e.target).data();
154 updateNote(marker, e.target.form, data.method, data.url);
158 content.find("textarea").val(comment).trigger("input");
164 var addNoteButton = $(".control-note .control-button");
166 function createNote(marker, form, url) {
167 var location = marker.getLatLng();
169 marker.options.draggable = false;
170 marker.dragging.disable();
172 $(form).find("input[type=submit]").prop("disabled", true);
181 text: $(form.text).val()
186 function noteCreated(feature) {
187 $(marker._popup._content).find("textarea").val("");
189 notes[feature.properties.id] = updateMarker(marker, feature);
192 addNoteButton.removeClass("active");
196 function updateNote(marker, form, method, url) {
197 $(form).find("input[type=submit]").prop("disabled", true);
204 text: $(form.text).val()
206 success: function (feature) {
207 if (feature.properties.status == "hidden") {
208 noteLayer.removeLayer(marker);
210 delete notes[feature.properties.id];
212 var popupContent = createPopupContent(marker, feature.properties);
214 marker.setIcon(noteIcons[feature.properties.status]);
215 marker.setPopupContent(popupContent);
221 addNoteButton.on("click", function (e) {
225 if (addNoteButton.hasClass("disabled")) return;
226 if (addNoteButton.hasClass("active")) return;
228 addNoteButton.addClass("active");
230 map.addLayer(noteLayer);
232 var mapSize = map.getSize();
235 if (mapSize.y > 800) {
236 markerPosition = [mapSize.x / 2, mapSize.y / 2];
237 } else if (mapSize.y > 400) {
238 markerPosition = [mapSize.x / 2, 400];
240 markerPosition = [mapSize.x / 2, mapSize.y];
243 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
244 icon: noteIcons["new"],
249 var popupContent = $(JST["templates/notes/new"]());
251 popupContent.find("textarea").on("input", disableWhenBlank);
253 function disableWhenBlank(e) {
254 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
257 popupContent.find("input[type=submit]").on("click", function (e) {
259 createNote(newNote, e.target.form, '/api/0.6/notes.json');
262 newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
264 newNote.on("remove", function (e) {
265 addNoteButton.removeClass("active");
266 }).on("dragstart", function (e) {
267 $(newNote).stopTime("removenote");
268 }).on("dragend", function (e) {
269 e.target.openPopup();