]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/maplibre/map.js
Fix header button height
[rails.git] / app / assets / javascripts / maplibre / map.js
1 //= require maplibre/controls
2 //= require maplibre/dom_util
3
4 maplibregl.Map.prototype._getUIString = function (key) {
5   const snakeCaseKey = key.replaceAll(/(?<=\w)[A-Z]/g, "_$&").toLowerCase();
6   return OSM.i18n.t(`javascripts.map.${snakeCaseKey}`);
7 };
8
9 OSM.MapLibre.showWebGLError = function (container) {
10   const containerElement =
11     typeof container === "string" ? document.getElementById(container) : container;
12
13   if (containerElement) {
14     fetch("/panes/webgl_error")
15       .then(response => response.text())
16       .then(html => containerElement.innerHTML = html);
17   }
18 };
19
20 OSM.MapLibre.Map = class extends maplibregl.Map {
21   constructor({ allowRotation, ...options } = {}) {
22     const rotationOptions = {};
23     if (allowRotation === false) {
24       Object.assign(rotationOptions, {
25         rollEnabled: false,
26         dragRotate: false,
27         pitchWithRotate: false,
28         bearingSnap: 180
29       });
30     }
31
32     let map;
33     try {
34       map = super({
35         // Style validation only affects debug output.
36         // Style errors are usually reported to authors, who should validate the style in CI for better error messages.
37         validateStyle: false,
38         ...rotationOptions,
39         ...options
40       });
41     } catch (error) {
42       const structuredError = JSON.parse(error.message);
43       if (structuredError.type === "webglcontextcreationerror") {
44         OSM.MapLibre.showWebGLError(options.container);
45       }
46       // the constructor panicked => we need to re-throw
47       throw error;
48     }
49     if (allowRotation === false) {
50       map.touchZoomRotate.disableRotation();
51       map.keyboard.disableRotation();
52     }
53     return map;
54   }
55 };
56
57 OSM.MapLibre.SecondaryMap = class extends OSM.MapLibre.Map {
58   constructor(options = {}) {
59     const defaultHomeZoom = 11;
60     super({
61       container: "map",
62       style: OSM.LAYER_DEFINITIONS[0].style,
63       attributionControl: false,
64       allowRotation: false,
65       maxPitch: 0,
66       center: OSM.home ? [OSM.home.lon, OSM.home.lat] : [0, 0],
67       zoom: OSM.home ? defaultHomeZoom : 0,
68       zoomSnap: 1.0,
69       ...options
70     });
71   }
72 };