Skip to content

Commit

Permalink
Change how opacity is handled by layer - add _opacity cache that
Browse files Browse the repository at this point in the history
gets updated by layer control slider and layer- setter.
  • Loading branch information
prushforth committed Sep 19, 2023
1 parent d83a65d commit 5105086
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
27 changes: 9 additions & 18 deletions src/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class MapLayer extends HTMLElement {
}

get opacity() {
return this._layer._container.style.opacity || this._layer.options.opacity;
return this._opacity;
}

set opacity(val) {
Expand Down Expand Up @@ -126,9 +126,6 @@ export class MapLayer extends HTMLElement {
{ once: true }
);
let base = this.baseURI ? this.baseURI : document.baseURI;
let opacity_value = this.hasAttribute('opacity')
? this.getAttribute('opacity')
: '1.0';

const headers = new Headers();
headers.append('Accept', 'text/mapml');
Expand Down Expand Up @@ -157,7 +154,7 @@ export class MapLayer extends HTMLElement {
content,
{
mapprojection: this.parentElement.projection,
opacity: opacity_value
opacity: this.opacity
}
);
resolve();
Expand All @@ -171,7 +168,7 @@ export class MapLayer extends HTMLElement {
}
this._layer = M.mapMLLayer(null, this, null, {
mapprojection: this.parentElement.projection,
opacity: opacity_value
opacity: this.opacity
});
resolve();
}
Expand All @@ -184,6 +181,7 @@ export class MapLayer extends HTMLElement {
if (e.type === 'changeprojection') {
this.src = e.detail.href;
} else {
console.log(e);
this.dispatchEvent(
new CustomEvent('error', { detail: { target: this } })
);
Expand Down Expand Up @@ -292,28 +290,21 @@ export class MapLayer extends HTMLElement {
break;
case 'opacity':
if (oldValue !== newValue && this._layer) {
this._opacity = newValue;
this._layer.changeOpacity(newValue);
}
break;
case 'src':
if (oldValue !== newValue && this._layer) {
this._reload();
this._onRemove();
if (this.isConnected) {
this._onAdd();
}
// the original inline content will not be removed
// but has NO EFFECT and works as a fallback
}
}
}
// re-load the layer element when the src attribute is changed
_reload() {
let oldOpacity = this.opacity;
// go through the same sequence as if the layer had been removed from
// the DOM and re-attached with a new URL source.
this._onRemove();
if (this.isConnected) {
this._onAdd();
}
this.opacity = oldOpacity;
}
_validateDisabled() {
setTimeout(() => {
let layer = this._layer,
Expand Down
2 changes: 1 addition & 1 deletion src/mapml/layers/MapMLLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export var MapMLLayer = L.Layer.extend({
},
changeOpacity: function (opacity) {
this._container.style.opacity = opacity;
this._layerEl.opacity = opacity;
this._layerEl._opacity = opacity;
if (this.opacityEl) this.opacityEl.value = opacity;
},
_changeExtentOpacity: function (e) {
Expand Down

0 comments on commit 5105086

Please sign in to comment.