]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index.js
Make sure we always have a base layer
[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 {
63         map.removeLayer(layers[i]);
64       }
65     }
66
67     if (layersAdded == "") {
68       map.addLayer(layers[0]);
69     }
70   }
71
72   updateLayers(params);
73
74   $(window).on("hashchange", function () {
75     updateLayers(OSM.mapParams());
76   });
77
78   map.noteLayer = new L.LayerGroup();
79   map.noteLayer.options = {code: 'N'};
80
81   map.dataLayer = new L.OSM.DataLayer(null);
82   map.dataLayer.options.code = 'D';
83
84   $("#sidebar").on("opened closed", function () {
85     map.invalidateSize();
86   });
87
88   var position = $('html').attr('dir') === 'rtl' ? 'topleft' : 'topright';
89
90   L.OSM.zoom({position: position})
91     .addTo(map);
92
93   L.control.locate({
94     position: position,
95     strings: {
96       title: I18n.t('javascripts.map.locate.title'),
97       popup: I18n.t('javascripts.map.locate.popup')
98     }
99   }).addTo(map);
100
101   var sidebar = L.OSM.sidebar('#map-ui')
102     .addTo(map);
103
104   L.OSM.layers({
105     position: position,
106     layers: layers,
107     sidebar: sidebar
108   }).addTo(map);
109
110   L.OSM.key({
111     position: position,
112     sidebar: sidebar
113   }).addTo(map);
114
115   L.OSM.share({
116     position: position,
117     sidebar: sidebar,
118     short: true
119   }).addTo(map);
120
121   L.OSM.note({
122     position: position,
123     sidebar: sidebar
124   }).addTo(map);
125
126   L.control.scale()
127     .addTo(map);
128
129   $('.leaflet-control .control-button').tooltip({placement: 'left', container: 'body'});
130
131   map.on('moveend layeradd layerremove', updateLocation);
132
133   if (OSM.PIWIK) {
134     map.on('layeradd', function (e) {
135       if (e.layer.options) {
136         var goal = OSM.PIWIK.goals[e.layer.options.keyid];
137
138         if (goal) {
139           $('body').trigger('piwikgoal', goal);
140         }
141       }
142     });
143   }
144
145   var marker = L.marker([0, 0], {icon: getUserIcon()});
146
147   if (!params.object_zoom) {
148     if (params.bounds) {
149       map.fitBounds(params.bounds);
150     } else {
151       map.setView([params.lat, params.lon], params.zoom);
152     }
153   }
154
155   if (params.box) {
156     L.rectangle(params.box, {
157       weight: 2,
158       color: '#e90',
159       fillOpacity: 0
160     }).addTo(map);
161   }
162
163   if (params.marker) {
164     marker.setLatLng([params.mlat, params.mlon]).addTo(map);
165   }
166
167   if (params.object) {
168     map.addObject(params.object, { zoom: params.object_zoom });
169   }
170
171   $("#homeanchor").on("click", function(e) {
172     e.preventDefault();
173
174     var data = $(this).data(),
175       center = L.latLng(data.lat, data.lon);
176
177     map.setView(center, data.zoom);
178     marker.setLatLng(center).addTo(map);
179   });
180
181   $("a[data-editor=remote]").click(function(e) {
182       remoteEditHandler(map.getBounds());
183       e.preventDefault();
184   });
185
186   if (OSM.preferred_editor == "remote" && $('body').hasClass("site-edit")) {
187     remoteEditHandler(map.getBounds());
188   }
189
190   if (OSM.params().edit_help) {
191     $('#editanchor')
192       .removeAttr('title')
193       .tooltip({
194         placement: 'bottom',
195         title: I18n.t('javascripts.edit_help')
196       })
197       .tooltip('show');
198
199     $('body').one('click', function() {
200       $('#editanchor').tooltip('hide');
201     });
202   }
203
204   initializeSearch(map);
205   initializeExport(map);
206   initializeBrowse(map, params);
207   initializeNotes(map, params);
208 });
209
210 function updateLocation() {
211   updatelinks(this.getCenter().wrap(),
212       this.getZoom(),
213       this.getLayersCode(),
214       this.getBounds().wrap());
215
216   var expiry = new Date();
217   expiry.setYear(expiry.getFullYear() + 10);
218   $.cookie("_osm_location", cookieContent(this), { expires: expiry });
219
220   // Trigger hash update on layer changes.
221   this.hash.onMapMove();
222 }