@kiva/kv-components 3.37.0 → 3.38.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/package.json +2 -2
- package/vue/KvMap.vue +350 -0
- package/vue/stories/KvMap.stories.js +69 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,28 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [3.38.1](https://github.com/kiva/kv-ui-elements/compare/@kiva/kv-components@3.38.0...@kiva/kv-components@3.38.1) (2023-08-30)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* kvmap component refactor to avoid using vue meta package ([#287](https://github.com/kiva/kv-ui-elements/issues/287)) ([72f5240](https://github.com/kiva/kv-ui-elements/commit/72f524037358f039032ebd1c8d1f4e106ab6ff10))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# [3.38.0](https://github.com/kiva/kv-ui-elements/compare/@kiva/kv-components@3.37.0...@kiva/kv-components@3.38.0) (2023-08-29)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* kvmap ported to kv components library ([#286](https://github.com/kiva/kv-ui-elements/issues/286)) ([373e53f](https://github.com/kiva/kv-ui-elements/commit/373e53fd407d161e79033826a6063fbd5ca5b2ce))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
6
28
|
# [3.37.0](https://github.com/kiva/kv-ui-elements/compare/@kiva/kv-components@3.36.0...@kiva/kv-components@3.37.0) (2023-08-18)
|
|
7
29
|
|
|
8
30
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kiva/kv-components",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.38.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -75,5 +75,5 @@
|
|
|
75
75
|
"optional": true
|
|
76
76
|
}
|
|
77
77
|
},
|
|
78
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "75d76cdba8cf94ff0ae555f9933fcbd038df8404"
|
|
79
79
|
}
|
package/vue/KvMap.vue
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div
|
|
3
|
+
class="tw-relative tw-block tw-w-full"
|
|
4
|
+
:style="mapDimensions"
|
|
5
|
+
>
|
|
6
|
+
<div
|
|
7
|
+
:id="`kv-map-holder-${mapId}`"
|
|
8
|
+
:ref="refString"
|
|
9
|
+
class="tw-w-full tw-h-full tw-bg-black"
|
|
10
|
+
:style="{ position: 'absolute' }"
|
|
11
|
+
></div>
|
|
12
|
+
</div>
|
|
13
|
+
</template>
|
|
14
|
+
|
|
15
|
+
<script>
|
|
16
|
+
export default {
|
|
17
|
+
name: 'KvMap',
|
|
18
|
+
props: {
|
|
19
|
+
/**
|
|
20
|
+
* Aspect Ration for computed map dimensions
|
|
21
|
+
* We'll divide the container width by this to determine the height
|
|
22
|
+
*/
|
|
23
|
+
aspectRatio: {
|
|
24
|
+
type: Number,
|
|
25
|
+
default: 1,
|
|
26
|
+
},
|
|
27
|
+
/**
|
|
28
|
+
* Control how quickly the autoZoom occurs
|
|
29
|
+
*/
|
|
30
|
+
autoZoomDelay: {
|
|
31
|
+
type: Number,
|
|
32
|
+
default: 1500,
|
|
33
|
+
},
|
|
34
|
+
/**
|
|
35
|
+
* Set the height to override aspect ratio driven and/or default dimensions
|
|
36
|
+
*/
|
|
37
|
+
height: {
|
|
38
|
+
type: Number,
|
|
39
|
+
default: null,
|
|
40
|
+
},
|
|
41
|
+
/**
|
|
42
|
+
* Setting this initialZoom will zoom the map from initialZoom to zoom when the map enters the viewport
|
|
43
|
+
*/
|
|
44
|
+
initialZoom: {
|
|
45
|
+
type: Number,
|
|
46
|
+
default: null,
|
|
47
|
+
},
|
|
48
|
+
/**
|
|
49
|
+
* Set the center point latitude
|
|
50
|
+
*/
|
|
51
|
+
lat: {
|
|
52
|
+
type: Number,
|
|
53
|
+
default: null,
|
|
54
|
+
},
|
|
55
|
+
/**
|
|
56
|
+
* Set the center point longitude
|
|
57
|
+
*/
|
|
58
|
+
long: {
|
|
59
|
+
type: Number,
|
|
60
|
+
default: null,
|
|
61
|
+
},
|
|
62
|
+
/**
|
|
63
|
+
* Set this if there are more than one map on the page
|
|
64
|
+
*/
|
|
65
|
+
mapId: {
|
|
66
|
+
type: Number,
|
|
67
|
+
default: 0,
|
|
68
|
+
},
|
|
69
|
+
/**
|
|
70
|
+
* Force use of Leaflet
|
|
71
|
+
*/
|
|
72
|
+
useLeaflet: {
|
|
73
|
+
type: Boolean,
|
|
74
|
+
default: false,
|
|
75
|
+
},
|
|
76
|
+
/**
|
|
77
|
+
* Set the width to override aspect ratio driven and/or default dimensions
|
|
78
|
+
*/
|
|
79
|
+
width: {
|
|
80
|
+
type: Number,
|
|
81
|
+
default: null,
|
|
82
|
+
},
|
|
83
|
+
/**
|
|
84
|
+
* Default zoom level
|
|
85
|
+
*/
|
|
86
|
+
zoomLevel: {
|
|
87
|
+
type: Number,
|
|
88
|
+
default: 4,
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
data() {
|
|
92
|
+
return {
|
|
93
|
+
hasWebGL: false,
|
|
94
|
+
leafletReady: false,
|
|
95
|
+
mapInstance: null,
|
|
96
|
+
mapLibreReady: false,
|
|
97
|
+
mapLoaded: false,
|
|
98
|
+
zoomActive: false,
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
computed: {
|
|
102
|
+
mapDimensions() {
|
|
103
|
+
// Use container to derive height based on aspect ration + width
|
|
104
|
+
const container = this.$el?.getBoundingClientRect();
|
|
105
|
+
const height = container ? `${container.width / this.aspectRatio}px` : '300px';
|
|
106
|
+
const width = container ? `${container.width}px` : '100%';
|
|
107
|
+
// Override values if deliberate height or width are provided
|
|
108
|
+
return {
|
|
109
|
+
height: this.height ? `${this.height}px` : height,
|
|
110
|
+
width: this.width ? `${this.width}px` : width,
|
|
111
|
+
paddingBottom: this.height ? `${this.height}px` : `${100 / this.aspectRatio}%`,
|
|
112
|
+
};
|
|
113
|
+
},
|
|
114
|
+
refString() {
|
|
115
|
+
return `mapholder${this.mapId}`;
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
watch: {
|
|
119
|
+
lat(next, prev) {
|
|
120
|
+
if (prev === null && this.long && !this.mapLibreReady && !this.leafletReady) {
|
|
121
|
+
this.initializeMap();
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
long(next, prev) {
|
|
125
|
+
if (prev === null && this.lat && !this.mapLibreReady && !this.leafletReady) {
|
|
126
|
+
this.initializeMap();
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
mounted() {
|
|
131
|
+
if (!this.mapLibreReady && !this.leafletReady) {
|
|
132
|
+
this.initializeMap();
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
beforeDestroy() {
|
|
136
|
+
if (this.mapInstance) {
|
|
137
|
+
if (!this.hasWebGL && !this.leafletReady) {
|
|
138
|
+
// turn off the leaflet instance
|
|
139
|
+
this.mapInstance.off();
|
|
140
|
+
}
|
|
141
|
+
// remove either leaflet or maplibregl
|
|
142
|
+
this.mapInstance.remove();
|
|
143
|
+
}
|
|
144
|
+
this.destroyWrapperObserver();
|
|
145
|
+
},
|
|
146
|
+
methods: {
|
|
147
|
+
activateZoom(zoomOut = false) {
|
|
148
|
+
const { mapInstance, hasWebGL, mapLibreReady } = this;
|
|
149
|
+
const currentZoomLevel = mapInstance.getZoom();
|
|
150
|
+
// exit if already zoomed in (getZoom() works for both leaflet + maplibregl)
|
|
151
|
+
if ((!zoomOut && currentZoomLevel === this.zoomLevel)
|
|
152
|
+
|| (zoomOut && currentZoomLevel === this.initialZoom)) return false;
|
|
153
|
+
|
|
154
|
+
this.zoomActive = true;
|
|
155
|
+
// establish delayed zoom duration
|
|
156
|
+
const timedZoom = window.setTimeout(() => {
|
|
157
|
+
if (hasWebGL && mapLibreReady) {
|
|
158
|
+
// maplibregl specific zoom method
|
|
159
|
+
mapInstance.zoomTo(
|
|
160
|
+
zoomOut ? this.initialZoom : this.zoomLevel,
|
|
161
|
+
{ duration: 1200 },
|
|
162
|
+
);
|
|
163
|
+
} else {
|
|
164
|
+
// leaflet specific zoom method
|
|
165
|
+
mapInstance.setZoom(zoomOut ? this.initialZoom : this.zoomLevel);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
clearTimeout(timedZoom);
|
|
169
|
+
this.zoomActive = false;
|
|
170
|
+
}, this.autoZoomDelay);
|
|
171
|
+
},
|
|
172
|
+
createWrapperObserver() {
|
|
173
|
+
// Watch for the wrapper element moving in and out of the viewport
|
|
174
|
+
this.wrapperObserver = this.createIntersectionObserver({
|
|
175
|
+
targets: [this.$refs?.[this.refString]],
|
|
176
|
+
callback: (entries) => {
|
|
177
|
+
entries.forEach((entry) => {
|
|
178
|
+
if (entry.target === this.$refs?.[this.refString] && !this.zoomActive) {
|
|
179
|
+
if (entry.intersectionRatio > 0) {
|
|
180
|
+
this.activateZoom();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
},
|
|
187
|
+
destroyWrapperObserver() {
|
|
188
|
+
if (this.wrapperObserver) {
|
|
189
|
+
this.wrapperObserver.disconnect();
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
checkWebGL() {
|
|
193
|
+
// exit and use leaflet if specified or document isn't present
|
|
194
|
+
if (this.useLeaflet || typeof document === 'undefined') return false;
|
|
195
|
+
// via. https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/By_example/Detect_WebGL
|
|
196
|
+
// Create canvas element. The canvas is not added to the document itself,
|
|
197
|
+
// so it is never displayed in the browser window.
|
|
198
|
+
const canvas = document.createElement('canvas');
|
|
199
|
+
// Get WebGLRenderingContext from canvas element.
|
|
200
|
+
const gl = canvas.getContext('webgl')
|
|
201
|
+
|| canvas.getContext('experimental-webgl');
|
|
202
|
+
// Report the result.
|
|
203
|
+
if (gl && gl instanceof WebGLRenderingContext) {
|
|
204
|
+
this.hasWebGL = true;
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
return false;
|
|
208
|
+
},
|
|
209
|
+
initializeMap() {
|
|
210
|
+
/**
|
|
211
|
+
* This initial checkWebGL() call kicks off the library asset inclusion
|
|
212
|
+
* We then start polling for the readiness of our selected map library and initialize it once ready
|
|
213
|
+
*/
|
|
214
|
+
const mapScript = document.createElement('script');
|
|
215
|
+
const mapStyle = document.createElement('link');
|
|
216
|
+
mapScript.setAttribute('async', true);
|
|
217
|
+
mapScript.setAttribute('defer', true);
|
|
218
|
+
mapStyle.setAttribute('rel', 'stylesheet');
|
|
219
|
+
if (this.checkWebGL()) {
|
|
220
|
+
mapScript.setAttribute('vmid', `maplibregljs${this.mapId}`);
|
|
221
|
+
mapStyle.setAttribute('vmid', `maplibreglcss${this.mapId}`);
|
|
222
|
+
mapScript.setAttribute('src', 'https://unpkg.com/maplibre-gl@latest/dist/maplibre-gl.js');
|
|
223
|
+
mapStyle.setAttribute('href', 'https://unpkg.com/maplibre-gl@latest/dist/maplibre-gl.css');
|
|
224
|
+
|
|
225
|
+
this.testDelayedGlobalLibrary('maplibregl').then((response) => {
|
|
226
|
+
if (response.loaded && !this.mapLoaded && !this.useLeaflet && this.lat && this.long) {
|
|
227
|
+
this.initializeMapLibre();
|
|
228
|
+
this.mapLibreReady = true;
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
} else {
|
|
232
|
+
mapScript.setAttribute('vmid', `leafletjs${this.mapId}`);
|
|
233
|
+
mapStyle.setAttribute('vmid', `leaftletcss${this.mapId}`);
|
|
234
|
+
mapScript.setAttribute('src', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js');
|
|
235
|
+
mapStyle.setAttribute('href', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css');
|
|
236
|
+
|
|
237
|
+
this.testDelayedGlobalLibrary('L').then((leafletTest) => {
|
|
238
|
+
if (leafletTest.loaded && !this.mapLoaded && this.lat && this.long) {
|
|
239
|
+
this.initializeLeaflet();
|
|
240
|
+
this.leafletReady = true;
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
document.head.appendChild(mapScript);
|
|
245
|
+
document.head.appendChild(mapStyle);
|
|
246
|
+
},
|
|
247
|
+
initializeLeaflet() {
|
|
248
|
+
/* eslint-disable no-undef, max-len */
|
|
249
|
+
// Initialize primary mapInstance
|
|
250
|
+
this.mapInstance = L.map(`kv-map-holder-${this.mapId}`, {
|
|
251
|
+
center: [this.lat, this.long],
|
|
252
|
+
zoom: this.initialZoom || this.zoomLevel,
|
|
253
|
+
// todo make props for the following options
|
|
254
|
+
dragging: false,
|
|
255
|
+
zoomControl: false,
|
|
256
|
+
animate: true,
|
|
257
|
+
scrollWheelZoom: false,
|
|
258
|
+
doubleClickZoom: false,
|
|
259
|
+
attributionControl: false,
|
|
260
|
+
});
|
|
261
|
+
/* eslint-disable quotes */
|
|
262
|
+
// Add our tileset to the mapInstance
|
|
263
|
+
L.tileLayer('https://api.maptiler.com/maps/bright/{z}/{x}/{y}.png?key=n1Mz5ziX3k6JfdjFe7mx', {
|
|
264
|
+
tileSize: 512,
|
|
265
|
+
zoomOffset: -1,
|
|
266
|
+
minZoom: 1,
|
|
267
|
+
crossOrigin: true,
|
|
268
|
+
}).addTo(this.mapInstance);
|
|
269
|
+
/* eslint-enable quotes */
|
|
270
|
+
/* eslint-enable no-undef, max-len */
|
|
271
|
+
|
|
272
|
+
// signify map has loaded
|
|
273
|
+
this.mapLoaded = true;
|
|
274
|
+
// only activate autoZoom if we have an initialZoom set
|
|
275
|
+
if (this.initialZoom !== null) {
|
|
276
|
+
this.createWrapperObserver();
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
initializeMapLibre() {
|
|
280
|
+
// Initialize primary mapInstance
|
|
281
|
+
// eslint-disable-next-line no-undef
|
|
282
|
+
this.mapInstance = new maplibregl.Map({
|
|
283
|
+
container: `kv-map-holder-${this.mapId}`,
|
|
284
|
+
style: 'https://api.maptiler.com/maps/bright/style.json?key=n1Mz5ziX3k6JfdjFe7mx',
|
|
285
|
+
center: [this.long, this.lat],
|
|
286
|
+
zoom: this.initialZoom || this.zoomLevel,
|
|
287
|
+
attributionControl: false,
|
|
288
|
+
dragPan: false,
|
|
289
|
+
scrollZoom: false,
|
|
290
|
+
doubleClickZoom: false,
|
|
291
|
+
dragRotate: false,
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
// signify map has loaded
|
|
295
|
+
this.mapLoaded = true;
|
|
296
|
+
|
|
297
|
+
// only activate autoZoom if we have an initialZoom set
|
|
298
|
+
if (this.initialZoom !== null) {
|
|
299
|
+
this.createWrapperObserver();
|
|
300
|
+
}
|
|
301
|
+
},
|
|
302
|
+
checkIntersectionObserverSupport() {
|
|
303
|
+
if (typeof window === 'undefined'
|
|
304
|
+
|| !('IntersectionObserver' in window)
|
|
305
|
+
|| !('IntersectionObserverEntry' in window)
|
|
306
|
+
|| !('intersectionRatio' in window.IntersectionObserverEntry.prototype)) {
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
return true;
|
|
310
|
+
},
|
|
311
|
+
createIntersectionObserver({ callback, options, targets } = {}) {
|
|
312
|
+
if (this.checkIntersectionObserverSupport()) {
|
|
313
|
+
const observer = new IntersectionObserver(callback, options);
|
|
314
|
+
targets.forEach((target) => observer.observe(target));
|
|
315
|
+
return observer;
|
|
316
|
+
}
|
|
317
|
+
},
|
|
318
|
+
testDelayedGlobalLibrary(library, timeout = 3000) {
|
|
319
|
+
// return a promise
|
|
320
|
+
return new Promise((resolve, reject) => {
|
|
321
|
+
if (typeof window === 'undefined') {
|
|
322
|
+
reject(new Error('window object not available'));
|
|
323
|
+
}
|
|
324
|
+
// establish timeout to limit time until promise resolution
|
|
325
|
+
let readyStateTimeout;
|
|
326
|
+
// establish interval to check for library presence
|
|
327
|
+
const readyStateInterval = window.setInterval(() => {
|
|
328
|
+
// determine if library is present on window
|
|
329
|
+
if (typeof window[library] !== 'undefined') {
|
|
330
|
+
// cleanup timers
|
|
331
|
+
clearInterval(readyStateInterval);
|
|
332
|
+
clearTimeout(readyStateTimeout);
|
|
333
|
+
// resolve the promise
|
|
334
|
+
resolve({ loaded: true });
|
|
335
|
+
}
|
|
336
|
+
}, 100);
|
|
337
|
+
|
|
338
|
+
// activate timeout
|
|
339
|
+
readyStateTimeout = window.setTimeout(() => {
|
|
340
|
+
// clean up interval and timeout
|
|
341
|
+
clearInterval(readyStateInterval);
|
|
342
|
+
clearTimeout(readyStateTimeout);
|
|
343
|
+
// resolve the promise
|
|
344
|
+
resolve({ loaded: false });
|
|
345
|
+
}, timeout);
|
|
346
|
+
});
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
};
|
|
350
|
+
</script>
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import KvMap from '../KvMap.vue';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
title: 'KvMap',
|
|
5
|
+
component: KvMap,
|
|
6
|
+
args: {
|
|
7
|
+
autoZoomDelay: 1000,
|
|
8
|
+
height: null,
|
|
9
|
+
initialZoom: null,
|
|
10
|
+
lat: 37.700091,
|
|
11
|
+
long: -123.013243,
|
|
12
|
+
mapId: 0,
|
|
13
|
+
useLeaflet: false,
|
|
14
|
+
width: null,
|
|
15
|
+
zoomLevel: 4,
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const Template = (args, { argTypes }) => ({
|
|
20
|
+
props: Object.keys(argTypes),
|
|
21
|
+
components: { KvMap },
|
|
22
|
+
template: `<kv-map
|
|
23
|
+
class="tw-rounded tw-overflow-hidden"
|
|
24
|
+
:auto-zoom-delay="autoZoomDelay"
|
|
25
|
+
:height="height"
|
|
26
|
+
:lat="lat"
|
|
27
|
+
:long="long"
|
|
28
|
+
:initial-zoom="initialZoom"
|
|
29
|
+
:map-id="mapId"
|
|
30
|
+
:use-leaflet="useLeaflet"
|
|
31
|
+
:width="width"
|
|
32
|
+
:zoom-level="zoomLevel"
|
|
33
|
+
/>`,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
export const Default = Template.bind({});
|
|
37
|
+
Default.args = {
|
|
38
|
+
initialZoom: null,
|
|
39
|
+
mapId: 1,
|
|
40
|
+
zoomLevel: 14,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const AutoZoom = Template.bind({});
|
|
44
|
+
AutoZoom.args = {
|
|
45
|
+
initialZoom: 1,
|
|
46
|
+
lat: -0.023559,
|
|
47
|
+
long: 37.906193,
|
|
48
|
+
mapId: 2,
|
|
49
|
+
zoomLevel: 4,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const FixedDimensions = Template.bind({});
|
|
53
|
+
FixedDimensions.args = {
|
|
54
|
+
initialZoom: null,
|
|
55
|
+
height: 250,
|
|
56
|
+
mapId: 3,
|
|
57
|
+
width: 250,
|
|
58
|
+
zoomLevel: 14,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const Leaflet = Template.bind({});
|
|
62
|
+
Leaflet.args = {
|
|
63
|
+
initialZoom: null,
|
|
64
|
+
lat: -0.023559,
|
|
65
|
+
long: 37.906193,
|
|
66
|
+
mapId: 4,
|
|
67
|
+
useLeaflet: true,
|
|
68
|
+
zoomLevel: 6,
|
|
69
|
+
};
|