]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index.js
Stop default layer flickering when notes or data are shown
[rails.git] / app / assets / javascripts / index.js
1 //= require_self
2 //= require leaflet.sidebar
3 //= require leaflet.locate
4 //= require leaflet.layers
5 //= require leaflet.key
6 //= require leaflet.note
7 //= require leaflet.share
8 //= require index/search
9 //= require index/browse
10 //= require index/export
11 //= require index/notes
12
13 $(document).ready(function () {
14   var params = OSM.mapParams();
15
16   var map = L.map("map", {
17     zoomControl: false,
18     layerControl: false
19   });
20
21   map.attributionControl.setPrefix('');
22
23   map.hash = L.hash(map);
24
25   var copyright = I18n.t('javascripts.map.copyright', {copyright_url: '/copyright'});
26
27   var layers = [
28     new L.OSM.Mapnik({
29       attribution: copyright,
30       code: "M",
31       keyid: "mapnik",
32       name: I18n.t("javascripts.map.base.standard")
33     }),
34     new L.OSM.CycleMap({
35       attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
36       code: "C",
37       keyid: "cyclemap",
38       name: I18n.t("javascripts.map.base.cycle_map")
39     }),
40     new L.OSM.TransportMap({
41       attribution: copyright + ". Tiles courtesy of <a href='http://www.opencyclemap.org/' target='_blank'>Andy Allan</a>",
42       code: "T",
43       keyid: "transportmap",
44       name: I18n.t("javascripts.map.base.transport_map")
45     }),
46     new L.OSM.MapQuestOpen({
47       attribution: copyright + ". Tiles courtesy of <a href='http://www.mapquest.com/' target='_blank'>MapQuest</a> <img src='http://developer.mapquest.com/content/osm/mq_logo.png'>",
48       code: "Q",
49       keyid: "mapquest",
50       name: I18n.t("javascripts.map.base.mapquest")
51     })
52   ];
53
54   function updateLayers(params) {
55     var layerParam = params.layers || "M";
56     var layersAdded = "";
57
58     for (var i = layers.length - 1; i >= 0; i--) {
59       if (layerParam.indexOf(layers[i].options.code) >= 0) {
60         map.addLayer(layers[i]);
61         layersAdded = layersAdded + layers[i].options.code;
62       } else if (i == 0 && layersAdded == "") {
63         map.addLayer(layers[i]);
64       } else {
65         map.removeLayer(layers[i]);
66       }
67     }
68   }
69
70   updateLayers(params);
71
72   $(window).on("hashchange", function () {
73     updateLayers(OSM.mapParams());
74   });
75
76   map.noteLayer = new L.LayerGroup();
77   map.noteLayer.options = {code: 'N'};
78
79   map.dataLayer = new L.OSM.DataLayer(null);
80   map.dataLayer.options.code = 'D';
81
82   $("#sidebar").on("opened closed", function () {
83     map.invalidateSize();
84   });
85
86   var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
87
88   L.OSM.zoom({position: position})
89     .addTo(map);
90
91   L.control.locate({
92     position: position,
93     strings: {
94       title: I18n.t('javascripts.map.locate.title'),
95       popup: I18n.t('javascripts.map.locate.popup')
96     }
97   }).addTo(map);
98
99   var sidebar = L.OSM.sidebar('#map-ui')
100     .addTo(map);
101
102   L.OSM.layers({
103     position: position,
104     layers: layers,
105     sidebar: sidebar
106   }).addTo(map);
107
108   L.OSM.key({
109     position: position,
110     sidebar: sidebar
111   }).addTo(map);
112
113   L.OSM.share({
114     position: position,
115     sidebar: sidebar,
116     short: true
117   }).addTo(map);
118
119   L.OSM.note({
120     position: position,
121     sidebar: sidebar
122   }).addTo(map);
123
124   L.control.scale()
125     .addTo(map);
126
127   $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
128
129   map.on('moveend layeradd layerremove', updateLocation);
130
131   if (OSM.PIWIK) {
132     map.on('layeradd', function (e) {
133       if (e.layer.options) {
134         var goal = OSM.PIWIK.goals[e.layer.options.keyid];
135
136         if (goal) {
137           $('body').trigger('piwikgoal', goal);
138         }
139       }
140     });
141   }
142
143   var marker = L.marker([0, 0], {icon: getUserIcon()});
144
145   if (!params.object_zoom) {
146     if (params.bounds) {
147       map.fitBounds(params.bounds);
148     } else {
149       map.setView([params.lat, params.lon], params.zoom);
150     }
151   }
152
153   if (params.box) {
154     L.rectangle(params.box, {
155       weight: 2,
156       color: '#e90',
157       fillOpacity: 0
158     }).addTo(map);
159   }
160
161   if (params.marker) {
162     marker.setLatLng([params.mlat, params.mlon]).addTo(map);
163   }
164
165   if (params.object) {
166     map.addObject(params.object, { zoom: params.object_zoom });
167   }
168
169   $("#homeanchor").on("click", function(e) {
170     e.preventDefault();
171
172     var data = $(this).data(),
173       center = L.latLng(data.lat, data.lon);
174
175     map.setView(center, data.zoom);
176     marker.setLatLng(center).addTo(map);
177   });
178
179   $("a[data-editor=remote]").click(function(e) {
180       remoteEditHandler(map.getBounds());
181       e.preventDefault();
182   });
183
184   if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
185     remoteEditHandler(map.getBounds());
186   }
187
188   if (OSM.params().edit_help) {
189     $('#editanchor')
190       .removeAttr('title')
191       .tooltip({
192         placement: 'bottom',
193         title: I18n.t('javascripts.edit_help')
194       })
195       .tooltip('show');
196
197     $('body').one('click', function() {
198       $('#editanchor').tooltip('hide');
199     });
200   }
201
202   initializeSearch(map);
203   initializeExport(map);
204   initializeBrowse(map, params);
205   initializeNotes(map, params);
206 });
207
208 function updateLocation() {
209   updatelinks(this.getCenter().wrap(),
210       this.getZoom(),
211       this.getLayersCode(),
212       this.getBounds().wrap());
213
214   var expiry = new Date();
215   expiry.setYear(expiry.getFullYear() + 10);
216   $.cookie("_osm_location", cookieContent(this), { expires: expiry });
217
218   // Trigger hash update on layer changes.
219   this.hash.onMapMove();
220 }