Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor custom events dispatched by the Viewer #872

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class MapLayer extends HTMLElement {
}
new Promise((resolve, reject) => {
this.addEventListener(
'changestyle',
'map-changestyle',
function (e) {
e.stopPropagation();
this.src = e.detail.src;
Expand Down Expand Up @@ -200,9 +200,6 @@ export class MapLayer extends HTMLElement {
this.src = e.detail.href;
} else {
console.log(e);
this.dispatchEvent(
new CustomEvent('error', { detail: { target: this } })
);
}
});
}
Expand Down
1 change: 1 addition & 0 deletions src/map-extent.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class MapExtent extends HTMLElement {
this._handleChange();
this._calculateBounds();
this._layerControlCheckbox.checked = newValue !== null;
this.dispatchEvent(new CustomEvent('map-change'));
break;
case 'opacity':
if (oldValue !== newValue) {
Expand Down
43 changes: 7 additions & 36 deletions src/mapml-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,19 +612,6 @@ export class MapViewer extends HTMLElement {
_setUpEvents() {
this.addEventListener('drop', this._dropHandler, false);
this.addEventListener('dragover', this._dragoverHandler, false);
this.addEventListener(
'change',
function (e) {
if (e.target.tagName === 'LAYER-') {
this.dispatchEvent(
new CustomEvent('layerchange', {
details: { target: this, originalEvent: e }
})
);
}
},
false
);

this.parentElement.addEventListener('keyup', function (e) {
if (
Expand Down Expand Up @@ -663,7 +650,7 @@ export class MapViewer extends HTMLElement {
'locationfound',
function (e) {
this.dispatchEvent(
new CustomEvent('maplocationfound', {
new CustomEvent('map-locationfound', {
detail: { latlng: e.latlng, accuracy: e.accuracy }
})
);
Expand All @@ -674,7 +661,7 @@ export class MapViewer extends HTMLElement {
'locationerror',
function (e) {
this.dispatchEvent(
new CustomEvent('locationerror', { detail: { error: e.message } })
new CustomEvent('map-locationerror', { detail: { error: e.message } })
);
},
this
Expand All @@ -688,22 +675,6 @@ export class MapViewer extends HTMLElement {
},
this
);
this._map.on(
'preclick',
function (e) {
this.dispatchEvent(
new CustomEvent('preclick', {
detail: {
lat: e.latlng.lat,
lon: e.latlng.lng,
x: e.containerPoint.x,
y: e.containerPoint.y
}
})
);
},
this
);
this._map.on(
'click',
function (e) {
Expand Down Expand Up @@ -837,7 +808,7 @@ export class MapViewer extends HTMLElement {
function () {
this._updateMapCenter();
this.dispatchEvent(
new CustomEvent('movestart', { detail: { target: this } })
new CustomEvent('map-movestart', { detail: { target: this } })
);
},
this
Expand All @@ -847,7 +818,7 @@ export class MapViewer extends HTMLElement {
function () {
this._updateMapCenter();
this.dispatchEvent(
new CustomEvent('move', { detail: { target: this } })
new CustomEvent('map-move', { detail: { target: this } })
);
},
this
Expand All @@ -868,7 +839,7 @@ export class MapViewer extends HTMLElement {
function () {
this._updateMapCenter();
this.dispatchEvent(
new CustomEvent('zoomstart', { detail: { target: this } })
new CustomEvent('map-zoomstart', { detail: { target: this } })
);
},
this
Expand All @@ -878,7 +849,7 @@ export class MapViewer extends HTMLElement {
function () {
this._updateMapCenter();
this.dispatchEvent(
new CustomEvent('zoom', { detail: { target: this } })
new CustomEvent('map-zoom', { detail: { target: this } })
);
},
this
Expand All @@ -888,7 +859,7 @@ export class MapViewer extends HTMLElement {
function () {
this._updateMapCenter();
this.dispatchEvent(
new CustomEvent('zoomend', { detail: { target: this } })
new CustomEvent('map-zoomend', { detail: { target: this } })
);
},
this
Expand Down
11 changes: 2 additions & 9 deletions src/mapml/layers/Crosshair.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ export var Crosshair = L.Layer.extend({
map.isFocused = false;
this._isQueryable = false;

map.on(
'layerchange layeradd layerremove overlayremove',
this._toggleEvents,
this
);
map.on('layeradd layerremove overlayremove', this._toggleEvents, this);
map.on('popupopen', this._isMapFocused, this);
L.DomEvent.on(
map._container,
Expand All @@ -30,10 +26,7 @@ export var Crosshair = L.Layer.extend({
},

onRemove: function (map) {
map.off(
'layerchange layeradd layerremove overlayremove',
this._toggleEvents
);
map.off('layeradd layerremove overlayremove', this._toggleEvents);
map.off('popupopen', this._isMapFocused);
L.DomEvent.off(
map._container,
Expand Down
2 changes: 1 addition & 1 deletion src/mapml/layers/MapMLLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ export var MapMLLayer = L.Layer.extend({
var changeStyle = function (e) {
L.DomEvent.stop(e);
layer._layerEl.dispatchEvent(
new CustomEvent('changestyle', {
new CustomEvent('map-changestyle', {
detail: {
src: e.target.getAttribute('data-href')
}
Expand Down
2 changes: 1 addition & 1 deletion src/mapml/layers/TemplatedTileLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export var TemplatedTileLayer = L.TileLayer.extend({
L.DomUtil.addClass(tileGroup, 'leaflet-tile');

this._template.linkEl.dispatchEvent(
new CustomEvent('tileloadstart', {
new CustomEvent('map-tileloadstart', {
detail: {
x: coords.x,
y: coords.y,
Expand Down
43 changes: 7 additions & 36 deletions src/web-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,19 +657,6 @@ export class WebMap extends HTMLMapElement {
_setUpEvents() {
this.addEventListener('drop', this._dropHandler, false);
this.addEventListener('dragover', this._dragoverHandler, false);
this.addEventListener(
'change',
function (e) {
if (e.target.tagName === 'LAYER-') {
this.dispatchEvent(
new CustomEvent('layerchange', {
details: { target: this, originalEvent: e }
})
);
}
},
false
);

let mapEl = this;
this.parentElement.addEventListener('keyup', function (e) {
Expand Down Expand Up @@ -710,7 +697,7 @@ export class WebMap extends HTMLMapElement {
'locationfound',
function (e) {
this.dispatchEvent(
new CustomEvent('maplocationfound', {
new CustomEvent('map-locationfound', {
detail: { latlng: e.latlng, accuracy: e.accuracy }
})
);
Expand All @@ -721,7 +708,7 @@ export class WebMap extends HTMLMapElement {
'locationerror',
function (e) {
this.dispatchEvent(
new CustomEvent('locationerror', { detail: { error: e.message } })
new CustomEvent('map-locationerror', { detail: { error: e.message } })
);
},
this
Expand All @@ -735,22 +722,6 @@ export class WebMap extends HTMLMapElement {
},
this
);
this._map.on(
'preclick',
function (e) {
this.dispatchEvent(
new CustomEvent('preclick', {
detail: {
lat: e.latlng.lat,
lon: e.latlng.lng,
x: e.containerPoint.x,
y: e.containerPoint.y
}
})
);
},
this
);
this._map.on(
'click',
function (e) {
Expand Down Expand Up @@ -884,7 +855,7 @@ export class WebMap extends HTMLMapElement {
function () {
this._updateMapCenter();
this.dispatchEvent(
new CustomEvent('movestart', { detail: { target: this } })
new CustomEvent('map-movestart', { detail: { target: this } })
);
},
this
Expand All @@ -894,7 +865,7 @@ export class WebMap extends HTMLMapElement {
function () {
this._updateMapCenter();
this.dispatchEvent(
new CustomEvent('move', { detail: { target: this } })
new CustomEvent('map-move', { detail: { target: this } })
);
},
this
Expand All @@ -915,7 +886,7 @@ export class WebMap extends HTMLMapElement {
function () {
this._updateMapCenter();
this.dispatchEvent(
new CustomEvent('zoomstart', { detail: { target: this } })
new CustomEvent('map-zoomstart', { detail: { target: this } })
);
},
this
Expand All @@ -925,7 +896,7 @@ export class WebMap extends HTMLMapElement {
function () {
this._updateMapCenter();
this.dispatchEvent(
new CustomEvent('zoom', { detail: { target: this } })
new CustomEvent('map-zoom', { detail: { target: this } })
);
},
this
Expand All @@ -935,7 +906,7 @@ export class WebMap extends HTMLMapElement {
function () {
this._updateMapCenter();
this.dispatchEvent(
new CustomEvent('zoomend', { detail: { target: this } })
new CustomEvent('map-zoomend', { detail: { target: this } })
);
},
this
Expand Down
10 changes: 5 additions & 5 deletions test/e2e/api/locateApi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ test.describe('Locate API Test', () => {
expect(locateAPI_lng).toEqual('-73.568');
});

test('Testing maplocationfound event', async () => {
test('Testing map-locationfound event', async () => {
const latlng = await page.evaluate(() => {
const viewer = document.querySelector('body > mapml-viewer');
return new Promise((resolve) => {
viewer.addEventListener(
'maplocationfound',
'map-locationfound',
(e) => {
resolve(e.detail.latlng);
},
Expand All @@ -57,18 +57,18 @@ test.describe('Locate API Test', () => {
expect(latlng.lng).toEqual(-73.56766530667056);
});

test('Testing locationerror event', async () => {
test('Testing map-locationerror event', async () => {
const error = await page.evaluate(() => {
const viewer = document.querySelector('body > mapml-viewer');
return new Promise((resolve) => {
viewer.addEventListener(
'locationerror',
'map-locationerror',
(e) => {
resolve(e.detail.error);
},
{ once: true }
);
const errorEvent = new CustomEvent('locationerror', {
const errorEvent = new CustomEvent('map-locationerror', {
detail: { error: 'Your location could not be determined.' }
});
viewer.dispatchEvent(errorEvent);
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/layers/clientTemplatedTileLayer.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</mapml-viewer>
<script>
let layer = document.querySelector("body > mapml-viewer > layer- > map-extent > map-link");
layer.addEventListener("tileloadstart", (e) => {
layer.addEventListener("map-tileloadstart", (e) => {
let cust = document.createElement("P");
cust.textContent = `${e.detail.x}${e.detail.y}${e.detail.zoom}`;
e.detail.appendTile(cust);
Expand Down