]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/notes.js
Merge branch 'master' into tag-colour-preview-rebase
[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(old_marker, feature) {
43     var marker = old_marker;
44     if (marker) {
45       marker.setIcon(noteIcons[feature.properties.status]);
46     } else {
47       marker = L.marker(feature.geometry.coordinates.reverse(), {
48         icon: noteIcons[feature.properties.status],
49         title: feature.properties.comments[0].text,
50         opacity: 0.8,
51         interactive: true
52       });
53       marker.id = feature.properties.id;
54       marker.addTo(noteLayer);
55     }
56     return marker;
57   }
58
59   noteLayer.getLayerId = function (marker) {
60     return marker.id;
61   };
62
63   var noteLoader;
64
65   function loadNotes() {
66     var bounds = map.getBounds();
67     var size = bounds.getSize();
68
69     if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
70       var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBoxString();
71
72       if (noteLoader) noteLoader.abort();
73
74       noteLoader = $.ajax({
75         url: url,
76         success: success
77       });
78     }
79
80     function success(json) {
81       var oldNotes = notes;
82       notes = {};
83       json.features.forEach(updateMarkers);
84
85       function updateMarkers(feature) {
86         var marker = oldNotes[feature.properties.id];
87         delete oldNotes[feature.properties.id];
88         notes[feature.properties.id] = updateMarker(marker, feature);
89       }
90
91       for (var id in oldNotes) {
92         noteLayer.removeLayer(oldNotes[id]);
93       }
94
95       noteLoader = null;
96     }
97   }
98 };