Skip to content

Commit 3107025

Browse files
【fix】修复专题图层要素无法删除 review by luox
1 parent 46518c6 commit 3107025

File tree

10 files changed

+128
-95
lines changed

10 files changed

+128
-95
lines changed

src/leaflet/overlay/GraphThemeLayer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,12 @@ export var GraphThemeLayer = ThemeLayer.extend({
311311
/**
312312
* @function L.supermap.GraphThemeLayer.prototype.removeFeatures
313313
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素(数据)。
314-
* @param {Array.<SuperMap.Feature.Vector>} features - 待删除的要素
314+
* @param {(Array.<SuperMap.Feature.Vector>|Function)} features - 待删除的要素或用于过滤的回调函数
315315
*/
316316
removeFeatures: function (features) { // eslint-disable-line no-unused-vars
317317
var me = this;
318318
me.clearCache();
319-
ThemeLayer.prototype.removeFeatures.apply(me, arguments);
319+
ThemeLayer.prototype.removeFeatures.call(me, features);
320320
},
321321

322322
/**

src/leaflet/overlay/LabelThemeLayer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ export var LabelThemeLayer = GeoFeatureThemeLayer.extend({
149149
/**
150150
* @function L.supermap.LabelThemeLayer.prototype.removeFeatures
151151
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素。参数中的 features 数组中的每一项,必须是已经添加到当前图层中的 feature。
152-
* @param {Array.<SuperMap.Feature.Vector>} features - 要删除的要素
152+
* @param {(Array.<SuperMap.Feature.Vector>|Function)} features - 要删除的要素或用于过滤的回调函数
153153
*/
154154
removeFeatures: function (features) { // eslint-disable-line no-unused-vars
155155
this.labelFeatures = [];
156-
GeoFeatureThemeLayer.prototype.removeFeatures.call(this, arguments);
156+
GeoFeatureThemeLayer.prototype.removeFeatures.call(this, features);
157157
},
158158

159159
/**

src/leaflet/overlay/theme/GeoFeatureThemeLayer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ export var GeoFeatureThemeLayer = ThemeLayer.extend({
101101
/**
102102
* @function L.supermap.GeoFeatureThemeLayer.prototype.removeFeatures
103103
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素。参数中的 features 数组中的每一项,必须是已经添加到当前图层中的 feature。
104-
* @param {SuperMap.Feature.Vector} features - 要删除的要素
104+
* @param {(SuperMap.Feature.Vector|Function)} features - 要删除的要素或用于过滤的回调函数
105105
*/
106106
removeFeatures: function (features) { // eslint-disable-line no-unused-vars
107107
this.clearCache();
108-
ThemeLayer.prototype.removeFeatures.call(this, arguments);
108+
ThemeLayer.prototype.removeFeatures.call(this, features);
109109
},
110110

111111
/**

src/leaflet/overlay/theme/ThemeLayer.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,34 +159,39 @@ export var ThemeLayer = L.Layer.extend({
159159
/**
160160
* @function L.supermap.ThemeLayer.prototype.removeFeatures
161161
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素。
162-
* @param {Array.<SuperMap.Feature.Vector>} features - 将被删除的要素
162+
* @param {(Array.<SuperMap.Feature.Vector>|Function)} features - 将被删除的要素或用来过滤的回调函数
163163
*/
164164
removeFeatures: function (features) {
165165
var me = this;
166-
if (!features || features.length === 0) {
166+
if (!features) {
167167
return;
168168
}
169169
if (features === me.features) {
170170
return me.removeAllFeatures();
171171
}
172-
if (!(L.Util.isArray(features))) {
172+
if (!L.Util.isArray(features) && !typeof features === 'function') {
173173
features = [features];
174174
}
175175

176176
var featuresFailRemoved = [];
177177

178-
for (var i = features.length - 1; i >= 0; i--) {
179-
var feature = features[i];
178+
for (var i = 0; i < me.features.length; i++) {
179+
var feature = me.features[i];
180180

181181
//如果我们传入的feature在features数组中没有的话,则不进行删除,
182182
//并将其放入未删除的数组中。
183-
var findex = L.Util.indexOf(me.features, feature);
184-
185-
if (findex === -1) {
186-
featuresFailRemoved.push(feature);
187-
continue;
183+
if (features && typeof features === 'function') {
184+
if (features(feature)) {
185+
me.features.splice(i--, 1);
186+
}
187+
} else {
188+
var findex = L.Util.indexOf(features, feature);
189+
if (findex === -1) {
190+
featuresFailRemoved.push(feature);
191+
} else {
192+
me.features.splice(i--, 1);
193+
}
188194
}
189-
me.features.splice(findex, 1);
190195
}
191196

192197
var drawFeatures = [];
@@ -237,14 +242,16 @@ export var ThemeLayer = L.Layer.extend({
237242
/**
238243
* @function L.supermap.ThemeLayer.prototype.getFeatures
239244
* @description 查看当前图层中的有效数据。
245+
* @param {Function} [filter] - 根据条件过滤要素的回调函数。
240246
* @returns {Array} 返回图层中的有效数据。
241247
*/
242-
getFeatures: function () {
243-
var me = this;
244-
var len = me.features.length;
245-
var clonedFeatures = new Array(len);
248+
getFeatures: function (filter) {
249+
var len = this.features.length;
250+
var clonedFeatures = [];
246251
for (var i = 0; i < len; ++i) {
247-
clonedFeatures[i] = me.features[i];
252+
if (!filter || (filter && typeof filter === 'function' && filter(this.features[i]))) {
253+
clonedFeatures.push(this.features[i]);
254+
}
248255
}
249256
return clonedFeatures;
250257
},

src/mapboxgl/overlay/LabelThemeLayer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ export class Label extends GeoFeature {
128128
/**
129129
* @function mapboxgl.supermap.LabelThemeLayer.prototype.removeFeatures
130130
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素。
131-
* @param {SuperMap.Feature.Vector} features - 要删除的要素对象
131+
* @param {(SuperMap.Feature.Vector|Function)} features - 要删除的要素对象或用于过滤的回调函数
132132
*/
133133
removeFeatures(features) { // eslint-disable-line no-unused-vars
134134
this.labelFeatures = [];
135-
super.removeFeatures.call(this, arguments);
135+
super.removeFeatures.call(this, features);
136136
}
137137

138138
/**

src/mapboxgl/overlay/theme/GeoFeatureThemeLayer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ export class GeoFeature extends Theme {
113113
/**
114114
* @function mapboxgl.supermap.GeoFeatureThemeLayer.prototype.removeFeatures
115115
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素。
116-
* @param {SuperMap.Feature.Vector} features - 要删除的要素对象
116+
* @param {(SuperMap.Feature.Vector|Function)} features - 要删除的要素对象或用于过滤的回调函数
117117
*/
118118
removeFeatures(features) { // eslint-disable-line no-unused-vars
119119
this.clearCache();
120-
Theme.prototype.removeFeatures.apply(this, arguments);
120+
Theme.prototype.removeFeatures.call(this, features);
121121
}
122122

123123
/**

src/mapboxgl/overlay/theme/ThemeLayer.js

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -212,54 +212,65 @@ export class Theme {
212212

213213
/**
214214
* @function mapboxgl.supermap.ThemeLayer.prototype.removeFeatures
215-
* @param {Array.<SuperMap.Feature.Vector>} features - 要删除 feature 的数组
215+
* @param {(Array.<SuperMap.Feature.Vector>|Function)} features - 要删除 feature 的数组或用来过滤的回调函数
216216
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素。
217217
* 参数中的 features 数组中的每一项,必须是已经添加到当前图层中的 feature,
218218
* 如果无法确定 feature 数组,则可以调用 removeAllFeatures 来删除所有 feature。
219219
* 如果要删除的 feature 数组中的元素特别多,推荐使用 removeAllFeatures,
220220
* 删除所有 feature 后再重新添加。这样效率会更高。
221221
*/
222222
removeFeatures(features) {
223-
if (!features || features.length === 0) {
224-
return;
225-
}
226-
if (features === this.features) {
227-
return this.removeAllFeatures();
228-
}
229-
if (!(CommonUtil.isArray(features))) {
230-
features = [features];
231-
}
232-
var featuresFailRemoved = [];
233-
for (var i = features.length - 1; i >= 0; i--) {
234-
var feature = features[i];
235-
//如果我们传入的feature在features数组中没有的话,则不进行删除,
236-
//并将其放入未删除的数组中。
237-
var findex = CommonUtil.indexOf(this.features, feature);
223+
var me = this;
224+
if (!features) {
225+
return;
226+
}
227+
if (features === me.features) {
228+
return me.removeAllFeatures();
229+
}
230+
if (!CommonUtil.isArray(features) && !typeof features === 'function') {
231+
features = [features];
232+
}
233+
234+
var featuresFailRemoved = [];
235+
236+
for (var i = 0; i < me.features.length; i++) {
237+
var feature = me.features[i];
238+
239+
//如果我们传入的feature在features数组中没有的话,则不进行删除,
240+
//并将其放入未删除的数组中。
241+
if (features && typeof features === 'function') {
242+
if (features(feature)) {
243+
me.features.splice(i--, 1);
244+
}
245+
} else {
246+
var findex = CommonUtil.indexOf(features, feature);
238247
if (findex === -1) {
239248
featuresFailRemoved.push(feature);
240-
continue;
249+
} else {
250+
me.features.splice(i--, 1);
241251
}
242-
this.features.splice(findex, 1);
243-
}
244-
var drawFeatures = [];
245-
for (var hex = 0, len = this.features.length; hex < len; hex++) {
246-
feature = this.features[hex];
247-
drawFeatures.push(feature);
248-
}
249-
this.features = [];
250-
this.addFeatures(drawFeatures);
251-
//绘制专题要素
252-
if (this.renderer) {
253-
this.redrawThematicFeatures(this.map.getBounds());
254-
}
255-
var succeed = featuresFailRemoved.length == 0 ? true : false;
256-
/**
257-
* @event mapboxgl.supermap.ThemeLayer#featuresremoved
258-
* @description 要素删除之后触发。
259-
* @property {Array.<SuperMap.Feature.Vector>} features - 未被成功删除的要素。
260-
* @property {boolean} succeed - 删除成功与否。
261-
*/
262-
mapboxgl.Evented.prototype.fire("featuresremoved", {features: featuresFailRemoved, succeed: succeed});
252+
}
253+
}
254+
255+
var drawFeatures = [];
256+
for (var hex = 0, len = this.features.length; hex < len; hex++) {
257+
feature = this.features[hex];
258+
drawFeatures.push(feature);
259+
}
260+
this.features = [];
261+
this.addFeatures(drawFeatures);
262+
//绘制专题要素
263+
if (this.renderer) {
264+
this.redrawThematicFeatures(this.map.getBounds());
265+
}
266+
var succeed = featuresFailRemoved.length == 0 ? true : false;
267+
/**
268+
* @event mapboxgl.supermap.ThemeLayer#featuresremoved
269+
* @description 要素删除之后触发。
270+
* @property {Array.<SuperMap.Feature.Vector>} features - 未被成功删除的要素。
271+
* @property {boolean} succeed - 删除成功与否。
272+
*/
273+
mapboxgl.Evented.prototype.fire("featuresremoved", {features: featuresFailRemoved, succeed: succeed});
263274
}
264275

265276
/**
@@ -277,13 +288,16 @@ export class Theme {
277288
/**
278289
* @function mapboxgl.supermap.ThemeLayer.prototype.getFeatures
279290
* @description 查看当前图层中的有效数据。
291+
* @param {Function} [filter] - 根据条件过滤要素的回调函数。
280292
* @returns {SuperMap.Feature.Vector} 用户加入图层的有效数据。
281293
*/
282-
getFeatures() {
283-
var len = this.features.length;
284-
var clonedFeatures = new Array(len);
294+
getFeatures(filter) {
295+
var len = this.features.length;
296+
var clonedFeatures = [];
285297
for (var i = 0; i < len; ++i) {
286-
clonedFeatures[i] = this.features[i];
298+
if (!filter || (filter && typeof filter === 'function' && filter(this.features[i]))) {
299+
clonedFeatures.push(this.features[i]);
300+
}
287301
}
288302
return clonedFeatures;
289303
}

src/openlayers/overlay/Label.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ export class Label extends GeoFeature {
141141
/**
142142
* @function ol.source.Label.prototype.removeFeatures
143143
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素。
144-
* @param {SuperMap.Feature.Vector} features - 要删除的要素对象
144+
* @param {(SuperMap.Feature.Vector|Function)} features - 要删除的要素对象或用于过滤的回调函数
145145
*/
146146
removeFeatures(features) { // eslint-disable-line no-unused-vars
147147
this.labelFeatures = [];
148-
super.removeFeatures.call(this, arguments);
148+
super.removeFeatures.call(this, features);
149149
}
150150

151151
/**

src/openlayers/overlay/theme/GeoFeature.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ export class GeoFeature extends Theme {
8282
/**
8383
* @function ol.source.GeoFeature.prototype.removeFeatures
8484
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素。
85-
* @param {SuperMap.Feature.Vector} features - 要删除的要素对象
85+
* @param {(SuperMap.Feature.Vector|Function)} features - 要删除的要素对象或用于过滤的回调函数
8686
*/
8787
removeFeatures(features) { // eslint-disable-line no-unused-vars
8888
this.clearCache();
89-
Theme.prototype.removeFeatures.apply(this, arguments);
89+
Theme.prototype.removeFeatures.call(this, features);
9090
}
9191

9292
/**

src/openlayers/overlay/theme/Theme.js

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -195,35 +195,45 @@ export class Theme extends ImageCanvasSource {
195195

196196
/**
197197
* @function ol.source.Theme.prototype.removeFeatures
198-
* @param {Array.<SuperMap.Feature.Vector>} features - 要删除 feature 的数组
198+
* @param {(Array.<SuperMap.Feature.Vector>|Function)} features - 要删除 feature 的数组或用来过滤的回调函数
199199
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素。
200200
* 参数中的 features 数组中的每一项,必须是已经添加到当前图层中的 feature,
201201
* 如果无法确定 feature 数组,则可以调用 removeAllFeatures 来删除所有 feature。
202202
* 如果要删除的 feature 数组中的元素特别多,推荐使用 removeAllFeatures,
203203
* 删除所有 feature 后再重新添加。这样效率会更高。
204204
*/
205205
removeFeatures(features) {
206-
if (!features || features.length === 0) {
207-
return;
208-
}
209-
if (features === this.features) {
210-
return this.removeAllFeatures();
211-
}
212-
if (!(CommonUtil.isArray(features))) {
213-
features = [features];
214-
}
215-
var featuresFailRemoved = [];
216-
for (var i = features.length - 1; i >= 0; i--) {
217-
var feature = features[i];
218-
//如果我们传入的feature在features数组中没有的话,则不进行删除,
219-
//并将其放入未删除的数组中。
220-
var findex = CommonUtil.indexOf(this.features, feature);
206+
var me = this;
207+
if (!features) {
208+
return;
209+
}
210+
if (features === me.features) {
211+
return me.removeAllFeatures();
212+
}
213+
if (!CommonUtil.isArray(features) && !typeof features === 'function') {
214+
features = [features];
215+
}
216+
217+
var featuresFailRemoved = [];
218+
219+
for (var i = 0; i < me.features.length; i++) {
220+
var feature = me.features[i];
221+
222+
//如果我们传入的feature在features数组中没有的话,则不进行删除,
223+
//并将其放入未删除的数组中。
224+
if (features && typeof features === 'function') {
225+
if (features(feature)) {
226+
me.features.splice(i--, 1);
227+
}
228+
} else {
229+
var findex = CommonUtil.indexOf(features, feature);
221230
if (findex === -1) {
222231
featuresFailRemoved.push(feature);
223-
continue;
232+
} else {
233+
me.features.splice(i--, 1);
224234
}
225-
this.features.splice(findex, 1);
226-
}
235+
}
236+
}
227237
var drawFeatures = [];
228238
for (var hex = 0, len = this.features.length; hex < len; hex++) {
229239
feature = this.features[hex];
@@ -254,14 +264,16 @@ export class Theme extends ImageCanvasSource {
254264
/**
255265
* @function ol.source.Theme.prototype.getFeatures
256266
* @description 查看当前图层中的有效数据。
267+
* @param {Function} [filter] - 根据条件过滤要素的回调函数。
257268
* @returns {SuperMap.Feature.Vector} 用户加入图层的有效数据。
258269
*/
259-
getFeatures() {
270+
getFeatures(filter) {
260271
var len = this.features.length;
261-
var clonedFeatures = new Array(len);
272+
var clonedFeatures = [];
262273
for (var i = 0; i < len; ++i) {
263-
clonedFeatures[i] = this.features[i];
264-
//clonedFeatures[i] = this.features[i].clone();
274+
if (!filter || (filter && typeof filter === 'function' && filter(this.features[i]))) {
275+
clonedFeatures.push(this.features[i]);
276+
}
265277
}
266278
return clonedFeatures;
267279
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy