@yappkit/front-module-map 0.1.8 → 0.2.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.
@@ -1,245 +1,28 @@
1
- import { defineComponent as m, ref as v, toRefs as y, shallowRef as b, provide as g, watch as d, h as x, inject as M, onMounted as S, onUnmounted as L, nextTick as f } from "vue";
2
- class O {
3
- constructor({ unwatchFn: e }) {
4
- this.overlay = null, this.watching = { options: !1, layers: !1 }, this.unwatchFn = e;
5
- }
6
- watchOptions(e, a = {}) {
7
- if (!this.overlay)
8
- throw new Error("overlay should be set");
9
- if (this.watching.options) {
10
- console.warn("No need to call MapboxDeckGLOverlay.watchOptions(). options are already being watched");
11
- return;
12
- }
13
- this.watching.options = !0;
14
- const t = d(e, () => {
15
- this.overlay.setProps({ ...e.value });
16
- }, {
17
- immediate: !0,
18
- deep: !0,
19
- ...a
20
- });
21
- this.unwatchFn.push(t);
22
- }
23
- watchLayers(e, a = {}) {
24
- if (!this.overlay)
25
- throw new Error("overlay should be set");
26
- if (this.watching.layers) {
27
- console.warn("No need to call MapboxDeckGLOverlay.watchLayers(). layers are already being watched");
28
- return;
29
- }
30
- this.watching.layers = !0;
31
- const t = d(e, () => {
32
- this.overlay.setProps({ layers: e.value });
33
- }, {
34
- immediate: !0,
35
- ...a
36
- });
37
- this.unwatchFn.push(t);
38
- }
39
- }
40
- class k {
41
- constructor() {
42
- this.map = null, this.watching = { sources: !1, layers: !1 }, this.unwatchFn = [];
43
- }
44
- watchSources(e, a = {}) {
45
- if (!this.map)
46
- throw new Error("map should be set");
47
- if (this.watching.sources) {
48
- console.warn("No need to call MapboxMap.watchSources(). sources are already being watched");
49
- return;
50
- }
51
- this.watching.sources = !0;
52
- const t = d(() => (e.value || []).map((s) => {
53
- if (!s.propsId) {
54
- const { data: n, ...o } = s;
55
- s.propsId = JSON.stringify(o);
56
- }
57
- return s;
58
- }), async (s, n) => {
59
- const o = n || [], i = s || [], c = i.filter((r, h) => {
60
- var u;
61
- return r !== o[h] && r.propsId === ((u = o[h]) == null ? void 0 : u.propsId);
62
- }), p = i.filter((r, h) => r !== o[h] && !c.find((u) => u.id === r.id)), l = o.filter((r, h) => r !== i[h] && !c.find((u) => u.id === r.id));
63
- for (const r of c)
64
- this.map.getSource(r.id).setData(r.data);
65
- await f();
66
- for (const r of l)
67
- this.map.removeSource(r.id);
68
- for (const r of p) {
69
- const h = { ...r };
70
- delete h.id, delete h.propsId, this.map.addSource(r.id, h);
71
- }
72
- }, {
73
- immediate: !0,
74
- ...a
75
- });
76
- this.unwatchFn.push(t);
77
- }
78
- watchLayers(e, a = {}) {
79
- if (!this.map)
80
- throw new Error("map should be set");
81
- if (this.watching.layers) {
82
- console.warn("No need to call MapboxMap.watchLayers(). layers are already being watched");
83
- return;
84
- }
85
- this.watching.layers = !0;
86
- const t = d(e, async (s, n) => {
87
- const o = n || [], i = s || [], c = i.filter((l, r) => l !== o[r]), p = o.filter((l, r) => l !== i[r]);
88
- for (const l of p)
89
- this.map.removeLayer(l.id), this.map.getSource(l.id) && this.map.removeSource(l.id);
90
- await f();
91
- for (const l of c)
92
- this.map.addLayer(l);
93
- }, {
94
- immediate: !0,
95
- ...a
96
- });
97
- this.unwatchFn.push(t);
98
- }
99
- async setDeckGLOverlay(e = {}) {
100
- if (!this.map)
101
- throw new Error("map should be set");
102
- const { MapboxOverlay: a } = await import("@deck.gl/mapbox"), t = new O({ unwatchFn: this.unwatchFn });
103
- return t.overlay = new a(e), this.map.addControl(t.overlay), t;
104
- }
105
- }
106
- class D {
107
- constructor(e) {
108
- if (this.apiKey = e.apiKey, !this.apiKey)
109
- throw new Error("apiKey is required");
110
- this.services = [...e.services || []], this.sdk = {}, this.mapboxgl = null, this.getSDK();
111
- }
112
- get Map() {
113
- return m(
114
- (e, { emit: a, slots: t }) => {
115
- const s = v(null), { options: n, layers: o, sources: i } = y(e), c = b(null);
116
- return g("mapboxMap", c), this.createMap(s, n.value).then((p) => {
117
- c.value = p, d(n, () => {
118
- console.error("Cannot update options (missing function similar to deckglMap.setProps()). Use SDK dedicated functions instead");
119
- }, { once: !0 }), p.watchSources(i, n.value.watchSourcesOptions), p.watchLayers(o, n.value.watchLayersOptions), a("loaded", p);
120
- }), () => x("div", { ref: s }, c.value ? t : []);
121
- },
122
- {
123
- props: {
124
- options: {
125
- type: Object,
126
- default: () => ({})
127
- },
128
- layers: {
129
- type: Array,
130
- default: () => []
131
- },
132
- sources: {
133
- type: Array,
134
- default: () => []
135
- }
136
- },
137
- emits: ["loaded"]
138
- }
139
- );
140
- }
141
- get DeckGLOverlay() {
142
- return m(
143
- (e, { emit: a, slots: t }) => {
144
- const { options: s, layers: n } = y(e), o = M("mapboxMap");
145
- if (!o || !o.value)
146
- throw new Error("map should exist");
147
- return o.value.setDeckGLOverlay(s.value).then((i) => {
148
- i.watchOptions(s, s.value.watchOptionsOptions), i.watchLayers(n, s.value.watchLayersOptions), a("loaded", i);
149
- }), () => {
150
- };
151
- },
152
- {
153
- props: {
154
- options: {
155
- type: Object,
156
- default: () => ({})
157
- },
158
- layers: {
159
- type: Array,
160
- default: () => []
161
- }
162
- },
163
- emits: ["loaded"]
164
- }
165
- );
166
- }
167
- async loadMapbox() {
168
- if (!this.mapboxgl) {
169
- const e = await import("../mapbox-gl.js").then((a) => a.mapboxGl);
170
- this.mapboxgl = e;
171
- }
172
- }
173
- get servicesLoader() {
174
- return {
175
- datasets: () => import("@mapbox/mapbox-sdk/services/datasets.js"),
176
- directions: () => import("@mapbox/mapbox-sdk/services/directions.js"),
177
- geocoding: () => import("@mapbox/mapbox-sdk/services/geocoding.js"),
178
- geocodingV6: () => import("@mapbox/mapbox-sdk/services/geocoding-v6.js"),
179
- mapMatching: () => import("@mapbox/mapbox-sdk/services/map-matching.js"),
180
- matrix: () => import("@mapbox/mapbox-sdk/services/matrix.js"),
181
- optimization: () => import("@mapbox/mapbox-sdk/services/optimization.js"),
182
- static: () => import("@mapbox/mapbox-sdk/services/static.js"),
183
- styles: () => import("@mapbox/mapbox-sdk/services/styles.js"),
184
- tilequery: () => import("@mapbox/mapbox-sdk/services/tilequery.js"),
185
- tilesets: () => import("@mapbox/mapbox-sdk/services/tilesets.js"),
186
- tokens: () => import("@mapbox/mapbox-sdk/services/tokens.js"),
187
- uploads: () => import("@mapbox/mapbox-sdk/services/uploads.js"),
188
- isochrone: () => import("@mapbox/mapbox-sdk/services/isochrone.js")
189
- };
190
- }
191
- async loadMapboxSDK({ services: e = [] } = {}) {
192
- const t = (await import("@mapbox/mapbox-sdk")).default({ accessToken: this.apiKey }), s = [.../* @__PURE__ */ new Set([...this.services, ...e])];
193
- this.services = s;
194
- const n = await Promise.all(this.services.map((o) => this.sdk[o] || this.servicesLoader[o]().then((i) => i.default(t))));
195
- for (const [o, i] of this.services.entries())
196
- this.sdk[i] = n[o];
197
- }
198
- async getSDK(e) {
199
- let a = {};
200
- if (typeof e == "string" ? a.services = [e] : a = { ...e }, await this.loadMapboxSDK(a), typeof e == "string") {
201
- if (!this.sdk[e])
202
- throw new Error(e + " service is not loaded");
203
- return this.sdk[e];
204
- }
205
- return this.sdk;
206
- }
207
- async createMap(e, a = {}) {
208
- const t = new k(), s = () => {
209
- for (const n of t.unwatchFn)
210
- n();
211
- t.unwatchFn = [], t.map && (t.map.remove(), t.map = null);
212
- };
213
- return new Promise((n, o) => {
214
- S(async () => {
215
- await this.loadMapbox();
216
- const i = () => {
217
- if (!e.value)
218
- throw new Error("Missing HTML element");
219
- const c = d(e, () => {
220
- e.value || (s(), c());
221
- });
222
- t.map = new this.mapboxgl.Map({
223
- container: e.value,
224
- accessToken: this.apiKey,
225
- ...a
226
- }), t.map.on("load", () => {
227
- n(t);
228
- });
229
- };
230
- a.onMounted ? a.onMounted(i) : i();
231
- }), L(() => {
232
- s();
233
- });
234
- });
235
- }
236
- }
237
- class F extends D {
238
- async loadMapbox() {
239
- const e = await import("maplibre-gl");
240
- this.mapboxgl = e;
241
- }
242
- }
243
- export {
244
- F as default
1
+ import e from "./mapbox.js";
2
+ import { isString as t } from "lodash-es";
3
+ var n = class extends e {
4
+ get requiresApiKey() {
5
+ return !1;
6
+ }
7
+ async loadMapbox() {
8
+ if (!this.mapboxgl) {
9
+ let [e, t] = await Promise.all([
10
+ import("maplibre-gl"),
11
+ import("maplibre-gl/dist/maplibre-gl-worker.mjs?worker&url"),
12
+ import("maplibre-gl/dist/maplibre-gl.css")
13
+ ]);
14
+ typeof t.default == "string" ? e.setWorkerUrl(t.default) : console.error("Maplibre worker url did not resolve to a string: no tile will be parsed. Pin it from the app with setWorkerUrl()"), this.mapboxgl = e;
15
+ }
16
+ }
17
+ async getSDK(e) {
18
+ if ((t(e) ? [e] : e?.services ?? []).length || this.services.length) throw Error("Mapbox REST services are not available on the maplibre provider");
19
+ return this.sdk;
20
+ }
21
+ _getMapOptions(e, t) {
22
+ return {
23
+ container: e,
24
+ ...t
25
+ };
26
+ }
245
27
  };
28
+ export { n as default };