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