Skip to content

Commit 9f39586

Browse files
prushforthprushfor
andauthored
Support for map-link[rel=stylesheet] and map-style in map-extent (#945)
* Support for map-link[rel=stylesheet] and map-style in map-extent Closes #944 Add sloMo to ArrowKeyNaveContextMenu.test.js * De-flake popupTabNavigation, hopefully for good --------- Co-authored-by: prushfor <prushfor@L-BSC-A146390.nrn.nrcan.gc.ca>
1 parent d98971f commit 9f39586

13 files changed

Lines changed: 398 additions & 258 deletions

src/map-extent.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ export class MapExtent extends HTMLElement {
262262
// this._layerControlHTML is the fieldset for the extent in the LayerControl
263263
this._layerControlHTML = this._createLayerControlExtentHTML();
264264
this._calculateBounds();
265+
// instead of children using parents' whenReady which can be cyclic,
266+
// when this element is ready, run stuff that is only available after
267+
// initialization
268+
this._runMutationObserver(this.children);
269+
// make sure same thing happens when children are added
265270
this._bindMutationObserver();
266271
}
267272
/*
@@ -290,6 +295,16 @@ export class MapExtent extends HTMLElement {
290295
this._validateDisabled();
291296
});
292297
};
298+
const _addStylesheetLink = (mapLink) => {
299+
this.whenReady().then(() => {
300+
this._extentLayer.appendStyleLink(mapLink);
301+
});
302+
};
303+
const _addStyleElement = (mapStyle) => {
304+
this.whenReady().then(() => {
305+
this._extentLayer.appendStyleElement(mapStyle);
306+
});
307+
};
293308
for (let i = 0; i < elementsGroup.length; ++i) {
294309
let element = elementsGroup[i];
295310
switch (element.nodeName) {
@@ -303,7 +318,14 @@ export class MapExtent extends HTMLElement {
303318
}
304319
break;
305320
case 'MAP-LINK':
306-
// might need this in future, among others
321+
if (element.link && !element.link.isConnected)
322+
_addStylesheetLink(element);
323+
break;
324+
case 'MAP-STYLE':
325+
if (element.styleElement && !element.styleElement.isConnected) {
326+
_addStyleElement(element);
327+
}
328+
break;
307329
default:
308330
break;
309331
}

src/map-link.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ export class MapLink extends HTMLElement {
316316
this._stylesheetHost._layer.appendStyleLink(this);
317317
} else if (this._stylesheetHost._templatedLayer) {
318318
this._stylesheetHost._templatedLayer.appendStyleLink(this);
319+
} else if (this._stylesheetHost._extentLayer) {
320+
this._stylesheetHost._extentLayer.appendStyleLink(this);
319321
}
320322

321323
function copyAttributes(source, target) {

src/map-style.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ export class MapStyle extends HTMLElement {
2727
this.styleElement.mapStyle = this;
2828
this.styleElement.textContent = this.textContent;
2929
copyAttributes(this, this.styleElement);
30-
3130
if (this._stylesheetHost._layer) {
3231
this._stylesheetHost._layer.appendStyleElement(this);
3332
} else if (this._stylesheetHost._templatedLayer) {
3433
this._stylesheetHost._templatedLayer.appendStyleElement(this);
34+
} else if (this._stylesheetHost._extentLayer) {
35+
this._stylesheetHost._extentLayer.appendStyleElement(this);
3536
}
3637

3738
function copyAttributes(source, target) {

src/mapml/layers/ExtentLayer.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,31 @@ export var ExtentLayer = L.LayerGroup.extend({
7575
this._extentEl._opacity = opacity;
7676
if (this._extentEl._opacitySlider)
7777
this._extentEl._opacitySlider.value = opacity;
78+
},
79+
appendStyleLink: function (mapLink) {
80+
if (!mapLink.link) return;
81+
let positionAndNode = this._getStylePositionAndNode();
82+
positionAndNode.node.insertAdjacentElement(
83+
positionAndNode.position,
84+
mapLink.link
85+
);
86+
},
87+
_getStylePositionAndNode: function () {
88+
return this._container.lastChild &&
89+
(this._container.lastChild.nodeName.toUpperCase() === 'SVG' ||
90+
this._container.lastChild.classList.contains('mapml-vector-container'))
91+
? { position: 'beforebegin', node: this._container.lastChild }
92+
: this._container.lastChild
93+
? { position: 'afterend', node: this._container.lastChild }
94+
: { position: 'afterbegin', node: this._container };
95+
},
96+
appendStyleElement: function (mapStyle) {
97+
if (!mapStyle.styleElement) return;
98+
let positionAndNode = this._getStylePositionAndNode();
99+
positionAndNode.node.insertAdjacentElement(
100+
positionAndNode.position,
101+
mapStyle.styleElement
102+
);
78103
}
79104
});
80105
export var extentLayer = function (options) {

src/mapml/layers/TemplatedTileLayer.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,15 @@ export var TemplatedTileLayer = L.TileLayer.extend({
196196
if (href) {
197197
if (!container.querySelector("link[href='" + href + "']")) {
198198
var linkElm = document.createElement('link');
199+
copyAttributes(stylesheets[i], linkElm);
199200
linkElm.setAttribute('href', href);
200-
linkElm.setAttribute('rel', 'stylesheet');
201201
ss.push(linkElm);
202202
}
203203
}
204204
} else {
205205
// <map-style>
206206
var styleElm = document.createElement('style');
207+
copyAttributes(stylesheets[i], styleElm);
207208
styleElm.textContent = stylesheets[i].textContent;
208209
ss.push(styleElm);
209210
}
@@ -215,6 +216,12 @@ export var TemplatedTileLayer = L.TileLayer.extend({
215216
for (var s = ss.length - 1; s >= 0; s--) {
216217
container.insertAdjacentElement('afterbegin', ss[s]);
217218
}
219+
function copyAttributes(source, target) {
220+
return Array.from(source.attributes).forEach((attribute) => {
221+
if (attribute.nodeName !== 'href')
222+
target.setAttribute(attribute.nodeName, attribute.nodeValue);
223+
});
224+
}
218225
},
219226

220227
_createFeatures: function (markup, coords, tile) {

test/e2e/core/ArrowKeyNavContextMenu.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ test.describe('Using arrow keys to navigate context menu', () => {
44
let page;
55
let context;
66
test.beforeAll(async function () {
7-
context = await chromium.launchPersistentContext('');
7+
context = await chromium.launchPersistentContext('', { slowMo: 250 });
88
page =
99
context.pages().find((page) => page.url() === 'about:blank') ||
1010
(await context.newPage());

test/e2e/core/popupTabNavigation.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
</head>
1010

1111
<body>
12-
<mapml-viewer style="width: 500px;height: 500px;" is="web-map" projection="CBMTILE" zoom="2" lat="45.5052040"
12+
<mapml-viewer data-testid="viewer" style="width: 500px;height: 500px;" is="web-map" projection="CBMTILE" zoom="2" lat="45.5052040"
1313
lon="-75.2202344" controls>
14-
<layer- id="query" label="Fire" checked>
14+
<layer- data-testid="query" id="query" label="Fire" checked>
1515
<map-extent units="CBMTILE" checked hidden>
1616
<map-input name="z" type="zoom" value="18" min="0" max="18" ></map-input>
1717
<map-input name="txmin" type="location" units="tilematrix" position="top-left" axis="easting" min="-2.003750834E7"
@@ -46,10 +46,10 @@
4646
<map-meta name="zoom" content="min=1,max=5,value=0"></map-meta>
4747
<map-meta name="cs" content="gcrs"></map-meta>
4848
<map-meta name="extent" content="zoom=0,top-left-column=0,top-left-row=0,bottom-right-column=5,bottom-right-row=5"></map-meta>
49-
<map-feature zoom="2" class="refDiff">
49+
<map-feature data-testid="big-square" zoom="2" class="refDiff">
5050
<map-properties>
5151
<h1>Test</h1>
52-
<a href="www.example.com">test</a>
52+
<a data-testid="anchor" href="www.example.com">test</a>
5353
</map-properties>
5454
<map-geometry cs="tilematrix">
5555
<map-polygon>
@@ -69,7 +69,7 @@ <h1>Test</h1>
6969
</map-geometry>
7070
</map-feature>
7171

72-
<map-feature zoom="2" class="refDiff">
72+
<map-feature data-testid="small-trapezoid" zoom="2" class="refDiff">
7373
<map-properties>
7474
<h1>Test</h1>
7575
</map-properties>
@@ -80,7 +80,7 @@ <h1>Test</h1>
8080
</map-geometry>
8181
</map-feature>
8282
</layer->
83-
<layer- id="vector" label="vector states" src="data/us_pop_density.mapml"></layer->
83+
<layer- data-testid="vector" id="vector" label="vector states" src="data/us_pop_density.mapml"></layer->
8484
</mapml-viewer>
8585
</body>
8686

0 commit comments

Comments
 (0)