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);
47 noteLayer.on('click', function(e) {
48 OSM.route('/browse/note/' + e.layer.id);
51 function updateMarker(marker, feature) {
53 marker.setIcon(noteIcons[feature.properties.status]);
55 marker = L.marker(feature.geometry.coordinates.reverse(), {
56 icon: noteIcons[feature.properties.status],
60 marker.id = feature.properties.id;
61 marker.addTo(noteLayer);
66 noteLayer.getLayerId = function(marker) {
72 function loadNotes() {
73 var bounds = map.getBounds();
74 var size = bounds.getSize();
76 if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
77 var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBoxString();
79 if (noteLoader) noteLoader.abort();
87 function success(json) {
90 json.features.forEach(updateMarkers);
92 function updateMarkers(feature) {
93 var marker = oldNotes[feature.properties.id];
94 delete oldNotes[feature.properties.id];
95 notes[feature.properties.id] = updateMarker(marker, feature);
98 for (id in oldNotes) {
99 noteLayer.removeLayer(oldNotes[id]);
106 function popupOptions() {
107 var mapSize = map.getSize();
111 maxWidth: mapSize.y * 1 / 3,
112 maxHeight: mapSize.y * 2 / 3,
113 offset: new L.Point(0, -40),
114 autoPanPadding: new L.Point(60, 40)
118 function createPopupContent(marker, properties, comment) {
119 var content = $(JST["templates/notes/show"]({ note: properties }));
121 content.find("textarea").on("input", function (e) {
122 var form = e.target.form;
124 if ($(e.target).val() == "") {
125 $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
126 $(form.comment).prop("disabled", true);
128 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
129 $(form.comment).prop("disabled", false);
133 content.find("input[type=submit]").on("click", function (e) {
135 var data = $(e.target).data();
136 updateNote(marker, e.target.form, data.method, data.url);
140 content.find("textarea").val(comment).trigger("input");
146 var addNoteButton = $(".control-note .control-button");
148 function createNote(marker, form, url) {
149 var location = marker.getLatLng();
151 marker.options.draggable = false;
152 marker.dragging.disable();
154 $(form).find("input[type=submit]").prop("disabled", true);
163 text: $(form.text).val()
165 success: function(feature) {
166 noteCreated(feature, marker);
170 function noteCreated(feature, marker) {
171 $(marker._popup._content).find("textarea").val("");
173 notes[feature.properties.id] = updateMarker(false, feature);
174 OSM.route('/browse/note/' + feature.properties.id);
177 noteLayer.removeLayer(marker);
178 addNoteButton.removeClass("active");
182 function updateNote(marker, form, method, url) {
183 $(form).find("input[type=submit]").prop("disabled", true);
190 text: $(form.text).val()
192 success: function (feature) {
193 if (feature.properties.status == "hidden") {
194 noteLayer.removeLayer(marker);
196 delete notes[feature.properties.id];
198 var popupContent = createPopupContent(marker, feature.properties);
200 marker.setIcon(noteIcons[feature.properties.status]);
201 marker.setPopupContent(popupContent);
207 addNoteButton.on("click", function (e) {
211 if (addNoteButton.hasClass("disabled")) return;
212 if (addNoteButton.hasClass("active")) return;
214 addNoteButton.addClass("active");
216 map.addLayer(noteLayer);
218 var mapSize = map.getSize();
221 if (mapSize.y > 800) {
222 markerPosition = [mapSize.x / 2, mapSize.y / 2];
223 } else if (mapSize.y > 400) {
224 markerPosition = [mapSize.x / 2, 400];
226 markerPosition = [mapSize.x / 2, mapSize.y];
229 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
230 icon: noteIcons["new"],
235 var popupContent = $(JST["templates/notes/new"]());
237 popupContent.find("textarea").on("input", disableWhenBlank);
239 function disableWhenBlank(e) {
240 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
243 popupContent.find("input[type=submit]").on("click", function (e) {
245 createNote(newNote, e.target.form, '/api/0.6/notes.json');
248 newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
250 newNote.on("remove", function (e) {
251 addNoteButton.removeClass("active");
252 }).on("dragstart", function (e) {
253 $(newNote).stopTime("removenote");
254 }).on("dragend", function (e) {
255 e.target.openPopup();