Skip to content

Commit 2642053

Browse files
author
Peter Rushforth
committed
Map map-tile attributes row,col,zoom immutable; add tests for that
1 parent 0cac899 commit 2642053

16 files changed

+58
-290
lines changed

src/map-tile.js

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,61 +12,38 @@ export class HTMLTileElement extends HTMLElement {
1212
}
1313
/* jshint ignore:start */
1414
#hasConnected;
15+
#initialRow;
16+
#initialCol;
17+
#initialZoom;
1518
/* jshint ignore:end */
1619
get row() {
17-
return +(this.hasAttribute('row') ? this.getAttribute('row') : 0);
20+
/* jshint ignore:start */
21+
return +this.#initialRow;
22+
/* jshint ignore:end */
1823
}
1924
set row(val) {
20-
if (this.#hasConnected) {
21-
return;
22-
}
2325
var parsedVal = parseInt(val, 10);
2426
if (!isNaN(parsedVal)) {
2527
this.setAttribute('row', parsedVal);
2628
}
2729
}
2830
get col() {
29-
return +(this.hasAttribute('col') ? this.getAttribute('col') : 0);
31+
/* jshint ignore:start */
32+
return +this.#initialCol;
33+
/* jshint ignore:end */
3034
}
3135
set col(val) {
32-
if (this.#hasConnected) {
33-
return;
34-
}
3536
var parsedVal = parseInt(val, 10);
3637
if (!isNaN(parsedVal)) {
3738
this.setAttribute('col', parsedVal);
3839
}
3940
}
4041
get zoom() {
41-
// for templated or queried features ** native zoom is only used for zoomTo() **
42-
let meta = {},
43-
metaEl = this.getMeta('zoom');
44-
if (metaEl)
45-
meta = Util._metaContentToObject(metaEl.getAttribute('content'));
46-
if (this._parentElement.nodeName === 'MAP-LINK') {
47-
// nativeZoom = zoom attribute || (sd.map-meta zoom 'value' || 'max') || this._initialZoom
48-
return +(this.hasAttribute('zoom')
49-
? this.getAttribute('zoom')
50-
: meta.value
51-
? meta.value
52-
: meta.max
53-
? meta.max
54-
: this._initialZoom);
55-
} else {
56-
// for "static" features
57-
// nativeZoom zoom attribute || this._initialZoom
58-
// NOTE we don't use map-meta here, because the map-meta is the minimum
59-
// zoom bounds for the layer, and is extended by additional features
60-
// if added / removed during layer lifetime
61-
return +(this.hasAttribute('zoom')
62-
? this.getAttribute('zoom')
63-
: this._initialZoom);
64-
}
42+
/* jshint ignore:start */
43+
return this.#initialZoom;
44+
/* jshint ignore:end */
6545
}
6646
set zoom(val) {
67-
if (this.#hasConnected) {
68-
return;
69-
}
7047
var parsedVal = parseInt(val, 10);
7148
if (!isNaN(parsedVal) && parsedVal >= 0 && parsedVal <= 25) {
7249
this.setAttribute('zoom', parsedVal);
@@ -93,18 +70,47 @@ export class HTMLTileElement extends HTMLElement {
9370
// Always call super first in constructor
9471
super();
9572
}
73+
getAttribute(name) {
74+
if (this.#hasConnected /* jshint ignore:line */) {
75+
switch (name) {
76+
case 'row':
77+
return String(this.#initialRow); /* jshint ignore:line */
78+
break;
79+
case 'col':
80+
return String(this.#initialCol); /* jshint ignore:line */
81+
break;
82+
case 'zoom':
83+
return String(this.#initialZoom); /* jshint ignore:line */
84+
break;
85+
}
86+
}
87+
return super.getAttribute(name);
88+
}
89+
setAttribute(name, value) {
90+
if (this.#hasConnected /* jshint ignore:line */) {
91+
switch (name) {
92+
case 'row':
93+
case 'col':
94+
case 'zoom':
95+
return;
96+
}
97+
}
98+
super.setAttribute(name, value);
99+
}
96100
async connectedCallback() {
97101
// initialization is done in connectedCallback, attribute initialization
98102
// calls (which happen first) are effectively ignored, so we should be able
99103
// to rely on them being all correctly set by this time e.g. zoom, row, col
100104
// all now have a value that together identify this tiled bit of space
105+
// row,col,zoom can't / shouldn't change
101106
/* jshint ignore:start */
107+
this.#initialZoom = this.hasAttribute('zoom')
108+
? +this.getAttribute('zoom')
109+
: this.getMapEl().zoom;
110+
this.#initialRow = this.hasAttribute('row') ? +this.getAttribute('row') : 0;
111+
this.#initialCol = this.hasAttribute('col') ? +this.getAttribute('col') : 0;
102112
this.#hasConnected = true;
103113
/* jshint ignore:end */
104-
// set the initial zoom of the map when features connected
105-
// used for fallback zoom getter for static features
106-
// ~copied from map-feature.js
107-
this._initialZoom = this.getMapEl().zoom;
108114
// Get parent element to determine how to handle the tile
109115
// Need to handle shadow DOM correctly like map-feature does
110116
this._parentElement =
@@ -182,11 +188,6 @@ export class HTMLTileElement extends HTMLElement {
182188
case 'row':
183189
case 'col':
184190
case 'zoom':
185-
// Prevent changes to row, col, and zoom after initialization
186-
if (oldValue !== newValue) {
187-
this.setAttribute(name, oldValue);
188-
return;
189-
}
190191
break;
191192
case 'src':
192193
if (oldValue !== newValue) {

test/e2e/api/matchMedia/map-bounding-box.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ test.describe('matchMedia map-bounding-box tests', () => {
88
page =
99
context.pages().find((page) => page.url() === 'about:blank') ||
1010
(await context.newPage());
11-
page = await context.newPage();
1211
await page.goto('map-bounding-box.html');
1312
});
1413

@@ -35,14 +34,15 @@ test.describe('matchMedia map-bounding-box tests', () => {
3534

3635
// the layer should be shown as we zoom out again
3736
await zoomOut.click();
38-
await page.waitForTimeout(250);
37+
await page.waitForTimeout(500);
3938
await expect(layer).not.toHaveAttribute('hidden');
4039

4140
// move the map so that the layer is out of the map's extent
4241
await page.evaluate(() => {
4342
const map = document.querySelector('mapml-viewer');
4443
map.zoomTo(0, 0, 13);
4544
});
45+
await page.waitForTimeout(500);
4646
await expect(layer).toHaveAttribute('hidden');
4747

4848
// move the map back so that the layer is within of the map's extent
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

test/e2e/elements/map-tile/test-immutable.test.js renamed to test/e2e/elements/map-tile/map-tile-row-col-zoom-immutable.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ test.describe('Map Tile Immutable Attributes Tests', () => {
4949
});
5050

5151
expect(afterSetAttribute.row).toBe('1'); // Should remain unchanged
52-
expect(afterSetAttribute.col).toBe('1'); // Should remain unchanged
52+
expect(afterSetAttribute.col).toBe('1'); // Should remain unchanged
5353
expect(afterSetAttribute.zoom).toBe('2'); // Should remain unchanged
5454

5555
// Try to change via property setters - should also fail
@@ -84,7 +84,8 @@ test.describe('Map Tile Immutable Attributes Tests', () => {
8484
});
8585

8686
// Change src attribute
87-
const newSrc = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mNkYPhfDwAChAGA6ej9MgAAAABJRU5ErkJggg==';
87+
const newSrc =
88+
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mNkYPhfDwAChAGA6ej9MgAAAABJRU5ErkJggg==';
8889
await page.evaluate((src) => {
8990
const tile = document.getElementById('test-tile');
9091
tile.setAttribute('src', src);
@@ -102,4 +103,4 @@ test.describe('Map Tile Immutable Attributes Tests', () => {
102103
test.afterAll(async () => {
103104
await context.close();
104105
});
105-
});
106+
});

0 commit comments

Comments
 (0)