UNPKG

10.5 kBPlain TextView Raw
1import assert from 'assert';
2import {supported} from '@mapbox/mapbox-gl-supported';
3
4import Map from './ui/map';
5import NavigationControl from './ui/control/navigation_control';
6import GeolocateControl from './ui/control/geolocate_control';
7import AttributionControl from './ui/control/attribution_control';
8import LogoControl from './ui/control/logo_control';
9import ScaleControl from './ui/control/scale_control';
10import FullscreenControl from './ui/control/fullscreen_control';
11import Popup from './ui/popup';
12import Marker from './ui/marker';
13import Style from './style/style';
14import LngLat from './geo/lng_lat';
15import LngLatBounds from './geo/lng_lat_bounds';
16import Point from '@mapbox/point-geometry';
17import MercatorCoordinate from './geo/mercator_coordinate';
18import {Evented} from './util/evented';
19import config from './util/config';
20import {Debug} from './util/debug';
21import {isSafari} from './util/util';
22import {setRTLTextPlugin, getRTLTextPluginStatus} from './source/rtl_text_plugin';
23import WorkerPool from './util/worker_pool';
24import {prewarm, clearPrewarmedResources} from './util/global_worker_pool';
25import {clearTileCache} from './util/tile_request_cache';
26import {PerformanceUtils} from './util/performance';
27import {AJAXError} from './util/ajax';
28import type {RequestParameters, ResponseCallback} from './util/ajax';
29import type {Cancelable} from './types/cancelable';
30import GeoJSONSource from './source/geojson_source';
31import CanvasSource from './source/canvas_source';
32import ImageSource from './source/image_source';
33import RasterDEMTileSource from './source/raster_dem_tile_source';
34import RasterTileSource from './source/raster_tile_source';
35import VectorTileSource from './source/vector_tile_source';
36import VideoSource from './source/video_source';
37
38const exported = {
39 supported,
40 setRTLTextPlugin,
41 getRTLTextPluginStatus,
42 Map,
43 NavigationControl,
44 GeolocateControl,
45 AttributionControl,
46 LogoControl,
47 ScaleControl,
48 FullscreenControl,
49 Popup,
50 Marker,
51 Style,
52 LngLat,
53 LngLatBounds,
54 Point,
55 MercatorCoordinate,
56 Evented,
57 AJAXError,
58 config,
59 CanvasSource,
60 GeoJSONSource,
61 ImageSource,
62 RasterDEMTileSource,
63 RasterTileSource,
64 VectorTileSource,
65 VideoSource,
66 /**
67 * Initializes resources like WebWorkers that can be shared across maps to lower load
68 * times in some situations. `maplibregl.workerUrl` and `maplibregl.workerCount`, if being
69 * used, must be set before `prewarm()` is called to have an effect.
70 *
71 * By default, the lifecycle of these resources is managed automatically, and they are
72 * lazily initialized when a Map is first created. By invoking `prewarm()`, these
73 * resources will be created ahead of time, and will not be cleared when the last Map
74 * is removed from the page. This allows them to be re-used by new Map instances that
75 * are created later. They can be manually cleared by calling
76 * `maplibregl.clearPrewarmedResources()`. This is only necessary if your web page remains
77 * active but stops using maps altogether.
78 *
79 * This is primarily useful when using GL-JS maps in a single page app, wherein a user
80 * would navigate between various views that can cause Map instances to constantly be
81 * created and destroyed.
82 *
83 * @function prewarm
84 * @example
85 * maplibregl.prewarm()
86 */
87 prewarm,
88 /**
89 * Clears up resources that have previously been created by `maplibregl.prewarm()`.
90 * Note that this is typically not necessary. You should only call this function
91 * if you expect the user of your app to not return to a Map view at any point
92 * in your application.
93 *
94 * @function clearPrewarmedResources
95 * @example
96 * maplibregl.clearPrewarmedResources()
97 */
98 clearPrewarmedResources,
99
100 /**
101 * Gets and sets the number of web workers instantiated on a page with GL JS maps.
102 * By default, it is set to half the number of CPU cores (capped at 6).
103 * Make sure to set this property before creating any map instances for it to have effect.
104 *
105 * @var {string} workerCount
106 * @returns {number} Number of workers currently configured.
107 * @example
108 * maplibregl.workerCount = 2;
109 */
110 get workerCount(): number {
111 return WorkerPool.workerCount;
112 },
113
114 set workerCount(count: number) {
115 WorkerPool.workerCount = count;
116 },
117
118 /**
119 * Gets and sets the maximum number of images (raster tiles, sprites, icons) to load in parallel,
120 * which affects performance in raster-heavy maps. 16 by default.
121 *
122 * @var {string} maxParallelImageRequests
123 * @returns {number} Number of parallel requests currently configured.
124 * @example
125 * maplibregl.maxParallelImageRequests = 10;
126 */
127 get maxParallelImageRequests(): number {
128 return config.MAX_PARALLEL_IMAGE_REQUESTS;
129 },
130
131 set maxParallelImageRequests(numRequests: number) {
132 config.MAX_PARALLEL_IMAGE_REQUESTS = numRequests;
133 },
134
135 /**
136 * Clears browser storage used by this library. Using this method flushes the MapLibre tile
137 * cache that is managed by this library. Tiles may still be cached by the browser
138 * in some cases.
139 *
140 * This API is supported on browsers where the [`Cache` API](https://developer.mozilla.org/en-US/docs/Web/API/Cache)
141 * is supported and enabled. This includes all major browsers when pages are served over
142 * `https://`, except Internet Explorer and Edge Mobile.
143 *
144 * When called in unsupported browsers or environments (private or incognito mode), the
145 * callback will be called with an error argument.
146 *
147 * @function clearStorage
148 * @param {Function} callback Called with an error argument if there is an error.
149 * @example
150 * maplibregl.clearStorage();
151 */
152 clearStorage(callback?: (err?: Error | null) => void) {
153 clearTileCache(callback);
154 },
155
156 workerUrl: '',
157
158 /**
159 * Sets a custom load tile function that will be called when using a source that starts with a custom url schema.
160 * The example below will be triggered for custom:// urls defined in the sources list in the style definitions.
161 * The function passed will receive the request parameters and should call the callback with the resulting request,
162 * for example a pbf vector tile, non-compressed, represented as ArrayBuffer.
163 *
164 * @function addProtocol
165 * @param {string} customProtocol - the protocol to hook, for example 'custom'
166 * @param {Function} loadFn - the function to use when trying to fetch a tile specified by the customProtocol
167 * @example
168 * // this will fetch a file using the fetch API (this is obviously a non iteresting example...)
169 * maplibre.addProtocol('custom', (params, callback) => {
170 fetch(`https://${params.url.split("://")[1]}`)
171 .then(t => {
172 if (t.status == 200) {
173 t.arrayBuffer().then(arr => {
174 callback(null, arr, null, null);
175 });
176 } else {
177 callback(new Error(`Tile fetch error: ${t.statusText}`));
178 }
179 })
180 .catch(e => {
181 callback(new Error(e));
182 });
183 return { cancel: () => { } };
184 });
185 * // the following is an example of a way to return an error when trying to load a tile
186 * maplibre.addProtocol('custom2', (params, callback) => {
187 * callback(new Error('someErrorMessage'));
188 * return { cancel: () => { } };
189 * });
190 */
191 addProtocol(customProtocol: string, loadFn: (requestParameters: RequestParameters, callback: ResponseCallback<any>) => Cancelable) {
192 config.REGISTERED_PROTOCOLS[customProtocol] = loadFn;
193 },
194
195 /**
196 * Removes a previusly added protocol
197 *
198 * @function removeProtocol
199 * @param {string} customProtocol - the custom protocol to remove registration for
200 * @example
201 * maplibregl.removeProtocol('custom');
202 */
203 removeProtocol(customProtocol: string) {
204 delete config.REGISTERED_PROTOCOLS[customProtocol];
205 }
206};
207
208//This gets automatically stripped out in production builds.
209Debug.extend(exported, {isSafari, getPerformanceMetrics: PerformanceUtils.getPerformanceMetrics});
210
211/**
212 * Test whether the browser supports MapLibre GL JS.
213 *
214 * @function supported
215 * @param {Object} [options]
216 * @param {boolean} [options.failIfMajorPerformanceCaveat=false] If `true`,
217 * the function will return `false` if the performance of MapLibre GL JS would
218 * be dramatically worse than expected (e.g. a software WebGL renderer would be used).
219 * @return {boolean}
220 * @example
221 * // Show an alert if the browser does not support MapLibre GL
222 * if (!maplibregl.supported()) {
223 * alert('Your browser does not support MapLibre GL');
224 * }
225 * @see [Check for browser support](https://maplibre.org/maplibre-gl-js-docs/example/check-for-support/)
226 */
227
228/**
229 * Sets the map's [RTL text plugin](https://www.mapbox.com/mapbox-gl-js/plugins/#mapbox-gl-rtl-text).
230 * Necessary for supporting the Arabic and Hebrew languages, which are written right-to-left.
231 *
232 * @function setRTLTextPlugin
233 * @param {string} pluginURL URL pointing to the Mapbox RTL text plugin source.
234 * @param {Function} callback Called with an error argument if there is an error.
235 * @param {boolean} lazy If set to `true`, mapboxgl will defer loading the plugin until rtl text is encountered,
236 * rtl text will then be rendered only after the plugin finishes loading.
237 * @example
238 * maplibregl.setRTLTextPlugin('https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.2.0/mapbox-gl-rtl-text.js');
239 * @see [Add support for right-to-left scripts](https://maplibre.org/maplibre-gl-js-docs/example/mapbox-gl-rtl-text/)
240 */
241
242/**
243 * Gets the map's [RTL text plugin](https://www.mapbox.com/mapbox-gl-js/plugins/#mapbox-gl-rtl-text) status.
244 * The status can be `unavailable` (i.e. not requested or removed), `loading`, `loaded` or `error`.
245 * If the status is `loaded` and the plugin is requested again, an error will be thrown.
246 *
247 * @function getRTLTextPluginStatus
248 * @example
249 * const pluginStatus = maplibregl.getRTLTextPluginStatus();
250 */
251
252export default exported;
253// canary assert: used to confirm that asserts have been removed from production build
254assert(true, 'canary assert');