@nativescript-community/ui-massifmaps 3.0.1 → 4.0.0
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 +37 -0
- package/README.md +237 -0
- package/api/bridge.android.d.ts +2 -0
- package/api/bridge.android.js +162 -0
- package/api/bridge.android.js.map +1 -0
- package/api/bridge.d.ts +90 -0
- package/api/bridge.ios.d.ts +2 -0
- package/api/bridge.ios.js +153 -0
- package/api/bridge.ios.js.map +1 -0
- package/api/index.common.d.ts +495 -0
- package/api/index.common.js +1449 -0
- package/api/index.common.js.map +1 -0
- package/api/index.d.ts +2 -0
- package/api/index.js +3 -0
- package/api/index.js.map +1 -0
- package/api/massif-api.d.ts +12398 -0
- package/api/resolve.d.ts +52 -0
- package/api/resolve.js +214 -0
- package/api/resolve.js.map +1 -0
- package/api/schema.d.ts +67 -0
- package/api/schema.js +538 -0
- package/api/schema.js.map +1 -0
- package/bindings/components/Options.d.ts +14 -13
- package/bindings/components/Options.js.map +1 -1
- package/bindings/core/Variant.d.ts +2 -1
- package/bindings/core/Variant.js.map +1 -1
- package/bindings/enums.d.ts +138 -0
- package/bindings/enums.js +153 -0
- package/bindings/enums.js.map +1 -0
- package/bindings/graphics/Bitmap.d.ts +2 -1
- package/bindings/graphics/Bitmap.js.map +1 -1
- package/bindings/layers/ClusterElementBuilder.d.ts +2 -1
- package/bindings/layers/ClusterElementBuilder.js.map +1 -1
- package/bindings/layers/CompositeVectorTileLayer.d.ts +3 -7
- package/bindings/layers/CompositeVectorTileLayer.js +3 -6
- package/bindings/layers/CompositeVectorTileLayer.js.map +1 -1
- package/bindings/layers/HillshadeRasterTileLayer.d.ts +3 -2
- package/bindings/layers/HillshadeRasterTileLayer.js.map +1 -1
- package/bindings/layers/Layer.d.ts +2 -1
- package/bindings/layers/Layer.js.map +1 -1
- package/bindings/layers/RasterTileLayer.d.ts +3 -2
- package/bindings/layers/RasterTileLayer.js.map +1 -1
- package/bindings/layers/TileLayer.d.ts +5 -3
- package/bindings/layers/TileLayer.js +1 -1
- package/bindings/layers/TileLayer.js.map +1 -1
- package/bindings/layers/VectorEditEventListener.d.ts +2 -1
- package/bindings/layers/VectorEditEventListener.js.map +1 -1
- package/bindings/layers/VectorTileLayer.d.ts +5 -4
- package/bindings/layers/VectorTileLayer.js.map +1 -1
- package/bindings/packagemanager/PackageInfo.d.ts +2 -1
- package/bindings/packagemanager/PackageInfo.js.map +1 -1
- package/bindings/packagemanager/PackageManagerListener.d.ts +2 -1
- package/bindings/packagemanager/PackageManagerListener.js.map +1 -1
- package/bindings/packagemanager/PackageStatus.d.ts +2 -1
- package/bindings/packagemanager/PackageStatus.js.map +1 -1
- package/bindings/packagemanager/PackageTileMask.d.ts +2 -1
- package/bindings/packagemanager/PackageTileMask.js.map +1 -1
- package/bindings/routing/RouteMatchingPoint.d.ts +2 -1
- package/bindings/routing/RouteMatchingPoint.js.map +1 -1
- package/bindings/routing/RoutingInstruction.js.map +1 -1
- package/bindings/styles/AnimationStyle.d.ts +3 -2
- package/bindings/styles/AnimationStyle.js.map +1 -1
- package/bindings/styles/AnimationStyleBuilder.d.ts +7 -6
- package/bindings/styles/AnimationStyleBuilder.js.map +1 -1
- package/bindings/styles/LabelStyle.js.map +1 -1
- package/bindings/styles/LabelStyleBuilder.d.ts +5 -4
- package/bindings/styles/LabelStyleBuilder.js.map +1 -1
- package/bindings/styles/LineStyle.d.ts +2 -1
- package/bindings/styles/LineStyle.js.map +1 -1
- package/bindings/styles/LineStyleBuilder.d.ts +6 -5
- package/bindings/styles/LineStyleBuilder.js.map +1 -1
- package/bindings/styles/MarkerStyle.js.map +1 -1
- package/bindings/styles/MarkerStyleBuilder.d.ts +5 -4
- package/bindings/styles/MarkerStyleBuilder.js.map +1 -1
- package/bindings/styles/NMLModelStyle.js.map +1 -1
- package/bindings/styles/NMLModelStyleBuilder.d.ts +5 -4
- package/bindings/styles/NMLModelStyleBuilder.js.map +1 -1
- package/bindings/ui/BalloonPopupButtonClickInfo.js.map +1 -1
- package/bindings/ui/ClickInfo.js.map +1 -1
- package/bindings/ui/MapClickInfo.js.map +1 -1
- package/bindings/ui/PopupClickInfo.js.map +1 -1
- package/bindings/ui/RasterTileClickInfo.js.map +1 -1
- package/bindings/ui/UTFGridClickInfo.js.map +1 -1
- package/bindings/ui/VectorElementClickInfo.js.map +1 -1
- package/bindings/ui/VectorElementDragInfo.d.ts +2 -1
- package/bindings/ui/VectorElementDragInfo.js.map +1 -1
- package/bindings/ui/VectorTileClickInfo.d.ts +3 -1
- package/bindings/ui/VectorTileClickInfo.js +5 -2
- package/bindings/ui/VectorTileClickInfo.js.map +1 -1
- package/bindings/utils/BundleAssetPackage.d.ts +15 -0
- package/bindings/utils/BundleAssetPackage.js +8 -0
- package/bindings/utils/BundleAssetPackage.js.map +1 -0
- package/bindings/utils/index.d.ts +1 -0
- package/bindings/utils/index.js +1 -0
- package/bindings/utils/index.js.map +1 -1
- package/bindings/vectortiles/MBVectorTileDecoder.d.ts +4 -3
- package/bindings/vectortiles/MBVectorTileDecoder.js.map +1 -1
- package/celestial/CelestialArc.android.d.ts +0 -1
- package/celestial/CelestialArc.android.js +0 -3
- package/celestial/CelestialArc.android.js.map +1 -1
- package/celestial/CelestialArc.d.ts +1 -3
- package/celestial/CelestialArc.ios.d.ts +0 -1
- package/celestial/CelestialArc.ios.js +0 -1
- package/celestial/CelestialArc.ios.js.map +1 -1
- package/celestial/CelestialObject.android.d.ts +0 -1
- package/celestial/CelestialObject.android.js +0 -1
- package/celestial/CelestialObject.android.js.map +1 -1
- package/celestial/CelestialObject.d.ts +0 -2
- package/celestial/CelestialObject.ios.d.ts +0 -1
- package/celestial/CelestialObject.ios.js +0 -1
- package/celestial/CelestialObject.ios.js.map +1 -1
- package/celestial/CelestialObjectVector.android.d.ts +0 -1
- package/celestial/CelestialObjectVector.android.js +0 -4
- package/celestial/CelestialObjectVector.android.js.map +1 -1
- package/celestial/CelestialObjectVector.d.ts +0 -2
- package/celestial/CelestialObjectVector.ios.d.ts +0 -1
- package/celestial/CelestialObjectVector.ios.js +0 -1
- package/celestial/CelestialObjectVector.ios.js.map +1 -1
- package/celestial/CelestialSprite.android.d.ts +0 -1
- package/celestial/CelestialSprite.android.js +0 -3
- package/celestial/CelestialSprite.android.js.map +1 -1
- package/celestial/CelestialSprite.d.ts +1 -3
- package/celestial/CelestialSprite.ios.d.ts +0 -1
- package/celestial/CelestialSprite.ios.js +0 -1
- package/celestial/CelestialSprite.ios.js.map +1 -1
- package/components/index.android.d.ts +9 -27
- package/components/index.android.js +3 -21
- package/components/index.android.js.map +1 -1
- package/components/index.d.ts +4 -26
- package/components/index.ios.d.ts +3 -21
- package/components/index.ios.js +3 -21
- package/components/index.ios.js.map +1 -1
- package/core/index.android.js +1 -1
- package/core/index.android.js.map +1 -1
- package/core/index.ios.js +1 -2
- package/core/index.ios.js.map +1 -1
- package/datasources/mbtiles.android.d.ts +2 -2
- package/index.android.d.ts +1 -6
- package/index.android.js +4 -8
- package/index.android.js.map +1 -1
- package/index.common.d.ts +3 -6
- package/index.common.js +3 -6
- package/index.common.js.map +1 -1
- package/index.d.ts +6 -0
- package/index.ios.d.ts +1 -6
- package/index.ios.js +1 -6
- package/index.ios.js.map +1 -1
- package/layers/CelestialLayer.android.d.ts +0 -1
- package/layers/CelestialLayer.android.js +0 -3
- package/layers/CelestialLayer.android.js.map +1 -1
- package/layers/CelestialLayer.d.ts +1 -4
- package/layers/CelestialLayer.ios.d.ts +0 -1
- package/layers/CelestialLayer.ios.js +1 -4
- package/layers/CelestialLayer.ios.js.map +1 -1
- package/layers/composite.android.d.ts +3 -5
- package/layers/composite.android.js +0 -6
- package/layers/composite.android.js.map +1 -1
- package/layers/composite.d.ts +6 -25
- package/layers/composite.ios.d.ts +2 -6
- package/layers/composite.ios.js +2 -10
- package/layers/composite.ios.js.map +1 -1
- package/layers/index.android.d.ts +3 -3
- package/layers/index.d.ts +4 -22
- package/layers/raster.android.d.ts +8 -8
- package/layers/raster.android.js +1 -1
- package/layers/raster.android.js.map +1 -1
- package/layers/raster.d.ts +2 -11
- package/layers/vector.android.d.ts +10 -10
- package/layers/vector.android.js +2 -2
- package/layers/vector.android.js.map +1 -1
- package/layers/vector.d.ts +15 -71
- package/nativeclass.common.d.ts +6 -31
- package/nativeclass.common.js +4 -21
- package/nativeclass.common.js.map +1 -1
- package/nativescript.config.ts +7 -6
- package/package.json +2 -2
- package/platforms/android/include-settings.gradle +18 -4
- package/platforms/android/include.gradle +9 -1
- package/platforms/android/java/com/nativescript/massifmaps/additions/CelestialEventListener.java +1 -1
- package/platforms/android/java/com/nativescript/massifmaps/additions/ClusterElementBuilder.java +1 -1
- package/platforms/android/java/com/nativescript/massifmaps/additions/HillshadeRasterTileLayer.java +4 -4
- package/platforms/android/java/com/nativescript/massifmaps/additions/RasterTileEventListener.java +1 -1
- package/platforms/android/java/com/nativescript/massifmaps/additions/RendererCaptureListener.java +1 -1
- package/platforms/android/java/com/nativescript/massifmaps/additions/SynchronousHandler.java +28 -0
- package/platforms/android/java/com/nativescript/massifmaps/additions/TileDownloadListener.java +4 -4
- package/platforms/android/java/com/nativescript/massifmaps/additions/VectorElementEventListener.java +1 -1
- package/platforms/android/java/com/nativescript/massifmaps/additions/VectorTileEventListener.java +1 -1
- package/platforms/android/java/com/nativescript/massifmaps/additions2/FeatureCollectionSearchService.java +1 -2
- package/platforms/android/java/com/nativescript/massifmaps/additions2/VectorEditEventListener.java +21 -20
- package/platforms/android/java/com/nativescript/massifmaps/additions2/VectorTileSearchService.java +1 -2
- package/platforms/android/java/com/nativescript/massifmaps/api/EventListener.java +60 -0
- package/platforms/android/java/com/nativescript/massifmaps/geocoding/GeocodingServiceAdditions.java +5 -5
- package/platforms/android/java/com/nativescript/massifmaps/packagemanager/PackageManagerListener.java +11 -11
- package/platforms/android/java/com/nativescript/massifmaps/routing/RoutingServiceAdditions.java +19 -36
- package/platforms/android/native-api-usage.json +8 -0
- package/platforms/ios/build.xcconfig +4 -1
- package/platforms/ios/native-api-usage.json +1 -0
- package/platforms/ios/src/MassifMapsAdditions.h +1 -0
- package/platforms/ios/src/NSMSFApiEventListener.h +23 -0
- package/platforms/ios/src/NSMSFApiEventListener.mm +33 -0
- package/platforms/ios/src/NSMSFFeatureCollectionSearchService.swift +1 -1
- package/platforms/ios/src/NSMSFGeocodingServiceAdditions.swift +1 -1
- package/platforms/ios/src/NSMSFHillshadeRasterTileLayer.swift +1 -1
- package/platforms/ios/src/NSMSFMainThread.swift +21 -0
- package/platforms/ios/src/NSMSFRoutingServiceAdditions.swift +1 -1
- package/platforms/ios/src/NSMSFVectorTileSearchService.swift +1 -1
- package/projections/index.ios.js.map +1 -1
- package/renderers/index.d.ts +3 -10
- package/routing/index.android.d.ts +17 -17
- package/tsconfig.tsbuildinfo +1 -1
- package/typings/massifmaps.android.d.ts +1913 -410
- package/typings/massifmaps.ios.d.ts +21 -4
- package/typings/ns.massifmaps.android.d.ts +44 -175
- package/typings/ns.massifmaps.ios.d.ts +10 -63
- package/ui/index.android.d.ts +10 -14
- package/ui/index.android.js +6 -66
- package/ui/index.android.js.map +1 -1
- package/ui/index.common.d.ts +31 -0
- package/ui/index.common.js +130 -2
- package/ui/index.common.js.map +1 -1
- package/ui/index.d.ts +60 -21
- package/ui/index.ios.d.ts +4 -6
- package/ui/index.ios.js +5 -92
- package/ui/index.ios.js.map +1 -1
- package/utils/index.android.d.ts +9 -22
- package/utils/index.android.js +20 -129
- package/utils/index.android.js.map +1 -1
- package/utils/index.d.ts +9 -7
- package/utils/index.ios.d.ts +10 -18
- package/utils/index.ios.js +20 -109
- package/utils/index.ios.js.map +1 -1
- package/vectorelements/index.android.d.ts +6 -6
- package/vectorelements/line.android.d.ts +8 -11
- package/vectorelements/line.android.js +1 -4
- package/vectorelements/line.android.js.map +1 -1
- package/vectorelements/line.ios.d.ts +1 -4
- package/vectorelements/line.ios.js +1 -4
- package/vectorelements/line.ios.js.map +1 -1
- package/vectortiles/index.android.d.ts +0 -2
- package/vectortiles/index.android.js +0 -9
- package/vectortiles/index.android.js.map +1 -1
- package/platforms/android/java/com/nativescript/massifmaps/additions/AssetPackage.java +0 -59
- package/platforms/android/java/com/nativescript/massifmaps/additions/DirAssetPackage.java +0 -53
- package/platforms/android/java/com/nativescript/massifmaps/additions/MapEventListener.java +0 -17
- package/platforms/android/java/com/nativescript/massifmaps/additions/MapView.java +0 -167
- package/platforms/android/java/com/nativescript/massifmaps/additions/TextureMapView.java +0 -166
- package/platforms/ios/src/NSMSFMapEventListener.swift +0 -43
- package/platforms/ios/src/NSMSFMapView.swift +0 -106
|
@@ -0,0 +1,1449 @@
|
|
|
1
|
+
import { Observable } from '@nativescript/core';
|
|
2
|
+
import { bridge } from './bridge';
|
|
3
|
+
import { classOfShortName, classOfSpec, enumName, enumValue, eventNames, findEvent, isKnownClass, isSubclassOf, propertyNames, resolveMethod, resolvePath, specKindOf } from './resolve';
|
|
4
|
+
/**
|
|
5
|
+
* Typed layer over the SDK's string facade (create, destroy, set, get, call, on). Paths, spec keys,
|
|
6
|
+
* methods and events are generated from the table the C++ resolves against. Lives beside the object API.
|
|
7
|
+
*/
|
|
8
|
+
/** Mirrors `massif::api::Result`; anything unlisted arrives as its number. */
|
|
9
|
+
export const Result = {
|
|
10
|
+
OK: 0,
|
|
11
|
+
BAD_HANDLE: 1,
|
|
12
|
+
UNKNOWN_CLASS: 2,
|
|
13
|
+
UNKNOWN_PROPERTY: 3,
|
|
14
|
+
READONLY: 4,
|
|
15
|
+
UNSUPPORTED_TYPE: 5,
|
|
16
|
+
NULL_OBJECT: 8
|
|
17
|
+
};
|
|
18
|
+
export class MassifApiError extends Error {
|
|
19
|
+
constructor(message, result) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.result = result;
|
|
22
|
+
this.name = 'MassifApiError';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function isAvailable() {
|
|
26
|
+
return bridge.available;
|
|
27
|
+
}
|
|
28
|
+
/** False on an SDK whose `MassifApi.on` lacks the consume flag: `consumed` is then accepted and ignored. */
|
|
29
|
+
export function canConsume() {
|
|
30
|
+
return bridge.canConsume;
|
|
31
|
+
}
|
|
32
|
+
function requireApi() {
|
|
33
|
+
if (!bridge.available) {
|
|
34
|
+
throw new MassifApiError('this MassifMaps build has no surface API - the SDK was built without all/native/api');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Handlers always run on the main thread: the SDK emits from threads with no JS runtime, so the native
|
|
39
|
+
* listener hops to main and WAITS (so `consumed` returns in time and the payload is still alive).
|
|
40
|
+
* The facade's DELIVERY_UI / DELIVERY_BACKGROUND cannot work from JS, so they are not exposed.
|
|
41
|
+
*/
|
|
42
|
+
const DELIVERY_ORIGIN = 0;
|
|
43
|
+
/**
|
|
44
|
+
* Per-payload-class event prototype with a lazy getter per property (`e.reason`), built once and
|
|
45
|
+
* cached since `map.moved` arrives up to 159/s. Event own properties (`get`, `payload`...) shadow it.
|
|
46
|
+
*/
|
|
47
|
+
const payloadProtos = {};
|
|
48
|
+
function payloadProto(payloadClass) {
|
|
49
|
+
let proto = payloadProtos[payloadClass];
|
|
50
|
+
if (!proto) {
|
|
51
|
+
proto = payloadProtos[payloadClass] = {};
|
|
52
|
+
for (const name of propertyNames(payloadClass)) {
|
|
53
|
+
Object.defineProperty(proto, name, {
|
|
54
|
+
enumerable: true,
|
|
55
|
+
configurable: true,
|
|
56
|
+
get() {
|
|
57
|
+
return this.get(name);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return proto;
|
|
63
|
+
}
|
|
64
|
+
/** `remove()` is idempotent: removing twice is an SDK error. */
|
|
65
|
+
export class Subscription {
|
|
66
|
+
constructor(id) {
|
|
67
|
+
this.id = id;
|
|
68
|
+
this.mRemoved = false;
|
|
69
|
+
/** Cancels a debounced delivery that has not fired yet, so `remove()` really means removed. */
|
|
70
|
+
this.mCancelPending = null;
|
|
71
|
+
}
|
|
72
|
+
get valid() {
|
|
73
|
+
return !this.mRemoved;
|
|
74
|
+
}
|
|
75
|
+
remove() {
|
|
76
|
+
if (this.mRemoved) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
this.mRemoved = true;
|
|
80
|
+
if (this.mCancelPending) {
|
|
81
|
+
this.mCancelPending();
|
|
82
|
+
this.mCancelPending = null;
|
|
83
|
+
}
|
|
84
|
+
return bridge.off(this.id);
|
|
85
|
+
}
|
|
86
|
+
/** @internal */
|
|
87
|
+
onRemove(cancel) {
|
|
88
|
+
this.mCancelPending = cancel;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function parseJson(text) {
|
|
92
|
+
if (text === null || text === '') {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
return JSON.parse(text);
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
// A plain string property that is not valid JSON is its own value.
|
|
100
|
+
return text;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function isSpec(value) {
|
|
104
|
+
return (!!value &&
|
|
105
|
+
typeof value === 'object' &&
|
|
106
|
+
!Array.isArray(value) &&
|
|
107
|
+
!(value instanceof MassifObject) &&
|
|
108
|
+
typeof value.handle !== 'number' &&
|
|
109
|
+
typeof value.type === 'string');
|
|
110
|
+
}
|
|
111
|
+
function handleOf(value) {
|
|
112
|
+
if (typeof value === 'number') {
|
|
113
|
+
return value;
|
|
114
|
+
}
|
|
115
|
+
if (value && typeof value.handle === 'number') {
|
|
116
|
+
return value.handle;
|
|
117
|
+
}
|
|
118
|
+
return 0;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* To the facade's `[lng, lat]`, never a MapPos (massif-maps/MassifMaps#159). Deliberately ignores the
|
|
122
|
+
* setMapPosKeys keys: a facade position must not change meaning with the view API's config.
|
|
123
|
+
*/
|
|
124
|
+
function toPosition(position) {
|
|
125
|
+
if (Array.isArray(position)) {
|
|
126
|
+
return position;
|
|
127
|
+
}
|
|
128
|
+
// `lon` too: the SDK's own MapPos spelling
|
|
129
|
+
const lng = position.longitude ?? position.lng ?? position.lon;
|
|
130
|
+
const lat = position.latitude ?? position.lat;
|
|
131
|
+
const altitude = position.altitude ?? position.alt;
|
|
132
|
+
if (lng === undefined || lat === undefined) {
|
|
133
|
+
throw new MassifApiError(`not a position: ${JSON.stringify(position)}`);
|
|
134
|
+
}
|
|
135
|
+
return altitude ? [lng, lat, altitude] : [lng, lat];
|
|
136
|
+
}
|
|
137
|
+
function argsJson(args) {
|
|
138
|
+
return JSON.stringify(args.map((a) => (a instanceof MassifObject ? a.handle : a)));
|
|
139
|
+
}
|
|
140
|
+
/** Path-prefix scope, e.g. `map.group('fogOptions').set('rangeStart', 2.5)`, instead of 700+ named accessors. */
|
|
141
|
+
export class PropertyGroup {
|
|
142
|
+
constructor(
|
|
143
|
+
// `any`: pinning the owner's class parameter makes every nested group() unassignable
|
|
144
|
+
owner, prefix, className) {
|
|
145
|
+
this.owner = owner;
|
|
146
|
+
this.prefix = prefix;
|
|
147
|
+
this.className = className;
|
|
148
|
+
}
|
|
149
|
+
path(path) {
|
|
150
|
+
return this.prefix ? `${this.prefix}.${path}` : path;
|
|
151
|
+
}
|
|
152
|
+
get(path) {
|
|
153
|
+
return this.owner.get(this.path(path));
|
|
154
|
+
}
|
|
155
|
+
set(path, value) {
|
|
156
|
+
this.owner.set(this.path(path), value);
|
|
157
|
+
return this;
|
|
158
|
+
}
|
|
159
|
+
apply(values) {
|
|
160
|
+
const prefixed = {};
|
|
161
|
+
for (const key of Object.keys(values)) {
|
|
162
|
+
prefixed[this.path(key)] = values[key];
|
|
163
|
+
}
|
|
164
|
+
this.owner.apply(prefixed);
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
167
|
+
getPos(path, projection) {
|
|
168
|
+
return this.owner.getPos(this.path(path), projection);
|
|
169
|
+
}
|
|
170
|
+
group(path) {
|
|
171
|
+
return this.owner.group(this.path(path));
|
|
172
|
+
}
|
|
173
|
+
/** @see MassifObject.child */
|
|
174
|
+
child(path) {
|
|
175
|
+
return this.owner.child(this.path(path));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** A native subscription is taken on an event's first handler and dropped with its last. */
|
|
179
|
+
export class MassifObject extends Observable {
|
|
180
|
+
constructor(handle, className,
|
|
181
|
+
/** The registry id, when the object has one. A call result does not. */
|
|
182
|
+
id) {
|
|
183
|
+
super();
|
|
184
|
+
this.handle = handle;
|
|
185
|
+
this.className = className;
|
|
186
|
+
this.id = id;
|
|
187
|
+
this.mSubscriptions = {};
|
|
188
|
+
this.mDefaults = {};
|
|
189
|
+
this.mDestroyed = false;
|
|
190
|
+
}
|
|
191
|
+
/** `instanceof` by class name, e.g. `layer.is('massif::RasterTileLayer')`. */
|
|
192
|
+
is(className) {
|
|
193
|
+
return isSubclassOf(this.className, className);
|
|
194
|
+
}
|
|
195
|
+
/** False after `destroy`, and after the id was dropped. */
|
|
196
|
+
get valid() {
|
|
197
|
+
return !this.mDestroyed && bridge.isValid(this.handle);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* The path may walk objects (`fogOptions.rangeStart`), structs (`clickInfo.clickType`) and free-form
|
|
201
|
+
* data (`properties.name`). Enums come back as names, positions as `[lon, lat]`, structs as JSON.
|
|
202
|
+
*/
|
|
203
|
+
get(path) {
|
|
204
|
+
return this.read(path);
|
|
205
|
+
}
|
|
206
|
+
read(path) {
|
|
207
|
+
const info = resolvePath(this.className, path);
|
|
208
|
+
switch (info?.code) {
|
|
209
|
+
case 'b':
|
|
210
|
+
return bridge.getBool(this.handle, path, false);
|
|
211
|
+
case 'i':
|
|
212
|
+
case 'c':
|
|
213
|
+
return bridge.getInt(this.handle, path, 0);
|
|
214
|
+
case 'f':
|
|
215
|
+
return bridge.getFloat(this.handle, path, 0);
|
|
216
|
+
case 's':
|
|
217
|
+
return bridge.getString(this.handle, path);
|
|
218
|
+
case 'e':
|
|
219
|
+
return enumName(info.arg, bridge.getInt(this.handle, path, 0));
|
|
220
|
+
case 'p':
|
|
221
|
+
return parseJson(bridge.getPos(this.handle, path, ''));
|
|
222
|
+
case 't':
|
|
223
|
+
return parseJson(bridge.getString(this.handle, path));
|
|
224
|
+
case 'o':
|
|
225
|
+
throw new MassifApiError(`${path} is an object property - reach it with group('${path}'), child('${path}') or read a path through it`);
|
|
226
|
+
default:
|
|
227
|
+
// Unknown to the tables (a Variant path, or a more derived concrete class): the C++
|
|
228
|
+
// resolves it, so read it as text
|
|
229
|
+
return parseJson(bridge.getString(this.handle, path));
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/** An enum takes its constant name, a position an array. */
|
|
233
|
+
set(path, value) {
|
|
234
|
+
const result = this.write(path, value);
|
|
235
|
+
if (result !== Result.OK) {
|
|
236
|
+
throw new MassifApiError(`${this.className}.${path}: ${resultName(result)}`, result);
|
|
237
|
+
}
|
|
238
|
+
return this;
|
|
239
|
+
}
|
|
240
|
+
/** The same without throwing, for a path that may legitimately not be there. */
|
|
241
|
+
trySet(path, value) {
|
|
242
|
+
return this.write(path, value);
|
|
243
|
+
}
|
|
244
|
+
write(path, value) {
|
|
245
|
+
const info = resolvePath(this.className, path);
|
|
246
|
+
switch (info?.code) {
|
|
247
|
+
case 'b':
|
|
248
|
+
return bridge.setBool(this.handle, path, !!value);
|
|
249
|
+
case 'i':
|
|
250
|
+
case 'c':
|
|
251
|
+
return bridge.setInt(this.handle, path, value);
|
|
252
|
+
case 'f':
|
|
253
|
+
return bridge.setFloat(this.handle, path, value);
|
|
254
|
+
case 's':
|
|
255
|
+
return bridge.setString(this.handle, path, String(value));
|
|
256
|
+
case 'e':
|
|
257
|
+
return bridge.setInt(this.handle, path, enumValue(String(value)) ?? Number(value));
|
|
258
|
+
case 'o':
|
|
259
|
+
return isSpec(value)
|
|
260
|
+
? this.writeSpec(path, info.arg, value)
|
|
261
|
+
: bridge.setObject(this.handle, path, handleOf(value));
|
|
262
|
+
case 'p':
|
|
263
|
+
case 't':
|
|
264
|
+
case 'v':
|
|
265
|
+
return bridge.setString(this.handle, path, JSON.stringify(value));
|
|
266
|
+
default:
|
|
267
|
+
return this.writeByType(path, value);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Plain `setObject` would collapse a spec to NULL_HANDLE, which CLEARS the property and returns OK.
|
|
272
|
+
* The built object gets an id derived from the target, so it lives with the property and a rewrite replaces it.
|
|
273
|
+
*/
|
|
274
|
+
writeSpec(path, objectClass, spec) {
|
|
275
|
+
const kind = specKindOf(objectClass);
|
|
276
|
+
if (!kind) {
|
|
277
|
+
return Result.UNKNOWN_CLASS;
|
|
278
|
+
}
|
|
279
|
+
const id = `${this.id ?? this.handle}.${path}`;
|
|
280
|
+
// A different spec under a live id is refused, so the previous one goes first.
|
|
281
|
+
find(kind, id, objectClass)?.destroy();
|
|
282
|
+
const built = create(kind, id, spec, objectClass);
|
|
283
|
+
return bridge.setObject(this.handle, path, built.handle);
|
|
284
|
+
}
|
|
285
|
+
writeByType(path, value) {
|
|
286
|
+
if (typeof value === 'boolean') {
|
|
287
|
+
return bridge.setBool(this.handle, path, value);
|
|
288
|
+
}
|
|
289
|
+
if (typeof value === 'number') {
|
|
290
|
+
return bridge.setFloat(this.handle, path, value);
|
|
291
|
+
}
|
|
292
|
+
if (typeof value === 'string') {
|
|
293
|
+
// An enum constant read as text would parse as 0, which is a real value.
|
|
294
|
+
const asEnum = enumValue(value);
|
|
295
|
+
return asEnum === undefined ? bridge.setString(this.handle, path, value) : bridge.setInt(this.handle, path, asEnum);
|
|
296
|
+
}
|
|
297
|
+
if (value instanceof MassifObject) {
|
|
298
|
+
return bridge.setObject(this.handle, path, value.handle);
|
|
299
|
+
}
|
|
300
|
+
return bridge.setString(this.handle, path, JSON.stringify(value));
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Several properties in ONE JNI/JSI crossing via `setAll`. Object values (handles, inline specs) cannot
|
|
304
|
+
* be JSON and go one by one, as does everything on an SDK without `setAll`.
|
|
305
|
+
*/
|
|
306
|
+
apply(values) {
|
|
307
|
+
const setAll = bridge.setAll;
|
|
308
|
+
const batched = {};
|
|
309
|
+
const single = [];
|
|
310
|
+
for (const key of Object.keys(values)) {
|
|
311
|
+
const value = values[key];
|
|
312
|
+
if (!setAll || value instanceof MassifObject || isSpec(value) || resolvePath(this.className, key)?.code === 'o') {
|
|
313
|
+
single.push(key);
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
batched[key] = value;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (setAll && Object.keys(batched).length) {
|
|
320
|
+
const result = setAll(this.handle, JSON.stringify(batched), '');
|
|
321
|
+
// replay per key on failure so the error names the key, not the object
|
|
322
|
+
if (result !== Result.OK) {
|
|
323
|
+
single.unshift(...Object.keys(batched));
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
for (const key of single) {
|
|
327
|
+
this.set(key, values[key]);
|
|
328
|
+
}
|
|
329
|
+
return this;
|
|
330
|
+
}
|
|
331
|
+
getPos(path, projection) {
|
|
332
|
+
const json = bridge.getPos(this.handle, path, projection ?? '');
|
|
333
|
+
if (!json) {
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
// Before its first layout a map view answers `[0, null]`, which JSON.parse throws on.
|
|
337
|
+
try {
|
|
338
|
+
return JSON.parse(json);
|
|
339
|
+
}
|
|
340
|
+
catch {
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
group(path) {
|
|
345
|
+
const info = resolvePath(this.className, path);
|
|
346
|
+
if (!info || info.code !== 'o') {
|
|
347
|
+
throw new MassifApiError(`${this.className}.${path} is not an object property`);
|
|
348
|
+
}
|
|
349
|
+
return new PropertyGroup(this, path, info.arg);
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* The object behind an object property, OWNED by the caller (`destroy()` it); null when empty.
|
|
353
|
+
* Unlike `group` it hands the object over, e.g. to share a source between layers.
|
|
354
|
+
*/
|
|
355
|
+
child(path) {
|
|
356
|
+
const info = resolvePath(this.className, path);
|
|
357
|
+
if (!info || info.code !== 'o') {
|
|
358
|
+
throw new MassifApiError(`${this.className}.${path} is not an object property`);
|
|
359
|
+
}
|
|
360
|
+
const handle = bridge.getObject(this.handle, path);
|
|
361
|
+
return handle ? new MassifObject(handle, info.arg) : null;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* The path may traverse object properties (`layer.call('tileDecoder.setStyleParameter', ...)`).
|
|
365
|
+
* An object result is a `MassifObject` the CALLER OWNS - `destroy()` it, or it stays registered.
|
|
366
|
+
*/
|
|
367
|
+
call(method, ...args) {
|
|
368
|
+
return this.invoke(method, args);
|
|
369
|
+
}
|
|
370
|
+
/** Untyped `call` for the named wrappers: they know the declaring class, `call`'s `C` does not. */
|
|
371
|
+
invoke(method, args) {
|
|
372
|
+
const info = resolveMethod(this.className, method);
|
|
373
|
+
let result;
|
|
374
|
+
try {
|
|
375
|
+
result = bridge.call(this.handle, method, argsJson(args));
|
|
376
|
+
}
|
|
377
|
+
catch (e) {
|
|
378
|
+
throw new MassifApiError(`${this.className}.${method}: ${e?.message ?? e}`);
|
|
379
|
+
}
|
|
380
|
+
return this.readResult(result, info?.code, info?.arg, true);
|
|
381
|
+
}
|
|
382
|
+
readResult(handle, code, cls, owned) {
|
|
383
|
+
if (!handle) {
|
|
384
|
+
return undefined;
|
|
385
|
+
}
|
|
386
|
+
try {
|
|
387
|
+
switch (code) {
|
|
388
|
+
case 'v':
|
|
389
|
+
return undefined;
|
|
390
|
+
case 'b':
|
|
391
|
+
return bridge.getBool(handle, '', false);
|
|
392
|
+
case 'i':
|
|
393
|
+
return bridge.getInt(handle, '', 0);
|
|
394
|
+
case 'f':
|
|
395
|
+
return bridge.getFloat(handle, '', 0);
|
|
396
|
+
case 's':
|
|
397
|
+
return bridge.getString(handle, '');
|
|
398
|
+
case 'd':
|
|
399
|
+
return bridge.getDoubles(handle);
|
|
400
|
+
case 'o': {
|
|
401
|
+
if (!cls) {
|
|
402
|
+
// a missing class means the tables are older than the SDK
|
|
403
|
+
throw new MassifApiError('an object result with no class - regenerate the typings with `npm run typings.api`');
|
|
404
|
+
}
|
|
405
|
+
// The caller owns it, so it must NOT be destroyed here.
|
|
406
|
+
owned = false;
|
|
407
|
+
return new MassifObject(handle, cls);
|
|
408
|
+
}
|
|
409
|
+
default:
|
|
410
|
+
return parseJson(bridge.getString(handle, ''));
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
finally {
|
|
414
|
+
if (owned) {
|
|
415
|
+
bridge.destroy(handle);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* `call` on a worker. `extract` runs INSIDE the delivery, the only time an object result is alive,
|
|
421
|
+
* e.g. `(tile) => tile.getData('data')`. Scalars and flat arrays need no `extract`.
|
|
422
|
+
*/
|
|
423
|
+
callAsync(method, args, extract) {
|
|
424
|
+
const info = resolveMethod(this.className, method);
|
|
425
|
+
if (info?.code === 'o' && !extract) {
|
|
426
|
+
// an async object result dies with the delivery: handing it back would dangle
|
|
427
|
+
throw new MassifApiError(`${this.className}.${String(method)} returns an object - pass an extract function, it runs while the result is alive`);
|
|
428
|
+
}
|
|
429
|
+
const event = `${String(method)}.done.${nextCallId()}`;
|
|
430
|
+
let call = 0;
|
|
431
|
+
let subscription;
|
|
432
|
+
const promise = new Promise((resolve, reject) => {
|
|
433
|
+
subscription = this.subscribeRaw(event, (payload) => {
|
|
434
|
+
subscription?.remove();
|
|
435
|
+
if (!payload) {
|
|
436
|
+
reject(new MassifApiError(`${this.className}.${String(method)} failed`));
|
|
437
|
+
return false;
|
|
438
|
+
}
|
|
439
|
+
try {
|
|
440
|
+
// Never owned: the facade releases an async payload after the handlers run.
|
|
441
|
+
const value = info?.code === 'o' || extract ? new MassifObject(payload, (info?.arg ?? this.className)) : null;
|
|
442
|
+
resolve(extract ? extract(value) : this.readResult(payload, info?.code, info?.arg, false));
|
|
443
|
+
}
|
|
444
|
+
catch (e) {
|
|
445
|
+
reject(e);
|
|
446
|
+
}
|
|
447
|
+
return false;
|
|
448
|
+
});
|
|
449
|
+
try {
|
|
450
|
+
call = bridge.callAsync(this.handle, method, argsJson(args), event);
|
|
451
|
+
}
|
|
452
|
+
catch (e) {
|
|
453
|
+
subscription?.remove();
|
|
454
|
+
reject(new MassifApiError(`${this.className}.${String(method)}: ${e?.message ?? e}`));
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
promise.cancel = () => {
|
|
458
|
+
const cancelled = call ? bridge.cancelCall(call) : false;
|
|
459
|
+
subscription?.remove();
|
|
460
|
+
return cancelled;
|
|
461
|
+
};
|
|
462
|
+
return promise;
|
|
463
|
+
}
|
|
464
|
+
cancelCalls() {
|
|
465
|
+
return bridge.cancelCalls(this.handle);
|
|
466
|
+
}
|
|
467
|
+
collect(map, options = {}) {
|
|
468
|
+
const countPath = options.countPath ?? 'featureCount';
|
|
469
|
+
const method = options.method ?? 'getFeature';
|
|
470
|
+
const count = bridge.getInt(this.handle, countPath, 0);
|
|
471
|
+
const out = [];
|
|
472
|
+
for (let i = 0; i < count; i++) {
|
|
473
|
+
const element = this.invoke(method, [i]);
|
|
474
|
+
if (!element) {
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
try {
|
|
478
|
+
out.push(map(element, i));
|
|
479
|
+
}
|
|
480
|
+
finally {
|
|
481
|
+
element.destroy();
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
return out;
|
|
485
|
+
}
|
|
486
|
+
/** Escape hatch for paths the tables cannot type: free-form feature properties, a more derived class. */
|
|
487
|
+
json(path = '') {
|
|
488
|
+
return parseJson(bridge.getString(this.handle, path));
|
|
489
|
+
}
|
|
490
|
+
/** An empty path means the handle IS the blob. */
|
|
491
|
+
getData(path = '') {
|
|
492
|
+
return bridge.getData(this.handle, path);
|
|
493
|
+
}
|
|
494
|
+
getDoubles() {
|
|
495
|
+
return bridge.getDoubles(this.handle);
|
|
496
|
+
}
|
|
497
|
+
/** Defaults for every later subscription on this object. */
|
|
498
|
+
eventOptions(options) {
|
|
499
|
+
this.mDefaults = { ...this.mDefaults, ...options };
|
|
500
|
+
return this;
|
|
501
|
+
}
|
|
502
|
+
get events() {
|
|
503
|
+
return eventNames(this.className);
|
|
504
|
+
}
|
|
505
|
+
/** For a handler needing its own projection or delivery; `on` covers most cases. */
|
|
506
|
+
subscribe(event, handler, options) {
|
|
507
|
+
const consumable = this.consumes(event);
|
|
508
|
+
return this.subscribeRaw(event, (payload, snapshot) => {
|
|
509
|
+
const data = this.eventData(event, payload, consumable, snapshot);
|
|
510
|
+
handler(data);
|
|
511
|
+
warnIfIgnored(data, consumable, event);
|
|
512
|
+
return consumable && data.consumed;
|
|
513
|
+
}, options);
|
|
514
|
+
}
|
|
515
|
+
consumes(event) {
|
|
516
|
+
return bridge.canConsume && !!findEvent(this.className, event)?.consume;
|
|
517
|
+
}
|
|
518
|
+
subscribeRaw(event, handler, options) {
|
|
519
|
+
requireApi();
|
|
520
|
+
this.beforeSubscribe(event);
|
|
521
|
+
const merged = { ...this.mDefaults, ...options };
|
|
522
|
+
const throttle = merged.throttle ?? 0;
|
|
523
|
+
const debounce = merged.debounce ?? 0;
|
|
524
|
+
const consumable = this.consumes(event);
|
|
525
|
+
if (debounce && consumable) {
|
|
526
|
+
throw new MassifApiError(`${event} is consumable, so it cannot be debounced - the SDK is waiting for the answer now, not in ${debounce} ms`);
|
|
527
|
+
}
|
|
528
|
+
let last = 0;
|
|
529
|
+
let timer = null;
|
|
530
|
+
const deliver = (payload) => {
|
|
531
|
+
if (throttle) {
|
|
532
|
+
// Date.now() rather than a timer: dropping is the point, and a queued handler
|
|
533
|
+
// would read a payload the facade has already freed.
|
|
534
|
+
const now = Date.now();
|
|
535
|
+
if (now - last < throttle) {
|
|
536
|
+
return false;
|
|
537
|
+
}
|
|
538
|
+
last = now;
|
|
539
|
+
}
|
|
540
|
+
if (debounce) {
|
|
541
|
+
// the payload dies when this returns, so snapshot it NOW; trailing edge, only the
|
|
542
|
+
// last of a burst is delivered
|
|
543
|
+
const snapshot = payload ? parseJson(bridge.getString(payload, '')) : null;
|
|
544
|
+
if (timer) {
|
|
545
|
+
clearTimeout(timer);
|
|
546
|
+
}
|
|
547
|
+
timer = setTimeout(() => {
|
|
548
|
+
timer = null;
|
|
549
|
+
try {
|
|
550
|
+
handler(0, snapshot);
|
|
551
|
+
}
|
|
552
|
+
catch (error) {
|
|
553
|
+
console.error(`MassifMaps: debounced handler for '${event}' threw`, error);
|
|
554
|
+
}
|
|
555
|
+
}, debounce);
|
|
556
|
+
return false;
|
|
557
|
+
}
|
|
558
|
+
return handler(payload);
|
|
559
|
+
};
|
|
560
|
+
const id = bridge.on(this.handle, event, (_target, _event, payload) => {
|
|
561
|
+
try {
|
|
562
|
+
return deliver(payload);
|
|
563
|
+
}
|
|
564
|
+
catch (error) {
|
|
565
|
+
// returns through a SWIG director into C++: an exception crossing it aborts the PROCESS
|
|
566
|
+
console.error(`MassifMaps: handler for '${event}' threw`, error);
|
|
567
|
+
return false;
|
|
568
|
+
}
|
|
569
|
+
}, DELIVERY_ORIGIN, false, merged.projection ?? '', consumable);
|
|
570
|
+
if (!id) {
|
|
571
|
+
throw new MassifApiError(`could not subscribe to ${event} on ${this.className} - a stale handle, or an unknown projection`);
|
|
572
|
+
}
|
|
573
|
+
const subscription = new Subscription(id);
|
|
574
|
+
if (debounce) {
|
|
575
|
+
subscription.onRemove(() => {
|
|
576
|
+
if (timer) {
|
|
577
|
+
clearTimeout(timer);
|
|
578
|
+
timer = null;
|
|
579
|
+
}
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
return subscription;
|
|
583
|
+
}
|
|
584
|
+
/** Hook for a subclass that has to install a native listener before the first subscription. */
|
|
585
|
+
beforeSubscribe(event) {
|
|
586
|
+
// nothing by default
|
|
587
|
+
}
|
|
588
|
+
eventData(event, payload, consumable, snapshot) {
|
|
589
|
+
const info = findEvent(this.className, event);
|
|
590
|
+
// a debounced delivery has no live payload left, it reads the snapshot taken at emit time
|
|
591
|
+
const fromSnapshot = snapshot !== undefined && snapshot !== null;
|
|
592
|
+
// Wrapped lazily: an event nobody reads should not allocate an Observable per delivery.
|
|
593
|
+
let object;
|
|
594
|
+
const payloadObject = () => {
|
|
595
|
+
if (object === undefined) {
|
|
596
|
+
object = payload && info?.payload ? new MassifObject(payload, info.payload) : null;
|
|
597
|
+
}
|
|
598
|
+
return object;
|
|
599
|
+
};
|
|
600
|
+
// Only when a payload actually ARRIVED, not merely when the schema says the event has
|
|
601
|
+
// one: an older SDK sends none, and a getter that read through would throw.
|
|
602
|
+
const proto = (payload || fromSnapshot) && info?.payload ? payloadProto(info.payload) : null;
|
|
603
|
+
const data = Object.assign(proto ? Object.create(proto) : {}, {
|
|
604
|
+
eventName: event,
|
|
605
|
+
object: this,
|
|
606
|
+
consumed: false,
|
|
607
|
+
consumable,
|
|
608
|
+
get: (path) => {
|
|
609
|
+
if (fromSnapshot) {
|
|
610
|
+
return path ? snapshot[path] : snapshot;
|
|
611
|
+
}
|
|
612
|
+
const target = payloadObject();
|
|
613
|
+
if (!target) {
|
|
614
|
+
throw new MassifApiError(`${event} carries no payload`);
|
|
615
|
+
}
|
|
616
|
+
return target.get(path);
|
|
617
|
+
},
|
|
618
|
+
getPos: (path, projection) => {
|
|
619
|
+
if (fromSnapshot) {
|
|
620
|
+
return (snapshot[path] ?? null);
|
|
621
|
+
}
|
|
622
|
+
const target = payloadObject();
|
|
623
|
+
return target ? target.getPos(path, projection) : null;
|
|
624
|
+
}
|
|
625
|
+
});
|
|
626
|
+
// `payload` is a getter of its own so the wrapper is still only built if it is asked for.
|
|
627
|
+
Object.defineProperty(data, 'payload', { enumerable: true, configurable: true, get: payloadObject });
|
|
628
|
+
return data;
|
|
629
|
+
}
|
|
630
|
+
/** Deliberately NOT widened to `string` so event-name typos fail; `addEventListener` takes any name. */
|
|
631
|
+
on(event, callback, thisArg) {
|
|
632
|
+
super.on(event, callback, thisArg);
|
|
633
|
+
}
|
|
634
|
+
once(event, callback, thisArg) {
|
|
635
|
+
super.once(event, callback, thisArg);
|
|
636
|
+
}
|
|
637
|
+
off(event, callback, thisArg) {
|
|
638
|
+
super.off(event, callback, thisArg);
|
|
639
|
+
}
|
|
640
|
+
/** A facade event takes its native subscription here, on the first handler. */
|
|
641
|
+
addEventListener(eventNames, callback, thisArg, ...rest) {
|
|
642
|
+
super.addEventListener(eventNames, callback, thisArg, ...rest);
|
|
643
|
+
for (const name of splitEvents(eventNames)) {
|
|
644
|
+
if (!this.mSubscriptions[name] && findEvent(this.className, name) !== null) {
|
|
645
|
+
const consumable = this.consumes(name);
|
|
646
|
+
this.mSubscriptions[name] = this.subscribeRaw(name, (payload, snapshot) => {
|
|
647
|
+
// One data object for every listener, so whichever of them sets `consumed`
|
|
648
|
+
// claims the event - the same rule as a DOM handler calling preventDefault.
|
|
649
|
+
const data = this.eventData(name, payload, consumable, snapshot);
|
|
650
|
+
this.notify(data);
|
|
651
|
+
warnIfIgnored(data, consumable, name);
|
|
652
|
+
return consumable && data.consumed;
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
removeEventListener(eventNames, callback, thisArg) {
|
|
658
|
+
super.removeEventListener(eventNames, callback, thisArg);
|
|
659
|
+
for (const name of splitEvents(eventNames)) {
|
|
660
|
+
if (this.mSubscriptions[name] && !this.hasListeners(name)) {
|
|
661
|
+
this.mSubscriptions[name].remove();
|
|
662
|
+
delete this.mSubscriptions[name];
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
/** Subscriptions and pending async calls die with it, so no handler runs against a freed object. */
|
|
667
|
+
destroy() {
|
|
668
|
+
if (this.mDestroyed) {
|
|
669
|
+
return false;
|
|
670
|
+
}
|
|
671
|
+
this.mDestroyed = true;
|
|
672
|
+
for (const name of Object.keys(this.mSubscriptions)) {
|
|
673
|
+
this.mSubscriptions[name].remove();
|
|
674
|
+
}
|
|
675
|
+
this.mSubscriptions = {};
|
|
676
|
+
return bridge.destroy(this.handle);
|
|
677
|
+
}
|
|
678
|
+
toString() {
|
|
679
|
+
return `${this.constructor.name}(${this.className}#${this.handle}${this.id ? ` "${this.id}"` : ''})`;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
function splitEvents(eventNames) {
|
|
683
|
+
return eventNames
|
|
684
|
+
.split(',')
|
|
685
|
+
.map((n) => n.trim())
|
|
686
|
+
.filter(Boolean);
|
|
687
|
+
}
|
|
688
|
+
let callId = 0;
|
|
689
|
+
function nextCallId() {
|
|
690
|
+
return ++callId;
|
|
691
|
+
}
|
|
692
|
+
const warnedConsume = {};
|
|
693
|
+
function warnIfIgnored(data, consumable, event) {
|
|
694
|
+
if (!data.consumed || consumable || warnedConsume[event]) {
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
warnedConsume[event] = true;
|
|
698
|
+
const why = bridge.canConsume ? 'it is not a consumable event' : "this SDK's MassifApi.on does not take the consume flag";
|
|
699
|
+
console.warn(`massif: a handler consumed ${event} but nothing will act on it - ${why}`);
|
|
700
|
+
}
|
|
701
|
+
function resultName(result) {
|
|
702
|
+
for (const name of Object.keys(Result)) {
|
|
703
|
+
if (Result[name] === result) {
|
|
704
|
+
return name;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
return `result ${result}`;
|
|
708
|
+
}
|
|
709
|
+
/** Click events need a listener on the NATIVE layer, installed on first subscription (chaining any existing one). */
|
|
710
|
+
export class MassifLayer extends MassifObject {
|
|
711
|
+
constructor() {
|
|
712
|
+
super(...arguments);
|
|
713
|
+
this.mBridged = {};
|
|
714
|
+
}
|
|
715
|
+
beforeSubscribe(event) {
|
|
716
|
+
if (this.mBridged[event] || !this.id) {
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
const native = bridge.getNativeLayer(this.id);
|
|
720
|
+
if (!native) {
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
if (event === 'vectortile.clicked') {
|
|
724
|
+
bridge.attachVectorTileEvents(native, this.handle);
|
|
725
|
+
}
|
|
726
|
+
else if (event === 'vectorelement.clicked') {
|
|
727
|
+
bridge.attachVectorElementEvents(native, this.handle);
|
|
728
|
+
}
|
|
729
|
+
else if (event === 'celestial.clicked' || event === 'sky.clicked') {
|
|
730
|
+
// One native bridge emits both: attaching a second would report every click twice.
|
|
731
|
+
if (!this.mBridged['celestial.clicked'] && !this.mBridged['sky.clicked']) {
|
|
732
|
+
bridge.attachCelestialEvents(native, this.handle);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
else {
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
this.mBridged[event] = true;
|
|
739
|
+
}
|
|
740
|
+
get native() {
|
|
741
|
+
return this.id ? bridge.getNativeLayer(this.id) : bridge.getNativeLayerByHandle(this.handle);
|
|
742
|
+
}
|
|
743
|
+
opacity(value) {
|
|
744
|
+
return value === undefined ? this.get('opacity') : this.set('opacity', value);
|
|
745
|
+
}
|
|
746
|
+
visible(value) {
|
|
747
|
+
return value === undefined ? this.get('visible') : this.set('visible', value);
|
|
748
|
+
}
|
|
749
|
+
zoomRange(range) {
|
|
750
|
+
return range === undefined ? this.get('visibleZoomRange') : this.set('visibleZoomRange', range);
|
|
751
|
+
}
|
|
752
|
+
/** 0 is the bottom of the map's stack. */
|
|
753
|
+
moveTo(index) {
|
|
754
|
+
// insert does not take a layer out first: without the remove it is on the stack twice.
|
|
755
|
+
const layers = this.requireMap().layers();
|
|
756
|
+
layers.remove(this);
|
|
757
|
+
layers.insert(Math.min(index, layers.count()), this);
|
|
758
|
+
return this;
|
|
759
|
+
}
|
|
760
|
+
/** The object stays registered until it is destroyed. */
|
|
761
|
+
detach() {
|
|
762
|
+
this.requireMap().layers().remove(this);
|
|
763
|
+
return this;
|
|
764
|
+
}
|
|
765
|
+
refresh() {
|
|
766
|
+
this.invoke('refresh', []);
|
|
767
|
+
return this;
|
|
768
|
+
}
|
|
769
|
+
/** OWNED by the caller. Typed as a source so `.native` is there, to hand the tiles to native code. */
|
|
770
|
+
source() {
|
|
771
|
+
const child = this.child('dataSource');
|
|
772
|
+
return child ? new MassifSource(child.handle, child.className) : null;
|
|
773
|
+
}
|
|
774
|
+
/** `all` includes the preloading caches, not just the visible set. */
|
|
775
|
+
clearTileCaches(all = false) {
|
|
776
|
+
this.invoke('clearTileCaches', [all]);
|
|
777
|
+
return this;
|
|
778
|
+
}
|
|
779
|
+
/** Only a hillshade layer answers; anything else gives an empty array. */
|
|
780
|
+
elevations(positions) {
|
|
781
|
+
try {
|
|
782
|
+
return this.invoke('getElevations', [positions.map(toPosition)]);
|
|
783
|
+
}
|
|
784
|
+
catch (e) {
|
|
785
|
+
return [];
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
// payload class named OUTRIGHT: on an unnarrowed MassifLayer (the common case) MassifEventData<C, EventName<C>>
|
|
789
|
+
// resolves to MassifEventData<any, string>, which has no payload fields
|
|
790
|
+
onFeatureClick(handler, options) {
|
|
791
|
+
return this.subscribe('vectortile.clicked', handler, options);
|
|
792
|
+
}
|
|
793
|
+
onElementClick(handler, options) {
|
|
794
|
+
return this.subscribe('vectorelement.clicked', handler, options);
|
|
795
|
+
}
|
|
796
|
+
onCelestialClick(handler, options) {
|
|
797
|
+
return this.subscribe('celestial.clicked', handler, options);
|
|
798
|
+
}
|
|
799
|
+
onSkyClick(handler, options) {
|
|
800
|
+
return this.subscribe('sky.clicked', handler, options);
|
|
801
|
+
}
|
|
802
|
+
requireMap() {
|
|
803
|
+
if (!this.map) {
|
|
804
|
+
throw new MassifApiError(`layer is not attached to a map: ${this}`);
|
|
805
|
+
}
|
|
806
|
+
return this.map;
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
export class MassifSource extends MassifObject {
|
|
810
|
+
get native() {
|
|
811
|
+
return this.id ? bridge.getNativeSource(this.id) : bridge.getNativeSourceByHandle(this.handle);
|
|
812
|
+
}
|
|
813
|
+
/** BLOCKING: an HTTP source fetches on the calling thread. Use `loadTileAsync` from the UI thread. */
|
|
814
|
+
loadTile(tile) {
|
|
815
|
+
const result = this.invoke('loadTile', [tile]);
|
|
816
|
+
try {
|
|
817
|
+
return result ? result.getData('data') : null;
|
|
818
|
+
}
|
|
819
|
+
finally {
|
|
820
|
+
result?.destroy();
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
/** For a `geojson` source; returns the layer index. */
|
|
824
|
+
createLayer(name) {
|
|
825
|
+
return this.invoke('createLayer', [name]);
|
|
826
|
+
}
|
|
827
|
+
/** A string is parsed here: args cross as JSON, so text would arrive as a string argument, not a document. */
|
|
828
|
+
setGeoJSON(layer, geojson) {
|
|
829
|
+
this.invoke('setLayerGeoJSON', [layer, typeof geojson === 'string' ? JSON.parse(geojson) : geojson]);
|
|
830
|
+
return this;
|
|
831
|
+
}
|
|
832
|
+
deleteLayer(layer) {
|
|
833
|
+
this.invoke('deleteLayer', [layer]);
|
|
834
|
+
return this;
|
|
835
|
+
}
|
|
836
|
+
/** Avoids re-tiling the whole document as `setGeoJSON` does. `update` matches on the feature's own `id`. */
|
|
837
|
+
addFeature(layer, feature) {
|
|
838
|
+
this.invoke('addFeature', [layer, feature]);
|
|
839
|
+
return this;
|
|
840
|
+
}
|
|
841
|
+
updateFeature(layer, feature) {
|
|
842
|
+
this.invoke('updateFeature', [layer, feature]);
|
|
843
|
+
return this;
|
|
844
|
+
}
|
|
845
|
+
removeFeature(layer, id) {
|
|
846
|
+
this.invoke('removeFeature', [layer, id]);
|
|
847
|
+
return this;
|
|
848
|
+
}
|
|
849
|
+
loadTileAsync(tile) {
|
|
850
|
+
return this.callAsync('loadTile', [tile], ((result) => result.getData('data')));
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
const WRAPPERS = {
|
|
854
|
+
layer: MassifLayer,
|
|
855
|
+
source: MassifSource
|
|
856
|
+
};
|
|
857
|
+
function wrapperFor(kind) {
|
|
858
|
+
return WRAPPERS[kind] ?? MassifObject;
|
|
859
|
+
}
|
|
860
|
+
export function create(kind, id, spec, className) {
|
|
861
|
+
requireApi();
|
|
862
|
+
let handle;
|
|
863
|
+
try {
|
|
864
|
+
handle = bridge.create(kind, id, JSON.stringify(spec));
|
|
865
|
+
}
|
|
866
|
+
catch (e) {
|
|
867
|
+
throw new MassifApiError(`create ${kind}:${id}: ${e?.message ?? e}`);
|
|
868
|
+
}
|
|
869
|
+
if (!handle) {
|
|
870
|
+
throw new MassifApiError(`create ${kind}:${id} returned no handle`);
|
|
871
|
+
}
|
|
872
|
+
// hand-written factory kinds do not name their class; a bare Layer is the least wrong fallback
|
|
873
|
+
const resolved = className ?? classOfSpec(kind, spec.type) ?? 'massif::Layer';
|
|
874
|
+
return new (wrapperFor(kind))(handle, resolved, id);
|
|
875
|
+
}
|
|
876
|
+
export function createLayer(id, spec) {
|
|
877
|
+
return create('layer', id, spec);
|
|
878
|
+
}
|
|
879
|
+
export function createSource(id, spec) {
|
|
880
|
+
return create('source', id, spec);
|
|
881
|
+
}
|
|
882
|
+
/** The class cannot be read back off a handle; it is what the property paths resolve against. */
|
|
883
|
+
export function find(kind, id, className) {
|
|
884
|
+
requireApi();
|
|
885
|
+
const handle = bridge.findObject(kind, id);
|
|
886
|
+
if (!handle) {
|
|
887
|
+
return null;
|
|
888
|
+
}
|
|
889
|
+
return new (wrapperFor(kind))(handle, className, id);
|
|
890
|
+
}
|
|
891
|
+
export function destroy(kind, id) {
|
|
892
|
+
return bridge.available ? bridge.unregisterObject(kind, id) : false;
|
|
893
|
+
}
|
|
894
|
+
/** Nothing is checked; the class is what the paths resolve against. */
|
|
895
|
+
export function wrap(handle, className, id) {
|
|
896
|
+
return new MassifObject(handle, className, id);
|
|
897
|
+
}
|
|
898
|
+
/**
|
|
899
|
+
* For a layer unreachable by id, e.g. a CompositeVectorTileLayer's external child from `getExternalChildLayer`.
|
|
900
|
+
* Not in a map's stack, so `moveTo` / `detach` throw.
|
|
901
|
+
*/
|
|
902
|
+
export function wrapLayer(handle, className, id) {
|
|
903
|
+
return new MassifLayer(handle, className, id);
|
|
904
|
+
}
|
|
905
|
+
/**
|
|
906
|
+
* Gives an object built with the OBJECT api an id. The class is read off the native runtime class, so an
|
|
907
|
+
* adopted `VectorTileLayer` answers to its own properties, not only `Layer`'s.
|
|
908
|
+
*/
|
|
909
|
+
export function adoptLayer(id, nativeLayer, className) {
|
|
910
|
+
requireApi();
|
|
911
|
+
const handle = bridge.adoptLayer('layer', id, nativeLayer);
|
|
912
|
+
if (!handle) {
|
|
913
|
+
throw new MassifApiError(`could not adopt layer "${id}" - the id is taken, or the class is not a wrapped one`);
|
|
914
|
+
}
|
|
915
|
+
return new MassifLayer(handle, resolveNativeClass(nativeLayer, className, 'massif::Layer'), id);
|
|
916
|
+
}
|
|
917
|
+
/** @copydoc adoptLayer */
|
|
918
|
+
export function adoptSource(id, nativeSource, className) {
|
|
919
|
+
requireApi();
|
|
920
|
+
const handle = bridge.adoptSource('source', id, nativeSource);
|
|
921
|
+
if (!handle) {
|
|
922
|
+
throw new MassifApiError(`could not adopt source "${id}" - the id is taken, or the class is not a wrapped one`);
|
|
923
|
+
}
|
|
924
|
+
return new MassifSource(handle, resolveNativeClass(nativeSource, className, 'massif::TileDataSource'), id);
|
|
925
|
+
}
|
|
926
|
+
/**
|
|
927
|
+
* Also takes an app SUBCLASS, which a spec cannot express; specs then pass the id as `assets`. A Swig
|
|
928
|
+
* director has no class the registry knows, so it is adopted as `massif::AssetPackage`.
|
|
929
|
+
*/
|
|
930
|
+
export function adoptAssets(id, nativeAssets, className) {
|
|
931
|
+
requireApi();
|
|
932
|
+
const handle = bridge.adoptAssets('assets', id, nativeAssets);
|
|
933
|
+
if (!handle) {
|
|
934
|
+
throw new MassifApiError(`could not adopt assets "${id}" - the id is taken, or the class is not a wrapped one`);
|
|
935
|
+
}
|
|
936
|
+
return new MassifObject(handle, resolveNativeClass(nativeAssets, className, 'massif::AssetPackage'), id);
|
|
937
|
+
}
|
|
938
|
+
function resolveNativeClass(nativeObject, given, fallback) {
|
|
939
|
+
if (given) {
|
|
940
|
+
if (!isKnownClass(given)) {
|
|
941
|
+
throw new MassifApiError(`${given} is not a class the typings know - regenerate them with \`npm run typings.api\``);
|
|
942
|
+
}
|
|
943
|
+
return given;
|
|
944
|
+
}
|
|
945
|
+
return classOfShortName(bridge.nativeShortClassName(nativeObject)) ?? fallback;
|
|
946
|
+
}
|
|
947
|
+
/** Markers and popups; elements and styles are specs. The layer and source are created on first use, released with the map. */
|
|
948
|
+
export class MassifElements {
|
|
949
|
+
constructor(map, id,
|
|
950
|
+
/** Where the positions are read. lon/lat by default, matching the rest of this API. */
|
|
951
|
+
sourceSpec) {
|
|
952
|
+
this.map = map;
|
|
953
|
+
this.id = id;
|
|
954
|
+
this.sourceSpec = sourceSpec;
|
|
955
|
+
this.mCounter = 0;
|
|
956
|
+
}
|
|
957
|
+
/** The spec's `style` either names a registered style or carries it inline. */
|
|
958
|
+
add(spec) {
|
|
959
|
+
const element = this.map.object('element', `${this.id}.el${++this.mCounter}`, spec);
|
|
960
|
+
this.built().source.call('add', element.handle);
|
|
961
|
+
return element;
|
|
962
|
+
}
|
|
963
|
+
/** Lets many elements share ONE style object, which matters once there are thousands. */
|
|
964
|
+
style(styleId, spec) {
|
|
965
|
+
// `id` narrowed to non-optional: a style is only useful as the `style` key of an element
|
|
966
|
+
// spec, and an optional string would make every call site write `!`.
|
|
967
|
+
return this.map.object('elementstyle', `${this.id}.${styleId}`, spec);
|
|
968
|
+
}
|
|
969
|
+
/** The element stays registered until it is destroyed. */
|
|
970
|
+
remove(element) {
|
|
971
|
+
return this.mSource && element ? this.mSource.call('remove', element.handle) : false;
|
|
972
|
+
}
|
|
973
|
+
clear() {
|
|
974
|
+
this.mSource?.call('clear');
|
|
975
|
+
return this;
|
|
976
|
+
}
|
|
977
|
+
/** Set `e.consumed` to claim the tap, otherwise it also reaches the map. */
|
|
978
|
+
onClick(handler, options) {
|
|
979
|
+
return this.built().layer.subscribe('vectorelement.clicked', handler, options);
|
|
980
|
+
}
|
|
981
|
+
layer() {
|
|
982
|
+
return this.built().layer;
|
|
983
|
+
}
|
|
984
|
+
source() {
|
|
985
|
+
return this.built().source;
|
|
986
|
+
}
|
|
987
|
+
built() {
|
|
988
|
+
if (!this.mSource || !this.mLayer) {
|
|
989
|
+
// projection as a NESTED SPEC: a string is a registry id lookup, and EPSG:4326 is a
|
|
990
|
+
// type the projection factory builds, not a registered id
|
|
991
|
+
this.mSource = this.map.object('source', this.id, this.sourceSpec ?? { type: 'local', projection: { type: 'EPSG:4326' } });
|
|
992
|
+
this.mLayer = this.map.addLayer(this.id, { type: 'elements', source: this.id });
|
|
993
|
+
}
|
|
994
|
+
return { source: this.mSource, layer: this.mLayer };
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
/** `Log` is static-only, so the context registers it as `static:Log`. */
|
|
998
|
+
export function log() {
|
|
999
|
+
return find('static', 'Log', 'massif::Log');
|
|
1000
|
+
}
|
|
1001
|
+
export function mapViewClass() {
|
|
1002
|
+
// required lazily, so `api` does not pull `ui` in at import time
|
|
1003
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
1004
|
+
return require('../ui').MassifMap;
|
|
1005
|
+
}
|
|
1006
|
+
/**
|
|
1007
|
+
* Facade calls on the ADOPTED map view, so no position becomes a `MapPos` proxy (massif-maps/MassifMaps#159).
|
|
1008
|
+
* Positions are WGS84 `[lng, lat]`.
|
|
1009
|
+
*/
|
|
1010
|
+
export class MapCamera {
|
|
1011
|
+
/** @param size The view's pixel size: BaseMapView exposes none, so the map measures it. */
|
|
1012
|
+
constructor(view, size) {
|
|
1013
|
+
this.view = view;
|
|
1014
|
+
this.size = size;
|
|
1015
|
+
this.mDuration = 0;
|
|
1016
|
+
}
|
|
1017
|
+
/** Milliseconds the NEXT move animates over; reset once used. */
|
|
1018
|
+
animate(ms) {
|
|
1019
|
+
this.mDuration = ms;
|
|
1020
|
+
return this;
|
|
1021
|
+
}
|
|
1022
|
+
/** Seconds, which is what the facade takes; the public API is milliseconds. */
|
|
1023
|
+
take(duration) {
|
|
1024
|
+
const ms = duration ?? this.mDuration;
|
|
1025
|
+
this.mDuration = 0;
|
|
1026
|
+
return ms / 1000;
|
|
1027
|
+
}
|
|
1028
|
+
position(value) {
|
|
1029
|
+
if (value === undefined) {
|
|
1030
|
+
return this.view.getPos('focusPos');
|
|
1031
|
+
}
|
|
1032
|
+
return this.moveTo(value);
|
|
1033
|
+
}
|
|
1034
|
+
eyePosition(value) {
|
|
1035
|
+
if (value === undefined) {
|
|
1036
|
+
return this.view.getPos('cameraPos');
|
|
1037
|
+
}
|
|
1038
|
+
return this.moveEyeTo(value);
|
|
1039
|
+
}
|
|
1040
|
+
/** Puts the EYE at a position, where `moveTo` puts the focus. */
|
|
1041
|
+
moveEyeTo(position, options = {}) {
|
|
1042
|
+
const target = toPosition(position);
|
|
1043
|
+
const seconds = this.take(options.duration);
|
|
1044
|
+
// Probed by calling it: the schema these typings come from can be newer than the linked
|
|
1045
|
+
// binary. Older SDKs fall back to solving for the focus below.
|
|
1046
|
+
if (seconds <= 0 && MapCamera.nativeCameraMove !== false) {
|
|
1047
|
+
try {
|
|
1048
|
+
this.view.call('moveCameraTo', target, options.zoom ?? this.zoom(), options.rotation ?? this.rotation(), options.tilt ?? this.tilt());
|
|
1049
|
+
MapCamera.nativeCameraMove = true;
|
|
1050
|
+
return this;
|
|
1051
|
+
}
|
|
1052
|
+
catch {
|
|
1053
|
+
MapCamera.nativeCameraMove = false;
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
// Resolved once: the getters read a snapshot republished per DRAWN frame, so re-reading
|
|
1057
|
+
// them mid-solve on an undrawn map snaps the orientation back to the previous view.
|
|
1058
|
+
const orientation = {
|
|
1059
|
+
zoom: options.zoom ?? this.zoom(),
|
|
1060
|
+
rotation: options.rotation ?? this.rotation(),
|
|
1061
|
+
tilt: options.tilt ?? this.tilt()
|
|
1062
|
+
};
|
|
1063
|
+
// Start as moveTo would, so a failed or clamped solve still leaves the view on the target.
|
|
1064
|
+
this.moveTo(target, orientation);
|
|
1065
|
+
// 1e-7 degrees is about a centimetre.
|
|
1066
|
+
const TOLERANCE = 1e-7;
|
|
1067
|
+
const MAX_STEPS = 6;
|
|
1068
|
+
const startFocus = this.readPos('focusPos');
|
|
1069
|
+
if (!startFocus) {
|
|
1070
|
+
return this;
|
|
1071
|
+
}
|
|
1072
|
+
let focus = startFocus;
|
|
1073
|
+
// The focus is clamped (restricted panning, pan bounds), so a step may not apply: keep the
|
|
1074
|
+
// best focus seen instead of the last one.
|
|
1075
|
+
let bestFocus = focus;
|
|
1076
|
+
let bestError = Number.POSITIVE_INFINITY;
|
|
1077
|
+
for (let step = 0; step < MAX_STEPS; step++) {
|
|
1078
|
+
const eye = this.readPos('cameraPos');
|
|
1079
|
+
if (!eye) {
|
|
1080
|
+
break;
|
|
1081
|
+
}
|
|
1082
|
+
const deltaLng = target[0] - eye[0];
|
|
1083
|
+
const deltaLat = target[1] - eye[1];
|
|
1084
|
+
const error = Math.abs(deltaLng) + Math.abs(deltaLat);
|
|
1085
|
+
if (error < bestError) {
|
|
1086
|
+
bestError = error;
|
|
1087
|
+
bestFocus = focus;
|
|
1088
|
+
}
|
|
1089
|
+
if (error < TOLERANCE) {
|
|
1090
|
+
break;
|
|
1091
|
+
}
|
|
1092
|
+
const candidate = [focus[0] + deltaLng, focus[1] + deltaLat];
|
|
1093
|
+
this.moveTo(candidate, orientation);
|
|
1094
|
+
const applied = this.readPos('focusPos');
|
|
1095
|
+
if (!applied) {
|
|
1096
|
+
break;
|
|
1097
|
+
}
|
|
1098
|
+
if (Math.abs(applied[0] - focus[0]) < TOLERANCE && Math.abs(applied[1] - focus[1]) < TOLERANCE) {
|
|
1099
|
+
break;
|
|
1100
|
+
}
|
|
1101
|
+
focus = applied;
|
|
1102
|
+
}
|
|
1103
|
+
focus = bestFocus;
|
|
1104
|
+
this.moveTo(focus, orientation);
|
|
1105
|
+
if (seconds > 0) {
|
|
1106
|
+
// The solve moved the camera; rewind to the start and fly the rest.
|
|
1107
|
+
this.moveTo(startFocus, orientation);
|
|
1108
|
+
this.view.call('flyTo', focus, orientation.zoom, orientation.rotation, orientation.tilt, options.climbHeight ?? 0, seconds, 'ease');
|
|
1109
|
+
}
|
|
1110
|
+
return this;
|
|
1111
|
+
}
|
|
1112
|
+
/** Null, not a throw or NaN, for a view that has not had its first layout. */
|
|
1113
|
+
readPos(path) {
|
|
1114
|
+
try {
|
|
1115
|
+
const pos = this.view.getPos(path);
|
|
1116
|
+
if (!pos || !isFinite(pos[0]) || !isFinite(pos[1])) {
|
|
1117
|
+
return null;
|
|
1118
|
+
}
|
|
1119
|
+
return [pos[0], pos[1]];
|
|
1120
|
+
}
|
|
1121
|
+
catch {
|
|
1122
|
+
return null;
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
zoom(value, target) {
|
|
1126
|
+
if (value === undefined) {
|
|
1127
|
+
return this.view.get('zoom');
|
|
1128
|
+
}
|
|
1129
|
+
return this.moveTo(target ?? this.position(), { zoom: value });
|
|
1130
|
+
}
|
|
1131
|
+
rotation(degrees) {
|
|
1132
|
+
if (degrees === undefined) {
|
|
1133
|
+
return this.view.get('rotation');
|
|
1134
|
+
}
|
|
1135
|
+
return this.moveTo(this.position(), { rotation: degrees });
|
|
1136
|
+
}
|
|
1137
|
+
tilt(degrees) {
|
|
1138
|
+
if (degrees === undefined) {
|
|
1139
|
+
return this.view.get('tilt');
|
|
1140
|
+
}
|
|
1141
|
+
return this.moveTo(this.position(), { tilt: degrees });
|
|
1142
|
+
}
|
|
1143
|
+
/**
|
|
1144
|
+
* All in ONE move: separate setters animate independently and fight each other. With no duration it
|
|
1145
|
+
* is immediate and works before the map has drawn.
|
|
1146
|
+
*/
|
|
1147
|
+
moveTo(position, options = {}) {
|
|
1148
|
+
const pos = toPosition(position);
|
|
1149
|
+
const zoom = options.zoom ?? this.zoom();
|
|
1150
|
+
const rotation = options.rotation ?? this.rotation();
|
|
1151
|
+
const tilt = options.tilt ?? this.tilt();
|
|
1152
|
+
const seconds = this.take(options.duration);
|
|
1153
|
+
if (seconds > 0) {
|
|
1154
|
+
this.view.call('flyTo', pos, zoom, rotation, tilt, options.climbHeight ?? 0, seconds, 'ease');
|
|
1155
|
+
}
|
|
1156
|
+
else {
|
|
1157
|
+
this.view.call('moveTo', pos, zoom, rotation, tilt);
|
|
1158
|
+
}
|
|
1159
|
+
return this;
|
|
1160
|
+
}
|
|
1161
|
+
/** `screen` defaults to the whole view. */
|
|
1162
|
+
fitBounds(bounds, options = {}) {
|
|
1163
|
+
const { width, height } = this.size();
|
|
1164
|
+
const screen = options.screen
|
|
1165
|
+
? [[options.screen.min.x, options.screen.min.y], [options.screen.max.x, options.screen.max.y]]
|
|
1166
|
+
: [[0, 0], [width, height]];
|
|
1167
|
+
this.view.call('fitBounds', [toPosition(bounds[0]), toPosition(bounds[1])], screen, !!options.integerZoom, !!options.resetRotation, !!options.resetTilt, this.take(options.duration));
|
|
1168
|
+
return this;
|
|
1169
|
+
}
|
|
1170
|
+
isMoving() {
|
|
1171
|
+
return this.view.get('flightActive');
|
|
1172
|
+
}
|
|
1173
|
+
/** 0 to 1 through the current flight, or -1 when none is running. */
|
|
1174
|
+
progress() {
|
|
1175
|
+
return this.view.get('flightProgress');
|
|
1176
|
+
}
|
|
1177
|
+
stop() {
|
|
1178
|
+
this.view.call('stopFlight');
|
|
1179
|
+
return this;
|
|
1180
|
+
}
|
|
1181
|
+
screenToMap(x, y) {
|
|
1182
|
+
return this.view.call('screenToMap', x, y);
|
|
1183
|
+
}
|
|
1184
|
+
mapToScreen(position) {
|
|
1185
|
+
const point = this.view.call('mapToScreen', toPosition(position));
|
|
1186
|
+
return { x: point[0], y: point[1] };
|
|
1187
|
+
}
|
|
1188
|
+
/** Axis-aligned box around the two unprojected screen corners: a tilted/rotated camera has no true bounds. */
|
|
1189
|
+
bounds() {
|
|
1190
|
+
const { height, width } = this.size();
|
|
1191
|
+
const first = this.screenToMap(0, 0);
|
|
1192
|
+
const second = this.screenToMap(width, height);
|
|
1193
|
+
return [
|
|
1194
|
+
[Math.min(first[0], second[0]), Math.min(first[1], second[1])],
|
|
1195
|
+
[Math.max(first[0], second[0]), Math.max(first[1], second[1])]
|
|
1196
|
+
];
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
/** Drawn in list order, 0 at the bottom. Facade calls on the adopted `Layers` only. */
|
|
1200
|
+
export class MapLayers extends MassifObject {
|
|
1201
|
+
constructor(handle, id) {
|
|
1202
|
+
super(handle, 'massif::Layers', id);
|
|
1203
|
+
}
|
|
1204
|
+
/** Includes layers the object API put there. */
|
|
1205
|
+
count() {
|
|
1206
|
+
return this.get('count');
|
|
1207
|
+
}
|
|
1208
|
+
/** Appends on top: 0 is the bottom. */
|
|
1209
|
+
add(layer) {
|
|
1210
|
+
this.call('add', handleOf(layer));
|
|
1211
|
+
return this;
|
|
1212
|
+
}
|
|
1213
|
+
insert(index, layer) {
|
|
1214
|
+
this.call('insert', index, handleOf(layer));
|
|
1215
|
+
return this;
|
|
1216
|
+
}
|
|
1217
|
+
/** Not `set`: a two-argument overload of the property writer would let `layers.set('count', …)` typos compile. */
|
|
1218
|
+
replace(index, layer) {
|
|
1219
|
+
this.call('set', index, handleOf(layer));
|
|
1220
|
+
return this;
|
|
1221
|
+
}
|
|
1222
|
+
/** A bare `Layer`: the list does not record the concrete class. */
|
|
1223
|
+
at(index) {
|
|
1224
|
+
const layer = this.call('get', index);
|
|
1225
|
+
return layer ? new MassifLayer(layer.handle, 'massif::Layer') : null;
|
|
1226
|
+
}
|
|
1227
|
+
remove(layer) {
|
|
1228
|
+
return this.call('remove', handleOf(layer));
|
|
1229
|
+
}
|
|
1230
|
+
clear() {
|
|
1231
|
+
this.call('clear');
|
|
1232
|
+
return this;
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
/** Addressed through its Options: fog, sky, light, terrain, camera limits... all hang off it. */
|
|
1236
|
+
export class MassifMap extends MassifObject {
|
|
1237
|
+
constructor(handle, id, view) {
|
|
1238
|
+
super(handle, 'massif::Options', id);
|
|
1239
|
+
this.view = view;
|
|
1240
|
+
this.mLayerById = {};
|
|
1241
|
+
/** Ids this map built, released when it is destroyed. */
|
|
1242
|
+
this.mOwned = [];
|
|
1243
|
+
}
|
|
1244
|
+
layers() {
|
|
1245
|
+
if (!this.mLayers) {
|
|
1246
|
+
const id = `${this.id}:layers`;
|
|
1247
|
+
// reuse an existing one: adopt() REFUSES a duplicate id (a map re-attached under the
|
|
1248
|
+
// same id), and the id outlives the MassifMap that made it
|
|
1249
|
+
let handle = bridge.findObject('layers', id);
|
|
1250
|
+
if (!handle) {
|
|
1251
|
+
handle = bridge.adoptLayers('layers', id, this.view.mapView.getLayers());
|
|
1252
|
+
if (!handle) {
|
|
1253
|
+
throw new MassifApiError(`could not register the layer list of "${this.id}" under "${id}"`);
|
|
1254
|
+
}
|
|
1255
|
+
this.mOwned.push(['layers', id]);
|
|
1256
|
+
}
|
|
1257
|
+
this.mLayers = new MapLayers(handle, id);
|
|
1258
|
+
}
|
|
1259
|
+
return this.mLayers;
|
|
1260
|
+
}
|
|
1261
|
+
addLayer(id, spec) {
|
|
1262
|
+
// this.object, not the module-level createLayer: a map-built layer must be released with
|
|
1263
|
+
// the map, or a revisited screen fails on a duplicate id
|
|
1264
|
+
return this.add(this.object('layer', id, spec));
|
|
1265
|
+
}
|
|
1266
|
+
/** Owned by the map but not placed: for `add(layer, index)` or `layers().insert(...)`. */
|
|
1267
|
+
buildLayer(id, spec) {
|
|
1268
|
+
const layer = this.object('layer', id, spec);
|
|
1269
|
+
layer.map = this;
|
|
1270
|
+
return layer;
|
|
1271
|
+
}
|
|
1272
|
+
add(layer, index) {
|
|
1273
|
+
const layers = this.layers();
|
|
1274
|
+
if (index === undefined) {
|
|
1275
|
+
layers.add(layer);
|
|
1276
|
+
}
|
|
1277
|
+
else {
|
|
1278
|
+
layers.insert(index, layer);
|
|
1279
|
+
}
|
|
1280
|
+
layer.map = this;
|
|
1281
|
+
if (layer.id) {
|
|
1282
|
+
this.mLayerById[layer.id] = layer;
|
|
1283
|
+
}
|
|
1284
|
+
return layer;
|
|
1285
|
+
}
|
|
1286
|
+
/** The layer stays registered until it is destroyed. */
|
|
1287
|
+
removeLayer(layer) {
|
|
1288
|
+
if (layer instanceof MassifLayer && layer.id) {
|
|
1289
|
+
delete this.mLayerById[layer.id];
|
|
1290
|
+
}
|
|
1291
|
+
return this.layers().remove(layer);
|
|
1292
|
+
}
|
|
1293
|
+
/** A layer this map added or adopted, by the id it was given. */
|
|
1294
|
+
layer(id) {
|
|
1295
|
+
return this.mLayerById[id] ?? null;
|
|
1296
|
+
}
|
|
1297
|
+
/** Includes layers the object API put there. */
|
|
1298
|
+
layerCount() {
|
|
1299
|
+
return this.layers().count();
|
|
1300
|
+
}
|
|
1301
|
+
adopt(id, nativeLayer, className) {
|
|
1302
|
+
const layer = adoptLayer(id, nativeLayer, className);
|
|
1303
|
+
layer.map = this;
|
|
1304
|
+
this.mLayerById[id] = layer;
|
|
1305
|
+
return layer;
|
|
1306
|
+
}
|
|
1307
|
+
object(kind, id, spec, className) {
|
|
1308
|
+
const built = create(kind, id, spec, className);
|
|
1309
|
+
this.mOwned.push([kind, id]);
|
|
1310
|
+
return built;
|
|
1311
|
+
}
|
|
1312
|
+
source(id, spec) {
|
|
1313
|
+
return this.object('source', id, spec);
|
|
1314
|
+
}
|
|
1315
|
+
/** Worth an id when the app talks to it later: a layer's style property cannot be read back as a handle. */
|
|
1316
|
+
style(id, spec) {
|
|
1317
|
+
return this.object('style', id, spec);
|
|
1318
|
+
}
|
|
1319
|
+
/** `sourceSpec` is for a map not in lon/lat; only the first call uses it. */
|
|
1320
|
+
elements(sourceSpec) {
|
|
1321
|
+
if (!this.mElements) {
|
|
1322
|
+
this.mElements = new MassifElements(this, `${this.id}.elements`, sourceSpec);
|
|
1323
|
+
}
|
|
1324
|
+
return this.mElements;
|
|
1325
|
+
}
|
|
1326
|
+
addMarker(spec) {
|
|
1327
|
+
return this.elements().add(spec);
|
|
1328
|
+
}
|
|
1329
|
+
addPopup(spec) {
|
|
1330
|
+
return this.elements().add(spec);
|
|
1331
|
+
}
|
|
1332
|
+
/** Releases every id the map built, not the view: the object API's map keeps working. */
|
|
1333
|
+
destroy() {
|
|
1334
|
+
for (let i = this.mOwned.length - 1; i >= 0; i--) {
|
|
1335
|
+
destroy(this.mOwned[i][0], this.mOwned[i][1]);
|
|
1336
|
+
}
|
|
1337
|
+
this.mOwned.length = 0;
|
|
1338
|
+
this.mElements = undefined;
|
|
1339
|
+
this.mCamera = undefined;
|
|
1340
|
+
return super.destroy();
|
|
1341
|
+
}
|
|
1342
|
+
size() {
|
|
1343
|
+
return { width: this.view.getMeasuredWidth?.() ?? 0, height: this.view.getMeasuredHeight?.() ?? 0 };
|
|
1344
|
+
}
|
|
1345
|
+
/** `wait` holds the capture until the tiles have settled. */
|
|
1346
|
+
capture(wait = false) {
|
|
1347
|
+
if (!this.view.captureRendering) {
|
|
1348
|
+
throw new MassifApiError('this map view cannot capture its rendering');
|
|
1349
|
+
}
|
|
1350
|
+
return this.view.captureRendering(wait);
|
|
1351
|
+
}
|
|
1352
|
+
/** Rarely needed: the SDK redraws on picture-changing writes. For off-UI-thread changes or a view hidden while moving. */
|
|
1353
|
+
requestRedraw() {
|
|
1354
|
+
this.view.requestRedraw?.();
|
|
1355
|
+
return this;
|
|
1356
|
+
}
|
|
1357
|
+
camera() {
|
|
1358
|
+
if (!this.mCamera) {
|
|
1359
|
+
// The map view is adopted under its own kind: it is what carries the camera, and
|
|
1360
|
+
// going through the facade is also what gives moveTo the map's projection.
|
|
1361
|
+
const id = `${this.id}:view`;
|
|
1362
|
+
let handle = bridge.findObject('view', id);
|
|
1363
|
+
if (!handle) {
|
|
1364
|
+
handle = bridge.adoptView('view', id, this.view.mapView.getBaseMapView());
|
|
1365
|
+
if (!handle) {
|
|
1366
|
+
throw new MassifApiError(`could not register the map view of "${this.id}"`);
|
|
1367
|
+
}
|
|
1368
|
+
this.mOwned.push(['view', id]);
|
|
1369
|
+
}
|
|
1370
|
+
const view = new MassifObject(handle, 'massif::BaseMapView', id);
|
|
1371
|
+
this.mCamera = new MapCamera(view, () => this.size());
|
|
1372
|
+
}
|
|
1373
|
+
return this.mCamera;
|
|
1374
|
+
}
|
|
1375
|
+
/** Shorthand for `eventOptions({ projection })`. */
|
|
1376
|
+
eventProjection(name) {
|
|
1377
|
+
return this.eventOptions({ projection: name });
|
|
1378
|
+
}
|
|
1379
|
+
onClick(handler, options) {
|
|
1380
|
+
return this.subscribe('map.clicked', handler, options);
|
|
1381
|
+
}
|
|
1382
|
+
onMove(handler, options) {
|
|
1383
|
+
return this.subscribe('map.moved', handler, options);
|
|
1384
|
+
}
|
|
1385
|
+
onIdle(handler, options) {
|
|
1386
|
+
return this.subscribe('map.idle', handler, options);
|
|
1387
|
+
}
|
|
1388
|
+
onStable(handler, options) {
|
|
1389
|
+
return this.subscribe('map.stable', handler, options);
|
|
1390
|
+
}
|
|
1391
|
+
onInteraction(handler, options) {
|
|
1392
|
+
return this.subscribe('map.interaction', handler, options);
|
|
1393
|
+
}
|
|
1394
|
+
// with a spec these BUILD the options first: Options starts with them empty, and writing
|
|
1395
|
+
// through an empty one is an error
|
|
1396
|
+
fog(spec) {
|
|
1397
|
+
return this.optionGroup('fogOptions', 'fog', spec);
|
|
1398
|
+
}
|
|
1399
|
+
sky(spec) {
|
|
1400
|
+
return this.optionGroup('skyOptions', 'sky', spec);
|
|
1401
|
+
}
|
|
1402
|
+
light(spec) {
|
|
1403
|
+
return this.optionGroup('lightOptions', 'light', spec);
|
|
1404
|
+
}
|
|
1405
|
+
/** The elevation decoder comes from the source's own `encoding`. */
|
|
1406
|
+
terrain(spec) {
|
|
1407
|
+
return this.optionGroup('terrainOptions', 'terrain', spec);
|
|
1408
|
+
}
|
|
1409
|
+
optionGroup(property, type, spec) {
|
|
1410
|
+
if (spec) {
|
|
1411
|
+
const built = this.object('options', `${this.id}.${type}`, spec);
|
|
1412
|
+
this.set(property, built.handle);
|
|
1413
|
+
}
|
|
1414
|
+
return this.group(property);
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
/** For a loaded map view. Its event listener is CHAINED, not replaced, so existing handlers keep firing. */
|
|
1418
|
+
export function attach(view, options = {}) {
|
|
1419
|
+
requireApi();
|
|
1420
|
+
// the view attaches itself on load; a second registration would leave two handles onto one
|
|
1421
|
+
// object, one dangling once the other is destroyed
|
|
1422
|
+
const existing = view.facadeMap?.();
|
|
1423
|
+
if (existing) {
|
|
1424
|
+
const { projection: existingProjection, throttle: existingThrottle, debounce: existingDebounce } = options;
|
|
1425
|
+
if (existingProjection || existingThrottle || existingDebounce) {
|
|
1426
|
+
existing.eventOptions({ projection: existingProjection, throttle: existingThrottle, debounce: existingDebounce });
|
|
1427
|
+
// Re-subscribes the view's own events, which were made before these options existed.
|
|
1428
|
+
view.enableFacadeEvents?.();
|
|
1429
|
+
}
|
|
1430
|
+
return existing;
|
|
1431
|
+
}
|
|
1432
|
+
const nativeOptions = view.getOptions()?.getNative();
|
|
1433
|
+
if (!nativeOptions) {
|
|
1434
|
+
throw new MassifApiError('the map is not ready yet - attach from the mapReady event');
|
|
1435
|
+
}
|
|
1436
|
+
const id = options.id ?? 'map';
|
|
1437
|
+
const handle = bridge.adoptOptions('options', id, nativeOptions);
|
|
1438
|
+
if (!handle) {
|
|
1439
|
+
throw new MassifApiError(`could not register the map under "${id}" - the id is taken`);
|
|
1440
|
+
}
|
|
1441
|
+
bridge.attachMapEvents(view.mapView, handle);
|
|
1442
|
+
const map = new MassifMap(handle, id, view);
|
|
1443
|
+
const { projection, throttle, debounce } = options;
|
|
1444
|
+
if (projection || throttle || debounce) {
|
|
1445
|
+
map.eventOptions({ projection, throttle, debounce });
|
|
1446
|
+
}
|
|
1447
|
+
return map;
|
|
1448
|
+
}
|
|
1449
|
+
//# sourceMappingURL=index.common.js.map
|