Skip to content

Commit 59291b4

Browse files
【fix】修复专题图层要素无法删除 review by luox
1 parent 4d21278 commit 59291b4

File tree

10 files changed

+135
-100
lines changed

10 files changed

+135
-100
lines changed

src/leaflet/overlay/GraphThemeLayer.js

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

325325
/**

src/leaflet/overlay/LabelThemeLayer.js

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

154154
/**

src/leaflet/overlay/theme/GeoFeatureThemeLayer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ export var GeoFeatureThemeLayer = ThemeLayer.extend({
9999
/**
100100
* @function GeoFeatureThemeLayer.prototype.removeFeatures
101101
* @description 删除专题图中 features。参数中的 features 数组中的每一项,必须是已经添加到当前图层中的 feature。
102-
* @param {FeatureVector} features - 待删除的要素
102+
* @param {(FeatureVector|Function)} features - 待删除的要素或用于过滤的回调函数
103103
*/
104104
removeFeatures: function (features) { // eslint-disable-line no-unused-vars
105105
this.clearCache();
106-
ThemeLayer.prototype.removeFeatures.call(this, arguments);
106+
ThemeLayer.prototype.removeFeatures.call(this, features);
107107
},
108108

109109
/**

src/leaflet/overlay/theme/ThemeLayer.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,36 +156,41 @@ export var ThemeLayer = L.Layer.extend({
156156
},
157157

158158
/**
159-
* @function ThemeLayer.prototype.removeFeatures
160-
* @description 删除专题图中 features
161-
* @param {Array.<FeatureVector>} features - 待删除的要素
159+
* @function L.supermap.ThemeLayer.prototype.removeFeatures
160+
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素
161+
* @param {(Array.<SuperMap.Feature.Vector>|Function)} features - 将被删除的要素或用来过滤的回调函数
162162
*/
163163
removeFeatures: function (features) {
164164
var me = this;
165-
if (!features || features.length === 0) {
165+
if (!features) {
166166
return;
167167
}
168168
if (features === me.features) {
169169
return me.removeAllFeatures();
170170
}
171-
if (!(L.Util.isArray(features))) {
171+
if (!L.Util.isArray(features) && !typeof features === 'function') {
172172
features = [features];
173173
}
174174

175175
var featuresFailRemoved = [];
176176

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

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

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

src/mapboxgl/overlay/LabelThemeLayer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ export class Label extends GeoFeature {
125125
/**
126126
* @function LabelThemeLayer.prototype.removeFeatures
127127
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素。
128+
* @param {(SuperMap.Feature.Vector|Function)} features - 要删除的要素对象或用于过滤的回调函数。
128129
*/
129130
removeFeatures() {
130131
this.labelFeatures = [];
131-
super.removeFeatures.call(this, arguments);
132+
super.removeFeatures.call(this, features);
132133
}
133134

134135
/**

src/mapboxgl/overlay/theme/GeoFeatureThemeLayer.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,11 @@ export class GeoFeature extends Theme {
116116
/**
117117
* @function GeoFeatureThemeLayer.prototype.removeFeatures
118118
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素。
119+
* @param {(FeatureVector|Function)} features - 要删除的要素对象或用于过滤的回调函数。
119120
*/
120-
removeFeatures() {
121+
removeFeatures(features) {
121122
this.clearCache();
122-
Theme.prototype.removeFeatures.apply(this, arguments);
123+
Theme.prototype.removeFeatures.call(this, features);
123124
}
124125

125126
/**

src/mapboxgl/overlay/theme/ThemeLayer.js

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -211,52 +211,63 @@ export class Theme {
211211

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

262273
/**
@@ -274,15 +285,18 @@ export class Theme {
274285
/**
275286
* @function ThemeLayer.prototype.getFeatures
276287
* @description 查看当前图层中的有效数据。
288+
* @param {Function} [filter] - 根据条件过滤要素的回调函数。
277289
* @returns {FeatureVector} 用户加入图层的有效数据。
278290
*/
279-
getFeatures() {
280-
var len = this.features.length;
281-
var clonedFeatures = new Array(len);
282-
for (var i = 0; i < len; ++i) {
283-
clonedFeatures[i] = this.features[i];
284-
}
285-
return clonedFeatures;
291+
getFeatures(filter) {
292+
var len = this.features.length;
293+
var clonedFeatures = [];
294+
for (var i = 0; i < len; ++i) {
295+
if (!filter || (filter && typeof filter === 'function' && filter(this.features[i]))) {
296+
clonedFeatures.push(this.features[i]);
297+
}
298+
}
299+
return clonedFeatures;
286300
}
287301

288302
/**

src/openlayers/overlay/Label.js

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

150150
/**

src/openlayers/overlay/theme/GeoFeature.js

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

9595
/**

src/openlayers/overlay/theme/Theme.js

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -193,35 +193,45 @@ export class Theme extends ImageCanvasSource {
193193

194194
/**
195195
* @function ol.source.Theme.prototype.removeFeatures
196-
* @param {Array.<FeatureVector>} features - 要删除 feature 的数组
196+
* @param {(Array.<SuperMap.Feature.Vector>|Function)} features - 要删除 feature 的数组或用来过滤的回调函数
197197
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素。
198198
* 参数中的 features 数组中的每一项,必须是已经添加到当前图层中的 feature,
199199
* 如果无法确定 feature 数组,则可以调用 removeAllFeatures 来删除所有 feature。
200200
* 如果要删除的 feature 数组中的元素特别多,推荐使用 removeAllFeatures,
201201
* 删除所有 feature 后再重新添加。这样效率会更高。
202202
*/
203203
removeFeatures(features) {
204-
if (!features || features.length === 0) {
205-
return;
206-
}
207-
if (features === this.features) {
208-
return this.removeAllFeatures();
209-
}
210-
if (!(CommonUtil.isArray(features))) {
211-
features = [features];
212-
}
213-
var featuresFailRemoved = [];
214-
for (var i = features.length - 1; i >= 0; i--) {
215-
var feature = features[i];
216-
//如果我们传入的feature在features数组中没有的话,则不进行删除,
217-
//并将其放入未删除的数组中。
218-
var findex = CommonUtil.indexOf(this.features, feature);
204+
var me = this;
205+
if (!features) {
206+
return;
207+
}
208+
if (features === me.features) {
209+
return me.removeAllFeatures();
210+
}
211+
if (!CommonUtil.isArray(features) && !typeof features === 'function') {
212+
features = [features];
213+
}
214+
215+
var featuresFailRemoved = [];
216+
217+
for (var i = 0; i < me.features.length; i++) {
218+
var feature = me.features[i];
219+
220+
//如果我们传入的feature在features数组中没有的话,则不进行删除,
221+
//并将其放入未删除的数组中。
222+
if (features && typeof features === 'function') {
223+
if (features(feature)) {
224+
me.features.splice(i--, 1);
225+
}
226+
} else {
227+
var findex = CommonUtil.indexOf(features, feature);
219228
if (findex === -1) {
220229
featuresFailRemoved.push(feature);
221-
continue;
230+
} else {
231+
me.features.splice(i--, 1);
222232
}
223-
this.features.splice(findex, 1);
224-
}
233+
}
234+
}
225235
var drawFeatures = [];
226236
for (var hex = 0, len = this.features.length; hex < len; hex++) {
227237
feature = this.features[hex];
@@ -252,14 +262,16 @@ export class Theme extends ImageCanvasSource {
252262
/**
253263
* @function ol.source.Theme.prototype.getFeatures
254264
* @description 查看当前图层中的有效数据。
255-
* @returns {FeatureVector} 用户加入图层的有效数据。
265+
* @param {Function} [filter] - 根据条件过滤要素的回调函数。
266+
* @returns {SuperMap.Feature.Vector} 用户加入图层的有效数据。
256267
*/
257-
getFeatures() {
268+
getFeatures(filter) {
258269
var len = this.features.length;
259-
var clonedFeatures = new Array(len);
270+
var clonedFeatures = [];
260271
for (var i = 0; i < len; ++i) {
261-
clonedFeatures[i] = this.features[i];
262-
//clonedFeatures[i] = this.features[i].clone();
272+
if (!filter || (filter && typeof filter === 'function' && filter(this.features[i]))) {
273+
clonedFeatures.push(this.features[i]);
274+
}
263275
}
264276
return clonedFeatures;
265277
}

0 commit comments

Comments
 (0)