maplair 4.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2804 -0
- package/LICENSE +27 -0
- package/README.md +40 -0
- package/dist/esm/Maplair.js +71 -0
- package/dist/esm/Maplair.js.map +1 -0
- package/dist/esm/_virtual/_rolldown/runtime.js +13 -0
- package/dist/esm/control/AttributionControl.js +79 -0
- package/dist/esm/control/AttributionControl.js.map +1 -0
- package/dist/esm/control/Control.js +81 -0
- package/dist/esm/control/Control.js.map +1 -0
- package/dist/esm/control/LayersControl.js +220 -0
- package/dist/esm/control/LayersControl.js.map +1 -0
- package/dist/esm/control/ScaleControl.js +72 -0
- package/dist/esm/control/ScaleControl.js.map +1 -0
- package/dist/esm/control/ZoomControl.js +86 -0
- package/dist/esm/control/ZoomControl.js.map +1 -0
- package/dist/esm/core/Browser.js +31 -0
- package/dist/esm/core/Browser.js.map +1 -0
- package/dist/esm/core/Class.js +52 -0
- package/dist/esm/core/Class.js.map +1 -0
- package/dist/esm/core/Events.js +149 -0
- package/dist/esm/core/Events.js.map +1 -0
- package/dist/esm/core/Handler.js +32 -0
- package/dist/esm/core/Handler.js.map +1 -0
- package/dist/esm/core/I18n.js +36 -0
- package/dist/esm/core/I18n.js.map +1 -0
- package/dist/esm/core/Util.js +71 -0
- package/dist/esm/core/Util.js.map +1 -0
- package/dist/esm/dom/DomEvent.DoubleTap.js +74 -0
- package/dist/esm/dom/DomEvent.DoubleTap.js.map +1 -0
- package/dist/esm/dom/DomEvent.PointerEvents.js +46 -0
- package/dist/esm/dom/DomEvent.PointerEvents.js.map +1 -0
- package/dist/esm/dom/DomEvent.js +135 -0
- package/dist/esm/dom/DomEvent.js.map +1 -0
- package/dist/esm/dom/DomUtil.js +107 -0
- package/dist/esm/dom/DomUtil.js.map +1 -0
- package/dist/esm/dom/Draggable.js +107 -0
- package/dist/esm/dom/Draggable.js.map +1 -0
- package/dist/esm/dom/PosAnimation.js +56 -0
- package/dist/esm/dom/PosAnimation.js.map +1 -0
- package/dist/esm/geo/LatLng.js +65 -0
- package/dist/esm/geo/LatLng.js.map +1 -0
- package/dist/esm/geo/LatLngBounds.js +114 -0
- package/dist/esm/geo/LatLngBounds.js.map +1 -0
- package/dist/esm/geo/crs/CRS.js +51 -0
- package/dist/esm/geo/crs/CRS.js.map +1 -0
- package/dist/esm/geo/crs/EPSG3395.js +16 -0
- package/dist/esm/geo/crs/EPSG3395.js.map +1 -0
- package/dist/esm/geo/crs/EPSG3857.js +19 -0
- package/dist/esm/geo/crs/EPSG3857.js.map +1 -0
- package/dist/esm/geo/crs/EPSG4326.js +13 -0
- package/dist/esm/geo/crs/EPSG4326.js.map +1 -0
- package/dist/esm/geo/crs/EarthCRS.js +14 -0
- package/dist/esm/geo/crs/EarthCRS.js.map +1 -0
- package/dist/esm/geo/crs/SimpleCRS.js +23 -0
- package/dist/esm/geo/crs/SimpleCRS.js.map +1 -0
- package/dist/esm/geo/projection/Projection.LonLat.js +19 -0
- package/dist/esm/geo/projection/Projection.LonLat.js.map +1 -0
- package/dist/esm/geo/projection/Projection.Mercator.js +33 -0
- package/dist/esm/geo/projection/Projection.Mercator.js.map +1 -0
- package/dist/esm/geo/projection/Projection.SphericalMercator.js +27 -0
- package/dist/esm/geo/projection/Projection.SphericalMercator.js.map +1 -0
- package/dist/esm/geo/projection/index.js +14 -0
- package/dist/esm/geo/projection/index.js.map +1 -0
- package/dist/esm/geometry/Bounds.js +91 -0
- package/dist/esm/geometry/Bounds.js.map +1 -0
- package/dist/esm/geometry/LineUtil.js +169 -0
- package/dist/esm/geometry/LineUtil.js.map +1 -0
- package/dist/esm/geometry/Point.js +119 -0
- package/dist/esm/geometry/Point.js.map +1 -0
- package/dist/esm/geometry/PolyUtil.js +89 -0
- package/dist/esm/geometry/PolyUtil.js.map +1 -0
- package/dist/esm/geometry/Transformation.js +34 -0
- package/dist/esm/geometry/Transformation.js.map +1 -0
- package/dist/esm/layer/BlanketOverlay.js +87 -0
- package/dist/esm/layer/BlanketOverlay.js.map +1 -0
- package/dist/esm/layer/DivOverlay.js +192 -0
- package/dist/esm/layer/DivOverlay.js.map +1 -0
- package/dist/esm/layer/FeatureGroup.js +40 -0
- package/dist/esm/layer/FeatureGroup.js.map +1 -0
- package/dist/esm/layer/GeoJSON.js +192 -0
- package/dist/esm/layer/GeoJSON.js.map +1 -0
- package/dist/esm/layer/ImageOverlay.js +140 -0
- package/dist/esm/layer/ImageOverlay.js.map +1 -0
- package/dist/esm/layer/Layer.js +119 -0
- package/dist/esm/layer/Layer.js.map +1 -0
- package/dist/esm/layer/LayerGroup.js +58 -0
- package/dist/esm/layer/LayerGroup.js.map +1 -0
- package/dist/esm/layer/Popup.js +224 -0
- package/dist/esm/layer/Popup.js.map +1 -0
- package/dist/esm/layer/SVGOverlay.js +21 -0
- package/dist/esm/layer/SVGOverlay.js.map +1 -0
- package/dist/esm/layer/Tooltip.js +228 -0
- package/dist/esm/layer/Tooltip.js.map +1 -0
- package/dist/esm/layer/VideoOverlay.js +54 -0
- package/dist/esm/layer/VideoOverlay.js.map +1 -0
- package/dist/esm/layer/marker/DefaultIcon.js +50 -0
- package/dist/esm/layer/marker/DefaultIcon.js.map +1 -0
- package/dist/esm/layer/marker/DivIcon.js +37 -0
- package/dist/esm/layer/marker/DivIcon.js.map +1 -0
- package/dist/esm/layer/marker/Icon.js +65 -0
- package/dist/esm/layer/marker/Icon.js.map +1 -0
- package/dist/esm/layer/marker/Marker.Drag.js +77 -0
- package/dist/esm/layer/marker/Marker.Drag.js.map +1 -0
- package/dist/esm/layer/marker/Marker.js +235 -0
- package/dist/esm/layer/marker/Marker.js.map +1 -0
- package/dist/esm/layer/tile/GridLayer.js +447 -0
- package/dist/esm/layer/tile/GridLayer.js.map +1 -0
- package/dist/esm/layer/tile/TileLayer.js +139 -0
- package/dist/esm/layer/tile/TileLayer.js.map +1 -0
- package/dist/esm/layer/tile/WMSTileLayer.js +68 -0
- package/dist/esm/layer/tile/WMSTileLayer.js.map +1 -0
- package/dist/esm/layer/vector/Canvas.js +292 -0
- package/dist/esm/layer/vector/Canvas.js.map +1 -0
- package/dist/esm/layer/vector/Circle.js +35 -0
- package/dist/esm/layer/vector/Circle.js.map +1 -0
- package/dist/esm/layer/vector/CircleMarker.js +69 -0
- package/dist/esm/layer/vector/CircleMarker.js.map +1 -0
- package/dist/esm/layer/vector/Path.js +68 -0
- package/dist/esm/layer/vector/Path.js.map +1 -0
- package/dist/esm/layer/vector/Polygon.js +70 -0
- package/dist/esm/layer/vector/Polygon.js.map +1 -0
- package/dist/esm/layer/vector/Polyline.js +152 -0
- package/dist/esm/layer/vector/Polyline.js.map +1 -0
- package/dist/esm/layer/vector/Rectangle.js +25 -0
- package/dist/esm/layer/vector/Rectangle.js.map +1 -0
- package/dist/esm/layer/vector/Renderer.getRenderer.js +31 -0
- package/dist/esm/layer/vector/Renderer.getRenderer.js.map +1 -0
- package/dist/esm/layer/vector/Renderer.js +39 -0
- package/dist/esm/layer/vector/Renderer.js.map +1 -0
- package/dist/esm/layer/vector/SVG.js +115 -0
- package/dist/esm/layer/vector/SVG.js.map +1 -0
- package/dist/esm/map/Map.js +844 -0
- package/dist/esm/map/Map.js.map +1 -0
- package/dist/esm/map/handler/BoxZoomHandler.js +105 -0
- package/dist/esm/map/handler/BoxZoomHandler.js.map +1 -0
- package/dist/esm/map/handler/DoubleClickZoomHandler.js +27 -0
- package/dist/esm/map/handler/DoubleClickZoomHandler.js.map +1 -0
- package/dist/esm/map/handler/DragHandler.js +133 -0
- package/dist/esm/map/handler/DragHandler.js.map +1 -0
- package/dist/esm/map/handler/KeyboardHandler.js +117 -0
- package/dist/esm/map/handler/KeyboardHandler.js.map +1 -0
- package/dist/esm/map/handler/PinchZoomHandler.js +96 -0
- package/dist/esm/map/handler/PinchZoomHandler.js.map +1 -0
- package/dist/esm/map/handler/ScrollWheelZoomHandler.js +50 -0
- package/dist/esm/map/handler/ScrollWheelZoomHandler.js.map +1 -0
- package/dist/esm/map/handler/TapHoldHandler.js +77 -0
- package/dist/esm/map/handler/TapHoldHandler.js.map +1 -0
- package/dist/esm/package.js +6 -0
- package/dist/esm/package.js.map +1 -0
- package/dist/images/layers.svg +1 -0
- package/dist/images/logo.svg +1 -0
- package/dist/images/marker-icon.svg +1 -0
- package/dist/images/marker-shadow.svg +1 -0
- package/dist/maplair-global-src.js +6810 -0
- package/dist/maplair-global-src.js.map +1 -0
- package/dist/maplair-global.js +12 -0
- package/dist/maplair-global.js.map +1 -0
- package/dist/maplair-src.js +6704 -0
- package/dist/maplair-src.js.map +1 -0
- package/dist/maplair.css +730 -0
- package/dist/maplair.js +7 -0
- package/dist/maplair.js.map +1 -0
- package/i18n/cs.json +12 -0
- package/i18n/de.json +12 -0
- package/i18n/en.json +12 -0
- package/i18n/es.json +12 -0
- package/i18n/fr.json +12 -0
- package/i18n/hu.json +12 -0
- package/i18n/it.json +12 -0
- package/i18n/rm.json +12 -0
- package/i18n/sk.json +12 -0
- package/i18n/sl.json +12 -0
- package/package.json +87 -0
- package/src/Maplair.js +24 -0
- package/src/control/AttributionControl.js +147 -0
- package/src/control/Control.js +174 -0
- package/src/control/LayersControl.js +444 -0
- package/src/control/ScaleControl.js +141 -0
- package/src/control/ZoomControl.js +147 -0
- package/src/control/index.js +5 -0
- package/src/core/Browser.js +75 -0
- package/src/core/Class.js +97 -0
- package/src/core/Events.js +334 -0
- package/src/core/Handler.js +59 -0
- package/src/core/I18n.js +76 -0
- package/src/core/Util.js +122 -0
- package/src/core/index.js +8 -0
- package/src/dom/DomEvent.DoubleTap.js +120 -0
- package/src/dom/DomEvent.PointerEvents.js +75 -0
- package/src/dom/DomEvent.js +282 -0
- package/src/dom/DomUtil.js +175 -0
- package/src/dom/Draggable.js +204 -0
- package/src/dom/PosAnimation.js +115 -0
- package/src/dom/index.js +9 -0
- package/src/geo/LatLng.js +163 -0
- package/src/geo/LatLngBounds.js +260 -0
- package/src/geo/crs/CRS.js +143 -0
- package/src/geo/crs/EPSG3395.js +19 -0
- package/src/geo/crs/EPSG3857.js +26 -0
- package/src/geo/crs/EPSG4326.js +21 -0
- package/src/geo/crs/EarthCRS.js +32 -0
- package/src/geo/crs/SimpleCRS.js +35 -0
- package/src/geo/crs/index.js +6 -0
- package/src/geo/index.js +7 -0
- package/src/geo/projection/Projection.LonLat.js +30 -0
- package/src/geo/projection/Projection.Mercator.js +53 -0
- package/src/geo/projection/Projection.SphericalMercator.js +46 -0
- package/src/geo/projection/index.js +26 -0
- package/src/geometry/Bounds.js +222 -0
- package/src/geometry/LineUtil.js +299 -0
- package/src/geometry/Point.js +239 -0
- package/src/geometry/PolyUtil.js +129 -0
- package/src/geometry/Transformation.js +67 -0
- package/src/geometry/index.js +8 -0
- package/src/images/layers.svg +1 -0
- package/src/images/logo.svg +1 -0
- package/src/images/marker-icon.svg +1 -0
- package/src/images/marker-shadow.svg +1 -0
- package/src/layer/BlanketOverlay.js +171 -0
- package/src/layer/DivOverlay.js +353 -0
- package/src/layer/FeatureGroup.js +93 -0
- package/src/layer/GeoJSON.js +432 -0
- package/src/layer/ImageOverlay.js +279 -0
- package/src/layer/Layer.js +273 -0
- package/src/layer/LayerGroup.js +129 -0
- package/src/layer/Popup.js +528 -0
- package/src/layer/SVGOverlay.js +47 -0
- package/src/layer/Tooltip.js +469 -0
- package/src/layer/VideoOverlay.js +111 -0
- package/src/layer/index.js +18 -0
- package/src/layer/marker/DefaultIcon.js +79 -0
- package/src/layer/marker/DivIcon.js +76 -0
- package/src/layer/marker/Icon.js +164 -0
- package/src/layer/marker/Marker.Drag.js +161 -0
- package/src/layer/marker/Marker.js +465 -0
- package/src/layer/marker/index.js +5 -0
- package/src/layer/tile/GridLayer.js +901 -0
- package/src/layer/tile/TileLayer.js +299 -0
- package/src/layer/tile/WMSTileLayer.js +133 -0
- package/src/layer/tile/index.js +3 -0
- package/src/layer/vector/Canvas.js +479 -0
- package/src/layer/vector/Circle.js +87 -0
- package/src/layer/vector/CircleMarker.js +110 -0
- package/src/layer/vector/Path.js +145 -0
- package/src/layer/vector/Polygon.js +160 -0
- package/src/layer/vector/Polyline.js +296 -0
- package/src/layer/vector/Rectangle.js +52 -0
- package/src/layer/vector/Renderer.getRenderer.js +48 -0
- package/src/layer/vector/Renderer.js +75 -0
- package/src/layer/vector/SVG.js +216 -0
- package/src/layer/vector/index.js +10 -0
- package/src/map/Map.js +1830 -0
- package/src/map/handler/BoxZoomHandler.js +156 -0
- package/src/map/handler/DoubleClickZoomHandler.js +62 -0
- package/src/map/handler/DragHandler.js +240 -0
- package/src/map/handler/KeyboardHandler.js +188 -0
- package/src/map/handler/PinchZoomHandler.js +157 -0
- package/src/map/handler/ScrollWheelZoomHandler.js +98 -0
- package/src/map/handler/TapHoldHandler.js +111 -0
- package/src/map/index.js +8 -0
- package/src/maplair.css +730 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {Class} from './Class.js';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
Handler is a base class for handler classes that are used internally to inject
|
|
5
|
+
interaction features like dragging to classes like Map and Marker.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// @class Handler
|
|
9
|
+
// Abstract class for map interaction handlers
|
|
10
|
+
|
|
11
|
+
export class Handler extends Class {
|
|
12
|
+
constructor(map) {
|
|
13
|
+
super();
|
|
14
|
+
this._map = map;
|
|
15
|
+
|
|
16
|
+
this.callInitHooks(Handler);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// @method enable(): this
|
|
20
|
+
// Enables the handler
|
|
21
|
+
enable() {
|
|
22
|
+
if (this._enabled) { return this; }
|
|
23
|
+
|
|
24
|
+
this._enabled = true;
|
|
25
|
+
this.addHooks();
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// @method disable(): this
|
|
30
|
+
// Disables the handler
|
|
31
|
+
disable() {
|
|
32
|
+
if (!this._enabled) { return this; }
|
|
33
|
+
|
|
34
|
+
this._enabled = false;
|
|
35
|
+
this.removeHooks();
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// @method enabled(): Boolean
|
|
40
|
+
// Returns `true` if the handler is enabled
|
|
41
|
+
enabled() {
|
|
42
|
+
return !!this._enabled;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// @section Extension methods
|
|
46
|
+
// Classes inheriting from `Handler` must implement the two following methods:
|
|
47
|
+
// @method addHooks()
|
|
48
|
+
// Called when the handler is enabled, should add event hooks.
|
|
49
|
+
// @method removeHooks()
|
|
50
|
+
// Called when the handler is disabled, should remove the event hooks added previously.
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// @section There is static function which can be called without instantiating Handler:
|
|
54
|
+
// @function addTo(map: MaplairMap, name: String): this
|
|
55
|
+
// Adds a new Handler to the given map with the given name.
|
|
56
|
+
Handler.addTo = function (map, name) {
|
|
57
|
+
map.addHandler(name, this);
|
|
58
|
+
return this;
|
|
59
|
+
};
|
package/src/core/I18n.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import * as Util from './Util.js';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* @namespace I18n
|
|
5
|
+
*
|
|
6
|
+
* Utility functions to deal with translations.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* Translations of Maplair's built-in strings are maintained on
|
|
11
|
+
* [Transifex](https://app.transifex.com/simon04/maplair/) and published in the
|
|
12
|
+
* `i18n` directory of the package. Load one before creating the map:
|
|
13
|
+
*
|
|
14
|
+
* ```js
|
|
15
|
+
* import {I18n} from 'maplair';
|
|
16
|
+
* import de from 'maplair/i18n/de.json' with {type: 'json'};
|
|
17
|
+
*
|
|
18
|
+
* I18n.registerLocale('de', de);
|
|
19
|
+
* I18n.setLocale('de');
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export class I18n {
|
|
24
|
+
// @property messages: Object
|
|
25
|
+
// All localized strings.
|
|
26
|
+
static #messages = {};
|
|
27
|
+
|
|
28
|
+
// @property locale: String
|
|
29
|
+
// The current locale code, that will be used when translating strings.
|
|
30
|
+
static #locale = null;
|
|
31
|
+
|
|
32
|
+
// @function registerLocale(locale: String, messages?: Object): undefined
|
|
33
|
+
// Define localized strings for a given locale, defined by `locale`.
|
|
34
|
+
static registerLocale(locale, messages) {
|
|
35
|
+
this.#messages[locale] = {...this.#messages[locale], ...messages};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// @function setLocale(locale: String): undefined
|
|
39
|
+
// Define or change the locale code to be used when translating strings.
|
|
40
|
+
// Note that changing the locale does not affect already instantiated objects.
|
|
41
|
+
static setLocale(locale) {
|
|
42
|
+
this.#locale = locale;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// @method getLocale(): String
|
|
46
|
+
// Returns the current locale code, that will be used when translating strings.
|
|
47
|
+
static getLocale() {
|
|
48
|
+
return this.#locale;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// @function translate(string: String, data?: Object): String
|
|
52
|
+
// Actually try to translate the `string`, with optional variable passed in `data`.
|
|
53
|
+
static translate(string, data = {}) {
|
|
54
|
+
// If the locale is not set, localization is not enabled.
|
|
55
|
+
if (this.#locale === null) {
|
|
56
|
+
return Util.template(string, data);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const translation = this.#messages[this.#locale]?.[string];
|
|
60
|
+
|
|
61
|
+
if (!translation) {
|
|
62
|
+
console.warn(`No translation found for "${string}", falling back to the original string.`);
|
|
63
|
+
return Util.template(string, data);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
// Do not fail if some data is missing
|
|
68
|
+
// a bad translation should not break the app
|
|
69
|
+
return Util.template(translation, data);
|
|
70
|
+
} catch (err) {
|
|
71
|
+
console.error(`Error while translating "${translation}".`, err);
|
|
72
|
+
return translation;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
}
|
package/src/core/Util.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @namespace Util
|
|
3
|
+
*
|
|
4
|
+
* Various utility functions, used by Maplair internally.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// @property lastId: Number
|
|
8
|
+
// Last unique ID used by [`stamp()`](#util-stamp)
|
|
9
|
+
export let lastId = 0;
|
|
10
|
+
|
|
11
|
+
// @function stamp(obj: Object): Number
|
|
12
|
+
// Returns the unique ID of an object, assigning it one if it doesn't have it.
|
|
13
|
+
export function stamp(obj) {
|
|
14
|
+
if (!('_maplair_id' in obj)) {
|
|
15
|
+
obj['_maplair_id'] = ++lastId;
|
|
16
|
+
}
|
|
17
|
+
return obj._maplair_id;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// @function throttle(fn: Function, time: Number, context: Object): Function
|
|
21
|
+
// Returns a function which executes function `fn` with the given scope `context`
|
|
22
|
+
// (so that the `this` keyword refers to `context` inside `fn`'s code). The function
|
|
23
|
+
// `fn` will be called no more than one time per given amount of `time`. The arguments
|
|
24
|
+
// received by the bound function will be any arguments passed when binding the
|
|
25
|
+
// function, followed by any arguments passed when invoking the bound function.
|
|
26
|
+
export function throttle(fn, time, context) {
|
|
27
|
+
let lock, queuedArgs;
|
|
28
|
+
|
|
29
|
+
function later() {
|
|
30
|
+
// reset lock and call if queued
|
|
31
|
+
lock = false;
|
|
32
|
+
if (queuedArgs) {
|
|
33
|
+
wrapperFn.apply(context, queuedArgs);
|
|
34
|
+
queuedArgs = false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function wrapperFn(...args) {
|
|
39
|
+
if (lock) {
|
|
40
|
+
// called too soon, queue to call later
|
|
41
|
+
queuedArgs = args;
|
|
42
|
+
|
|
43
|
+
} else {
|
|
44
|
+
// call and lock until later
|
|
45
|
+
fn.apply(context, args);
|
|
46
|
+
setTimeout(later, time);
|
|
47
|
+
lock = true;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return wrapperFn;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// @function wrapNum(num: Number, range: Number[], includeMax?: Boolean): Number
|
|
55
|
+
// Returns the number `num` modulo `range` in such a way so it lies within
|
|
56
|
+
// `range[0]` and `range[1]`. The returned value will be always smaller than
|
|
57
|
+
// `range[1]` unless `includeMax` is set to `true`.
|
|
58
|
+
export function wrapNum(x, range, includeMax) {
|
|
59
|
+
const max = range[1],
|
|
60
|
+
min = range[0],
|
|
61
|
+
d = max - min;
|
|
62
|
+
return x === max && includeMax ? x : ((x - min) % d + d) % d + min;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// @function falseFn(): Function
|
|
66
|
+
// Returns a function which always returns `false`.
|
|
67
|
+
export function falseFn() { return false; }
|
|
68
|
+
|
|
69
|
+
// @function formatNum(num: Number, precision?: Number|false): Number
|
|
70
|
+
// Returns the number `num` rounded with specified `precision`.
|
|
71
|
+
// The default `precision` value is 6 decimal places.
|
|
72
|
+
// `false` can be passed to skip any processing (can be useful to avoid round-off errors).
|
|
73
|
+
export function formatNum(num, precision) {
|
|
74
|
+
if (precision === false) { return num; }
|
|
75
|
+
const pow = 10 ** (precision === undefined ? 6 : precision);
|
|
76
|
+
return Math.round(num * pow) / pow;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const WHITESPACE_RE = /\s+/;
|
|
80
|
+
|
|
81
|
+
// @function splitWords(str: String): String[]
|
|
82
|
+
// Trims and splits the string on whitespace and returns the array of parts.
|
|
83
|
+
export function splitWords(str) {
|
|
84
|
+
return str.trim().split(WHITESPACE_RE);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// @function setOptions(obj: Object, options: Object): Object
|
|
88
|
+
// Merges the given properties to the `options` of the `obj` object, returning the resulting options. See `Class options`.
|
|
89
|
+
export function setOptions(obj, options) {
|
|
90
|
+
if (!Object.hasOwn(obj, 'options')) {
|
|
91
|
+
obj.options = obj.options ? Object.create(obj.options) : {};
|
|
92
|
+
}
|
|
93
|
+
for (const i in options) {
|
|
94
|
+
if (Object.hasOwn(options, i)) {
|
|
95
|
+
obj.options[i] = options[i];
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return obj.options;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const templateRe = /\{ *([\w_ -]+) *\}/g;
|
|
102
|
+
|
|
103
|
+
// @function template(str: String, data: Object): String
|
|
104
|
+
// Simple templating facility, accepts a template string of the form `'Hello {a}, {b}'`
|
|
105
|
+
// and a data object like `{a: 'foo', b: 'bar'}`, returns evaluated string
|
|
106
|
+
// `('Hello foo, bar')`. You can also specify functions instead of strings for
|
|
107
|
+
// data values — they will be evaluated passing `data` as an argument.
|
|
108
|
+
export function template(str, data) {
|
|
109
|
+
return str.replace(templateRe, (str, key) => {
|
|
110
|
+
let value = data[key];
|
|
111
|
+
|
|
112
|
+
if (value === undefined) {
|
|
113
|
+
throw new Error(`No value provided for variable ${str}`);
|
|
114
|
+
|
|
115
|
+
} else if (typeof value === 'function') {
|
|
116
|
+
value = value(data);
|
|
117
|
+
}
|
|
118
|
+
return value;
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import * as DomEvent from './DomEvent.js';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Extends the event handling code with double tap support for mobile browsers.
|
|
5
|
+
*
|
|
6
|
+
* Note: currently most browsers fire native dblclick, with only a few exceptions
|
|
7
|
+
* (see https://github.com/Leaflet/Leaflet/issues/7012#issuecomment-595087386)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
function makeDblclick(ev) {
|
|
11
|
+
let init = {
|
|
12
|
+
// EventInit
|
|
13
|
+
bubbles: ev.bubbles,
|
|
14
|
+
cancelable: ev.cancelable,
|
|
15
|
+
composed: ev.composed,
|
|
16
|
+
|
|
17
|
+
// UIEventInit
|
|
18
|
+
detail: 2,
|
|
19
|
+
view: ev.view,
|
|
20
|
+
|
|
21
|
+
// mouseEventInit
|
|
22
|
+
screenX: ev.screenX,
|
|
23
|
+
screenY: ev.screenY,
|
|
24
|
+
clientX: ev.clientX,
|
|
25
|
+
clientY: ev.clientY,
|
|
26
|
+
ctrlKey: ev.ctrlKey,
|
|
27
|
+
shiftKey: ev.shiftKey,
|
|
28
|
+
altKey: ev.altKey,
|
|
29
|
+
metaKey: ev.metaKey,
|
|
30
|
+
button: ev.button,
|
|
31
|
+
buttons: ev.buttons,
|
|
32
|
+
relatedTarget: ev.relatedTarget,
|
|
33
|
+
region: ev.region,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
let newEvent;
|
|
37
|
+
// The `click` event received should be a PointerEvent - but some
|
|
38
|
+
// Firefox versions still use MouseEvent.
|
|
39
|
+
if (ev instanceof PointerEvent) {
|
|
40
|
+
init = {
|
|
41
|
+
...init,
|
|
42
|
+
pointerId: ev.pointerId,
|
|
43
|
+
width: ev.width,
|
|
44
|
+
height: ev.height,
|
|
45
|
+
pressure: ev.pressure,
|
|
46
|
+
tangentialPressure: ev.tangentialPressure,
|
|
47
|
+
tiltX: ev.tiltX,
|
|
48
|
+
tiltY: ev.tiltY,
|
|
49
|
+
twist: ev.twist,
|
|
50
|
+
pointerType: ev.pointerType,
|
|
51
|
+
isPrimary: ev.isPrimary,
|
|
52
|
+
};
|
|
53
|
+
newEvent = new PointerEvent('dblclick', init);
|
|
54
|
+
} else {
|
|
55
|
+
newEvent = new MouseEvent('dblclick', init);
|
|
56
|
+
}
|
|
57
|
+
return newEvent;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const delay = 200;
|
|
61
|
+
export function addDoubleTapListener(obj, handler) {
|
|
62
|
+
// Most browsers handle double tap natively
|
|
63
|
+
obj.addEventListener('dblclick', handler);
|
|
64
|
+
|
|
65
|
+
// On some platforms the browser doesn't fire native dblclicks for touch events.
|
|
66
|
+
// It seems that in all such cases `detail` property of `click` event is always `1`.
|
|
67
|
+
// So here we rely on that fact to avoid excessive 'dblclick' simulation when not needed.
|
|
68
|
+
let last = 0,
|
|
69
|
+
detail;
|
|
70
|
+
function simDblclick(ev) {
|
|
71
|
+
if (ev.detail !== 1) {
|
|
72
|
+
detail = ev.detail; // keep in sync to avoid false dblclick in some cases
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (ev.pointerType === 'mouse' ||
|
|
77
|
+
(ev.sourceCapabilities && !ev.sourceCapabilities.firesTouchEvents)) {
|
|
78
|
+
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// When clicking on an <input>, the browser generates a click on its
|
|
83
|
+
// <label> (and vice versa) triggering two clicks in quick succession.
|
|
84
|
+
// This ignores clicks on elements which are a label with a 'for'
|
|
85
|
+
// attribute (or children of such a label), but not children of
|
|
86
|
+
// a <input>.
|
|
87
|
+
const path = DomEvent.getPropagationPath(ev);
|
|
88
|
+
if (path.some(el => el instanceof HTMLLabelElement && el.attributes.for) &&
|
|
89
|
+
!path.some(el => (
|
|
90
|
+
el instanceof HTMLInputElement ||
|
|
91
|
+
el instanceof HTMLSelectElement
|
|
92
|
+
))
|
|
93
|
+
) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const now = Date.now();
|
|
98
|
+
if (now - last <= delay) {
|
|
99
|
+
detail++;
|
|
100
|
+
if (detail === 2) {
|
|
101
|
+
ev.target.dispatchEvent(makeDblclick(ev));
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
detail = 1;
|
|
105
|
+
}
|
|
106
|
+
last = now;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
obj.addEventListener('click', simDblclick);
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
dblclick: handler,
|
|
113
|
+
simDblclick
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function removeDoubleTapListener(obj, handlers) {
|
|
118
|
+
obj.removeEventListener('dblclick', handlers.dblclick);
|
|
119
|
+
obj.removeEventListener('click', handlers.simDblclick);
|
|
120
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/*
|
|
2
|
+
/* @namespace DomEvent
|
|
3
|
+
* @section Pointer detection
|
|
4
|
+
* Detects the pointers that are currently active on the document.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const activePointers = new Map();
|
|
8
|
+
|
|
9
|
+
// @function enablePointerDetection(el: HTMLElement)
|
|
10
|
+
// Enables pointer detection and capture for the document.
|
|
11
|
+
function enablePointerDetection(el) {
|
|
12
|
+
el.addEventListener('pointerdown', _onSet, {capture: true});
|
|
13
|
+
el.addEventListener('pointermove', _onUpdate, {capture: true});
|
|
14
|
+
el.addEventListener('pointerup', _onDelete, {capture: true});
|
|
15
|
+
el.addEventListener('pointercancel', _onDelete, {capture: true});
|
|
16
|
+
activePointers.clear();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// @function disablePointerDetection(el: HTMLElement)
|
|
20
|
+
// Disables pointer detection and capture for the document.
|
|
21
|
+
function disablePointerDetection(el) {
|
|
22
|
+
el.removeEventListener('pointerdown', _onSet, {capture: true});
|
|
23
|
+
el.removeEventListener('pointermove', _onUpdate, {capture: true});
|
|
24
|
+
el.removeEventListener('pointerup', _onDelete, {capture: true});
|
|
25
|
+
el.removeEventListener('pointercancel', _onDelete, {capture: true});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// NOTE: pointers are captured unconditionally, which can become a problem for
|
|
29
|
+
// synthetic events. The prosthetic-hand library does handle pointer capture,
|
|
30
|
+
// but others don't.
|
|
31
|
+
// If this becomers an issue, replace with `e.isTrusted && e.target.setPointerCapture()`
|
|
32
|
+
function _onSet(e) {
|
|
33
|
+
e.target.setPointerCapture(e.pointerId);
|
|
34
|
+
activePointers.set(e.pointerId, e);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function _onUpdate(e) {
|
|
38
|
+
if (activePointers.has(e.pointerId)) {
|
|
39
|
+
activePointers.set(e.pointerId, e);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// NOTE: idem as _onSet.
|
|
44
|
+
function _onDelete(e) {
|
|
45
|
+
e.target.releasePointerCapture(e.pointerId);
|
|
46
|
+
activePointers.delete(e.pointerId);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// @function getPointers(): PointerEvent[]
|
|
50
|
+
// Returns the active pointers on the document.
|
|
51
|
+
function getPointers() {
|
|
52
|
+
return [...activePointers.values()];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// @function cleanupPointers()
|
|
56
|
+
// Clears the detected pointers on the document.
|
|
57
|
+
// Note: This function should be not necessary to call, as the pointers are automatically cleared with `pointerup`, `pointercancel` and `pointerout` events.
|
|
58
|
+
function cleanupPointers() {
|
|
59
|
+
for (const e of activePointers.values()) {
|
|
60
|
+
try {
|
|
61
|
+
e.target.releasePointerCapture(e.pointerId);
|
|
62
|
+
} catch {
|
|
63
|
+
// target may already be detached; capture was on a dead node
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
activePointers.clear();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export {
|
|
70
|
+
enablePointerDetection,
|
|
71
|
+
disablePointerDetection,
|
|
72
|
+
getPointers,
|
|
73
|
+
cleanupPointers
|
|
74
|
+
};
|
|
75
|
+
|