@@ -73,32 +73,14 @@ var RubyBench = (function() {
7373 }
7474
7575 // Plot a Highcharts chart
76- function plotChart ( graphElement , config , retryCount ) {
77- retryCount = retryCount || 0 ;
76+ function plotChart ( graphElement , config ) {
7877 initHighchartsDefaults ( ) ;
7978
80- // Ensure the element is visible and has dimensions
79+ // Ensure the element is visible before Highcharts measures it
8180 if ( ! graphElement . is ( ':visible' ) ) {
8281 graphElement . show ( ) ;
8382 }
8483
85- // Check if element has actual dimensions
86- var width = graphElement . width ( ) ;
87- var height = graphElement . height ( ) ;
88-
89- if ( width === 0 || height === 0 ) {
90- console . error ( 'Chart container has no dimensions (width: ' + width + ', height: ' + height + ')' ) ;
91- // Try to use setTimeout to allow DOM to update, but only retry once
92- if ( retryCount < 1 ) {
93- setTimeout ( function ( ) {
94- plotChart ( graphElement , config , retryCount + 1 ) ; // Retry once
95- } , 50 ) ;
96- } else {
97- graphElement . html ( '<div style="padding: 100px; text-align: center; color: red;">Chart container has no dimensions</div>' ) ;
98- }
99- return null ;
100- }
101-
10284 // Use the DOM element directly instead of ID string
10385 // graphElement is a jQuery object, so get the first DOM element
10486 var domElement = graphElement . get ( 0 ) ;
@@ -121,18 +103,47 @@ var RubyBench = (function() {
121103 function setupBenchmarkNavigation ( options ) {
122104 var activateChart = options . activateChart ;
123105 var defaultBenchmark = options . defaultBenchmark ;
106+ var enableKeyboardNavigation = options . enableKeyboardNavigation ;
124107
125- // Switch active tab and render it
126- $ ( '.activate-chart' ) . on ( 'click' , function ( event ) {
127- event . preventDefault ( ) ;
108+ function activateBenchmarkLink ( link ) {
128109 $ ( '.benchmark_navbar .nav-link' ) . removeClass ( 'active' ) ;
129- $ ( this ) . addClass ( 'active' ) ;
130- var id = $ ( this ) . data ( 'id' ) ;
110+ link . addClass ( 'active' ) ;
111+ var id = link . data ( 'id' ) ;
131112 var graphElement = $ ( "#" + id ) ;
132113 activateChart ( graphElement ) ;
133114 window . location . hash = id ;
115+ }
116+
117+ // Switch active tab and render it
118+ $ ( '.activate-chart' ) . on ( 'click' , function ( event ) {
119+ event . preventDefault ( ) ;
120+ activateBenchmarkLink ( $ ( this ) ) ;
134121 } ) ;
135122
123+ if ( enableKeyboardNavigation ) {
124+ $ ( document ) . on ( 'keydown' , function ( event ) {
125+ if ( event . key !== 'ArrowLeft' && event . key !== 'ArrowRight' ) {
126+ return ;
127+ }
128+ if ( event . altKey || event . ctrlKey || event . metaKey || event . shiftKey ) {
129+ return ;
130+ }
131+ if ( $ ( event . target ) . closest ( 'input, textarea, select, [contenteditable="true"]' ) . length > 0 ) {
132+ return ;
133+ }
134+
135+ var links = $ ( '.benchmark_navbar .nav-link' ) ;
136+ var activeIndex = links . index ( links . filter ( '.active' ) ) ;
137+ var nextIndex = activeIndex + ( event . key === 'ArrowLeft' ? - 1 : 1 ) ;
138+ if ( activeIndex < 0 || nextIndex < 0 || nextIndex >= links . length ) {
139+ return ;
140+ }
141+
142+ event . preventDefault ( ) ;
143+ activateBenchmarkLink ( links . eq ( nextIndex ) ) ;
144+ } ) ;
145+ }
146+
136147 // Handle URL hash navigation
137148 function handleHashNavigation ( ) {
138149 var url = document . location . toString ( ) ;
@@ -207,4 +218,4 @@ var RubyBench = (function() {
207218 showError : showError ,
208219 showOnlyGraph : showOnlyGraph
209220 } ;
210- } ) ( ) ;
221+ } ) ( ) ;
0 commit comments