Skip to content

Commit fe97987

Browse files
[fix]mapboxgl.convertFilter
1 parent 445a840 commit fe97987

File tree

3 files changed

+37
-117
lines changed

3 files changed

+37
-117
lines changed

src/common/mapping/WebMapV2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,8 +1387,8 @@ export function createWebMapV2Extending(SuperClass, { MapManager, mapRepo, crsMa
13871387
const labelStyle = layerInfo.labelStyle;
13881388
const properties = features[0] && features[0].properties;
13891389
const textField = labelStyle.labelField.replace(/{(.+)}/g, '$1');
1390-
if (!properties || !properties[textField]) {
1391-
return;
1390+
if (!properties || !Object.prototype.hasOwnProperty.call(properties, textField)) {
1391+
return;
13921392
}
13931393
let { backgroundFill = [255, 255, 255, 0] } = labelStyle;
13941394
const fontFamily = labelStyle.fontFamily;

src/common/mapping/WebMapV3.js

Lines changed: 11 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -336,31 +336,6 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
336336
}
337337
return epsgCode;
338338
}
339-
_getFilterByCatalog(catalog, res = {}) {
340-
const { catalogType, children } = catalog;
341-
if(catalogType === 'group' && children) {
342-
children.forEach(child => {
343-
this._getFiltersByCatalog(child, res);
344-
})
345-
}
346-
if (catalogType === 'layer') {
347-
const { filter, layersContent = [] } = catalog;
348-
if (filter) {
349-
layersContent.forEach(layerId => {
350-
res[layerId] = filter;
351-
})
352-
}
353-
}
354-
}
355-
_getFiltersByCatalog(_mapResourceInfo = this._mapResourceInfo) {
356-
const { catalogs = [] } = _mapResourceInfo;
357-
const res = [];
358-
catalogs.forEach((item) => {
359-
this._getFilterByCatalog(item, res);
360-
})
361-
return res;
362-
}
363-
364339
/**
365340
* @private
366341
* @function WebMapV3.prototype._initLayers
@@ -375,8 +350,7 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
375350
};
376351
if (this._relatedInfo.projectInfo) {
377352
this._mapResourceInfo = JSON.parse(this._relatedInfo.projectInfo);
378-
const catalogFilters = this._getFiltersByCatalog();
379-
this._changeMapInfoFilter(catalogFilters);
353+
this._changeMapInfoFilter();
380354
}
381355
this._createMapRelatedInfo();
382356
this._addLayersToMap();
@@ -389,8 +363,7 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
389363
description: relatedInfo.description
390364
};
391365
this._mapResourceInfo = JSON.parse(relatedInfo.projectInfo);
392-
const catalogFilters = this._getFiltersByCatalog();
393-
this._changeMapInfoFilter(catalogFilters);
366+
this._changeMapInfoFilter();
394367
this._createMapRelatedInfo();
395368
this._addLayersToMap();
396369
})
@@ -404,73 +377,19 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
404377
* @function WebMapV3.prototype._changeMapInfoFilter
405378
* @description 更新地图图层的过滤器, 将filter的内容 ['==', 'Ctype', '']转换为['==', ['get', 'Ctype'], 'label']
406379
*/
407-
_changeMapInfoFilter(catalogFilters = {}) {
408-
const { layers =[]} = this._mapInfo;
409-
layers.forEach(layer => {
410-
if (layer.filter && catalogFilters[layer.id]) {
411-
const catalogFilter = catalogFilters[layer.id];
412-
const matchKeys = this._collectMatchKeys(catalogFilter);
413-
const filter = this._transformFilterByMatchKeys(layer.filter, matchKeys);
414-
layer.filter = filter;
380+
_changeMapInfoFilter() {
381+
if (mapRepo && mapRepo.convertFilter) {
382+
const { layers = [] } = this._mapInfo;
383+
layers.forEach((layer) => {
384+
if (layer.filter) {
385+
layer.filter = mapRepo.convertFilter(layer.filter);
415386
}
416-
})
417-
this._mapInfo ={
387+
});
388+
this._mapInfo = {
418389
...this._mapInfo,
419390
layers
420-
}
421-
}
422-
423-
_collectMatchKeys(filter) {
424-
const keys = [];
425-
const excludeKeys = ['$type', '$id', '$layer'];
426-
if (!Array.isArray(filter)) {return keys;}
427-
const traverse = (arr) => {
428-
for (const item of arr) {
429-
if (!Array.isArray(item)) {continue;}
430-
if (item.length >= 3 && this._isComparisonOperator(item[0])) {
431-
const prop = this._getPropertyKey(item[1]);
432-
if (prop && !excludeKeys.includes(prop)) {
433-
keys.push(prop);
434-
continue;
435-
}
436-
if (typeof item[1] === 'string' && !excludeKeys.includes(item[1])) {
437-
keys.push(item[1]);
438-
}
439-
} else {
440-
traverse(item);
441-
}
442-
}
443-
};
444-
traverse(filter);
445-
return [...new Set(keys)];
446-
}
447-
448-
_getPropertyKey(item) {
449-
if (Array.isArray(item) && item.length === 2 && item[0] === 'get' && typeof item[1] === 'string') {
450-
return item[1];
451-
}
452-
return null;
453-
}
454-
455-
_transformFilterByMatchKeys(filter, matchKeys) {
456-
if (!Array.isArray(filter)) {
457-
return filter;
458-
}
459-
if (filter.length >= 3 && typeof filter[1] === 'string' && this._isComparisonOperator(filter[0])) {
460-
if (matchKeys.includes(filter[1])) {
461-
return [filter[0], ['get', filter[1]], ...filter.slice(2)];
462-
}
463-
return filter;
464-
}
465-
if (filter.length >= 2 && typeof filter[1] !== 'string' && this._isComparisonOperator(filter[0])) {
466-
const operands = filter.slice(1).map(item => this._transformFilterByMatchKeys(item, matchKeys));
467-
return [filter[0], ...operands];
391+
};
468392
}
469-
return filter.map(item => this._transformFilterByMatchKeys(item, matchKeys));
470-
}
471-
472-
_isComparisonOperator(op) {
473-
return ['==', '!=', '>', '<', '>=', '<=', 'in', '!in', 'all', 'any', 'none'].includes(op);
474393
}
475394

476395
/**

test/mapboxgl/mapping/WebMapV3Spec.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -177,24 +177,28 @@ describe('mapboxgl-webmap3.0', () => {
177177
}
178178
return Promise.resolve();
179179
});
180+
// const Spy = spyOn(mapboxgl, 'convertFilter').and.callThrough();
181+
const Spy = jasmine.createSpy('myMock');
182+
Spy.and.callFake((arg) => arg);
183+
mapboxgl.convertFilter = Spy
180184
mapstudioWebmap = new WebMap('932266699', {
181185
server: server
182186
});
183-
184187
mapstudioWebmap.on('mapcreatesucceeded', ({ map }) => {
185188
expect(map).not.toBeUndefined();
186189
expect(mapstudioWebmap.map).toEqual(map);
187-
const webMapV3 = mapstudioWebmap._getWebMapInstance();
188-
expect(webMapV3._mapInfo.layers[1].filter).toEqual([
189-
'all',
190-
['all', ['==', ['get', 'Ctype'], ''], ['!=', ['get', 'smpid'], '']],
191-
['all', ['all', ['all', ['all', ['!=', ['get', 'smpid'], 121]]]]]
192-
]);
193-
expect(webMapV3._mapInfo.layers[2].filter).toEqual([
194-
'all',
195-
['all', ['==', ['get', 'Ctype'], ''], ['!=', ['get', 'smpid'], '']],
196-
['all', ['all', ['all', ['any', ['==', ['get', 'smpid'], 121]]]]]
197-
]);
190+
expect(Spy).toHaveBeenCalledTimes(2);
191+
// const webMapV3 = mapstudioWebmap._getWebMapInstance();
192+
// expect(webMapV3._mapInfo.layers[1].filter).toEqual([
193+
// 'all',
194+
// ['all', ['==', ['get', 'Ctype'], ''], ['!=', ['get', 'smpid'], '']],
195+
// ['all', ['all', ['all', ['all', ['!=', ['get', 'smpid'], 121]]]]]
196+
// ]);
197+
// expect(webMapV3._mapInfo.layers[2].filter).toEqual([
198+
// 'all',
199+
// ['all', ['==', ['get', 'Ctype'], ''], ['!=', ['get', 'smpid'], '']],
200+
// ['all', ['all', ['all', ['any', ['==', ['get', 'smpid'], 121]]]]]
201+
// ]);
198202
done();
199203
});
200204
});
@@ -206,27 +210,24 @@ describe('mapboxgl-webmap3.0', () => {
206210
return Promise.resolve();
207211
});
208212
const mapInfo = JSON.parse(mapstudioWebMap_filters);
213+
// const Spy = spyOn(mapboxgl, 'convertFilter').and.callThrough();
214+
const Spy = jasmine.createSpy('myMock');
215+
Spy.and.callFake((arg) => arg);
216+
mapboxgl.convertFilter = Spy
217+
209218
mapstudioWebmap = new WebMapV3(mapInfo, {
210219
server: server,
211220
target: 'map',
212221
iportalServiceProxyUrlPrefix: 'mapId is JSON',
213222
relatedInfo: JSON.parse(msProjectINfo_filters)
214223
});
215224
mapstudioWebmap.initializeMap(mapInfo);
216-
217225
mapstudioWebmap.on('mapcreatesucceeded', ({ map }) => {
218226
expect(map).not.toBeUndefined();
219227
expect(mapstudioWebmap.map).toEqual(map);
220-
expect(mapstudioWebmap._mapInfo.layers[1].filter).toEqual([
221-
'all',
222-
['all', ['==', ['get', 'Ctype'], ''], ['!=', ['get', 'smpid'], '']],
223-
['all', ['all', ['all', ['all', ['!=', ['get', 'smpid'], 121]]]]]
224-
]);
225-
expect(mapstudioWebmap._mapInfo.layers[2].filter).toEqual([
226-
'all',
227-
['all', ['==', ['get', 'Ctype'], ''], ['!=', ['get', 'smpid'], '']],
228-
['all', ['all', ['all', ['any', ['==', ['get', 'smpid'], 121]]]]]
229-
]);
228+
expect(Spy).toHaveBeenCalledTimes(2);
229+
const result2 = mapstudioWebmap._getPopupInfos({});
230+
expect(result2).toEqual([]);
230231
done();
231232
});
232233
});

0 commit comments

Comments
 (0)