Skip to content

Commit b927361

Browse files
authored
Switch dashboard categories with the arrow keys (#19)
Extract the benchmark pages' left/right arrow-key handling into a reusable RubyBench.setupArrowKeyNavigation and use it to move between Headline, Other, and Micro on the top page.
1 parent 88bb768 commit b927361

2 files changed

Lines changed: 38 additions & 21 deletions

File tree

‎source/_dashboard.html.haml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@
138138
activateChart($('#' + id));
139139
});
140140

141+
// Switch categories with the left/right arrow keys
142+
RubyBench.setupArrowKeyNavigation(function() {
143+
return $('.category_switch');
144+
}, function(categorySwitch) {
145+
categorySwitch.trigger('click');
146+
});
147+
141148
// Switch active tab and render it
142149
$('.activate-chart').on('click', function(event) {
143150
event.preventDefault();

‎source/javascripts/site.js‎

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,33 @@ var RubyBench = (function() {
100100
}
101101
}
102102

103+
// Move between the .active element's siblings with the left/right arrow keys.
104+
// `links` is a function returning the elements to navigate, and `activate` is
105+
// called with the element that an arrow key moved to.
106+
function setupArrowKeyNavigation(links, activate) {
107+
$(document).on('keydown', function(event) {
108+
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') {
109+
return;
110+
}
111+
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
112+
return;
113+
}
114+
if ($(event.target).closest('input, textarea, select, [contenteditable="true"]').length > 0) {
115+
return;
116+
}
117+
118+
var elements = links();
119+
var activeIndex = elements.index(elements.filter('.active'));
120+
var nextIndex = activeIndex + (event.key === 'ArrowLeft' ? -1 : 1);
121+
if (activeIndex < 0 || nextIndex < 0 || nextIndex >= elements.length) {
122+
return;
123+
}
124+
125+
event.preventDefault();
126+
activate(elements.eq(nextIndex));
127+
});
128+
}
129+
103130
// Handle benchmark navigation
104131
function setupBenchmarkNavigation(options) {
105132
var activateChart = options.activateChart;
@@ -122,27 +149,9 @@ var RubyBench = (function() {
122149
});
123150

124151
if (enableKeyboardNavigation) {
125-
$(document).on('keydown', function(event) {
126-
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') {
127-
return;
128-
}
129-
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
130-
return;
131-
}
132-
if ($(event.target).closest('input, textarea, select, [contenteditable="true"]').length > 0) {
133-
return;
134-
}
135-
136-
var links = $('.benchmark_navbar .nav-link');
137-
var activeIndex = links.index(links.filter('.active'));
138-
var nextIndex = activeIndex + (event.key === 'ArrowLeft' ? -1 : 1);
139-
if (activeIndex < 0 || nextIndex < 0 || nextIndex >= links.length) {
140-
return;
141-
}
142-
143-
event.preventDefault();
144-
activateBenchmarkLink(links.eq(nextIndex));
145-
});
152+
setupArrowKeyNavigation(function() {
153+
return $('.benchmark_navbar .nav-link');
154+
}, activateBenchmarkLink);
146155
}
147156

148157
// Handle URL hash navigation
@@ -211,6 +220,7 @@ var RubyBench = (function() {
211220
initHighchartsDefaults: initHighchartsDefaults,
212221
createBaseChartConfig: createBaseChartConfig,
213222
plotChart: plotChart,
223+
setupArrowKeyNavigation: setupArrowKeyNavigation,
214224
setupBenchmarkNavigation: setupBenchmarkNavigation,
215225
setupMetricTypeHandlers: setupMetricTypeHandlers,
216226
getActiveBenchmarkId: getActiveBenchmarkId,

0 commit comments

Comments
 (0)