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

Announce zoom and move and implementing map boundaries #533

Merged
merged 6 commits into from
Nov 23, 2021
Merged
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
20 changes: 18 additions & 2 deletions src/mapml-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ export class MapViewer extends HTMLElement {
constructor() {
// Always call super first in constructor
super();

this._source = this.outerHTML;
let tmpl = document.createElement('template');
tmpl.innerHTML =
Expand All @@ -110,7 +109,10 @@ export class MapViewer extends HTMLElement {

let shadowRoot = this.attachShadow({mode: 'open'});
this._container = document.createElement('div');


let output = "<output role='status' aria-live='polite' aria-atomic='true' class='mapml-screen-reader-output'></output>";
this._container.insertAdjacentHTML("beforeend", output);

// Set default styles for the map element.
let mapDefaultCSS = document.createElement('style');
mapDefaultCSS.innerHTML =
Expand Down Expand Up @@ -198,6 +200,7 @@ export class MapViewer extends HTMLElement {
projection: this.projection,
query: true,
contextMenu: true,
announceMovement: M.options.announceMovement,
mapEl: this,
crs: M[this.projection],
zoom: this.zoom,
Expand Down Expand Up @@ -361,6 +364,19 @@ export class MapViewer extends HTMLElement {
this.dispatchEvent(new CustomEvent("layerchange", {details:{target: this, originalEvent: e}}));
}
}, false);

this.parentElement.addEventListener('keyup', function (e) {
if(e.keyCode === 9 && document.activeElement.nodeName === "MAPML-VIEWER"){
document.activeElement.dispatchEvent(new CustomEvent('mapfocused', {detail:
{target: this}}));
}
});
this.parentElement.addEventListener('mousedown', function (e) {
if(document.activeElement.nodeName === "MAPML-VIEWER"){
document.activeElement.dispatchEvent(new CustomEvent('mapfocused', {detail:
{target: this}}));
}
});
this._map.on('load',
function () {
this.dispatchEvent(new CustomEvent('load', {detail: {target: this}}));
Expand Down
12 changes: 11 additions & 1 deletion src/mapml.css
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
.mapml-layer-item-name a {
color: revert;
}

.leaflet-top .leaflet-control {
margin-top: 5px;
}
Expand Down Expand Up @@ -703,3 +703,13 @@ label.mapml-layer-item-toggle {
padding-inline-start: 2rem;
padding-inline-end: 1rem;
}

.mapml-screen-reader-output {
clip: rect(0 0 0 0);
clip-path: inset(50%);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}
130 changes: 130 additions & 0 deletions src/mapml/handlers/AnnounceMovement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
export var AnnounceMovement = L.Handler.extend({
addHooks: function () {
this._map.on({
layeradd: this.totalBounds,
layerremove: this.totalBounds,
});

this._map.options.mapEl.addEventListener('moveend', this.announceBounds);
this._map.dragging._draggable.addEventListener('dragstart', this.dragged);
this._map.options.mapEl.addEventListener('mapfocused', this.focusAnnouncement);
},
removeHooks: function () {
this._map.off({
layeradd: this.totalBounds,
layerremove: this.totalBounds,
});

this._map.options.mapEl.removeEventListener('moveend', this.announceBounds);
this._map.dragging._draggable.removeEventListener('dragstart', this.dragged);
this._map.options.mapEl.removeEventListener('mapfocused', this.focusAnnouncement);
},

focusAnnouncement: function () {
let mapEl = this;
setTimeout(function (){
let el = mapEl.querySelector(".mapml-web-map") ? mapEl.querySelector(".mapml-web-map").shadowRoot.querySelector(".leaflet-container") :
mapEl.shadowRoot.querySelector(".leaflet-container");

let mapZoom = mapEl._map.getZoom();
let location = M.gcrsToTileMatrix(mapEl);
let standard = M.options.locale.amZoom + " " + mapZoom + " " + M.options.locale.amColumn + " " + location[0] + " " + M.options.locale.amRow + " " + location[1];

if(mapZoom === mapEl._map._layersMaxZoom){
standard = M.options.locale.amMaxZoom + " " + standard;
}
else if(mapZoom === mapEl._map._layersMinZoom){
standard = M.options.locale.amMinZoom + " " + standard;
}

el.setAttribute("aria-roledescription", "region " + standard);
setTimeout(function () {
el.removeAttribute("aria-roledescription");
}, 2000);
}, 0);
},

announceBounds: function () {
if(this._traversalCall > 0){
return;
}
let mapZoom = this._map.getZoom();
let mapBounds = M.pixelToPCRSBounds(this._map.getPixelBounds(),mapZoom,this._map.options.projection);

let visible = true;
if(this._map.totalLayerBounds){
visible = mapZoom <= this._map._layersMaxZoom && mapZoom >= this._map._layersMinZoom &&
this._map.totalLayerBounds.overlaps(mapBounds);
}

let output = this.querySelector(".mapml-web-map") ? this.querySelector(".mapml-web-map").shadowRoot.querySelector(".mapml-screen-reader-output") :
this.shadowRoot.querySelector(".mapml-screen-reader-output");

//GCRS to TileMatrix
let location = M.gcrsToTileMatrix(this);
let standard = M.options.locale.amZoom + " " + mapZoom + " " + M.options.locale.amColumn + " " + location[0] + " " + M.options.locale.amRow + " " + location[1];

if(!visible){
let outOfBoundsPos = this._history[this._historyIndex];
let inBoundsPos = this._history[this._historyIndex - 1];
this.back();
this._history.pop();

if(outOfBoundsPos.zoom !== inBoundsPos.zoom){
output.innerText = M.options.locale.amZoomedOut;
}
else if(this._map.dragging._draggable.wasDragged){
output.innerText = M.options.locale.amDraggedOut;
}
else if(outOfBoundsPos.x > inBoundsPos.x){
output.innerText = M.options.locale.amEastBound;
}
else if(outOfBoundsPos.x < inBoundsPos.x){
output.innerText = M.options.locale.amWestBound;
}
else if(outOfBoundsPos.y < inBoundsPos.y){
output.innerText = M.options.locale.amNorthBound;
}
else if(outOfBoundsPos.y > inBoundsPos.y){
output.innerText = M.options.locale.amSouthBound;
}

}
else{
let prevZoom = this._history[this._historyIndex - 1] ? this._history[this._historyIndex - 1].zoom : this._history[this._historyIndex].zoom;
if(mapZoom === this._map._layersMaxZoom && mapZoom !== prevZoom){
output.innerText = M.options.locale.amMaxZoom + " " + standard;
}
else if(mapZoom === this._map._layersMinZoom && mapZoom !== prevZoom){
output.innerText = M.options.locale.amMinZoom + " " + standard;
}
else {
output.innerText = standard;
}
}
this._map.dragging._draggable.wasDragged = false;
},

totalBounds: function () {
let layers = Object.keys(this._layers);
let bounds = L.bounds();

layers.forEach(i => {
if(this._layers[i].layerBounds){
if(!bounds){
let point = this._layers[i].layerBounds.getCenter();
bounds = L.bounds(point, point);
}
bounds.extend(this._layers[i].layerBounds.min);
bounds.extend(this._layers[i].layerBounds.max);
}
});

this.totalLayerBounds = bounds;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The calculated bounds seems to be static, if a layer is deselected the value doesn't reflect that. If a layer is deselected I'm not sure if it should be considered, unless that was something that was decided on.

},

dragged: function () {
this.wasDragged = true;
}

});
4 changes: 4 additions & 0 deletions src/mapml/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { Crosshair, crosshair } from "./layers/Crosshair";
import { Feature, feature } from "./features/feature";
import { FeatureRenderer, featureRenderer } from './features/featureRenderer';
import { FeatureGroup, featureGroup} from './features/featureGroup';
import {AnnounceMovement} from "./handlers/AnnounceMovement";
import { Options } from "./options";
import "./keyboard";

Expand Down Expand Up @@ -600,13 +601,16 @@ M.coordsToArray = Util.coordsToArray;
M.parseStylesheetAsHTML = Util.parseStylesheetAsHTML;
M.pointToPCRSPoint = Util.pointToPCRSPoint;
M.pixelToPCRSPoint = Util.pixelToPCRSPoint;
M.gcrsToTileMatrix = Util.gcrsToTileMatrix;

M.QueryHandler = QueryHandler;
M.ContextMenu = ContextMenu;
M.AnnounceMovement = AnnounceMovement;

// see https://leafletjs.com/examples/extending/extending-3-controls.html#handlers
L.Map.addInitHook('addHandler', 'query', M.QueryHandler);
L.Map.addInitHook('addHandler', 'contextMenu', M.ContextMenu);
L.Map.addInitHook('addHandler', 'announceMovement', M.AnnounceMovement);

M.MapMLLayer = MapMLLayer;
M.mapMLLayer = mapMLLayer;
Expand Down
15 changes: 15 additions & 0 deletions src/mapml/layers/DebugLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export var DebugVectors = L.LayerGroup.extend({
j = 0;

this.addLayer(this._centerVector);

for (let i of id) {
if (layers[i].layerBounds) {
let boundsArray = [
Expand All @@ -199,6 +200,20 @@ export var DebugVectors = L.LayerGroup.extend({
j++;
}
}

if(map.totalLayerBounds){
let totalBoundsArray = [
map.totalLayerBounds.min,
L.point(map.totalLayerBounds.max.x, map.totalLayerBounds.min.y),
map.totalLayerBounds.max,
L.point(map.totalLayerBounds.min.x, map.totalLayerBounds.max.y)
];

let totalBounds = projectedExtent(
totalBoundsArray,
{color: "#808080", weight: 5, opacity: 0.5, fill: false});
this.addLayer(totalBounds);
}
},

_mapLayerUpdate: function (e) {
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 @@ -57,7 +57,7 @@ export var TemplatedTileLayer = L.TileLayer.extend({
let mapBounds = M.pixelToPCRSBounds(this._map.getPixelBounds(),mapZoom,this._map.options.projection);
this.isVisible = mapZoom <= this.options.maxZoom && mapZoom >= this.options.minZoom &&
this.layerBounds.overlaps(mapBounds);
if(!(this.isVisible))return;
if(!(this.isVisible))return;
this._parentOnMoveEnd();
},
createTile: function (coords) {
Expand Down
13 changes: 12 additions & 1 deletion src/mapml/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ export var Options = {
lcOpacity: "Opacity",
btnZoomIn: "Zoom in",
btnZoomOut: "Zoom out",
btnFullScreen: "View fullscreen"
btnFullScreen: "View fullscreen",
amZoom: "zoom level",
amColumn: "column",
amRow: "row",
amMaxZoom: "At maximum zoom level, zoom in disabled",
amMinZoom: "At minimum zoom level, zoom out disabled",
amZoomedOut: "Zoomed out of bounds, returning to",
amDraggedOut: "Dragged out of bounds, returning to",
amEastBound: "Reached east bound, panning east disabled",
amWestBound: "Reached west bound, panning west disabled",
amNorthBound: "Reached north bound, panning north disabled",
amSouthBound: "Reached south bound, panning south disabled"
}
};
8 changes: 8 additions & 0 deletions src/mapml/utils/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,12 @@ export var Util = {
map.getContainer().focus();
}
},

gcrsToTileMatrix: function (mapEl) {
let point = mapEl._map.project(mapEl._map.getCenter());
let tileSize = mapEl._map.options.crs.options.crs.tile.bounds.max.y;
let column = Math.trunc(point.x / tileSize);
let row = Math.trunc(point.y / tileSize);
return [column, row];
}
};
19 changes: 18 additions & 1 deletion src/web-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ export class WebMap extends HTMLMapElement {

let shadowRoot = rootDiv.attachShadow({mode: 'open'});
this._container = document.createElement('div');


let output = "<output role='status' aria-live='polite' aria-atomic='true' class='mapml-screen-reader-output'></output>";
this._container.insertAdjacentHTML("beforeend", output);

// Set default styles for the map element.
let mapDefaultCSS = document.createElement('style');
mapDefaultCSS.innerHTML =
Expand Down Expand Up @@ -211,6 +214,7 @@ export class WebMap extends HTMLMapElement {
projection: this.projection,
query: true,
contextMenu: true,
announceMovement: M.options.announceMovement,
mapEl: this,
crs: M[this.projection],
zoom: this.zoom,
Expand Down Expand Up @@ -398,6 +402,19 @@ export class WebMap extends HTMLMapElement {
this.dispatchEvent(new CustomEvent("layerchange", {details:{target: this, originalEvent: e}}));
}
}, false);

this.parentElement.addEventListener('keyup', function (e) {
if(e.keyCode === 9 && document.activeElement.nodeName === "MAPML-VIEWER"){
document.activeElement.dispatchEvent(new CustomEvent('mapfocused', {detail:
{target: this}}));
}
});
this.parentElement.addEventListener('mousedown', function (e) {
if(document.activeElement.nodeName === "MAPML-VIEWER"){
document.activeElement.dispatchEvent(new CustomEvent('mapfocused', {detail:
{target: this}}));
}
});
this._map.on('load',
function () {
this.dispatchEvent(new CustomEvent('load', {detail: {target: this}}));
Expand Down
Loading