From 3952bb441b6ac5a1c92e9fe21b6fd3f59d80d0cf Mon Sep 17 00:00:00 2001 From: Bob Bol Date: Thu, 8 Oct 2020 17:12:22 +0200 Subject: [PATCH 1/2] keep an internal list of ordered layers that matches the order of layers on the mapbox instance --- src/components/VMapbox.vue | 97 +++++++++++++++++++++++++++----- src/components/v-mapbox-layer.js | 57 +++++-------------- src/stories/index.stories.js | 4 +- src/utils/helpers.js | 1 + 4 files changed, 101 insertions(+), 58 deletions(-) create mode 100644 src/utils/helpers.js diff --git a/src/components/VMapbox.vue b/src/components/VMapbox.vue index 117d14e..f5d84a3 100644 --- a/src/components/VMapbox.vue +++ b/src/components/VMapbox.vue @@ -14,6 +14,7 @@ diff --git a/src/components/v-mapbox-layer.js b/src/components/v-mapbox-layer.js index c3a67d7..99039b9 100644 --- a/src/components/v-mapbox-layer.js +++ b/src/components/v-mapbox-layer.js @@ -1,7 +1,7 @@ export default { name: 'v-mapbox-layer', - inject: ['getMap'], + inject: [ 'getMapMethods' ], render: () => null, @@ -10,71 +10,44 @@ export default { type: Object, default: () => ({}) }, - - // Allows to place a layer before another before: { type: String, default: undefined } }, - data: () => ({ - isInitialized: false - }), - methods: { - deferredMountedTo() { - if(!this.isInitialized) { - this.renderLayer(); - this.isInitialized = true; - } - }, - - renderLayer() { - this.removeLayer(); - this.addLayer(); - }, - addLayer() { - const map = this.getMap(); - map.addLayer(this.options, this.before); + const mapMethods = this.getMapMethods() + mapMethods.addLayer(this.options, this.before) }, removeLayer() { - const map = this.getMap(); - if(map) { - const layerId = this.options.id; - const layer = map.getLayer(layerId); - if(layer) { - const layerSource = layer.source; - map.removeLayer(layerId); - if(layerSource && !map.getStyle().layers.some(({ source }) => source === layerSource)) { - map.removeSource(layerSource); - } - } - } + const mapMethods = this.getMapMethods() + const layerId = this.options.id + mapMethods.removeLayer(layerId) + }, + + updateLayer() { + const mapMethods = this.getMapMethods() + mapMethods.updateLayer(this.options) }, }, mounted() { - const map = this.getMap(); - // We can immediately initialize if we have the map ready - if(map && map.isStyleLoaded()) { - this.renderLayer(); - this.isInitialized = true; - } + this.addLayer() }, destroyed() { - this.removeLayer(); + this.removeLayer() }, watch: { options: { deep: true, handler() { - this.renderLayer(); + this.updateLayer() } } } -}; +} diff --git a/src/stories/index.stories.js b/src/stories/index.stories.js index f204a02..4d9a2d1 100644 --- a/src/stories/index.stories.js +++ b/src/stories/index.stories.js @@ -160,10 +160,10 @@ const sortingTemplate = ` style="height: 300px;" :center="[0, 0]" > - - + + ` diff --git a/src/utils/helpers.js b/src/utils/helpers.js new file mode 100644 index 0000000..0cc758e --- /dev/null +++ b/src/utils/helpers.js @@ -0,0 +1 @@ +export const uniqueInArrayById = (arr1, arr2) => arr1.filter(({ id }) => !arr2.some(el => el.id === id)) From 35aaa4b9f3044d2a113741eaf6e202a988729c28 Mon Sep 17 00:00:00 2001 From: Bob Bol Date: Thu, 8 Oct 2020 18:34:52 +0200 Subject: [PATCH 2/2] skip trying to find layer if not defined --- src/components/VMapbox.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/VMapbox.vue b/src/components/VMapbox.vue index f5d84a3..f313702 100644 --- a/src/components/VMapbox.vue +++ b/src/components/VMapbox.vue @@ -192,7 +192,7 @@ export default { const thisIndex = this.mapLayers.findIndex(({ id }) => id === layerOptions.id) const nextItem = this.mapLayers[thisIndex + 1] const maybeBefore = nextItem ? nextItem.id : undefined - const existingLayer = this.map.getLayer(maybeBefore) + const existingLayer = maybeBefore && this.map.getLayer(maybeBefore) const before = existingLayer ? maybeBefore : undefined this.map.addLayer(layerOptions, before) },