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