Skip to content

Commit 2379925

Browse files
authored
Update mapml-viewer to be shadow host-compatible (#950)
* Update mapml-viewer to be shadow host-compatible * Update ci-testing.yml Focus on getting flaky tests to pass
1 parent 18ba67e commit 2379925

26 files changed

+734
-91
lines changed

.github/workflows/ci-testing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- run: npx playwright install --with-deps
1919
- run: npm install -g grunt-cli
2020
- run: grunt default
21-
- run: xvfb-run --auto-servernum -- npx playwright test --workers=1 --retries=3
21+
- run: xvfb-run --auto-servernum -- npx playwright test tab menu --workers=1 --retries=3
2222
# - run: xvfb-run --auto-servernum -- npm run jest
2323
env:
2424
CI: true

src/map-extent.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,12 @@ export class MapExtent extends HTMLElement {
125125
animate: false
126126
});
127127
}
128+
128129
getMapEl() {
129-
return this.getRootNode() instanceof ShadowRoot
130-
? this.getRootNode().host.closest('mapml-viewer,map[is=web-map]')
131-
: this.closest('mapml-viewer,map[is=web-map]');
130+
return M.getClosest(this, 'mapml-viewer,map[is=web-map]');
132131
}
133132
getLayerEl() {
134-
return this.getRootNode() instanceof ShadowRoot
135-
? this.getRootNode().host
136-
: this.closest('layer-');
133+
return M.getClosest(this, 'layer-');
137134
}
138135
attributeChangedCallback(name, oldValue, newValue) {
139136
if (this.#hasConnected /* jshint ignore:line */) {

src/map-feature.js

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -128,39 +128,11 @@ export class MapFeature extends HTMLElement {
128128
return this._getFeatureExtent();
129129
}
130130
}
131-
getLayerEl() {
132-
let layerEl;
133-
if (this.getRootNode() instanceof ShadowRoot) {
134-
if (this.getRootNode().host.getRootNode() instanceof ShadowRoot) {
135-
// layer- src
136-
// > sd
137-
// map-extent
138-
// map-link
139-
// > sd
140-
// map-feature (1)
141-
layerEl = this.getRootNode().host.getRootNode().host;
142-
} else if (this.getRootNode().host.nodeName === 'MAP-LINK') {
143-
// layer-
144-
// map-extent
145-
// map-link
146-
// > sd
147-
// map-feature (4)
148-
layerEl = this.getRootNode().host.closest('layer-');
149-
} else {
150-
// layer- src
151-
// > sd
152-
// map-feature (2)
153-
layerEl = this.getRootNode().host;
154-
}
155-
} else {
156-
// layer-
157-
// map-feature (3)
158-
layerEl = this.closest('layer-');
159-
}
160-
return layerEl;
161-
}
162131
getMapEl() {
163-
return this.getLayerEl().closest('mapml-viewer,map[is=web-map]');
132+
return M.getClosest(this, 'mapml-viewer,map[is=web-map]');
133+
}
134+
getLayerEl() {
135+
return M.getClosest(this, 'layer-');
164136
}
165137

166138
attributeChangedCallback(name, oldValue, newValue) {

src/map-input.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,11 @@ export class MapInput extends HTMLElement {
172172
this.setAttribute('step', val);
173173
}
174174
}
175+
getMapEl() {
176+
return M.getClosest(this, 'mapml-viewer,map[is=web-map]');
177+
}
175178
getLayerEl() {
176-
return this.getRootNode() instanceof ShadowRoot
177-
? this.getRootNode().host
178-
: this.closest('layer-');
179+
return M.getClosest(this, 'layer-');
179180
}
180181
attributeChangedCallback(name, oldValue, newValue) {
181182
this.whenReady()

src/map-link.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,10 @@ export class MapLink extends HTMLElement {
152152
});
153153
}
154154
getMapEl() {
155-
return this.getRootNode() instanceof ShadowRoot
156-
? this.getRootNode().host.closest('mapml-viewer,map[is=web-map]')
157-
: this.closest('mapml-viewer,map[is=web-map]');
155+
return M.getClosest(this, 'mapml-viewer,map[is=web-map]');
158156
}
159157
getLayerEl() {
160-
return this.getRootNode() instanceof ShadowRoot
161-
? this.getRootNode().host
162-
: this.closest('layer-');
158+
return M.getClosest(this, 'layer-');
163159
}
164160

