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.indexOf('N') >= 0) map.addLayer(noteLayer);
56 url: "/api/" + OSM.API_VERSION + "/notes/" + params.note + ".json",
57 success: function (feature) {
58 var marker = updateMarker(notes[feature.properties.id], feature);
59 notes[feature.properties.id] = marker;
60 map.addLayer(noteLayer);
67 function updateMarker(marker, feature) {
69 marker.setIcon(noteIcons[feature.properties.status]);
70 marker.setPopupContent(createPopupContent(
71 marker, feature.properties,
72 $(marker._popup._content).find("textarea").val()
75 marker = L.marker(feature.geometry.coordinates.reverse(), {
76 icon: noteIcons[feature.properties.status],
79 marker.addTo(noteLayer).bindPopup(
80 createPopupContent(marker, feature.properties),
89 function loadNotes() {
90 var bounds = map.getBounds();
91 var size = bounds.getSize();
93 if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
94 var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBoxString();
96 if (noteLoader) noteLoader.abort();
104 function success(json) {
105 var oldNotes = notes;
107 json.features.forEach(updateMarkers);
109 function updateMarkers(feature) {
110 var marker = oldNotes[feature.properties.id];
111 delete oldNotes[feature.properties.id];
112 notes[feature.properties.id] = updateMarker(marker, feature);
115 for (id in oldNotes) {
116 noteLayer.removeLayer(oldNotes[id]);
123 function popupOptions() {
124 var mapSize = map.getSize();
128 maxWidth: mapSize.y * 1 / 3,
129 maxHeight: mapSize.y * 2 / 3,
130 offset: new L.Point(0, -40),
131 autoPanPadding: new L.Point(60, 40)
135 function createPopupContent(marker, properties, comment) {
136 var content = $(JST["templates/notes/show"]({ note: properties }));
138 content.find("textarea").on("input", function (e) {
139 var form = e.target.form;
141 if ($(e.target).val() == "") {
142 $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
143 $(form.comment).prop("disabled", true);
145 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
146 $(form.comment).prop("disabled", false);
150 content.find("input[type=submit]").on("click", function (e) {
152 var data = $(e.target).data();
153 updateNote(marker, e.target.form, data.method, data.url);
157 content.find("textarea").val(comment).trigger("input");
163 var addNoteButton = $(".control-note .control-button");
165 function createNote(marker, form, url) {
166 var location = marker.getLatLng();
168 marker.options.draggable = false;
169 marker.dragging.disable();
171 $(form).find("input[type=submit]").prop("disabled", true);
180 text: $(form.text).val()
185 function noteCreated(feature) {
186 $(marker._popup._content).find("textarea").val("");
188 notes[feature.properties.id] = updateMarker(marker, feature);
191 addNoteButton.removeClass("disabled");
195 function updateNote(marker, form, method, url) {
196 $(form).find("input[type=submit]").prop("disabled", true);
203 text: $(form.text).val()
205 success: function (feature) {
206 if (feature.properties.status == "hidden") {
207 noteLayer.removeLayer(marker);
209 delete notes[feature.properties.id];
211 var popupContent = createPopupContent(marker, feature.properties);
213 marker.setIcon(noteIcons[feature.properties.status]);
214 marker.setPopupContent(popupContent);
220 addNoteButton.on("click", function (e) {
224 if (addNoteButton.hasClass("disabled")) return;
226 addNoteButton.addClass("disabled");
228 map.addLayer(noteLayer);
230 var mapSize = map.getSize();
233 if (mapSize.y > 800) {
234 markerPosition = [mapSize.x / 2, mapSize.y / 2];
235 } else if (mapSize.y > 400) {
236 markerPosition = [mapSize.x / 2, 400];
238 markerPosition = [mapSize.x / 2, mapSize.y];
241 newNote = L.marker(map.containerPointToLatLng(markerPosition), {
242 icon: noteIcons["new"],
247 var popupContent = $(JST["templates/notes/new"]());
249 popupContent.find("textarea").on("input", disableWhenBlank);
251 function disableWhenBlank(e) {
252 $(e.target.form.add).prop("disabled", $(e.target).val() === "");
255 popupContent.find("input[type=submit]").on("click", function (e) {
257 createNote(newNote, e.target.form, '/api/0.6/notes.json');
260 newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
262 newNote.on("remove", function (e) {
263 addNoteButton.removeClass("disabled");
264 }).on("dragstart", function (e) {
265 $(newNote).stopTime("removenote");
266 }).on("dragend", function (e) {
267 e.target.openPopup();