From 05daa125cb68dde4aa39ba46799ead9de08636d0 Mon Sep 17 00:00:00 2001 From: chenxianhui Date: Fri, 13 Sep 2024 16:48:31 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=E5=B0=86label=E5=9B=BE=E5=B1=82=E7=9A=84?= =?UTF-8?q?=E6=98=BE=E9=9A=90=E9=80=9A=E8=BF=87viewModel=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E4=BC=A0=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mapboxgl/tdt/map-switcher/TdtMapSwitcher.vue | 7 ++----- .../tdt/map-switcher/TdtMapSwitcherViewModel.js | 5 ++++- .../map-switcher/__tests__/TdtMapSwitcher.spec.js | 5 +++-- .../__tests__/TdtMapSwitcherViewModel.spec.js | 12 +++++++++++- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/mapboxgl/tdt/map-switcher/TdtMapSwitcher.vue b/src/mapboxgl/tdt/map-switcher/TdtMapSwitcher.vue index 219c6ee7..ebc7ad45 100644 --- a/src/mapboxgl/tdt/map-switcher/TdtMapSwitcher.vue +++ b/src/mapboxgl/tdt/map-switcher/TdtMapSwitcher.vue @@ -98,11 +98,8 @@ export default { this.labelChecked = checked; this.$emit('change-label-status', this.labelChecked); }, - layerUpdate() { - const labelLayer = this.map && this.map.getLayer(this.$t(`tdtMapSwitcher.Tianditu${this.viewModel.tdtLabelType}`)); - if (labelLayer && labelLayer.visibility) { - this.labelChecked = labelLayer.visibility === 'visible'; - } + layerUpdate(e) { + this.labelChecked = e.visible; } }, loaded() { diff --git a/src/mapboxgl/tdt/map-switcher/TdtMapSwitcherViewModel.js b/src/mapboxgl/tdt/map-switcher/TdtMapSwitcherViewModel.js index 24d8a294..4382792c 100644 --- a/src/mapboxgl/tdt/map-switcher/TdtMapSwitcherViewModel.js +++ b/src/mapboxgl/tdt/map-switcher/TdtMapSwitcherViewModel.js @@ -29,7 +29,10 @@ export default class TdtMapSwitcherViewModel extends mapboxgl.Evented { } _updateLayers() { - this.fire('layersUpdated'); + const labelLayer = this.map && this.map.getLayer(geti18n().tc(`tdtMapSwitcher.Tianditu${this.tdtLabelType}`)); + if (labelLayer && labelLayer.visibility) { + this.fire('layersUpdated', { visible: labelLayer.visibility === 'visible' }); + } } setTk(tk) { diff --git a/src/mapboxgl/tdt/map-switcher/__tests__/TdtMapSwitcher.spec.js b/src/mapboxgl/tdt/map-switcher/__tests__/TdtMapSwitcher.spec.js index 768cf4f3..d9f2b3e3 100644 --- a/src/mapboxgl/tdt/map-switcher/__tests__/TdtMapSwitcher.spec.js +++ b/src/mapboxgl/tdt/map-switcher/__tests__/TdtMapSwitcher.spec.js @@ -113,7 +113,8 @@ describe('TdtMapSwitcher.vue', () => { minzoom: 0, maxzoom: 18 } - } + }, + addLayer: () => {} }; await mapSubComponentLoaded(wrapper); const callback = function () { @@ -125,6 +126,6 @@ describe('TdtMapSwitcher.vue', () => { webmap, map }); - wrapper.vm.viewModel.togglerLabelLayer(false); + mockOnOptions.layersupdated(); }); }); diff --git a/src/mapboxgl/tdt/map-switcher/__tests__/TdtMapSwitcherViewModel.spec.js b/src/mapboxgl/tdt/map-switcher/__tests__/TdtMapSwitcherViewModel.spec.js index 7789fb2b..26c6f989 100644 --- a/src/mapboxgl/tdt/map-switcher/__tests__/TdtMapSwitcherViewModel.spec.js +++ b/src/mapboxgl/tdt/map-switcher/__tests__/TdtMapSwitcherViewModel.spec.js @@ -68,8 +68,18 @@ describe('TdtMapSwitcherViewModel', () => { proxy: null } }; + let mockOnOptions; + const webmap = { + getLayerList: () => layerCatalogs, + changeItemVisible: jest.fn(), + un: jest.fn(), + on: jest.fn(options => { + mockOnOptions = options; + }) + }; viewModel.setMap({ - map: mapMockFn(initLayers, initSources) + map: mapMockFn(initLayers, initSources), + webmap: webmap }); expect(viewModel.map.getStyle().layers).toEqual(initLayers); expect(viewModel.map.getStyle().sources).toEqual(initSources);