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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions templates/CRM/common/TabHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
CRM.alert(ts('Your changes in the <em>%1</em> tab have not been saved.', {1: ui.oldTab.text()}), ts('Unsaved Changes'), 'warning');
}
})
.on('tabsactivate', function(e, ui) {
var tabId = ui.newTab.attr('id');
if (tabId && tabId.length) {
tabId = tabId.slice(4); // Remove leading 'tab_'
history.replaceState(null, '', updateUrlParameter('selectedChild', tabId));
}
})
.on('tabsbeforeload', function(e, ui) {
// Use civicrm ajax wrappers rather than the default $.load
if (!ui.panel.data("civiCrmSnippet")) {
Expand Down Expand Up @@ -148,4 +155,31 @@
$panel.crmSnippet('destroy');
}
};

/**
* Updates the query parameter in the page URL,
* or adds the parameter if its not currently there.
*
* @param {string} param
* @param {string} value
* @return void
*/
function updateUrlParameter(param, value) {
var newUrl,
newSearch,
href = window.location.href,
search = window.location.search;
if (search.indexOf('?' + param) !== -1 || search.indexOf('&' + param) !== -1 ) {
var regExp = new RegExp(param + "(.+?)(&|$)", "g");
newSearch = search.replace(regExp, param + "=" + value + "$2");
newUrl = href.replace(search, newSearch);
} else if (search.length) {
newSearch = search + '&' + param + "=" + value;
newUrl = href.replace(search, newSearch);
} else {
newSearch = '?' + param + "=" + value;
newUrl = location.protocol + '//' + location.hostname + location.pathname + newSearch + location.hash;
}
window.history.replaceState("", "", newUrl);
}
})(CRM.$, CRM._);