165161
attributeChangedCallback(name, oldValue, newValue) {

src/mapml-viewer.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,11 @@ export class MapViewer extends HTMLElement {
636636
false
637637
);
638638

639-
this.parentElement.addEventListener('keyup', function (e) {
639+
let host =
640+
this.getRootNode() instanceof ShadowRoot
641+
? this.getRootNode().host
642+
: this.parentElement;
643+
host.addEventListener('keyup', function (e) {
640644
if (
641645
e.keyCode === 9 &&
642646
document.activeElement.nodeName === 'MAPML-VIEWER'
@@ -661,7 +665,7 @@ export class MapViewer extends HTMLElement {
661665
this._map.fire('keypress', { originalEvent: e });
662666
}
663667
});
664-
this.parentElement.addEventListener('mousedown', function (e) {
668+
host.addEventListener('mousedown', function (e) {
665669
if (document.activeElement.nodeName === 'MAPML-VIEWER') {
666670
document.activeElement.dispatchEvent(
667671
new CustomEvent('mapfocused', { detail: { target: this } })

src/mapml.css

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ button.mapml-contextmenu-item:disabled {
228228
}
229229

230230
/*
231-
* Fullscreen control.
231+
* Fullscreen control. Image contains the on and off images, toggled via
232+
* the background-position property
232233
*/
233234

234235
.leaflet-control-fullscreen a {
@@ -238,7 +239,10 @@ button.mapml-contextmenu-item:disabled {
238239
background-position: 3px 3px;
239240
}
240241

241-
:host(.mapml-fullscreen-on) .leaflet-control-fullscreen a {
242+
/*
243+
* See: https://developer.mozilla.org/en-US/docs/Web/CSS/:host_function
244+
and: https://developer.mozilla.org/en-US/docs/Web/CSS/:fullscreen */
245+
:host(:fullscreen) .leaflet-control-fullscreen a {
242246
background-position: 3px -35px;
243247
}
244248

@@ -247,11 +251,6 @@ button.mapml-contextmenu-item:disabled {
247251
height: 100%!important;
248252
}
249253

250-
:host(.mapml-fullscreen-on) {
251-
width: 100%!important;
252-
height: 100%!important;
253-
}
254-
255254
:host(.leaflet-pseudo-fullscreen) {
256255
position: fixed!important;
257256
width: 100%!important;

src/mapml/control/FullscreenButton.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ L.Map.include({
5353
toggleFullscreen: function (options) {
5454
// the <map> element can't contain a shadow root, so we used a child <div>
5555
// <mapml-viewer> can contain a shadow root, so return it directly
56-
var mapEl = this.getContainer().getRootNode().host,
57-
container = mapEl.nodeName === 'DIV' ? mapEl.parentElement : mapEl;
56+
var mapEl = M.getClosest(this.getContainer(), 'mapml-viewer,[is=web-map]');
5857
if (this.isFullscreen()) {
5958
if (options && options.pseudoFullscreen) {
60-
this._disablePseudoFullscreen(container);
59+
this._disablePseudoFullscreen(mapEl);
6160
} else if (document.exitFullscreen) {
6261
document.exitFullscreen();
6362
} else if (document.mozCancelFullScreen) {
@@ -67,21 +66,21 @@ L.Map.include({
6766
} else if (document.msExitFullscreen) {
6867
document.msExitFullscreen();
6968
} else {
70-
this._disablePseudoFullscreen(container);
69+
this._disablePseudoFullscreen(mapEl);
7170
}
7271
} else {
7372
if (options && options.pseudoFullscreen) {
74-
this._enablePseudoFullscreen(container);
75-
} else if (container.requestFullscreen) {
76-
container.requestFullscreen();
77-
} else if (container.mozRequestFullScreen) {
78-
container.mozRequestFullScreen();
79-
} else if (container.webkitRequestFullscreen) {
80-
container.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
81-
} else if (container.msRequestFullscreen) {
82-
container.msRequestFullscreen();
73+
this._enablePseudoFullscreen(mapEl);
74+
} else if (mapEl.requestFullscreen) {
75+
mapEl.requestFullscreen();
76+
} else if (mapEl.mozRequestFullScreen) {
77+
mapEl.mozRequestFullScreen();
78+
} else if (mapEl.webkitRequestFullscreen) {
79+
mapEl.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
80+
} else if (mapEl.msRequestFullscreen) {
81+
mapEl.msRequestFullscreen();
8382
} else {
84-
this._enablePseudoFullscreen(container);
83+
this._enablePseudoFullscreen(mapEl);
8584
}
8685
}
8786
this.getContainer().focus();
@@ -101,7 +100,10 @@ L.Map.include({
101100

102101
_setFullscreen: function (fullscreen) {
103102
this._isFullscreen = fullscreen;
104-
var container = this.getContainer().getRootNode().host;
103+
var container = M.getClosest(
104+
this.getContainer(),
105+
'mapml-viewer,[is=web-map]'
106+
);
105107
if (fullscreen) {
106108
L.DomUtil.addClass(container, 'mapml-fullscreen-on');
107109
} else {
@@ -111,18 +113,12 @@ L.Map.include({
111113
},
112114

113115
_onFullscreenChange: function (e) {
114-
var fullscreenElement =
115-
document.fullscreenElement ||
116-
document.mozFullScreenElement ||
117-
document.webkitFullscreenElement ||
118-
document.msFullscreenElement,
119-
mapEl = this.getContainer().getRootNode().host,
120-
container = mapEl.nodeName === 'DIV' ? mapEl.parentElement : mapEl;
121-
122-
if (fullscreenElement === container && !this._isFullscreen) {
116+
var fullscreenElement = M.getClosest(this.getContainer(), ':fullscreen'),
117+
mapEl = M.getClosest(this.getContainer(), 'mapml-viewer,[is=web-map]');
118+
if (fullscreenElement === mapEl && !this._isFullscreen) {
123119
this._setFullscreen(true);
124120
this.fire('fullscreenchange');
125-
} else if (fullscreenElement !== container && this._isFullscreen) {
121+
} else if (fullscreenElement !== mapEl && this._isFullscreen) {
126122
this._setFullscreen(false);
127123
this.fire('fullscreenchange');
128124
}

src/mapml/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ import { DOMTokenList } from './utils/DOMTokenList';
840840
M.getZoomBoundsFromMeta = Util.getZoomBoundsFromMeta;
841841
M.getZoomBounds = Util.getZoomBounds;
842842
M.getNativeVariables = Util.getNativeVariables;
843+
M.getClosest = Util.getClosest;
843844

844845
M.QueryHandler = QueryHandler;
845846
M.ContextMenu = ContextMenu;

src/mapml/utils/Util.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,5 +1519,23 @@ export var Util = {
15191519
)
15201520
newZoom--;
15211521
return newZoom;
1522+
},
1523+
getClosest(node, selector) {
1524+
if (!node) {
1525+
return null;
1526+
}
1527+
if (node instanceof ShadowRoot) {
1528+
return M.getClosest(node.host, selector);
1529+
}
1530+
1531+
if (node instanceof HTMLElement) {
1532+
if (node.matches(selector)) {
1533+
return node;
1534+
} else {
1535+
return M.getClosest(node.parentNode, selector);
1536+
}
1537+
}
1538+
1539+
return M.getClosest(node.parentNode, selector);
15221540
}
15231541
};

0 commit comments

Comments
 (0)