]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/leaflet.map.js
Replace querystring parser with qs yarn module
[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 = I18n.t("javascripts.map.copyright", { copyright_url: "/copyright" });
19     var donate = I18n.t("javascripts.map.donate_link_text", { donate_url: "https://donate.openstreetmap.org" });
20     var terms = I18n.t("javascripts.map.terms", { terms_url: "https://wiki.osmfoundation.org/wiki/Terms_of_Use" });
21     var thunderforest = I18n.t("javascripts.map.thunderforest", { thunderforest_url: "https://www.thunderforest.com/" });
22     var memomaps = I18n.t("javascripts.map.opnvkarte", { memomaps_url: "https://memomaps.de/" });
23     var hotosm = I18n.t("javascripts.map.hotosm", { hotosm_url: "https://www.hotosm.org/", osmfrance_url: "https://openstreetmap.fr/" });
24
25     this.baseLayers = [];
26
27     this.baseLayers.push(new L.OSM.Mapnik({
28       attribution: copyright + " ♥ " + donate + ". " + terms,
29       code: "M",
30       keyid: "mapnik",
31       name: I18n.t("javascripts.map.base.standard")
32     }));
33
34     if (OSM.THUNDERFOREST_KEY) {
35       this.baseLayers.push(new L.OSM.CycleMap({
36         attribution: copyright + ". " + thunderforest + ". " + terms,
37         apikey: OSM.THUNDERFOREST_KEY,
38         code: "C",
39         keyid: "cyclemap",
40         name: I18n.t("javascripts.map.base.cycle_map")
41       }));
42
43       this.baseLayers.push(new L.OSM.TransportMap({
44         attribution: copyright + ". " + thunderforest + ". " + terms,
45         apikey: OSM.THUNDERFOREST_KEY,
46         code: "T",
47         keyid: "transportmap",
48         name: I18n.t("javascripts.map.base.transport_map")
49       }));
50     }
51
52     this.baseLayers.push(new L.OSM.OPNVKarte({
53       attribution: copyright + ". " + memomaps + ". " + terms,
54       code: "O",
55       keyid: "opnvkarte",
56       name: I18n.t("javascripts.map.base.opnvkarte")
57     }));
58
59     this.baseLayers.push(new L.OSM.HOT({
60       attribution: copyright + ". " + hotosm + ". " + terms,
61       code: "H",
62       keyid: "hot",
63       name: I18n.t("javascripts.map.base.hot")
64     }));
65
66     this.noteLayer = new L.FeatureGroup();
67     this.noteLayer.options = { code: "N" };
68
69     this.dataLayer = new L.OSM.DataLayer(null);
70     this.dataLayer.options.code = "D";
71
72     this.gpsLayer = new L.OSM.GPS({
73       pane: "overlayPane",
74       code: "G",
75       name: I18n.t("javascripts.map.base.gps")
76     });
77
78     this.on("layeradd", function (event) {
79       if (this.baseLayers.indexOf(event.layer) >= 0) {
80         this.setMaxZoom(event.layer.options.maxZoom);
81       }
82     });
83   },
84
85   updateLayers: function (layerParam) {
86     var layers = layerParam || "M",
87         layersAdded = "";
88
89     for (var i = this.baseLayers.length - 1; i >= 0; i--) {
90       if (layers.indexOf(this.baseLayers[i].options.code) >= 0) {
91         this.addLayer(this.baseLayers[i]);
92         layersAdded = layersAdded + this.baseLayers[i].options.code;
93       } else if (i === 0 && layersAdded === "") {
94         this.addLayer(this.baseLayers[i]);
95       } else {
96         this.removeLayer(this.baseLayers[i]);
97       }
98     }
99   },
100
101   getLayersCode: function () {
102     var layerConfig = "";
103     this.eachLayer(function (layer) {
104       if (layer.options && layer.options.code) {
105         layerConfig += layer.options.code;
106       }
107     });
108     return layerConfig;
109   },
110
111   getMapBaseLayerId: function () {
112     var baseLayerId;
113     this.eachLayer(function (layer) {
114       if (layer.options && layer.options.keyid) baseLayerId = layer.options.keyid;
115     });
116     return baseLayerId;
117   },
118
119   getUrl: function (marker) {
120     var precision = OSM.zoomPrecision(this.getZoom()),
121         params = {};
122
123     if (marker && this.hasLayer(marker)) {
124       var latLng = marker.getLatLng().wrap();
125       params.mlat = latLng.lat.toFixed(precision);
126       params.mlon = latLng.lng.toFixed(precision);
127     }
128
129     var url = window.location.protocol + "//" + OSM.SERVER_URL + "/",
130         query = Qs.stringify(params),
131         hash = OSM.formatHash(this);
132
133     if (query) url += "?" + query;
134     if (hash) url += hash;
135
136     return url;
137   },
138
139   getShortUrl: function (marker) {
140     var zoom = this.getZoom(),
141         latLng = marker && this.hasLayer(marker) ? marker.getLatLng().wrap() : this.getCenter().wrap(),
142         str = window.location.hostname.match(/^www\.openstreetmap\.org/i) ?
143           window.location.protocol + "//osm.org/go/" :
144           window.location.protocol + "//" + window.location.hostname + "/go/",
145         char_array = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_~",
146         x = Math.round((latLng.lng + 180.0) * ((1 << 30) / 90.0)),
147         y = Math.round((latLng.lat + 90.0) * ((1 << 30) / 45.0)),
148         // JavaScript only has to keep 32 bits of bitwise operators, so this has to be
149         // done in two parts. each of the parts c1/c2 has 30 bits of the total in it
150         // and drops the last 4 bits of the full 64 bit Morton code.
151         c1 = interlace(x >>> 17, y >>> 17), c2 = interlace((x >>> 2) & 0x7fff, (y >>> 2) & 0x7fff),
152         digit,
153         i;
154
155     for (i = 0; i < Math.ceil((zoom + 8) / 3.0) && i < 5; ++i) {
156       digit = (c1 >> (24 - (6 * i))) & 0x3f;
157       str += char_array.charAt(digit);
158     }
159     for (i = 5; i < Math.ceil((zoom + 8) / 3.0); ++i) {
160       digit = (c2 >> (24 - (6 * (i - 5)))) & 0x3f;
161       str += char_array.charAt(digit);
162     }
163     for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-";
164
165     // Called to interlace the bits in x and y, making a Morton code.
166     function interlace(x, y) {
167       var interlaced_x = x,
168           interlaced_y = y;
169       interlaced_x = (interlaced_x | (interlaced_x << 8)) & 0x00ff00ff;
170       interlaced_x = (interlaced_x | (interlaced_x << 4)) & 0x0f0f0f0f;
171       interlaced_x = (interlaced_x | (interlaced_x << 2)) & 0x33333333;
172       interlaced_x = (interlaced_x | (interlaced_x << 1)) & 0x55555555;
173       interlaced_y = (interlaced_y | (interlaced_y << 8)) & 0x00ff00ff;
174       interlaced_y = (interlaced_y | (interlaced_y << 4)) & 0x0f0f0f0f;
175       interlaced_y = (interlaced_y | (interlaced_y << 2)) & 0x33333333;
176       interlaced_y = (interlaced_y | (interlaced_y << 1)) & 0x55555555;
177       return (interlaced_x << 1) | interlaced_y;
178     }
179
180     var params = {};
181     var layers = this.getLayersCode().replace("M", "");
182
183     if (layers) {
184       params.layers = layers;
185     }
186
187     if (marker && this.hasLayer(marker)) {
188       params.m = "";
189     }
190
191     if (this._object) {
192       params[this._object.type] = this._object.id;
193     }
194
195     var query = Qs.stringify(params);
196     if (query) {
197       str += "?" + query;
198     }
199
200     return str;
201   },
202
203   getGeoUri: function (marker) {
204     var precision = OSM.zoomPrecision(this.getZoom()),
205         latLng,
206         params = {};
207
208     if (marker && this.hasLayer(marker)) {
209       latLng = marker.getLatLng().wrap();
210     } else {
211       latLng = this.getCenter();
212     }
213
214     params.lat = latLng.lat.toFixed(precision);
215     params.lon = latLng.lng.toFixed(precision);
216     params.zoom = this.getZoom();
217
218     return "geo:" + params.lat + "," + params.lon + "?z=" + params.zoom;
219   },
220
221   addObject: function (object, callback) {
222     var objectStyle = {
223       color: "#FF6200",
224       weight: 4,
225       opacity: 1,
226       fillOpacity: 0.5
227     };
228
229     var changesetStyle = {
230       weight: 4,
231       color: "#FF9500",
232       opacity: 1,
233       fillOpacity: 0,
234       interactive: false
235     };
236
237     this.removeObject();
238
239     var map = this;
240     this._objectLoader = $.ajax({
241       url: OSM.apiUrl(object),
242       dataType: "xml",
243       success: function (xml) {
244         map._object = object;
245
246         map._objectLayer = new L.OSM.DataLayer(null, {
247           styles: {
248             node: objectStyle,
249             way: objectStyle,
250             area: objectStyle,
251             changeset: changesetStyle
252           }
253         });
254
255         map._objectLayer.interestingNode = function (node, ways, relations) {
256           if (object.type === "node") {
257             return true;
258           } else if (object.type === "relation") {
259             for (var i = 0; i < relations.length; i++) {
260               if (relations[i].members.indexOf(node) !== -1) return true;
261             }
262           } else {
263             return false;
264           }
265         };
266
267         map._objectLayer.addData(xml);
268         map._objectLayer.addTo(map);
269
270         if (callback) callback(map._objectLayer.getBounds());
271       }
272     });
273   },
274
275   removeObject: function () {
276     this._object = null;
277     if (this._objectLoader) this._objectLoader.abort();
278     if (this._objectLayer) this.removeLayer(this._objectLayer);
279   },
280
281   getState: function () {
282     return {
283       center: this.getCenter().wrap(),
284       zoom: this.getZoom(),
285       layers: this.getLayersCode()
286     };
287   },
288
289   setState: function (state, options) {
290     if (state.center) this.setView(state.center, state.zoom, options);
291     if (state.layers) this.updateLayers(state.layers);
292   },
293
294   setSidebarOverlaid: function (overlaid) {
295     if (overlaid && !$("#content").hasClass("overlay-sidebar")) {
296       $("#content").addClass("overlay-sidebar");
297       this.invalidateSize({ pan: false })
298         .panBy([-350, 0], { animate: false });
299     } else if (!overlaid && $("#content").hasClass("overlay-sidebar")) {
300       this.panBy([350, 0], { animate: false });
301       $("#content").removeClass("overlay-sidebar");
302       this.invalidateSize({ pan: false });
303     }
304     return this;
305   }
306 });
307
308 L.Icon.Default.imagePath = "/images/";
309
310 L.Icon.Default.imageUrls = {
311   "/images/marker-icon.png": OSM.MARKER_ICON,
312   "/images/marker-icon-2x.png": OSM.MARKER_ICON_2X,
313   "/images/marker-shadow.png": OSM.MARKER_SHADOW
314 };
315
316 L.extend(L.Icon.Default.prototype, {
317   _oldGetIconUrl: L.Icon.Default.prototype._getIconUrl,
318
319   _getIconUrl: function (name) {
320     var url = this._oldGetIconUrl(name);
321     return L.Icon.Default.imageUrls[url];
322   }
323 });
324
325 OSM.getUserIcon = function (url) {
326   return L.icon({
327     iconUrl: url || OSM.MARKER_RED,
328     iconSize: [25, 41],
329     iconAnchor: [12, 41],
330     popupAnchor: [1, -34],
331     shadowUrl: OSM.MARKER_SHADOW,
332     shadowSize: [41, 41]
333   });
334 };