Skip to content

Commit c41683d

Browse files
committed
Add reRender() method for map-feature - untested (for discussion)
1 parent face641 commit c41683d

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

src/map-feature.js

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,25 @@ export class MapFeature extends HTMLElement {
7070
attributeChangedCallback(name, oldValue, newValue) {
7171
switch (name) {
7272
case 'zoom': {
73-
if (oldValue !== newValue && this._layer) {
74-
let layer = this._layer,
75-
layerEl = layer._layerEl,
76-
mapmlvectors = layer._mapmlvectors;
77-
// if the vector layer only has static features, should update zoom bounds when zoom attribute is changed
78-
if (mapmlvectors?._staticFeature) {
79-
this._removeInFeatureList(oldValue);
80-
let native = this._getNativeZoomAndCS(layer._content);
81-
mapmlvectors.zoomBounds = M.getZoomBounds(
82-
layer._content,
83-
native.zoom
84-
);
73+
this.whenReady().then(() => {
74+
if (oldValue !== newValue) {
75+
let layer = this._layer,
76+
layerEl = layer._layerEl,
77+
mapmlvectors = layer._mapmlvectors;
78+
// if the vector layer only has static features, should update zoom bounds when zoom attribute is changed
79+
if (mapmlvectors?._staticFeature) {
80+
this._removeInFeatureList(oldValue);
81+
let native = this._getNativeZoomAndCS(layer._content);
82+
mapmlvectors.zoomBounds = M.getZoomBounds(
83+
layer._content,
84+
native.zoom
85+
);
86+
}
87+
//this._removeFeature();
88+
//this._updateFeature();
89+
this._reRender();
8590
}
86-
this._removeFeature();
87-
this._updateFeature();
88-
}
91+
});
8992
break;
9093
}
9194
}
@@ -140,6 +143,21 @@ export class MapFeature extends HTMLElement {
140143
this._observer.disconnect();
141144
}
142145

146+
_reRender() {
147+
if (this._groupEl.isConnected) {
148+
let native = this._getNativeZoomAndCS(this._layer._content);
149+
let placeholder = document.createElement('span');
150+
this._groupEl.insertAdjacentElement('beforebegin', placeholder);
151+
152+
this._featureGroup._map.removeLayer(this._featureGroup);
153+
// Garbage collection needed
154+
this._featureGroup = this._layer._mapmlvectors
155+
.addData(this, native.cs, native.zoom)
156+
.addTo(this._map);
157+
placeholder.replaceWith(this._featureGroup.options.group);
158+
}
159+
}
160+
143161
_removeFeature() {
144162
// if the <layer- > el is disconnected
145163
// the <g> el has already got removed at this point
@@ -424,7 +442,11 @@ export class MapFeature extends HTMLElement {
424442

425443
// find and remove the feature from mapmlvectors._features if vector layer only contains static features, helper function
426444
// prevent it from being rendered again when zooming in / out (mapmlvectors.resetFeature() is invoked)
445+
// TODO: Can be moved to FeatureLayer.js, pass in leaflet id for layer to remove
427446
_removeInFeatureList(zoom) {
447+
if (zoom === null) {
448+
return;
449+
}
428450
let mapmlvectors = this._layer._mapmlvectors;
429451
for (let i = 0; i < mapmlvectors._features[zoom].length; ++i) {
430452
let feature = mapmlvectors._features[zoom][i];

0 commit comments

Comments
 (0)