@@ -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