Skip to content

Commit

Permalink
Preserve selected tab when navigating between pages.
Browse files Browse the repository at this point in the history
  • Loading branch information
braders committed Dec 23, 2021
1 parent 16332a7 commit a208cd9
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 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,25 @@
$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 href = window.location.href;
if (href.indexOf('?' + param) !== -1 || href.indexOf('&' + param) !== -1 ) {
var regExp = new RegExp(param + "(.+?)(&|$)", "g");
var newUrl = href.replace(regExp, param + "=" + value + "$2");
} else if (href.indexOf('?' + param) !== -1) {
var newUrl = href + '&' + param + "=" + value;
} else {
var newUrl = href + '?' + param + "=" + value;
}
window.history.pushState("", "", newUrl);
}
})(CRM.$, CRM._);

0 comments on commit a208cd9

Please sign in to comment.