]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/notes.js
Allow configuration of storage server URL for security policy
[rails.git] / app / assets / javascripts / index / notes.js
1 OSM.initializeNotes = function (map) {
2   var noteLayer = map.noteLayer,
3       notes = {};
4
5   var noteIcons = {
6     "new": L.icon({
7       iconUrl: OSM.NEW_NOTE_MARKER,
8       iconSize: [25, 40],
9       iconAnchor: [12, 40]
10     }),
11     "open": L.icon({
12       iconUrl: OSM.OPEN_NOTE_MARKER,
13       iconSize: [25, 40],
14       iconAnchor: [12, 40]
15     }),
16     "closed": L.icon({
17       iconUrl: OSM.CLOSED_NOTE_MARKER,
18       iconSize: [25, 40],
19       iconAnchor: [12, 40]
20     })
21   };
22
23   map.on("layeradd", function (e) {
24     if (e.layer === noteLayer) {
25       loadNotes();
26       map.on("moveend", loadNotes);
27     }
28   }).on("layerremove", function (e) {
29     if (e.layer === noteLayer) {
30       map.off("moveend", loadNotes);
31       noteLayer.clearLayers();
32       notes = {};
33     }
34   });
35
36   noteLayer.on("click", function (e) {
37     if (e.layer.id) {
38       OSM.router.route("/note/" + e.layer.id);
39     }
40   });
41
42   function updateMarker(marker, feature) {
43     if (marker) {
44       marker.setIcon(noteIcons[feature.properties.status]);
45     } else {
46       marker = L.marker(feature.geometry.coordinates.reverse(), {
47         icon: noteIcons[feature.properties.status],
48         title: feature.properties.comments[0].text,
49         opacity: 0.8,
50         interactive: true
51       });
52       marker.id = feature.properties.id;
53       marker.addTo(noteLayer);
54     }
55     return marker;
56   }
57
58   noteLayer.getLayerId = function (marker) {
59     return marker.id;
60   };
61
62   var noteLoader;
63
64   function loadNotes() {
65     var bounds = map.getBounds();
66     var size = bounds.getSize();
67
68     if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
69       var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBoxString();
70
71       if (noteLoader) noteLoader.abort();
72
73       noteLoader = $.ajax({
74         url: url,
75         success: success
76       });
77     }
78
79     function success(json) {
80       var oldNotes = notes;
81       notes = {};
82       json.features.forEach(updateMarkers);
83
84       function updateMarkers(feature) {
85         var marker = oldNotes[feature.properties.id];
86         delete oldNotes[feature.properties.id];
87         notes[feature.properties.id] = updateMarker(marker, feature);
88       }
89
90       for (var id in oldNotes) {
91         noteLayer.removeLayer(oldNotes[id]);
92       }
93
94       noteLoader = null;
95     }
96   }
97 };