Skip to content

Commit 19fbf5d

Browse files
committed
Limit output to having 9 items at a time
1 parent 0c833bf commit 19fbf5d

File tree

2 files changed

+53
-51
lines changed

2 files changed

+53
-51
lines changed

src/mapml.css

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -807,34 +807,14 @@ label.mapml-layer-item-toggle {
807807
right: 5px;
808808
bottom: 5px;
809809
left: 5px;
810+
padding: 5px 10px;
810811
width: -webkit-fit-content;
811812
width: -moz-fit-content;
812813
width: fit-content;
813814
font: inherit;
814815
}
815-
816-
.mapml-feature-index-more-content {
817-
display: inline-block;
818-
}
819-
.mapml-feature-index-more-content > span{
820-
padding: 5px 10px 5px 0;
821-
display: inline-block;
822-
}
823-
.mapml-feature-index-content {
824-
padding: 5px 10px;
825-
display: inline-block;
826-
}
827-
.mapml-feature-index-more-content > span > span,
828816
.mapml-feature-index-content > span{
829817
display: block;
830818
font-family: monospace;
831819
}
832820

833-
.mapml-feature-index-header {
834-
font-weight: bold;
835-
text-transform: uppercase;
836-
display: inline-block;
837-
text-align: left;
838-
text-align: inline-start;
839-
line-height: 2;
840-
}

src/mapml/layers/FeatureIndex.js

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ export var FeatureIndex = L.Layer.extend({
1919

2020
this._output = L.DomUtil.create("output", "mapml-feature-index", map._container);
2121
this._body = L.DomUtil.create("span", "mapml-feature-index-content", this._output);
22-
this._moreContent = L.DomUtil.create("span", "mapml-feature-index-more-content", this._output);
23-
this._moreContent.style.display = "none";
2422

2523
map.on("layerchange layeradd layerremove overlayremove", this._toggleEvents, this);
2624
map.on('moveend focus', this._checkOverlap, this);
27-
map.on("keydown", this._toggleMoreContent, this);
25+
map.on("keydown", this._toggleContent, this);
2826
this._addOrRemoveFeatureIndex();
2927
},
3028

@@ -48,47 +46,71 @@ export var FeatureIndex = L.Layer.extend({
4846

4947
body.innerHTML = "";
5048

51-
let moreContent = this._moreContent;
52-
moreContent.innerHTML = "";
53-
49+
body.allFeatures = [];
5450
keys.forEach(i => {
5551
if(layers[i].featureAttributes && featureIndexBounds.overlaps(layers[i]._bounds)){
5652
let label = layers[i].group.getAttribute("aria-label");
5753

58-
if(index%9 === 0){
59-
let span = document.createElement("span");
60-
span.setAttribute("id", index/9);
61-
moreContent.appendChild(span);
62-
if(index === 9){
63-
body.appendChild(this._updateOutput("More results", 9));
64-
index += 1;
65-
}
54+
if (index < 8){
55+
body.appendChild(this._updateOutput(label, index, index));
6656
}
67-
68-
if(index > 9){
69-
let value = Math.floor((index - 1)/9);
70-
let span = moreContent.querySelector(`[id=${CSS.escape(value)}]`);
71-
span.appendChild(this._updateOutput(label, index));
72-
} else {
73-
body.appendChild(this._updateOutput(label, index));
57+
if (index % 7 === 0 || index === 1) {
58+
body.allFeatures.push([]);
59+
}
60+
body.allFeatures[Math.floor((index - 1) / 7)].push({label, index});
61+
if (body.allFeatures[1] && body.allFeatures[1].length === 1){
62+
body.appendChild(this._updateOutput("More results", 0, 9));
7463
}
7564
index += 1;
76-
7765
}
7866
});
67+
this._addToggleKeys();
7968
},
80-
_updateOutput: function (label, index) {
69+
70+
_updateOutput: function (label, index, key) {
8171
let span = document.createElement("span");
82-
span.innerHTML = `<kbd>${index}</kbd>` + " " + label;
72+
span.setAttribute("index", index);
73+
span.innerHTML = `<kbd>${key}</kbd>` + " " + label;
8374
return span;
8475
},
85-
_toggleMoreContent: function (e){
86-
let display = this._moreContent.style.display;
76+
77+
_addToggleKeys: function () {
78+
let allFeatures = this._body.allFeatures;
79+
for(let i = 0; i < allFeatures.length; i++){
80+
if(allFeatures[i].length === 0) return;
81+
if(allFeatures[i - 1]){
82+
let label = "Previous results";
83+
allFeatures[i].push({label});
84+
}
85+
86+
if(allFeatures[i + 1] && allFeatures[i + 1].length > 0){
87+
let label = "More results";
88+
allFeatures[i].push({label});
89+
}
90+
}
91+
},
92+
93+
_toggleContent: function (e){
94+
let body = this._body;
8795
if(e.originalEvent.keyCode === 57){
88-
if(display === "none"){
89-
this._moreContent.style.display = "inline-block";
90-
} else {
91-
this._moreContent.style.display = "none";
96+
this._newContent(body, 1);
97+
} else if(e.originalEvent.keyCode === 56){
98+
this._newContent(body, -1);
99+
}
100+
},
101+
102+
_newContent: function (body, direction) {
103+
let index = body.firstChild.getAttribute("index");
104+
let newContent = body.allFeatures[Math.floor(((index - 1) / 7) + direction)];
105+
if(newContent && newContent.length > 0){
106+
body.innerHTML = "";
107+
for(let i = 0; i < newContent.length; i++){
108+
let feature = newContent[i];
109+
let index = feature.index ? feature.index : 0;
110+
let key = i + 1;
111+
if (feature.label === "More results") key = 9;
112+
if (feature.label === "Previous results") key = 8;
113+
body.appendChild(this._updateOutput(feature.label, index, key));
92114
}
93115
}
94116
},

0 commit comments

Comments
 (0)