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