]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/notes.js.erb
Merge branch 'master' into redesign
[rails.git] / app / assets / javascripts / index / notes.js.erb
1 function initializeNotes(map) {
2   var noteLayer = map.noteLayer,
3       notes = {};
4
5   var noteIcons = {
6     "new": L.icon({
7       iconUrl: "<%= image_path 'new_note_marker.png' %>",
8       iconSize: [25, 40],
9       iconAnchor: [12, 40]
10     }),
11     "open": L.icon({
12       iconUrl: "<%= image_path 'open_note_marker.png' %>",
13       iconSize: [25, 40],
14       iconAnchor: [12, 40]
15     }),
16     "closed": L.icon({
17       iconUrl: "<%= image_path 'closed_note_marker.png' %>",
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     OSM.route('/browse/note/' + e.layer.id);
38   });
39
40   function updateMarker(marker, feature) {
41     if (marker) {
42       marker.setIcon(noteIcons[feature.properties.status]);
43     } else {
44       marker = L.marker(feature.geometry.coordinates.reverse(), {
45         icon: noteIcons[feature.properties.status],
46         opacity: 0.9,
47         clickable: true
48       });
49       marker.id = feature.properties.id;
50       marker.addTo(noteLayer);
51     }
52     return marker;
53   }
54
55   noteLayer.getLayerId = function(marker) {
56     return marker.id;
57   };
58
59   var noteLoader;
60
61   function loadNotes() {
62     var bounds = map.getBounds();
63     var size = bounds.getSize();
64
65     if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
66       var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBoxString();
67
68       if (noteLoader) noteLoader.abort();
69
70       noteLoader = $.ajax({
71         url: url,
72         success: success
73       });
74     }
75
76     function success(json) {
77       var oldNotes = notes;
78       notes = {};
79       json.features.forEach(updateMarkers);
80
81       function updateMarkers(feature) {
82         var marker = oldNotes[feature.properties.id];
83         delete oldNotes[feature.properties.id];
84         notes[feature.properties.id] = updateMarker(marker, feature);
85       }
86
87       for (var id in oldNotes) {
88         noteLayer.removeLayer(oldNotes[id]);
89       }
90
91       noteLoader = null;
92     }
93   }
94 }