Skip to content

Commit e372cc4

Browse files
prushforprushfor
authored andcommitted
Add map-projectionchange event. Delete map projection attrChgCallbk
from triggering map-change event. Allow map-extent to react to map-projectionchange event, enable/disable according to projection match. Change initialization logic for opacity, so that layer- and map-extent opacity value is maintained through map-projectionchange event. Remove MapMLLayer.validProjection attribute and flawed logic. Prettier formatting change only in multipleExtents.test.js Remove use of validProjection by layer context menu, was wrong anyway(?) Add waitfortimeout of 500ms in customTCRS.test.js (100ms was not enough) Add test for simple projection change.
1 parent fde273c commit e372cc4

File tree

10 files changed

+107
-62
lines changed

10 files changed

+107
-62
lines changed

src/layer.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class MapLayer extends HTMLElement {
108108
this._createLayerControlHTML = M._createLayerControlHTML.bind(this);
109109
// this._opacity is used to record the current opacity value (with or without updates),
110110
// the initial value of this._opacity should be set as opacity attribute value, if exists, or the default value 1.0
111-
this._opacity = +(this.getAttribute('opacity') || 1.0);
111+
this._opacity = this.opacity || 1.0;
112112
const doConnected = this._onAdd.bind(this);
113113
this.parentElement
114114
.whenReady()
@@ -345,25 +345,20 @@ export class MapLayer extends HTMLElement {
345345
'_mapmlvectors',
346346
'_templatedLayer'
347347
];
348-
if (layer.validProjection) {
349-
for (let j = 0; j < layerTypes.length; j++) {
350-
let type = layerTypes[j];
351-
if (this.checked) {
352-
if (type === '_templatedLayer' && mapExtents.length > 0) {
353-
for (let i = 0; i < mapExtents.length; i++) {
354-
totalExtentCount++;
355-
if (mapExtents[i]._validateDisabled()) disabledExtentCount++;
356-
}
357-
} else if (layer[type]) {
358-
// not a templated layer
348+
for (let j = 0; j < layerTypes.length; j++) {
349+
let type = layerTypes[j];
350+
if (this.checked) {
351+
if (type === '_templatedLayer' && mapExtents.length > 0) {
352+
for (let i = 0; i < mapExtents.length; i++) {
359353
totalExtentCount++;
360-
if (!layer[type].isVisible) disabledExtentCount++;
354+
if (mapExtents[i]._validateDisabled()) disabledExtentCount++;
361355
}
356+
} else if (layer[type]) {
357+
// not a templated layer
358+
totalExtentCount++;
359+
if (!layer[type].isVisible) disabledExtentCount++;
362360
}
363361
}
364-
} else {
365-
disabledExtentCount = 1;
366-
totalExtentCount = 1;
367362
}
368363
// if all extents are not visible / disabled, set layer to disabled
369364
if (

src/map-extent.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,11 @@ export class MapExtent extends HTMLElement {
168168
);
169169
this._changeHandler = this._handleChange.bind(this);
170170
this.parentLayer.addEventListener('map-change', this._changeHandler);
171+
this.mapEl = this.parentLayer.closest('mapml-viewer,map[is=web-map]');
172+
this.mapEl.addEventListener('map-projectionchange', this._changeHandler);
171173
// this._opacity is used to record the current opacity value (with or without updates),
172174
// the initial value of this._opacity should be set as opacity attribute value, if exists, or the default value 1.0
173-
this._opacity = +(this.getAttribute('opacity') || 1.0);
175+
this._opacity = this.opacity || 1.0;
174176
this._templatedLayer = M.templatedLayer(this._templateVars, {
175177
pane: this._layer._container,
176178
opacity: this.opacity,
@@ -522,6 +524,7 @@ export class MapExtent extends HTMLElement {
522524
this._layerControlHTML.remove();
523525
this._map.removeLayer(this._templatedLayer);
524526
this.parentLayer.removeEventListener('map-change', this._changeHandler);
527+
this.mapEl.removeEventListener('map-projectionchange', this._changeHandler);
525528
delete this._templatedLayer;
526529
delete this.parentLayer.bounds;
527530
}

src/mapml-viewer.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,13 @@ export class MapViewer extends HTMLElement {
377377
this.zoomTo(lat, lon, zoom);
378378
if (M.options.announceMovement)
379379
this._map.announceMovement.enable();
380-
this.querySelectorAll('layer-').forEach((layer) => {
381-
layer.dispatchEvent(new CustomEvent('map-change'));
382-
});
380+
// required to delay until map-extent.disabled is correctly set
381+
// which happens as a result of layer-._validateDisabled()
382+
// which happens so much we have to delay until they calls are
383+
// completed
384+
setTimeout(() => {
385+
this.dispatchEvent(new CustomEvent('map-projectionchange'));
386+
}, 0);
383387
});
384388
}
385389
};

src/mapml/handlers/ContextMenu.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,6 @@ export var ContextMenu = L.Handler.extend({
798798
.closest('fieldset')
799799
.parentNode.parentNode.parentNode.querySelector('span')
800800
: elem.querySelector('span');
801-
if (!elem.layer.validProjection) return;
802801
this._layerClicked = elem;
803802
this._layerMenu.removeAttribute('hidden');
804803
this._showAtPoint(e.containerPoint, e, this._layerMenu);

src/mapml/layers/MapMLLayer.js

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,6 @@ export var MapMLLayer = L.Layer.extend({
4343
// OR use the extent of the content provided
4444

4545
this._initialize(local ? layerEl : mapml);
46-
47-
// a default extent can't be correctly set without the map to provide
48-
// its bounds , projection, zoom range etc, so if that stuff's not
49-
// established by metadata in the content, we should use map properties
50-
// to set the extent, but the map won't be available until the <layer>
51-
// element is attached to the <map> element, wait for that to happen.
52-
// weirdness. options is actually undefined here, despite the hardcoded
53-
// options above. If you use this.options, you see the options defined
54-
// above. Not going to change this, but failing to understand ATM.
55-
// may revisit some time.
56-
this.validProjection = true;
5746
},
5847
setZIndex: function (zIndex) {
5948
this.options.zIndex = zIndex;
@@ -98,11 +87,6 @@ export var MapMLLayer = L.Layer.extend({
9887
},
9988

10089
onAdd: function (map) {
101-
// probably don't need it except for layer context menu usage
102-
if (this._properties && !this._validProjection(map)) {
103-
this.validProjection = false;
104-
return;
105-
}
10690
this._map = map;
10791
if (this._mapmlvectors) map.addLayer(this._mapmlvectors);
10892

@@ -218,26 +202,6 @@ export var MapMLLayer = L.Layer.extend({
218202
}
219203
},
220204

221-
_validProjection: function (map) {
222-
const mapExtents = this._layerEl.querySelectorAll('map-extent');
223-
let noLayer = false;
224-
if (this._properties && mapExtents.length > 0) {
225-
for (let i = 0; i < mapExtents.length; i++) {
226-
if (mapExtents[i]._templateVars) {
227-
for (let template of mapExtents[i]._templateVars)
228-
if (
229-
!template.projectionMatch &&
230-
template.projection !== map.options.projection
231-
) {
232-
noLayer = true; // if there's a single template where projections don't match, set noLayer to true
233-
break;
234-
}
235-
}
236-
}
237-
}
238-
return !(noLayer || this.getProjection() !== map.options.projection);
239-
},
240-
241205
addTo: function (map) {
242206
map.addLayer(this);
243207
return this;

src/web-map.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,13 @@ export class WebMap extends HTMLMapElement {
422422
this.zoomTo(lat, lon, zoom);
423423
if (M.options.announceMovement)
424424
this._map.announceMovement.enable();
425-
this.querySelectorAll('layer-').forEach((layer) => {
426-
layer.dispatchEvent(new CustomEvent('map-change'));
427-
});
425+
// required to delay until map-extent.disabled is correctly set
426+
// which happens as a result of layer-._validateDisabled()
427+
// which happens so much we have to delay until they calls are
428+
// completed
429+
setTimeout(() => {
430+
this.dispatchEvent(new CustomEvent('map-projectionchange'));
431+
}, 0);
428432
});
429433
}
430434
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>map-projectionchange event</title>
6+
<!-- the layer in this map should continue to be visible when you change
7+
the viewer projection from OSMTILE to CBMTILE. -->
8+
<script type="module" src="/mapml-viewer.js"></script>
9+
</head>
10+
<body>
11+
<mapml-viewer zoom="2" lon="-75.703611" lat="45.411105" width="500" height="500" controls projection="OSMTILE">
12+
<layer- label="Projection changer" checked>
13+
<map-extent label="National Geographic" units="OSMTILE" checked >
14+
<map-input name="TileMatrix" type="zoom" value="18" min="0" max="18"></map-input>
15+
<map-input name="TileCol" type="location" units="tilematrix" axis="column" min="0" max="262144"></map-input>
16+
<map-input name="TileRow" type="location" units="tilematrix" axis="row" min="0" max="262144"></map-input>
17+
<map-link rel="tile" tref="https://server.arcgisonline.com/arcgis/rest/services/NatGeo_World_Map/MapServer/WMTS/tile/1.0.0/NatGeo_World_Map/default/default028mm/{TileMatrix}/{TileRow}/{TileCol}.jpg"></map-link>
18+
</map-extent>
19+
<map-extent label="Canada Base Map - Transportation" units="CBMTILE" checked >
20+
<map-input name="z" type="zoom" min="0" max="18"></map-input>
21+
<map-input name="y" type="location" units="tilematrix" axis="row"></map-input>
22+
<map-input name="x" type="location" units="tilematrix" axis="column"></map-input>
23+
<map-link rel="tile" tref="/tiles/cbmt/{z}/c{x}_r{y}.png" ></map-link>
24+
</map-extent>
25+
</layer->
26+
</mapml-viewer>
27+
<script>
28+
function changeProjection() {
29+
const prj = document.body.querySelector('mapml-viewer').projection;
30+
if (document.body.querySelector('mapml-viewer').projection === "OSMTILE") {
31+
document.body.querySelector('mapml-viewer').projection = "CBMTILE";
32+
} else {
33+
document.body.querySelector('mapml-viewer').projection = "OSMTILE";
34+
}
35+
}
36+
</script>
37+
</body>
38+
</html>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { test, expect, chromium } from '@playwright/test';
2+
3+
test.describe('map-projectionchange test ', () => {
4+
let page;
5+
let context;
6+
test.beforeAll(async () => {
7+
context = await chromium.launchPersistentContext('');
8+
page =
9+
context.pages().find((page) => page.url() === 'about:blank') ||
10+
(await context.newPage());
11+
await page.goto('events/map-projectionchange.html');
12+
});
13+
14+
test.afterAll(async function () {
15+
await context.close();
16+
});
17+
18+
test('do something and test it', async () => {
19+
const viewer = await page.locator('mapml-viewer');
20+
expect(await viewer.evaluate((v)=>v.projection)).toEqual('OSMTILE');
21+
expect(await viewer.evaluate((v)=>{
22+
return v.querySelector('map-extent[units=OSMTILE]').disabled;
23+
})).toBe(false);
24+
expect(await viewer.evaluate((v)=>{
25+
return v.querySelector('map-extent[units=CBMTILE]').disabled;
26+
})).toBe(true);
27+
await viewer.evaluate(()=> changeProjection());
28+
await page.waitForTimeout(500);
29+
expect(await viewer.evaluate((v)=> v.projection)).toEqual('CBMTILE');
30+
expect(await viewer.evaluate((v)=>{
31+
return v.querySelector('map-extent[units=OSMTILE]').disabled;
32+
})).toBe(true);
33+
expect(await viewer.evaluate((v)=>{
34+
return v.querySelector('map-extent[units=CBMTILE]').disabled;
35+
})).toBe(false);
36+
37+
});
38+
});

test/e2e/layers/multipleExtents.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ test.describe('Multiple Extents Bounds Tests', () => {
352352
const alabamaExtentItem = page.getByText('alabama_feature');
353353
await expect(alabamaExtentItem).toHaveCount(1);
354354
await expect(alabamaExtentItem).toHaveCSS('font-style', 'normal');
355-
355+
356356
const alabamaMapExtent = page.locator('map-extent[label=alabama_feature]');
357357
await expect(alabamaMapExtent).toHaveCount(1);
358358
await expect(alabamaMapExtent).not.toHaveAttribute('disabled');

test/e2e/mapml-viewer/customTCRS.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test.describe('Playwright Custom TCRS Tests', () => {
1717
});
1818

1919
test('Simple Custom TCRS, tiles load, mismatched layer disabled', async () => {
20-
await page.waitForTimeout(100);
20+
await page.waitForTimeout(500);
2121
const misMatchedLayerDisabled = await page.$eval(
2222
'body > mapml-viewer:nth-child(1)',
2323
(map) => map.querySelectorAll('layer-')[0].hasAttribute('disabled')

0 commit comments

Comments
 (0)