From a65c64043ff8055ed8d42fecdac2d059f5dce79a Mon Sep 17 00:00:00 2001 From: jboyson Date: Thu, 29 Oct 2020 12:11:00 -0500 Subject: [PATCH 1/2] Update target path with current path Minor deprecation jquery fix: `.submit()` -> `.trigger('submit')` --- .../javascripts/pages/projects/project.js | 30 +++++++++++++++---- ...-in-repo-tree-view-navigates-backwards.yml | 5 ++++ 2 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 changelogs/unreleased/231777-switching-branches-in-repo-tree-view-navigates-backwards.yml diff --git a/app/assets/javascripts/pages/projects/project.js b/app/assets/javascripts/pages/projects/project.js index 2f27814a692a15..22e8de5218d134 100644 --- a/app/assets/javascripts/pages/projects/project.js +++ b/app/assets/javascripts/pages/projects/project.js @@ -57,7 +57,7 @@ export default class Project { $('.project-refs-select').on('change', function() { return $(this) .parents('form') - .submit(); + .trigger('submit'); }); } @@ -156,11 +156,31 @@ export default class Project { }, clicked(options) { const { e } = options; - if (!shouldVisit) { - e.preventDefault(); + e.preventDefault(); + + // Since this page does not reload when changing directories in a repo + // the rendered links do not have the path to the current directory. + // This updates the path based on the current url and then opens + // the the url with the updated path parameter. + if (shouldVisit) { + const selectedUrl = new URL(e.target.href); + + if (window.location.href.includes('/-/')) { + const targetPath = window.location.href + .split('/-/')[1] + .split('/') + .slice(2) + .join('/'); + selectedUrl.searchParams.set('path', targetPath); + } + + // Open in new window if "meta" key is pressed + if (e.metaKey) { + window.open(selectedUrl.href, '_blank'); + } else { + window.location.href = selectedUrl.href; + } } - /* The actual process is removed since `link.href` in `RenderRow` contains the full target. - * It makes the visitable link can be visited when opening on a new tab of browser */ }, }); }); diff --git a/changelogs/unreleased/231777-switching-branches-in-repo-tree-view-navigates-backwards.yml b/changelogs/unreleased/231777-switching-branches-in-repo-tree-view-navigates-backwards.yml new file mode 100644 index 00000000000000..9212cb18331742 --- /dev/null +++ b/changelogs/unreleased/231777-switching-branches-in-repo-tree-view-navigates-backwards.yml @@ -0,0 +1,5 @@ +--- +title: Fix loading current directory when changing branches +merge_request: 46479 +author: +type: fixed -- GitLab From 9d29349cfedb19cf48f1a05427eb196ff31a1d22 Mon Sep 17 00:00:00 2001 From: jboyson Date: Thu, 5 Nov 2020 17:17:31 -0600 Subject: [PATCH 2/2] Account for branches with forward slashes --- .../javascripts/pages/projects/project.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/pages/projects/project.js b/app/assets/javascripts/pages/projects/project.js index 22e8de5218d134..5317093c4cff42 100644 --- a/app/assets/javascripts/pages/projects/project.js +++ b/app/assets/javascripts/pages/projects/project.js @@ -164,14 +164,15 @@ export default class Project { // the the url with the updated path parameter. if (shouldVisit) { const selectedUrl = new URL(e.target.href); - - if (window.location.href.includes('/-/')) { - const targetPath = window.location.href - .split('/-/')[1] - .split('/') - .slice(2) - .join('/'); - selectedUrl.searchParams.set('path', targetPath); + const loc = window.location.href; + + if (loc.includes('/-/')) { + const refs = this.fullData.Branches.concat(this.fullData.Tags); + const currentRef = refs.find(ref => loc.indexOf(ref) > -1); + if (currentRef) { + const targetPath = loc.split(currentRef)[1].slice(1); + selectedUrl.searchParams.set('path', targetPath); + } } // Open in new window if "meta" key is pressed -- GitLab