Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG FIX Don't add existing layers again #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

erikvullings
Copy link

BUG: I've noticed that, when removing a layer, the remaining layers were added again. As they are added on top of each other, you cannot see it, but you can check it in your DOM tree.

FIX: The problem occurs in leaflet.js, in the attachLayers function. It doesn't check whether an existing layer is already attached. All I needed to do was to change (line 161)

    if (this.layers.hasOwnProperty('base')) {
      for (let layer of this.layers.base) {
        layersToAttach.base[this.getLayerId(layer)] = this.layerFactory.getLayer(layer);
      }
    }
    if (this.layers.hasOwnProperty('overlay')) {
      for (let layer of this.layers.overlay) {
        layersToAttach.overlay[this.getLayerId(layer)] = this.layerFactory.getLayer(layer);
      }
    }

into

    if (this.layers.hasOwnProperty('base')) {
      for (let layer of this.layers.base) {
        const id = this.getLayerId(layer);
        if (this.attachedLayers.base.hasOwnProperty(id)) { continue; }
        layersToAttach.base[id] = this.layerFactory.getLayer(layer);
      }
    }
    if (this.layers.hasOwnProperty('overlay')) {
      for (let layer of this.layers.overlay) {
        const id = this.getLayerId(layer);
        if (this.attachedLayers.overlay.hasOwnProperty(id)) { continue; }
        layersToAttach.overlay[this.getLayerId(layer)] = this.layerFactory.getLayer(layer);
      }
    }

BUG: I've noticed that, when removing a layer, the remaining layers were added again. As they are added on top of each other, you cannot see it, but you can check it in your DOM tree. 

FIX: The problem occurs in leaflet.js, in the attachLayers function. It doesn't check whether an existing layer is already attached. All I needed to do was to change (line 161)
```js
    if (this.layers.hasOwnProperty('base')) {
      for (let layer of this.layers.base) {
        layersToAttach.base[this.getLayerId(layer)] = this.layerFactory.getLayer(layer);
      }
    }
    if (this.layers.hasOwnProperty('overlay')) {
      for (let layer of this.layers.overlay) {
        layersToAttach.overlay[this.getLayerId(layer)] = this.layerFactory.getLayer(layer);
      }
    }
```
into
```ts
    if (this.layers.hasOwnProperty('base')) {
      for (let layer of this.layers.base) {
        const id = this.getLayerId(layer);
        if (this.attachedLayers.base.hasOwnProperty(id)) { continue; }
        layersToAttach.base[id] = this.layerFactory.getLayer(layer);
      }
    }
    if (this.layers.hasOwnProperty('overlay')) {
      for (let layer of this.layers.overlay) {
        const id = this.getLayerId(layer);
        if (this.attachedLayers.overlay.hasOwnProperty(id)) { continue; }
        layersToAttach.overlay[this.getLayerId(layer)] = this.layerFactory.getLayer(layer);
      }
    }
```
@benib benib self-assigned this Oct 17, 2017
@@ -160,12 +160,16 @@ export class LeafletCustomElement {
};
if (this.layers.hasOwnProperty('base')) {
for (let layer of this.layers.base) {
layersToAttach.base[this.getLayerId(layer)] = this.layerFactory.getLayer(layer);
const id = this.getLayerId(layer);
if (this.attachedLayers.base.hasOwnProperty(id)) { continue; }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you change this to

if (!this.attachedLayers.base....)

So checking if it doesn't have this layer attached already and then add in instead of the continue?
Also: please put the block inside if to it's own line.

Same comment goes for the check for the overlay layers.

@benib
Copy link
Owner

benib commented Nov 18, 2017

@erikvullings Thank you so much for this PR. Just now found the time to look into this. I left one small change request. If you could update your PR I'll happily merge it and release a new version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants