Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve selected tab when navigating between pages. #22316

Merged

Conversation

braders
Copy link
Contributor

@braders braders commented Dec 23, 2021

Overview

https://lab.civicrm.org/dev/core/-/issues/3003#note_68315

Many CiviCRM pages, in particular the contact page, are structured around a series of tabs: Summary, Contributions, Membership etc. There are often times when you click through from a contact to a related entity (say a related contact from the relationships tab). Prior to this change, using the back button returned the user to the first tab each time. With this change the tab the users previous tab is remembered as part of the URL history.

This is a particular issue for users with the "Enable Popup Forms" option unticked, but all users are affected by this.

Before

If on the contact page you clicked through from e.g. the relationships tab to a different contact, and then used the browsers back button you would be returned to the summary tab enstead of going back to the relationships tab.

This also improves the behaviour of other tab areas in the same fashion. For example, on the message templates screen, on the "System Workflow Messages", previously clicking through to a message, and then using the back button to go back would have returned the user to the "User-driven Messages" tab.

After

When switching between tabs the selectedChild query argument is updated in the URL (selectedChild already existed in CiviCRM and is used for internal linking). Therefore, the browser history, including the back-forward history, now includes a reference to the tab the user was viewing.

Technical Details

The updateUrlParameter isn't as clean as some code examples you find online for acheiving this functionality. However, I couldn't find anything about what browsers CiviCRM officially supports so wanted to go for a solution that would support as many browsers as possible including Internet Explorer.

Comments

This turned out to be simpler than I expected! Originally I was going to add a new data attribute to each tab with the ID corresponding to the selectedChild value, but then I realised that each tab consistenly has an ID attribute of the format tab_contribution'. Removing the leading tabgives us the component we need to make the URL segment&selectedChild=contribution`

@civibot
Copy link

civibot bot commented Dec 23, 2021

(Standard links)

@civibot civibot bot added the master label Dec 23, 2021
@braders braders force-pushed the core-3003-preserve-tab-between-pageloads branch from a208cd9 to 19d17b6 Compare December 23, 2021 20:21
* @return void
*/
function updateUrlParameter(param, value) {
var href = window.location.href;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's bit of a no-no in javascript to declare a var more than once. I would just declare it at the top and then assign values to it below.

Suggested change
var href = window.location.href;
var newUrl,
href = window.location.href;

@braders braders force-pushed the core-3003-preserve-tab-between-pageloads branch 2 times, most recently from 4fc11ec to b2dd96c Compare December 23, 2021 20:36
@colemanw
Copy link
Member

@braders I'm curious to know if this is compatible with anchors (which are used by some js components). Like if a # exists in the URL will it be preserved?

@braders
Copy link
Contributor Author

braders commented Dec 23, 2021

@braders I'm curious to know if this is compatible with anchors (which are used by some js components). Like if a # exists in the URL will it be preserved?

Hm, good catch. It looks like this case isn't supported at the moment. I'll have a think about the best way to handle this one.

@braders braders force-pushed the core-3003-preserve-tab-between-pageloads branch from b2dd96c to 8e36ca5 Compare December 24, 2021 19:04
@braders braders force-pushed the core-3003-preserve-tab-between-pageloads branch from 8e36ca5 to 1ed9f5c Compare December 24, 2021 19:06
@braders
Copy link
Contributor Author

braders commented Dec 24, 2021

@braders I'm curious to know if this is compatible with anchors (which are used by some js components). Like if a # exists in the URL will it be preserved?

@colemanw I've done some refactoring and I think it should support a full range of URL formats now. There are a lot of tabs on a lot of different pages though, so it probably makes sense for a fresh site of eyes to test the PR too.

@mattwire mattwire merged commit 6c23257 into civicrm:master Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants