From 1863f1cddfb0f3eb790c0a0f968dcbd1ebc5ea28 Mon Sep 17 00:00:00 2001 From: Grégoire Aubert Date: Wed, 4 Oct 2017 12:00:56 +0200 Subject: SONAR-9787 Allow to navigate back to the issue from the last issue locations --- .../sonar-web/src/main/js/apps/issues/actions.js | 23 ++++++++++++++++++---- .../src/main/js/apps/issues/components/App.js | 4 ++-- .../apps/issues/components/IssuesSourceViewer.js | 12 +++++++++-- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/issues/actions.js b/server/sonar-web/src/main/js/apps/issues/actions.js index 2303befa564..6f14185dd6c 100644 --- a/server/sonar-web/src/main/js/apps/issues/actions.js +++ b/server/sonar-web/src/main/js/apps/issues/actions.js @@ -21,12 +21,14 @@ /*:: import type { State } from './components/App'; */ export const enableLocationsNavigator = (state /*: State */) => { - const { openIssue } = state; + const { openIssue, selectedLocationIndex } = state; if (openIssue && (openIssue.secondaryLocations.length > 0 || openIssue.flows.length > 0)) { return { locationsNavigator: true, selectedFlowIndex: state.selectedFlowIndex || (openIssue.flows.length > 0 ? 0 : null), - selectedLocationIndex: state.selectedLocationIndex || 0 + // Also reset index = -1 to 0, we don't want to start on the issue when enabling the location navigator + selectedLocationIndex: + !selectedLocationIndex || selectedLocationIndex < 0 ? 0 : selectedLocationIndex }; } }; @@ -57,15 +59,28 @@ export const selectNextLocation = (state /*: State */) => { if (openIssue) { const locations = selectedFlowIndex != null ? openIssue.flows[selectedFlowIndex] : openIssue.secondaryLocations; + const lastLocationIdx = locations.length - 1; + if (index === lastLocationIdx) { + // -1 to jump back to the issue itself + return { selectedLocationIndex: -1 }; + } return { - selectedLocationIndex: index != null && locations.length > index + 1 ? index + 1 : index + selectedLocationIndex: index != null && index < lastLocationIdx ? index + 1 : index }; } }; export const selectPreviousLocation = (state /*: State */) => { - const { selectedLocationIndex: index, openIssue } = state; + const { selectedFlowIndex, selectedLocationIndex: index, openIssue } = state; if (openIssue) { + if (index === -1) { + const locations = + selectedFlowIndex != null + ? openIssue.flows[selectedFlowIndex] + : openIssue.secondaryLocations; + const lastLocationIdx = locations.length - 1; + return { selectedLocationIndex: lastLocationIdx }; + } return { selectedLocationIndex: index != null && index > 0 ? index - 1 : index }; } }; diff --git a/server/sonar-web/src/main/js/apps/issues/components/App.js b/server/sonar-web/src/main/js/apps/issues/components/App.js index f5f83df90e7..f7543b877c7 100644 --- a/server/sonar-web/src/main/js/apps/issues/components/App.js +++ b/server/sonar-web/src/main/js/apps/issues/components/App.js @@ -236,11 +236,11 @@ export default class App extends React.PureComponent { event.preventDefault(); this.setState(actions.enableLocationsNavigator); } else if (event.keyCode === 40 && event.altKey) { - // alt + up + // alt + down event.preventDefault(); this.selectNextLocation(); } else if (event.keyCode === 38 && event.altKey) { - // alt + down + // alt + up event.preventDefault(); this.selectPreviousLocation(); } else if (event.keyCode === 37 && event.altKey) { diff --git a/server/sonar-web/src/main/js/apps/issues/components/IssuesSourceViewer.js b/server/sonar-web/src/main/js/apps/issues/components/IssuesSourceViewer.js index 06ba694e1cd..9e00d1e57f7 100644 --- a/server/sonar-web/src/main/js/apps/issues/components/IssuesSourceViewer.js +++ b/server/sonar-web/src/main/js/apps/issues/components/IssuesSourceViewer.js @@ -43,9 +43,14 @@ export default class IssuesSourceViewer extends React.PureComponent { /*:: props: Props; */ componentDidUpdate(prevProps /*: Props */) { + const { openIssue, selectedLocationIndex } = this.props; + + // Scroll back to the issue when the selected location is set to -1 + const shouldScrollBackToIssue = + selectedLocationIndex === -1 && selectedLocationIndex !== prevProps.selectedLocationIndex; if ( - prevProps.openIssue !== this.props.openIssue && - prevProps.openIssue.component === this.props.openIssue.component + prevProps.openIssue.component === openIssue.component && + (prevProps.openIssue !== openIssue || shouldScrollBackToIssue) ) { this.scrollToIssue(); } @@ -77,9 +82,12 @@ export default class IssuesSourceViewer extends React.PureComponent { let locationMessage = undefined; let locationLine = undefined; + + // We don't want to display a location message when selected location is -1 if ( locations != null && selectedLocationIndex != null && + selectedLocationIndex >= 0 && locations.length >= selectedLocationIndex ) { locationMessage = { -- cgit v1.2.3