Skip to content

Commit 4057a1d

Browse files
author
Peter Rushforth
committed
Replace map-extent way of setting zIndex to use map-extent.position
Adapt calculatePosition to working within either shadow root or light DOM Un-break and de-flake tests as possible
1 parent db0ee63 commit 4057a1d

File tree

8 files changed

+32
-28
lines changed

8 files changed

+32
-28
lines changed

src/map-extent.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ export class HTMLExtentElement extends HTMLElement {
267267
// this._opacity is used to record the current opacity value (with or without updates),
268268
// the initial value of this._opacity should be set as opacity attribute value, if exists, or the default value 1.0
269269
this._opacity = this.opacity || 1.0;
270-
console.log(this.label + ' extent position: ' + this.position);
271270
this._extentLayer = mapExtentLayer({
272271
opacity: this.opacity,
273272
crs: M[this.units],
@@ -435,13 +434,7 @@ export class HTMLExtentElement extends HTMLElement {
435434
if (this.checked && !this.disabled && this.parentLayer._layer) {
436435
// can be added to MapLayer LayerGroup no matter map-layer is checked or not
437436
this._extentLayer.addTo(this.parentLayer._layer);
438-
this._extentLayer.setZIndex(
439-
Array.from(
440-
this.parentLayer.src
441-
? this.parentLayer.shadowRoot.querySelectorAll(':host > map-extent')
442-
: this.parentLayer.querySelectorAll(':scope > map-extent')
443-
).indexOf(this)
444-
);
437+
this._extentLayer.setZIndex(this.position);
445438
} else {
446439
this.parentLayer._layer?.removeLayer(this._extentLayer);
447440
}

src/mapml/elementSupport/layers/calculatePosition.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,29 @@ export function calculatePosition(element) {
1818
return 0;
1919
}
2020

21-
const parent = element.parentElement;
21+
// Get parent - could be Element or ShadowRoot
22+
const parent = element.parentNode;
2223
if (!parent) return 1;
2324

25+
// Get children - works for both Element and ShadowRoot
26+
// For ShadowRoot, we need to filter to get only element nodes
27+
const children =
28+
parent.children ||
29+
Array.from(parent.childNodes).filter(
30+
(node) => node.nodeType === Node.ELEMENT_NODE
31+
);
32+
33+
if (!children || children.length === 0) return 1;
34+
2435
let position = 0;
2536
let lastTag = null;
2637
let foundTarget = false;
2738

2839
// Iterate through all child elements
29-
for (const child of parent.children) {
40+
for (const child of children) {
41+
// Skip non-element nodes (shouldn't happen with .children, but safe for childNodes)
42+
if (child.nodeType !== Node.ELEMENT_NODE) continue;
43+
3044
const childTag = child.tagName.toLowerCase();
3145

3246
// Skip non-map elements
@@ -36,14 +50,14 @@ export function calculatePosition(element) {
3650
if (child === element) {
3751
foundTarget = true;
3852

39-
// map-extent always needs a new position
53+
// map-extent always needs a new z-index
4054
if (childTag === 'map-extent') {
4155
position++;
4256
return position;
4357
}
4458

4559
// For map-tile and map-feature:
46-
// If this element continues a sequence of the same type, return the current position
60+
// If this element continues a sequence of the same type, return the current z-index
4761
if (lastTag === childTag) {
4862
return position;
4963
}
@@ -56,13 +70,13 @@ export function calculatePosition(element) {
5670
// Before reaching target, count layer group transitions
5771
if (!foundTarget) {
5872
if (childTag === 'map-extent') {
59-
// Each map-extent increments position
73+
// Each map-extent increments z-index
6074
position++;
6175
} else if (lastTag !== null && lastTag !== childTag) {
6276
// Transition between different types (excluding map-extent)
6377
position++;
6478
} else if (lastTag === null) {
65-
// First valid element starts at position 1
79+
// First valid element starts at z-index 1
6680
position = 1;
6781
}
6882

src/mapml/layers/MapExtentLayer.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ export var MapExtentLayer = LayerGroup.extend({
3434
// Call LayerGroup's initialize to trigger Leaflet's setup
3535
LayerGroup.prototype.initialize.call(this, null, options);
3636
this._container = DomUtil.create('div', 'leaflet-layer');
37-
if (options.zIndex) {
38-
this._container.style.zIndex = options.zIndex;
39-
}
4037
this._extentEl = this.options.extentEl;
4138
this.changeOpacity(this.options.opacity);
39+
this.setZIndex(options.zIndex);
4240
// Add class to the container
4341
DomUtil.addClass(this._container, 'mapml-extentlayer-container');
4442
},

src/mapml/layers/MapLayer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export var MapLayer = LayerGroup.extend({
131131
}
132132
if (type === '_extentLayer' && mapExtents.length) {
133133
for (let i = 0; i < mapExtents.length; i++) {
134-
if (mapExtents[i]._extentLayer.bounds) {
134+
if (mapExtents[i]._extentLayer?.bounds) {
135135
let mapExtentLayer = mapExtents[i]._extentLayer;
136136
if (!bnds) {
137137
bnds = bounds(

test/e2e/elements/map-extent/map-extent-checked-ordering.test.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@ The imagery layer <map-extent> draws on top of the states layer.
4141
let imageryZIndex = await ext1.evaluate((e) => {
4242
return +e._extentLayer._container.style.zIndex;
4343
});
44-
expect(imageryZIndex).toEqual(0);
4544
const ext2 = page.getByTestId('ext2');
4645
let statesZIndex = await ext2.evaluate((e) => {
4746
return +e._extentLayer._container.style.zIndex;
4847
});
49-
expect(statesZIndex).toEqual(1);
48+
expect(statesZIndex).toBeGreaterThan(imageryZIndex);
5049
// re-order them via the layer control
5150
const imageryFieldset = layerControl.getByRole('group', {
5251
name: 'Extent One'
@@ -65,11 +64,10 @@ The imagery layer <map-extent> draws on top of the states layer.
6564
imageryZIndex = await ext1.evaluate((e) => {
6665
return +e._extentLayer._container.style.zIndex;
6766
});
68-
expect(imageryZIndex).toEqual(1);
6967
statesZIndex = await ext2.evaluate((e) => {
7068
return +e._extentLayer._container.style.zIndex;
7169
});
72-
expect(statesZIndex).toEqual(0);
70+
expect(statesZIndex).toBeLessThan(imageryZIndex);
7371

7472
await page.mouse.move(from.x, from.y);
7573
await page.mouse.down();
@@ -80,11 +78,10 @@ The imagery layer <map-extent> draws on top of the states layer.
8078
imageryZIndex = await ext1.evaluate((e) => {
8179
return +e._extentLayer._container.style.zIndex;
8280
});
83-
expect(imageryZIndex).toEqual(0);
8481
statesZIndex = await ext2.evaluate((e) => {
8582
return +e._extentLayer._container.style.zIndex;
8683
});
87-
expect(statesZIndex).toEqual(1);
84+
expect(statesZIndex).toBeGreaterThan(imageryZIndex);
8885
// TO DO re-order them via the DOM (insertAdjacentHTML),
8986
// ensure that
9087
// a) render order/z-index is correct

test/e2e/elements/map-extent/map-extent-in-shadow-root.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ test.describe('map-extent can be inside a shadow root or other custom element',
1111
await page.goto('map-extent-in-shadow-root.html');
1212
});
1313
test('map-extent getMapEl() works in shadow root', async () => {
14+
await page.waitForTimeout(500);
1415
const viewer = page.getByTestId('viewer');
1516
await expect(viewer).toBeTruthy();
1617
const layer = viewer.getByTestId('test-layer');

test/e2e/elements/map-link/map-link-media.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ test.describe('map-link media attribute', () => {
2020
});
2121

2222
test('map-link is disabled when media attribute does not match', async () => {
23+
await page.waitForTimeout(500);
2324
// const map = page.locator('mapml-viewer');
2425
const layer = page.locator('map-layer');
2526
const mapLink = page.locator('map-link').first();

test/e2e/layers/multipleExtents.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ test.describe('Adding and Removing Multiple Extents', () => {
149149
(extents) => extents.length
150150
);
151151
cbmt = await page.$eval(
152-
"div.mapml-extentlayer-container[style='opacity: 0.5; z-index: 0;'] > div",
152+
"div.mapml-extentlayer-container[style='opacity: 0.5; z-index: 1;'] > div",
153153
(div) => div.className
154154
);
155155
const alabama = await page.$eval(
156-
"div.mapml-extentlayer-container[style='opacity: 0.5; z-index: 1;'] > div",
156+
"div.mapml-extentlayer-container[style='opacity: 0.5; z-index: 2;'] > div",
157157
(div) => div.className
158158
);
159159
const layerOpacity = await page.$eval(
@@ -203,11 +203,11 @@ test.describe('Adding and Removing Multiple Extents', () => {
203203
// turn the Multiple Extents layer on
204204
await page.click("text='Multiple Extents'");
205205
const cbmtClass = await page.$eval(
206-
"div.mapml-extentlayer-container[style='opacity: 0.5; z-index: 0;'] > div",
206+
"div.mapml-extentlayer-container[style='opacity: 0.5; z-index: 1;'] > div",
207207
(div) => div.className
208208
);
209209
const alabamaClass = await page.$eval(
210-
"div.mapml-extentlayer-container[style='opacity: 0.5; z-index: 1;'] > div",
210+
"div.mapml-extentlayer-container[style='opacity: 0.5; z-index: 2;'] > div",
211211
(div) => div.className
212212
);
213213
const layer = page.getByTestId('multiple-extents');

0 commit comments

Comments
 (0)