]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/notes.js.erb
365d4f11c5384004fc88c24156c165dae1f45cf5
[rails.git] / app / assets / javascripts / index / notes.js.erb
1 //= require templates/notes/show
2 //= require templates/notes/new
3
4 $(document).ready(function () {
5   var params = OSM.mapParams();
6
7   var noteIcons = {
8     "new": L.icon({
9       iconUrl: "<%= image_path 'new_note_marker.png' %>",
10       iconSize: [22, 22],
11       iconAnchor: [11, 11]
12     }),
13     "open": L.icon({
14       iconUrl: "<%= image_path 'open_note_marker.png' %>",
15       iconSize: [22, 22],
16       iconAnchor: [11, 11]
17     }),
18     "closed": L.icon({
19       iconUrl: "<%= image_path 'closed_note_marker.png' %>",
20       iconSize: [22, 22],
21       iconAnchor: [11, 11]
22     })
23   };
24
25   var noteLayer = new L.LayerGroup();
26   var notes = {};
27
28   map.on("layeradd", function (e) {
29     if (e.layer == noteLayer) {
30       loadNotes();
31       map.on("moveend", loadNotes);
32     }
33   });
34
35   map.on("layerremove", function (e) {
36     if (e.layer == noteLayer) {
37       map.off("moveend", loadNotes);
38       noteLayer.clearLayers();
39     }
40   });
41
42   if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
43     map.layersControl.addOverlay(noteLayer, I18n.t("browse.start_rjs.notes_layer_name"));
44
45     if (params.notes) map.addLayer(noteLayer);
46   }
47
48   function updateMarker(marker, feature) {
49     var icon = noteIcons[feature.properties.status];
50     var popupContent = createPopupContent(marker, feature.properties);
51
52     if (marker)
53     {
54       marker.setIcon(noteIcons[feature.properties.status]);
55       marker._popup.setContent(popupContent);
56     }
57     else
58     {
59       marker = L.marker(feature.geometry.coordinates.reverse(), {
60         icon: icon,
61         opacity: 0.7
62       });
63
64       marker.addTo(noteLayer).bindPopup(popupContent, popupOptions());
65     }
66
67     return marker;
68   }
69
70   function loadNotes() {
71     var bounds = map.getBounds();
72     var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBOX();
73
74     $.ajax({
75       url: url,
76       success: function (json) {
77         var oldNotes = notes;
78
79         notes = {};
80
81         json.features.forEach(function (feature) {
82           var marker = oldNotes[feature.properties.id];
83
84           delete oldNotes[feature.properties.id];
85
86           notes[feature.properties.id] = updateMarker(marker, feature);
87         });
88
89         for (id in oldNotes) {
90           noteLayer.removeLayer(oldNotes[id]);
91         }
92       }
93     });
94   };
95
96   function popupOptions() {
97     var mapSize = map.getSize();
98
99     return { maxHeight: mapSize.y * 2 / 3, autoPanPadding: new L.Point(60, 40) };
100   }
101
102   function createPopupContent(marker, properties) {
103     var content = $(JST["templates/notes/show"]({ note: properties }));
104
105     content.find("textarea").on("input", function (e) {
106       var form = e.target.form;
107
108       if ($(e.target).val() == "") {
109         $(form.close).val(I18n.t("javascripts.notes.show.close"));
110         $(form.comment).prop("disabled", true);
111       } else {
112         $(form.close).val(I18n.t("javascripts.notes.show.comment_and_close"));
113         $(form.comment).prop("disabled", false);
114       }
115     });
116
117     content.find("input[type=submit]").on("click", function (e) {
118       e.preventDefault();
119       updateNote(marker, e.target.form, $(e.target).data("url"));
120     });
121
122     return content[0];
123   }
124
125   function createNote(marker, form, url) {
126     var location = marker.getLatLng();
127
128     $(form).find("input[type=submit]").prop("disabled", true);
129
130     $.ajax({
131       url: url,
132       type: "POST",
133       data: {
134         lat: location.lat,
135         lon: location.lng,
136         text: $(form.text).val()
137       },
138       success: function (feature) {
139         notes[feature.properties.id] = updateMarker(marker, feature);
140
141         $(".leaflet-popup-close-button").off("click.close");
142       }
143     });
144   }
145
146   function updateNote(marker, form, url) {
147     $(form).find("input[type=submit]").prop("disabled", true);
148
149     $.ajax({
150       url: url,
151       type: "POST",
152       data: {
153         text: $(form.text).val()
154       },
155       success: function (feature) {
156         var popupContent = createPopupContent(marker, feature.properties);
157
158         marker.setIcon(noteIcons[feature.properties.status]);
159         marker._popup.setContent(popupContent);
160       }
161     });
162   }
163
164   $("#createnoteanchor").click(function (e) {
165     e.preventDefault();
166
167     map.addLayer(noteLayer);
168
169     var marker = L.marker(map.getCenter(), {
170       icon: noteIcons["new"],
171       opacity: 0.7,
172       draggable: true
173     });
174
175     var popupContent = $(JST["templates/notes/new"]({ create_url: $(e.target).attr("href") }));
176
177     popupContent.find("input[type=submit]").on("click", function (e) {
178       e.preventDefault();
179       createNote(marker, e.target.form, $(e.target).data("url"));
180     });
181
182     marker.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
183
184     $(".leaflet-popup-close-button").on("click.close", function (e) {
185       map.removeLayer(marker);
186     });
187
188     marker.on("dragend", function (e) {
189       e.target.openPopup();
190     });
191   });
192 });