]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/leaflet.map.js
Merge pull request #4249 from AntonKhorev/no-traces-nav-empty-li
[rails.git] / app / assets / javascripts / leaflet.map.js
1 //= require qs/dist/qs
2
3 L.extend(L.LatLngBounds.prototype, {
4   getSize: function () {
5     return (this._northEast.lat - this._southWest.lat) *
6            (this._northEast.lng - this._southWest.lng);
7   },
8
9   wrap: function () {
10     return new L.LatLngBounds(this._southWest.wrap(), this._northEast.wrap());
11   }
12 });
13
14 L.OSM.Map = L.Map.extend({
15   initialize: function (id, options) {
16     L.Map.prototype.initialize.call(this, id, options);
17
18     var copyright_link = $("<a>", {
19       href: "/copyright",
20       text: I18n.t("javascripts.map.openstreetmap_contributors")
21     }).prop("outerHTML");
22     var copyright = I18n.t("javascripts.map.copyright_text", { copyright_link: copyright_link });
23
24     var donate = $("<a>", {
25       "href": "https://supporting.openstreetmap.org",
26       "class": "donate-attr",
27       "text": I18n.t("javascripts.map.make_a_donation")
28     }).prop("outerHTML");
29
30     var terms = $("<a>", {
31       href: "https://wiki.osmfoundation.org/wiki/Terms_of_Use",
32       text: I18n.t("javascripts.map.website_and_api_terms")
33     }).prop("outerHTML");
34
35     var cyclosm_link = $("<a>", {
36       href: "https://www.cyclosm.org",
37       target: "_blank",
38       text: I18n.t("javascripts.map.cyclosm_name")
39     }).prop("outerHTML");
40     var osm_france_link = $("<a>", {
41       href: "https://openstreetmap.fr/",
42       target: "_blank",
43       text: I18n.t("javascripts.map.osm_france")
44     }).prop("outerHTML");
45     var cyclosm = I18n.t("javascripts.map.cyclosm_credit", { cyclosm_link: cyclosm_link, osm_france_link: osm_france_link });
46
47     var thunderforest_link = $("<a>", {
48       href: "https://www.thunderforest.com/",
49       target: "_blank",
50       text: I18n.t("javascripts.map.andy_allan")
51     }).prop("outerHTML");
52     var thunderforest = I18n.t("javascripts.map.thunderforest_credit", { thunderforest_link: thunderforest_link });
53
54     var tracestrack_link = $("<a>", {
55       href: "https://www.tracestrack.com/",
56       target: "_blank",
57       text: I18n.t("javascripts.map.tracestrack")
58     }).prop("outerHTML");
59     var tracestrack = I18n.t("javascripts.map.tracestrack_credit", { tracestrack_link: tracestrack_link });
60
61     var memomaps_link = $("<a>", {
62       href: "https://memomaps.de/",
63       target: "_blank",
64       text: I18n.t("javascripts.map.memomaps")
65     }).prop("outerHTML");
66     var memomaps = I18n.t("javascripts.map.opnvkarte_credit", { memomaps_link: memomaps_link });
67
68     var hotosm_link = $("<a>", {
69       href: "https://www.hotosm.org/",
70       target: "_blank",
71       text: I18n.t("javascripts.map.hotosm_name")
72     }).prop("outerHTML");
73     var hotosm = I18n.t("javascripts.map.hotosm_credit", { hotosm_link: hotosm_link, osm_france_link: osm_france_link });
74
75     this.baseLayers = [];
76
77     this.baseLayers.push(new L.OSM.Mapnik({
78       attribution: copyright + " &hearts; " + donate + ". " + terms,
79       code: "M",
80       keyid: "mapnik",
81       name: I18n.t("javascripts.map.base.standard")
82     }));
83
84     this.baseLayers.push(new L.OSM.CyclOSM({
85       attribution: copyright + ". " + cyclosm + ". " + terms,
86       code: "Y",
87       keyid: "cyclosm",
88       name: I18n.t("javascripts.map.base.cyclosm")
89     }));
90
91     if (OSM.THUNDERFOREST_KEY) {
92       this.baseLayers.push(new L.OSM.CycleMap({
93         attribution: copyright + ". " + thunderforest + ". " + terms,
94         apikey: OSM.THUNDERFOREST_KEY,
95         code: "C",
96         keyid: "cyclemap",
97         name: I18n.t("javascripts.map.base.cycle_map")
98       }));
99
100       this.baseLayers.push(new L.OSM.TransportMap({
101         attribution: copyright + ". " + thunderforest + ". " + terms,
102         apikey: OSM.THUNDERFOREST_KEY,
103         code: "T",
104         keyid: "transportmap",
105         name: I18n.t("javascripts.map.base.transport_map")
106       }));
107     }
108
109     if (OSM.TRACESTRACK_KEY) {
110       this.baseLayers.push(new L.OSM.TracestrackTopo({
111         attribution: copyright + ". " + tracestrack + ". " + terms,
112         apikey: OSM.TRACESTRACK_KEY,
113         code: "P",
114         keyid: "tracestracktopo",
115         name: I18n.t("javascripts.map.base.tracestracktop_topo")
116       }));
117     }
118
119     this.baseLayers.push(new L.OSM.OPNVKarte({
120       attribution: copyright + ". " + memomaps + ". " + terms,
121       code: "O",
122       keyid: "opnvkarte",
123       name: I18n.t("javascripts.map.base.opnvkarte")
124     }));
125
126     this.baseLayers.push(new L.OSM.HOT({
127       attribution: copyright + ". " + hotosm + ". " + terms,
128       code: "H",
129       keyid: "hot",
130       name: I18n.t("javascripts.map.base.hot")
131     }));
132
133     this.noteLayer = new L.FeatureGroup();
134     this.noteLayer.options = { code: "N" };
135
136     this.dataLayer = new L.OSM.DataLayer(null);
137     this.dataLayer.options.code = "D";
138
139     this.gpsLayer = new L.OSM.GPS({
140       pane: "overlayPane",
141       code: "G",
142       name: I18n.t("javascripts.map.base.gps")
143     });
144
145     this.on("layeradd", function (event) {
146       if (this.baseLayers.indexOf(event.layer) >= 0) {
147         this.setMaxZoom(event.layer.options.maxZoom);
148       }
149     });
150   },
151
152   updateLayers: function (layerParam) {
153     var layers = layerParam || "M",
154         layersAdded = "";
155
156     for (var i = this.baseLayers.length - 1; i >= 0; i--) {
157       if (layers.indexOf(this.baseLayers[i].options.code) >= 0) {
158         this.addLayer(this.baseLayers[i]);
159         layersAdded = layersAdded + this.baseLayers[i].options.code;
160       } else if (i === 0 && layersAdded === "") {
161         this.addLayer(this.baseLayers[i]);
162       } else {
163         this.removeLayer(this.baseLayers[i]);
164       }
165     }
166   },
167
168   getLayersCode: function () {
169     var layerConfig = "";
170     this.eachLayer(function (layer) {
171       if (layer.options && layer.options.code) {
172         layerConfig += layer.options.code;
173       }
174     });
175     return layerConfig;
176   },
177
178   getMapBaseLayerId: function () {
179     var baseLayerId;
180     this.eachLayer(function (layer) {
181       if (layer.options && layer.options.keyid) baseLayerId = layer.options.keyid;
182     });
183     return baseLayerId;
184   },
185
186   getUrl: function (marker) {
187     var precision = OSM.zoomPrecision(this.getZoom()),
188         params = {};
189
190     if (marker && this.hasLayer(marker)) {
191       var latLng = marker.getLatLng().wrap();
192       params.mlat = latLng.lat.toFixed(precision);
193       params.mlon = latLng.lng.toFixed(precision);
194     }
195
196     var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
197         query = Qs.stringify(params),
198         hash = OSM.formatHash(this);
199
200     if (query) url += "?" + query;
201     if (hash) url += hash;
202
203     return url;
204   },
205
206   getShortUrl: function (marker) {
207     var zoom = this.getZoom(),
208         latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
209         str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
210           window.location.protocol + "//osm.org/go/" :
211           window.location.protocol + "//" + window.location.hostname + "/go/",
212         char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
213         x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
214         y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
215         // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
216         // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
217         // and drops the last 4 bits of the full 64 bit Morton code.
218         c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
219         digit,
220         i;
221
222     for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
223       digit = (c1 >> (24 - (6 * i))) & 0x3f;
224       str += char_array.charAt(digit);
225     }
226     for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
227       digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
228       str += char_array.charAt(digit);
229     }
230     for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
231
232     // Called to interlace the bits in x and y, making a Morton code.
233     function interlace(x, y) {
234       var interlaced_x = x,
235           interlaced_y = y;
236       interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
237       interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
238       interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
239       interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
240       interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
241       interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
242       interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
243       interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
244       return (interlaced_x << 1) | interlaced_y;
245     }
246
247     var params = {};
248     var layers = this.getLayersCode().replace("M", "");
249
250     if (layers) {
251       params.layers = layers;
252     }
253
254     if (marker && this.hasLayer(marker)) {
255       params.m = "";
256     }
257
258     if (this._object) {
259       params[this._object.type] = this._object.id;
260     }
261
262     var query = Qs.stringify(params);
263     if (query) {
264       str += "?" + query;
265     }
266
267     return str;
268   },
269
270   getGeoUri: function (marker) {
271     var precision = OSM.zoomPrecision(this.getZoom()),
272         latLng,
273         params = {};
274
275     if (marker && this.hasLayer(marker)) {
276       latLng = marker.getLatLng().wrap();
277     } else {
278       latLng = this.getCenter();
279     }
280
281     params.lat = latLng.lat.toFixed(precision);
282     params.lon = latLng.lng.toFixed(precision);
283     params.zoom = this.getZoom();
284
285     return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
286   },
287
288   addObject: function (object, callback) {
289     var objectStyle = {
290       color: "#FF6200",
291       weight: 4,
292       opacity: 1,
293       fillOpacity: 0.5
294     };
295
296     var changesetStyle = {
297       weight: 4,
298       color: "#FF9500",
299       opacity: 1,
300       fillOpacity: 0,
301       interactive: false
302     };
303
304     var haloStyle = {
305       weight: 2.5,
306       radius: 20,
307       fillOpacity: 0.5,
308       color: "#FF6200"
309     };
310
311     this.removeObject();
312
313     if (object.type === "note") {
314       this._objectLoader = {
315         abort: function () {}
316       };
317
318       this._object = object;
319       this._objectLayer = L.featureGroup().addTo(this);
320
321       L.circleMarker(object.latLng, haloStyle).addTo(this._objectLayer);
322
323       if (object.icon) {
324         L.marker(object.latLng, {
325           icon: object.icon,
326           opacity: 1,
327           interactive: true
328         }).addTo(this._objectLayer);
329       }
330
331       if (callback) callback(this._objectLayer.getBounds());
332     } else { // element or changeset handled by L.OSM.DataLayer
333       var map = this;
334       this._objectLoader = $.ajax({
335         url: OSM.apiUrl(object),
336         dataType: "xml",
337         success: function (xml) {
338           map._object = object;
339
340           map._objectLayer = new L.OSM.DataLayer(null, {
341             styles: {
342               node: objectStyle,
343               way: objectStyle,
344               area: objectStyle,
345               changeset: changesetStyle
346             }
347           });
348
349           map._objectLayer.interestingNode = function (node, ways, relations) {
350             if (object.type === "node") {
351               return true;
352             } else if (object.type === "relation") {
353               for (var i = 0; i < relations.length; i++) {
354                 if (relations[i].members.indexOf(node) !== -1) return true;
355               }
356             } else {
357               return false;
358             }
359           };
360
361           map._objectLayer.addData(xml);
362           map._objectLayer.addTo(map);
363
364           if (callback) callback(map._objectLayer.getBounds());
365         }
366       });
367     }
368   },
369
370   removeObject: function () {
371     this._object = null;
372     if (this._objectLoader) this._objectLoader.abort();
373     if (this._objectLayer) this.removeLayer(this._objectLayer);
374   },
375
376   getState: function () {
377     return {
378       center: this.getCenter().wrap(),
379       zoom: this.getZoom(),
380       layers: this.getLayersCode()
381     };
382   },
383
384   setState: function (state, options) {
385     if (state.center) this.setView(state.center, state.zoom, options);
386     if (state.layers) this.updateLayers(state.layers);
387   },
388
389   setSidebarOverlaid: function (overlaid) {
390     var sidebarWidth = 350;
391     if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
392       $("#content").addClass("overlay-sidebar");
393       this.invalidateSize({ pan: false });
394       if ($("html").attr("dir") !== "rtl") {
395         this.panBy([-sidebarWidth, 0], { animate: false });
396       }
397     } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
398       if ($("html").attr("dir") !== "rtl") {
399         this.panBy([sidebarWidth, 0], { animate: false });
400       }
401       $("#content").removeClass("overlay-sidebar");
402       this.invalidateSize({ pan: false });
403     }
404     return this;
405   }
406 });
407
408 L.Icon.Default.imagePath = "/images/";
409
410 L.Icon.Default.imageUrls = {
411   "/images/marker-icon.png": OSM.MARKER_ICON,
412   "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
413   "/images/marker-shadow.png": OSM.MARKER_SHADOW
414 };
415
416 L.extend(L.Icon.Default.prototype, {
417   _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
418
419   _getIconUrl: function (name) {
420     var url = this._oldGetIconUrl(name);
421     return L.Icon.Default.imageUrls[url];
422   }
423 });
424
425 OSM.getUserIcon = function (url) {
426   return L.icon({
427     iconUrl: url || OSM.MARKER_RED,
428     iconSize: [25, 41],
429     iconAnchor: [12, 41],
430     popupAnchor: [1, -34],
431     shadowUrl: OSM.MARKER_SHADOW,
432     shadowSize: [41, 41]
433   });
434 };