@yappkit/front-module-map 0.1.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.
@@ -0,0 +1,469 @@
1
+ import { defineComponent as h, ref as w, toRefs as u, shallowRef as f, provide as m, h as L, inject as v, onMounted as M, onUnmounted as k, watch as g, nextTick as G, computed as S } from "vue";
2
+ import { lineString as K, length as P, along as E, point as D } from "@turf/turf";
3
+ class j {
4
+ constructor() {
5
+ this.overlay = null, this.watching = { layers: !1 };
6
+ }
7
+ watchLayers(e, a = {}) {
8
+ if (!this.overlay)
9
+ throw new Error("overlay should be set");
10
+ if (this.watching.layers) {
11
+ console.warn("No need to call MapboxDeckGLOverlay.watchLayers(). layers are already being watched");
12
+ return;
13
+ }
14
+ this.watching.layers = !0, g(e, () => {
15
+ this.overlay.setProps({ layers: e.value });
16
+ }, a);
17
+ }
18
+ }
19
+ class T {
20
+ constructor() {
21
+ this.map = null, this.watching = { sources: !1, layers: !1 };
22
+ }
23
+ watchSources(e, a = {}) {
24
+ if (!this.map)
25
+ throw new Error("map should be set");
26
+ if (this.watching.sources) {
27
+ console.warn("No need to call MapboxMap.watchSources(). sources are already being watched");
28
+ return;
29
+ }
30
+ this.watching.sources = !0, g(e, (t, s) => {
31
+ const r = [...s || []], o = [...t || []], i = o.map((n) => n.id), c = r.map((n) => n.id), p = o.filter((n) => !c.includes(n.id)), y = r.filter((n) => !i.includes(n.id));
32
+ for (const n of y)
33
+ G(() => {
34
+ this.map.removeSource(n.id);
35
+ });
36
+ for (const n of p) {
37
+ const d = { ...n };
38
+ delete d.id, this.map.addSource(n.id, d);
39
+ }
40
+ }, a);
41
+ }
42
+ watchLayers(e, a = {}) {
43
+ if (!this.map)
44
+ throw new Error("map should be set");
45
+ if (this.watching.layers) {
46
+ console.warn("No need to call MapboxMap.watchLayers(). layers are already being watched");
47
+ return;
48
+ }
49
+ this.watching.layers = !0, g(e, (t, s) => {
50
+ const r = [...s || []], o = [...t || []], i = o.map((n) => n.id), c = r.map((n) => n.id), p = o.filter((n) => !c.includes(n.id)), y = r.filter((n) => !i.includes(n.id));
51
+ for (const n of y)
52
+ this.map.removeLayer(n.id);
53
+ for (const n of p)
54
+ G(() => {
55
+ this.map.addLayer(n);
56
+ });
57
+ }, a);
58
+ }
59
+ async setDeckGLOverlay(e = {}) {
60
+ if (!this.map)
61
+ throw new Error("map should be set");
62
+ const { MapboxOverlay: a } = await import("@deck.gl/mapbox"), t = new j();
63
+ return t.overlay = new a(e), this.map.addControl(t.overlay), t;
64
+ }
65
+ }
66
+ class A {
67
+ constructor(e) {
68
+ if (this.apiKey = e.apiKey, !this.apiKey)
69
+ throw new Error("apiKey is required");
70
+ this.services = e.services || [], this.sdk = {}, this.mapboxgl = null, this.services.length && this.loadMapboxSDK();
71
+ }
72
+ get Map() {
73
+ return h(
74
+ (e, { emit: a, slots: t }) => {
75
+ const s = w(null), { options: r, layers: o, sources: i } = u(e), c = f(null);
76
+ return m("mapboxMap", c), this.createMap(s, r.value).then((p) => {
77
+ c.value = p, p.watchSources(i, r.value.watchSourcesOptions || { immediate: !0 }), p.watchLayers(o, r.value.watchLayersOptions || { immediate: !0 }), a("loaded", p);
78
+ }), () => L("div", { ref: s }, c.value ? t : []);
79
+ },
80
+ {
81
+ props: {
82
+ options: {
83
+ type: Object,
84
+ default: () => ({})
85
+ },
86
+ layers: {
87
+ type: Array,
88
+ default: () => []
89
+ },
90
+ sources: {
91
+ type: Array,
92
+ default: () => []
93
+ }
94
+ }
95
+ }
96
+ );
97
+ }
98
+ get DeckGLOverlay() {
99
+ return h(
100
+ (e, { emit: a, slots: t }) => {
101
+ const { options: s, layers: r } = u(e), o = v("mapboxMap");
102
+ if (!o || !o.value)
103
+ throw new Error("map should exist");
104
+ return o.value.setDeckGLOverlay(s.value).then((i) => {
105
+ i.watchLayers(r, s.value.watchLayersOptions || { immediate: !0 }), a("loaded", i);
106
+ }), () => {
107
+ };
108
+ },
109
+ {
110
+ props: {
111
+ options: {
112
+ type: Object,
113
+ default: () => ({})
114
+ },
115
+ layers: {
116
+ type: Array,
117
+ default: () => []
118
+ }
119
+ }
120
+ }
121
+ );
122
+ }
123
+ async loadMapbox() {
124
+ const e = await import("./mapbox-gl.js").then((a) => a.m);
125
+ this.mapboxgl = e;
126
+ }
127
+ get servicesLoader() {
128
+ return {
129
+ datasets: () => import("./mapbox-sdk/datasets.js").then((e) => e.d),
130
+ directions: () => import("./mapbox-sdk/directions.js").then((e) => e.d),
131
+ geocoding: () => import("./mapbox-sdk/geocoding.js").then((e) => e.g),
132
+ geocodingV6: () => import("./mapbox-sdk/geocoding-v6.js").then((e) => e.g),
133
+ mapMatching: () => import("./mapbox-sdk/map-matching.js").then((e) => e.m),
134
+ matrix: () => import("./mapbox-sdk/matrix.js").then((e) => e.m),
135
+ optimization: () => import("./mapbox-sdk/optimization.js").then((e) => e.o),
136
+ static: () => import("./mapbox-sdk/static.js").then((e) => e._),
137
+ styles: () => import("./mapbox-sdk/styles.js").then((e) => e.s),
138
+ tilequery: () => import("./mapbox-sdk/tilequery.js").then((e) => e.t),
139
+ tilesets: () => import("./mapbox-sdk/tilesets.js").then((e) => e.t),
140
+ tokens: () => import("./mapbox-sdk/tokens.js").then((e) => e.t),
141
+ uploads: () => import("./mapbox-sdk/uploads.js").then((e) => e.u),
142
+ isochrone: () => import("./mapbox-sdk/isochrone.js").then((e) => e.i)
143
+ };
144
+ }
145
+ async loadMapboxSDK() {
146
+ const a = (await import("./mapbox-sdk.js").then((s) => s.i)).default({ accessToken: this.apiKey }), t = await Promise.all(this.services.map((s) => this.servicesLoader[s]().then((r) => r.default(a))));
147
+ for (const [s, r] of this.services.entries())
148
+ this.sdk[r] = t[s];
149
+ }
150
+ async getSDK() {
151
+ return await (Object.keys(this.sdk).length ? Promise.resolve() : this.loadMapboxSDK()), this.sdk;
152
+ }
153
+ async createMap(e, a = {}) {
154
+ const t = new T();
155
+ return new Promise((s, r) => {
156
+ M(async () => {
157
+ await (this.mapboxgl ? Promise.resolve() : this.loadMapbox());
158
+ const o = () => {
159
+ if (!e.value)
160
+ throw new Error("Missing HTML element");
161
+ t.map = new this.mapboxgl.Map({
162
+ container: e.value,
163
+ accessToken: this.apiKey,
164
+ ...a
165
+ }), t.map.on("load", () => {
166
+ s(t);
167
+ });
168
+ };
169
+ a.onMounted ? a.onMounted(o) : o();
170
+ }), k(() => {
171
+ t.map && (t.map.remove(), t.map = null);
172
+ });
173
+ });
174
+ }
175
+ }
176
+ class N {
177
+ constructor() {
178
+ this.overlay = null, this.watching = { layers: !1 };
179
+ }
180
+ watchLayers(e, a = {}) {
181
+ if (!this.overlay)
182
+ throw new Error("overlay should be set");
183
+ if (this.watching.layers) {
184
+ console.warn("No need to call GoogleMapsDeckGLOverlay.watchLayers(). layers are already being watched");
185
+ return;
186
+ }
187
+ this.watching.layers = !0, g(e, () => {
188
+ this.overlay.setProps({ layers: e.value });
189
+ }, a);
190
+ }
191
+ }
192
+ class R {
193
+ constructor() {
194
+ this.map = null;
195
+ }
196
+ async setDeckGLOverlay(e = {}) {
197
+ if (!this.map)
198
+ throw new Error("map should be set");
199
+ const { GoogleMapsOverlay: a } = await import("@deck.gl/google-maps"), t = new N();
200
+ return t.overlay = new a(e), t.overlay.setMap(this.map), t;
201
+ }
202
+ }
203
+ class O {
204
+ constructor(e) {
205
+ if (this.apiKey = e.apiKey, !this.apiKey)
206
+ throw new Error("apiKey is required");
207
+ this.libraries = e.libraries || ["maps"], this.version = e.version || "weekly", this.loader = null, this.sdk = {}, this.deckgl = null;
208
+ }
209
+ get Map() {
210
+ return h(
211
+ (e, { emit: a, slots: t }) => {
212
+ const s = w(null), { options: r } = u(e), o = f(null);
213
+ return m("googleMapsMap", o), this.createMap(s, r.value).then((i) => {
214
+ o.value = i, a("loaded", i);
215
+ }), () => L("div", { ref: s }, o.value ? t : []);
216
+ },
217
+ {
218
+ props: {
219
+ options: {
220
+ type: Object,
221
+ default: () => ({})
222
+ }
223
+ }
224
+ }
225
+ );
226
+ }
227
+ get DeckGLOverlay() {
228
+ return h(
229
+ (e, { emit: a, slots: t }) => {
230
+ const { options: s, layers: r } = u(e), o = v("googleMapsMap");
231
+ if (!o || !o.value)
232
+ throw new Error("map should exist");
233
+ return o.value.setDeckGLOverlay(s.value).then((i) => {
234
+ i.watchLayers(r, s.value.watchLayersOptions || { immediate: !0 }), a("loaded", i);
235
+ }), () => {
236
+ };
237
+ },
238
+ {
239
+ props: {
240
+ options: {
241
+ type: Object,
242
+ default: () => ({})
243
+ },
244
+ layers: {
245
+ type: Array,
246
+ default: () => []
247
+ }
248
+ }
249
+ }
250
+ );
251
+ }
252
+ get DeckGL3DTileLayer() {
253
+ return h(
254
+ (e, { emit: a, slots: t }) => {
255
+ const { options: s } = u(e), r = v("deckGLPrependLayers");
256
+ if (!r || !r.value)
257
+ throw new Error("deckGLPrependLayers should exist");
258
+ const o = w("");
259
+ this.createDeckGL3DTileLayer(o, s).then((c) => {
260
+ r.value = [
261
+ ...r.value,
262
+ c
263
+ ];
264
+ });
265
+ const i = v("deckGLAppendVNodeFn");
266
+ if (!i)
267
+ throw new Error("deckGLAppendVNodeFn should exist");
268
+ return t.appendMap && i.value.push(() => t.appendMap({ credits: o.value })), () => {
269
+ };
270
+ },
271
+ {
272
+ props: {
273
+ options: {
274
+ type: Object,
275
+ default: () => ({})
276
+ }
277
+ }
278
+ }
279
+ );
280
+ }
281
+ async loadGoogleMaps() {
282
+ const { Loader: e } = await import("@googlemaps/js-api-loader");
283
+ this.loader = new e({
284
+ apiKey: this.apiKey,
285
+ version: this.version,
286
+ libraries: this.libraries
287
+ });
288
+ const a = await Promise.all(this.libraries.map((t) => this.loader.importLibrary(t)));
289
+ for (const [t, s] of this.libraries.entries())
290
+ this.sdk[s] = a[t];
291
+ }
292
+ async getSDK() {
293
+ return await (Object.keys(this.sdk).length ? Promise.resolve() : this.loadGoogleMaps()), this.sdk;
294
+ }
295
+ async createMap(e, a = {}) {
296
+ const t = new R();
297
+ return new Promise((s, r) => {
298
+ M(async () => {
299
+ await (this.sdk.maps ? Promise.resolve() : this.loadGoogleMaps());
300
+ const o = () => {
301
+ if (!e.value)
302
+ throw new Error("Missing HTML element");
303
+ t.map = new this.sdk.maps.Map(e.value, {
304
+ ...a
305
+ }), s(t);
306
+ };
307
+ a.onMounted ? a.onMounted(o) : o();
308
+ }), k(() => {
309
+ t.map && (t.map = null);
310
+ });
311
+ });
312
+ }
313
+ async loadDeckGL() {
314
+ const { Tile3DLayer: e } = await import("deck.gl");
315
+ this.deckgl = { Tile3DLayer: e };
316
+ }
317
+ async createDeckGL3DTileLayer(e, a = {}) {
318
+ return await (this.deckgl ? Promise.resolve() : this.loadDeckGL()), new this.deckgl.Tile3DLayer({
319
+ id: "google-3d-tiles",
320
+ data: "https://tile.googleapis.com/v1/3dtiles/root.json",
321
+ loadOptions: {
322
+ fetch: {
323
+ headers: {
324
+ "X-GOOG-API-KEY": this.apiKey
325
+ }
326
+ }
327
+ },
328
+ onTilesetLoad: (t) => {
329
+ e && (t.options.onTraversalComplete = (s) => {
330
+ const r = /* @__PURE__ */ new Set();
331
+ return s.forEach((o) => {
332
+ const { copyright: i } = o.content.gltf.asset;
333
+ i.split(";").forEach(r.add, r), e.value = [...r].join("; ");
334
+ }), s;
335
+ });
336
+ },
337
+ operation: "terrain+draw",
338
+ ...a
339
+ });
340
+ }
341
+ }
342
+ class V {
343
+ constructor() {
344
+ this.deckgl = null, this.map = null, this.watching = { layers: !1 };
345
+ }
346
+ watchLayers(e, a = {}) {
347
+ if (!this.map)
348
+ throw new Error("map should be set");
349
+ if (this.watching.layers) {
350
+ console.warn("No need to call DeckGLMap.watchLayers(). layers are already being watched");
351
+ return;
352
+ }
353
+ this.watching.layers = !0, g(e, () => {
354
+ this.map.setProps({ layers: e.value });
355
+ }, a);
356
+ }
357
+ }
358
+ class x {
359
+ constructor(e) {
360
+ this.deckgl = null;
361
+ }
362
+ get Map() {
363
+ return h(
364
+ (e, { emit: a, attrs: t, slots: s }) => {
365
+ const r = w(null), { options: o, layers: i } = u(e), c = f(null);
366
+ m("deckGLMap", c);
367
+ const p = f([]);
368
+ m("deckGLPrependLayers", p);
369
+ const y = S(() => [
370
+ ...p.value,
371
+ ...i.value
372
+ ]);
373
+ this.createMap(r, o.value).then((d) => {
374
+ c.value = d, d.watchLayers(y, o.value.watchLayersOptions || { immediate: !0 }), a("loaded", d);
375
+ });
376
+ const n = w([]);
377
+ return m("deckGLAppendVNodeFn", n), () => [L("div", { ...t, ref: r }, c.value ? s : []), n.value.map((d) => d())];
378
+ },
379
+ {
380
+ props: {
381
+ options: {
382
+ type: Object,
383
+ default: () => ({})
384
+ },
385
+ layers: {
386
+ type: Array,
387
+ default: () => []
388
+ }
389
+ }
390
+ }
391
+ );
392
+ }
393
+ async loadDeckGL() {
394
+ const { Deck: e } = await import("deck.gl");
395
+ this.deckgl = { Deck: e };
396
+ }
397
+ async createMap(e, a = {}) {
398
+ const t = new V();
399
+ return new Promise((s, r) => {
400
+ M(async () => {
401
+ await (this.deckgl ? Promise.resolve() : this.loadDeckGL());
402
+ const o = () => {
403
+ if (!e.value)
404
+ throw new Error("Missing HTML element");
405
+ t.deckgl = this.deckgl, t.map = new this.deckgl.Deck({
406
+ parent: e.value,
407
+ ...a
408
+ }), s(t);
409
+ };
410
+ a.onMounted ? a.onMounted(o) : o();
411
+ }), k(() => {
412
+ t.map && (t.map.finalize(), t.map = null);
413
+ });
414
+ });
415
+ }
416
+ }
417
+ function b(l) {
418
+ let e = 0;
419
+ return l.map((a) => {
420
+ let t = a[0];
421
+ return t - e > 180 ? t += -360 : e - t > 180 ? t += 360 : t += 0, e = t, [t, a[1]];
422
+ });
423
+ }
424
+ function F({ origin: l, destination: e, step: a = 100 }) {
425
+ const t = K([l, e]), s = P(t), r = [];
426
+ for (let o = 0; o < s; o += s / a) {
427
+ const i = E(t, o);
428
+ r.push(i.geometry.coordinates);
429
+ }
430
+ return r.push(e), t.geometry.coordinates = b(r), t;
431
+ }
432
+ async function I({ origin: l, destination: e, includeBoundaries: a = !1 }) {
433
+ const t = (await import("./searoute.js")).default, s = t(D(l), D(e));
434
+ return a && (s.geometry.coordinates.unshift(l), s.geometry.coordinates.push(e)), s.geometry.coordinates = b(s.geometry.coordinates), s;
435
+ }
436
+ const U = {
437
+ searoute: async (...l) => (await import("./searoute.js")).default(...l),
438
+ getAirRoute: F,
439
+ getWaterRoute: I,
440
+ normalizeRoute: b
441
+ }, q = {
442
+ mapbox: A,
443
+ google: O,
444
+ "google-maps": O,
445
+ deckgl: x,
446
+ "deck-gl": x
447
+ };
448
+ class z {
449
+ constructor(e = {}) {
450
+ this.options = { ...e };
451
+ }
452
+ install(e) {
453
+ const a = {};
454
+ for (const [t, s] of Object.entries(e.config.maps || {})) {
455
+ if (!s.type)
456
+ throw new Error("Map type should be defined");
457
+ const r = q[s.type];
458
+ r ? a[t] = new r(s) : console.warn(`Map provider: ${s.type} is not supported`);
459
+ }
460
+ e.maps = a, e.map = a, this.yAppKit = e;
461
+ }
462
+ }
463
+ const W = (l) => new z(l);
464
+ export {
465
+ z as default,
466
+ W as loadMapModule,
467
+ q as mapProviders,
468
+ U as utils
469
+ };
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@yappkit/front-module-map",
3
+ "version": "0.1.0",
4
+ "description": "@yappkit/front module for map",
5
+ "module": "./lib/yappkit-map.js",
6
+ "main": "./lib/yappkit-map.js",
7
+ "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./lib/yappkit-map.js"
11
+ },
12
+ "./mapboxgl": {
13
+ "import": "./lib/mapbox-gl.js"
14
+ },
15
+ "./mapboxgl.css": {
16
+ "import": "./lib/mapbox-gl.css"
17
+ },
18
+ "./mapboxsdk": {
19
+ "import": "./lib/mapbox-sdk.js"
20
+ },
21
+ "./deckgl": {
22
+ "import": "./lib/providers/deckgl.js"
23
+ },
24
+ "./google-maps": {
25
+ "import": "./lib/providers/google-maps.js"
26
+ },
27
+ "./mapbox": {
28
+ "import": "./lib/providers/mapbox.js"
29
+ },
30
+ "./maplibre": {
31
+ "import": "./lib/providers/maplibre.js"
32
+ }
33
+ },
34
+ "dependencies": {},
35
+ "peerDependencies": {
36
+ "@deck.gl/google-maps": "^8.9.34",
37
+ "@deck.gl/mapbox": "^8.9.34",
38
+ "@googlemaps/js-api-loader": "^1.16.2",
39
+ "@turf/turf": "^6.5.0",
40
+ "deck.gl": "^8.9.34",
41
+ "geojson-path-finder": "2.0.2",
42
+ "maplibre-gl": "^4.0.0",
43
+ "vue": "^3.4.15"
44
+ }
45
+ }