Skip to content

Commit 7eb3ecf

Browse files
committed
Add preliminary implementation of _changeHandler that invokes _onAdd,
_onRemove. TBD if there need to be custom implementations of those for use by the media query handler alone. Add media query consideration to _validateDisabled
1 parent bb19e81 commit 7eb3ecf

File tree

1 file changed

+24
-50
lines changed

1 file changed

+24
-50
lines changed

src/layer.js

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,15 @@ export class BaseLayerElement extends HTMLElement {
134134
if (!this._changeHandler) {
135135
// Define and bind the change handler once
136136
this._changeHandler = () => {
137-
// TODO figure out how to propagate this correctly, cause there's no
138-
// disabled api per se.
139-
this.disabled = !this._mql.matches;
137+
if (this._mql.matches) {
138+
// TODO evaluate if _onAdd does the right thing for this situation
139+
this._onAdd();
140+
} else {
141+
// TODO evaluate if _onRemove does the right thing for this situation
142+
this._onRemove();
143+
}
144+
// set the disabled 'read-only' attribute indirectly, via _validateDisabled
145+
this._validateDisabled();
140146
};
141147
}
142148

@@ -175,6 +181,13 @@ export class BaseLayerElement extends HTMLElement {
175181
// removed from the map and the layer control
176182
if (this.hasAttribute('data-moving')) return;
177183
this._onRemove();
184+
185+
if (this._mql) {
186+
if (this._changeHandler) {
187+
this._mql.removeEventListener('change', this._changeHandler);
188+
}
189+
delete this._mql;
190+
}
178191
}
179192

180193
_onRemove() {
@@ -191,13 +204,6 @@ export class BaseLayerElement extends HTMLElement {
191204
delete this._fetchError;
192205
this.shadowRoot.innerHTML = '';
193206
if (this.src) this.innerHTML = '';
194-
195-
if (this._mql) {
196-
if (this._changeHandler) {
197-
this._mql.removeEventListener('change', this._changeHandler);
198-
}
199-
delete this._mql;
200-
}
201207

202208
if (l) {
203209
l.off();
@@ -220,7 +226,7 @@ export class BaseLayerElement extends HTMLElement {
220226
this._createLayerControlHTML = createLayerControlHTML.bind(this);
221227
const doConnected = this._onAdd.bind(this);
222228
const doRemove = this._onRemove.bind(this);
223-
const registerMediaQuery = this._registerMediaQuery(this);
229+
const registerMediaQuery = this._registerMediaQuery.bind(this);
224230
let mq = this.media;
225231
this.parentElement
226232
.whenReady()
@@ -250,15 +256,6 @@ export class BaseLayerElement extends HTMLElement {
250256
},
251257
{ once: true }
252258
);
253-
// get rid of me
254-
this.addEventListener(
255-
'zoomchangesrc',
256-
function (e) {
257-
e.stopPropagation();
258-
this.src = e.detail.href;
259-
},
260-
{ once: true }
261-
);
262259
let base = this.baseURI ? this.baseURI : document.baseURI;
263260
const headers = new Headers();
264261
headers.append('Accept', 'text/mapml');
@@ -641,7 +638,13 @@ export class BaseLayerElement extends HTMLElement {
641638
let layer = this._layer,
642639
map = layer?._map;
643640
if (map) {
644-
this._validateLayerZoom({ zoom: map.getZoom() });
641+
// if there's a media query in play, check it early
642+
if (this._mql && !this._mql.matches) {
643+
this.setAttribute('disabled', '');
644+
this.disabled = true;
645+
this.toggleLayerControlDisabled();
646+
return;
647+
}
645648
// prerequisite: no inline and remote mapml elements exists at the same time
646649
const mapExtents = this.src
647650
? this.shadowRoot.querySelectorAll('map-extent')
@@ -694,35 +697,6 @@ export class BaseLayerElement extends HTMLElement {
694697
}
695698
}, 0);
696699
}
697-
_validateLayerZoom(e) {
698-
// get the min and max zooms from all extents
699-
let toZoom = e.zoom;
700-
let min = this.extent.zoom.minZoom;
701-
let max = this.extent.zoom.maxZoom;
702-
let inLink = this.src
703-
? this.shadowRoot.querySelector('map-link[rel=zoomin]')
704-
: this.querySelector('map-link[rel=zoomin]'),
705-
outLink = this.src
706-
? this.shadowRoot.querySelector('map-link[rel=zoomout]')
707-
: this.querySelector('map-link[rel=zoomout]');
708-
let targetURL;
709-
if (!(min <= toZoom && toZoom <= max)) {
710-
if (inLink && toZoom > max) {
711-
targetURL = inLink.href;
712-
} else if (outLink && toZoom < min) {
713-
targetURL = outLink.href;
714-
}
715-
if (targetURL) {
716-
this.dispatchEvent(
717-
new CustomEvent('zoomchangesrc', {
718-
detail: {
719-
href: targetURL
720-
}
721-
})
722-
);
723-
}
724-
}
725-
}
726700
// disable/italicize layer control elements based on the map-layer.disabled property
727701
toggleLayerControlDisabled() {
728702
let input = this._layerControlCheckbox,

0 commit comments

Comments
 (0)