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.addTo(noteLayer).bindPopup(
76 createPopupContent(marker, feature.properties),
85 function loadNotes() {
86 var bounds = map.getBounds();
87 var size = bounds.getSize();
89 if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
90 var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBoxString();
92 if (noteLoader) noteLoader.abort();
100 function success(json) {
101 var oldNotes = notes;
103 json.features.forEach(updateMarkers);
105 function updateMarkers(feature) {
106 var marker = oldNotes[feature.properties.id];
107 delete oldNotes[feature.properties.id];
108 notes[feature.properties.id] = updateMarker(marker, feature);
111 for (id in oldNotes) {
112 noteLayer.removeLayer(oldNotes[id]);
119 function popupOptions() {
120 var mapSize = map.getSize();
124 maxWidth: mapSize.y * 1 / 3,
125 maxHeight: mapSize.y * 2 / 3,
126 offset: new L.Point(0, -40),
127 autoPanPadding: new L.Point(60, 40)
131 function createPopupContent(marker, properties, comment) {
132 var content = $(JST["templates/notes/show"]({ note: properties }));
134 content.find("textarea").on("input", function (e) {
135 var form = e.target.form;
137 if ($(e.target).val() == "") {
138 $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
139 $(form.comment).prop("disabled", true);
141 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
142 $(form.comment).prop("disabled", false);
146 content.find("input[type=submit]").on("click", function (e) {
148 var data = $(e.target).data();
149 updateNote(marker, e.target.form, data.method, data.url);
153 content.find("textarea").val(comment).trigger("input");
159 var addNoteButton = $(".control-note .control-button");
161 function createNote(marker, form, url) {
162 var location = marker.getLatLng();
164 marker.options.draggable = false;
165 marker.dragging.disable();
167 $(form).find("input[type=submit]").prop("disabled", true);
176 text: $(form.text).val()
181 function noteCreated(feature) {
182 $(marker._popup._content).find("textarea").val("");
184 notes[feature.properties.id] = updateMarker(marker, feature);
187 addNoteButton.removeClass("active");
191 function updateNote(marker, form, method, url) {
192 $(form).find("input[type=submit]").prop("disabled", true);
199 text: $(form.text).val()
201 success: function (feature) {
202 if (feature.properties.status == "hidden") {
203 noteLayer.removeLayer(marker);
205 delete notes[feature.properties.id];
207 var popupContent = createPopupContent(marker, feature.properties);
209 marker.setIcon(noteIcons[feature.properties.status]);
210 marker.setPopupContent(popupContent);
216 addNoteButton.on("click", function (e) {
220 if (addNoteButton.hasClass("disabled")) return;
221 if (addNoteButton.hasClass("active")) return;
223 addNoteButton.addClass("active");
225 map.addLayer(noteLayer);
227 var mapSize = map.getSize();
230 if (mapSize.y > 800) {
231 markerPosition = [mapSize.x / 2, mapSize.y / 2];
232 } else if (mapSize.y > 400) {
233 markerPosition = [mapSize.x / 2, 400];
235 markerPosition = [mapSize.x / 2, mapSize.y];
238 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
239 icon: noteIcons["new"],
244 var popupContent = $(JST["templates/notes/new"]());
246 popupContent.find("textarea").on("input", disableWhenBlank);
248 function disableWhenBlank(e) {
249 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
252 popupContent.find("input[type=submit]").on("click", function (e) {
254 createNote(newNote, e.target.form, '/api/0.6/notes.json');
257 newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
259 newNote.on("remove", function (e) {
260 addNoteButton.removeClass("active");
261 }).on("dragstart", function (e) {
262 $(newNote).stopTime("removenote");
263 }).on("dragend", function (e) {
264 e.target.openPopup();