Skip to content

Commit b53e31a

Browse files
committed
Focus on feature when number key is pressed
1 parent 25b3d91 commit b53e31a

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/mapml/layers/FeatureIndex.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ export var FeatureIndex = L.Layer.extend({
77

88
this._output = L.DomUtil.create("output", "mapml-feature-index", map._container);
99
this._body = L.DomUtil.create("span", "mapml-feature-index-content", this._output);
10+
this._body.index = 0;
1011

1112
map.on("layerchange layeradd layerremove overlayremove", this._toggleEvents, this);
1213
map.on('moveend focus', this._checkOverlap, this);
13-
map.on("keydown", this._toggleContent, this);
14+
map.on("keydown", this._onKeyDown, this);
1415
this._addOrRemoveFeatureIndex();
1516
},
1617

@@ -33,19 +34,21 @@ export var FeatureIndex = L.Layer.extend({
3334
let body = this._body;
3435

3536
body.innerHTML = "";
37+
body.index = 0;
3638

3739
body.allFeatures = [];
3840
keys.forEach(i => {
3941
if(layers[i].featureAttributes && featureIndexBounds.overlaps(layers[i]._bounds)){
40-
let label = layers[i].group.getAttribute("aria-label");
42+
let group = layers[i].group;
43+
let label = group.getAttribute("aria-label");
4144

4245
if (index < 8){
4346
body.appendChild(this._updateOutput(label, index, index));
4447
}
4548
if (index % 7 === 0 || index === 1) {
4649
body.allFeatures.push([]);
4750
}
48-
body.allFeatures[Math.floor((index - 1) / 7)].push({label, index});
51+
body.allFeatures[Math.floor((index - 1) / 7)].push({label, index, group});
4952
if (body.allFeatures[1] && body.allFeatures[1].length === 1){
5053
body.appendChild(this._updateOutput("More results", 0, 9));
5154
}
@@ -78,12 +81,16 @@ export var FeatureIndex = L.Layer.extend({
7881
}
7982
},
8083

81-
_toggleContent: function (e){
84+
_onKeyDown: function (e){
8285
let body = this._body;
83-
if(e.originalEvent.keyCode === 57){
84-
this._newContent(body, 1);
86+
let key = e.originalEvent.keyCode;
87+
if (key >= 49 && key <= 55){
88+
let group = body.allFeatures[body.index][key - 49].group;
89+
if (group) group.focus();
8590
} else if(e.originalEvent.keyCode === 56){
8691
this._newContent(body, -1);
92+
} else if(e.originalEvent.keyCode === 57){
93+
this._newContent(body, 1);
8794
}
8895
},
8996

@@ -92,6 +99,7 @@ export var FeatureIndex = L.Layer.extend({
9299
let newContent = body.allFeatures[Math.floor(((index - 1) / 7) + direction)];
93100
if(newContent && newContent.length > 0){
94101
body.innerHTML = "";
102+
body.index += direction;
95103
for(let i = 0; i < newContent.length; i++){
96104
let feature = newContent[i];
97105
let index = feature.index ? feature.index : 0;

0 commit comments

Comments
 (0)