Skip to content

Commit ae8a9ad

Browse files
[fix]webmap 提供setLegends,cleanLayers可配置参数
1 parent b950239 commit ae8a9ad

5 files changed

Lines changed: 193 additions & 4 deletions

File tree

src/common/mapping/MapBase.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export function createMapClassExtending(SuperClass = class {}) {
2626
return this._legendList;
2727
}
2828

29+
setLegends(legendList) {
30+
this._legendList = legendList || [];
31+
}
32+
2933
getSelfAppreciableLayers() {
3034
return (this._sourceListModel && this._sourceListModel.getSelfLayers(...arguments)) || [];
3135
}

src/common/mapping/WebMapBase.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,16 +499,23 @@ export function createWebMapBaseExtending(SuperClass, { mapRepo }) {
499499
* @version 11.2.1
500500
* @function WebMapBase.prototype.cleanLayers
501501
* @description 删除追加的图层和事件。当设置 `map` 时有效
502+
* @param {Array<Object>} [layersToClean] - 要删除的图层数组,默认为所有可删除的缓存图层(非 reused)。每个图层对象包含 id 属性
503+
* @param {boolean} [isClean=true] - 是否调用 clean(false) 销毁内部资源。为 false 时只删除指定图层并同步清理 legends
502504
*/
503-
cleanLayers() {
505+
cleanLayers(layersToClean = this._cacheCleanLayers.filter(item => !item.reused), isClean = true) {
504506
// 清空追加的地图图层以及对应的事件
505507
if (!this.map) {
506508
return;
507509
}
508-
const layersToClean = this._cacheCleanLayers.filter(item => !item.reused);
510+
const cacheLayers = this._cacheCleanLayers;
509511
this._handler && this._handler.cleanLayers(layersToClean);
510-
this._cacheCleanLayers = [];
511-
this.clean(false);
512+
this._cacheCleanLayers = cacheLayers.filter(item => !layersToClean.some((item1=> item1.id === item.id)));
513+
if (isClean) {
514+
this.clean(false);
515+
} else {
516+
const legends = this.getLegends();
517+
this._handler.setLegends(legends.filter(item => !layersToClean.some((item1=> item1.id === item.layerId))));
518+
}
512519
}
513520
/**
514521
* @typedef {Object} BaseLayerConfig

test/common/mapping/MapBaseSpec.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { createMapClassExtending } from '@supermapgis/iclient-common/mapping/MapBase';
2+
3+
describe('MapBase', () => {
4+
let MapBase;
5+
let instance;
6+
7+
beforeEach(() => {
8+
MapBase = createMapClassExtending();
9+
instance = new MapBase();
10+
});
11+
12+
describe('setLegends', () => {
13+
it('should set _legendList with provided array', () => {
14+
const legends = [
15+
{ layerId: 'layer1', items: [{ color: '#fff', label: 'test' }] },
16+
{ layerId: 'layer2', items: [{ color: '#000' }] }
17+
];
18+
19+
instance.setLegends(legends);
20+
21+
expect(instance._legendList).toEqual(legends);
22+
});
23+
24+
it('should set _legendList to empty array when null is passed', () => {
25+
instance.setLegends(null);
26+
27+
expect(instance._legendList).toEqual([]);
28+
});
29+
30+
it('should set _legendList to empty array when undefined is passed', () => {
31+
instance.setLegends(undefined);
32+
33+
expect(instance._legendList).toEqual([]);
34+
});
35+
36+
it('should set _legendList to empty array when no argument is passed', () => {
37+
instance.setLegends();
38+
39+
expect(instance._legendList).toEqual([]);
40+
});
41+
42+
it('should overwrite existing legends', () => {
43+
instance._legendList = [{ layerId: 'old' }];
44+
45+
instance.setLegends([{ layerId: 'new' }]);
46+
47+
expect(instance._legendList).toEqual([{ layerId: 'new' }]);
48+
});
49+
});
50+
51+
describe('getLegends', () => {
52+
it('should return _legendList', () => {
53+
const legends = [{ layerId: 'layer1' }];
54+
instance._legendList = legends;
55+
56+
expect(instance.getLegends()).toEqual(legends);
57+
});
58+
59+
it('should return empty array when _legendList is not set', () => {
60+
expect(instance.getLegends()).toEqual([]);
61+
});
62+
});
63+
64+
describe('getLayerCatalog', () => {
65+
it('should return _sourceListModel.getLayerCatalog() when _sourceListModel exists', () => {
66+
const catalogs = [{ id: 'cat1' }];
67+
instance._sourceListModel = jasmine.createSpyObj('sourceListModel', ['getLayerCatalog']);
68+
instance._sourceListModel.getLayerCatalog.and.returnValue(catalogs);
69+
70+
expect(instance.getLayerCatalog()).toEqual(catalogs);
71+
});
72+
73+
it('should return empty array when _sourceListModel is null', () => {
74+
instance._sourceListModel = null;
75+
76+
expect(instance.getLayerCatalog()).toEqual([]);
77+
});
78+
});
79+
80+
describe('getLayers', () => {
81+
it('should return _sourceListModel.getLayers() when _sourceListModel exists', () => {
82+
const layers = [{ id: 'layer1' }];
83+
instance._sourceListModel = jasmine.createSpyObj('sourceListModel', ['getLayers']);
84+
instance._sourceListModel.getLayers.and.returnValue(layers);
85+
86+
expect(instance.getLayers()).toEqual(layers);
87+
});
88+
89+
it('should return empty array when _sourceListModel is null', () => {
90+
instance._sourceListModel = null;
91+
92+
expect(instance.getLayers()).toEqual([]);
93+
});
94+
});
95+
});
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { createMapClassExtending } from '../../../src/common/mapping/MapBase';
2+
3+
describe('WebMapBase - MapBase Methods', () => {
4+
let MapBase;
5+
let instance;
6+
7+
beforeEach(() => {
8+
MapBase = createMapClassExtending();
9+
instance = new MapBase();
10+
});
11+
12+
describe('setLegends', () => {
13+
it('should set _legendList with provided array', () => {
14+
const legends = [
15+
{ layerId: 'layer1', items: [{ color: '#fff', label: 'test' }] },
16+
{ layerId: 'layer2', items: [{ color: '#000' }] }
17+
];
18+
19+
instance.setLegends(legends);
20+
21+
expect(instance._legendList).toEqual(legends);
22+
});
23+
24+
it('should set _legendList to empty array when null is passed', () => {
25+
instance.setLegends(null);
26+
27+
expect(instance._legendList).toEqual([]);
28+
});
29+
30+
it('should set _legendList to empty array when undefined is passed', () => {
31+
instance.setLegends(undefined);
32+
33+
expect(instance._legendList).toEqual([]);
34+
});
35+
});
36+
37+
describe('getLegends', () => {
38+
it('should return _legendList', () => {
39+
const legends = [{ layerId: 'layer1' }];
40+
instance._legendList = legends;
41+
42+
expect(instance.getLegends()).toEqual(legends);
43+
});
44+
45+
it('should return empty array when _legendList is not set', () => {
46+
expect(instance.getLegends()).toEqual([]);
47+
});
48+
});
49+
50+
describe('getLayerCatalog', () => {
51+
it('should return _sourceListModel.getLayerCatalog() when _sourceListModel exists', () => {
52+
const catalogs = [{ id: 'cat1' }];
53+
instance._sourceListModel = jasmine.createSpyObj('sourceListModel', ['getLayerCatalog']);
54+
instance._sourceListModel.getLayerCatalog.and.returnValue(catalogs);
55+
56+
expect(instance.getLayerCatalog()).toEqual(catalogs);
57+
});
58+
59+
it('should return empty array when _sourceListModel is null', () => {
60+
instance._sourceListModel = null;
61+
62+
expect(instance.getLayerCatalog()).toEqual([]);
63+
});
64+
});
65+
66+
describe('getLayers', () => {
67+
it('should return _sourceListModel.getLayers() when _sourceListModel exists', () => {
68+
const layers = [{ id: 'layer1' }];
69+
instance._sourceListModel = jasmine.createSpyObj('sourceListModel', ['getLayers']);
70+
instance._sourceListModel.getLayers.and.returnValue(layers);
71+
72+
expect(instance.getLayers()).toEqual(layers);
73+
});
74+
75+
it('should return empty array when _sourceListModel is null', () => {
76+
instance._sourceListModel = null;
77+
78+
expect(instance.getLayers()).toEqual([]);
79+
});
80+
});
81+
});

test/test-main-common.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,12 @@ import './common/util/GeometryAnalysisSpec.js';
228228

229229
import './common/mapping/utils/AppreciableLayerBaseSpec.js'
230230
import './common/mapping/utils/L7LayerUtilSpec';
231+
import './common/mapping/MapBaseSpec.js'
231232
import './common/mapping/utils/ColorUtilSpec.js';
232233
import './common/mapping/utils/SourceListModelV2Spec.js';
233234
import './common/mapping/utils/SourceListModelV3Spec.js';
234235
import './common/mapping/utils/epsgDefineSpec.js';
235236
import './common/mapping/utils/UtilSpec.js';
237+
import './common/mapping/WebMapBaseSpec.js';
236238
import './common/mapping/WebMapServiceSpec';
237239
import './common/mapping/WebMapV2BaseSpec';

0 commit comments

Comments
 (0)