diff --git a/src/components/modebar/buttons.js b/src/components/modebar/buttons.js index a496dd74f34..5a9fb54164c 100644 --- a/src/components/modebar/buttons.js +++ b/src/components/modebar/buttons.js @@ -199,7 +199,7 @@ function handleCartesian(gd, ev) { var fullLayout = gd._fullLayout; var aobj = {}; var axList = axisIds.list(gd, null, true); - var allSpikesEnabled = 'on'; + var allSpikesEnabled = fullLayout._cartesianSpikesEnabled; var ax, i; @@ -214,8 +214,9 @@ function handleCartesian(gd, ev) { if(!ax.fixedrange) { axName = ax._name; - if(val === 'auto') aobj[axName + '.autorange'] = true; - else if(val === 'reset') { + if(val === 'auto') { + aobj[axName + '.autorange'] = true; + } else if(val === 'reset') { if(ax._rangeInitial === undefined) { aobj[axName + '.autorange'] = true; } else { @@ -223,6 +224,8 @@ function handleCartesian(gd, ev) { aobj[axName + '.range[0]'] = rangeInitial[0]; aobj[axName + '.range[1]'] = rangeInitial[1]; } + + // N.B. "reset" also resets showspikes if(ax._showSpikeInitial !== undefined) { aobj[axName + '.showspikes'] = ax._showSpikeInitial; if(allSpikesEnabled === 'on' && !ax._showSpikeInitial) { @@ -245,25 +248,18 @@ function handleCartesian(gd, ev) { } } } - fullLayout._cartesianSpikesEnabled = allSpikesEnabled; } else { // if ALL traces have orientation 'h', 'hovermode': 'x' otherwise: 'y' if(astr === 'hovermode' && (val === 'x' || val === 'y')) { val = fullLayout._isHoriz ? 'y' : 'x'; button.setAttribute('data-val', val); - } else if(astr === 'hovermode' && val === 'closest') { - for(i = 0; i < axList.length; i++) { - ax = axList[i]; - if(allSpikesEnabled === 'on' && !ax.showspikes) { - allSpikesEnabled = 'off'; - } - } - fullLayout._cartesianSpikesEnabled = allSpikesEnabled; } aobj[astr] = val; } + fullLayout._cartesianSpikesEnabled = allSpikesEnabled; + Registry.call('_guiRelayout', gd, aobj); } @@ -581,26 +577,22 @@ modeBarButtons.toggleSpikelines = { val: 'on', click: function(gd) { var fullLayout = gd._fullLayout; + var allSpikesEnabled = fullLayout._cartesianSpikesEnabled; - fullLayout._cartesianSpikesEnabled = fullLayout._cartesianSpikesEnabled === 'on' ? 'off' : 'on'; - - var aobj = setSpikelineVisibility(gd); - - Registry.call('_guiRelayout', gd, aobj); + fullLayout._cartesianSpikesEnabled = allSpikesEnabled === 'on' ? 'off' : 'on'; + Registry.call('_guiRelayout', gd, setSpikelineVisibility(gd)); } }; function setSpikelineVisibility(gd) { var fullLayout = gd._fullLayout; + var areSpikesOn = fullLayout._cartesianSpikesEnabled === 'on'; var axList = axisIds.list(gd, null, true); var aobj = {}; - var ax, axName; - for(var i = 0; i < axList.length; i++) { - ax = axList[i]; - axName = ax._name; - aobj[axName + '.showspikes'] = fullLayout._cartesianSpikesEnabled === 'on' ? true : ax._showSpikeInitial; + var ax = axList[i]; + aobj[ax._name + '.showspikes'] = areSpikesOn ? true : ax._showSpikeInitial; } return aobj; diff --git a/test/jasmine/tests/modebar_test.js b/test/jasmine/tests/modebar_test.js index eca3ff4ef5c..de6fa2e245d 100644 --- a/test/jasmine/tests/modebar_test.js +++ b/test/jasmine/tests/modebar_test.js @@ -1201,6 +1201,7 @@ describe('ModeBar', function() { expect(gd._fullLayout.hovermode).toBe('x'); assertActive(hovermodeButtons, buttonCompare); }); + it('should makes spikelines visible', function() { buttonToggle.click(); expect(gd._fullLayout._cartesianSpikesEnabled).toBe('on'); @@ -1208,6 +1209,7 @@ describe('ModeBar', function() { buttonToggle.click(); expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off'); }); + it('should not become disabled when hovermode is switched off closest', function() { buttonToggle.click(); expect(gd._fullLayout._cartesianSpikesEnabled).toBe('on'); @@ -1215,6 +1217,7 @@ describe('ModeBar', function() { buttonCompare.click(); expect(gd._fullLayout._cartesianSpikesEnabled).toBe('on'); }); + it('should keep the state on changing the hovermode', function() { buttonToggle.click(); expect(gd._fullLayout._cartesianSpikesEnabled).toBe('on'); @@ -1228,6 +1231,36 @@ describe('ModeBar', function() { buttonClosest.click(); expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off'); }); + + it('should work after clicking on "autoScale2d"', function() { + var buttonAutoScale = selectButton(modeBar, 'autoScale2d'); + expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off'); + + buttonAutoScale.click(); + expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off'); + + buttonToggle.click(); + expect(gd._fullLayout._cartesianSpikesEnabled).toBe('on'); + + buttonToggle.click(); + expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off'); + }); + + it('should work after clicking on "resetScale2d"', function() { + var buttonResetScale = selectButton(modeBar, 'resetScale2d'); + expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off'); + + buttonToggle.click(); + expect(gd._fullLayout._cartesianSpikesEnabled).toBe('on'); + + buttonResetScale.click(); + expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off'); + + buttonToggle.click(); + expect(gd._fullLayout._cartesianSpikesEnabled).toBe('on'); + buttonToggle.click(); + expect(gd._fullLayout._cartesianSpikesEnabled).toBe('off'); + }); }); });