update: activate a code-tab based on the t query param.

pull/2853/head
Jason Stirnaman 2021-06-15 15:56:02 -05:00
parent e8c9039ce1
commit 55b81dfafe
1 changed files with 44 additions and 25 deletions

View File

@ -22,12 +22,13 @@ var elementWhiteList = [
function scrollToAnchor(target) {
var $target = $(target);
$('html, body').stop().animate({
'scrollTop': ($target.offset().top)
}, 400, 'swing', function () {
window.location.hash = target;
});
if($target && $target.length > 0) {
$('html, body').stop().animate({
'scrollTop': ($target.offset().top)
}, 400, 'swing', function () {
window.location.hash = target;
});
}
}
$('.article a[href^="#"]:not(' + elementWhiteList + ')').click(function (e) {
@ -82,28 +83,46 @@ function tabbedContent(container, tab, content) {
tabbedContent('.code-tabs-wrapper', '.code-tabs p a', '.code-tab-content');
tabbedContent('.tabs-wrapper', '.tabs p a', '.tab-content');
//////////////////////// Activate Tabs with Query Params ////////////////////////
const queryParams = new URLSearchParams(window.location.search);
var anchor = window.location.hash
tab = $('<textarea />').html(queryParams.get('t')).text();
if (tab !== "") {
var targetTab = $('.tabs a:contains("' + tab + '")')
targetTab.click()
if (anchor !== "") { scrollToAnchor(anchor) }
// Retrieve the user's programming language (client library) preference.
function getApiLibPreference() {
return Cookies.get('influx-docs-api-lib');
}
$('.tabs p a').click(function() {
if ($(this).is(':not(":first-child")')) {
queryParams.set('t', $(this).html())
window.history.replaceState({}, '', `${location.pathname}?${queryParams}${anchor}`);
} else {
queryParams.delete('t')
window.history.replaceState({}, '', `${location.pathname}${anchor}`);
function getTabQueryParam() {
const queryParams = new URLSearchParams(window.location.search);
return $('<textarea />').html(queryParams.get('t')).text();
}
function activateTabs(selector, tab) {
const anchor = window.location.hash;
if (tab !== "") {
var targetTab = $(`${selector} a:contains("${tab}")`);
if(targetTab.length > 0) {
targetTab.click();
scrollToAnchor(anchor);
}
}
})
const queryParams = new URLSearchParams(window.location.search);
$(`${selector} p a`).click(function() {
if ($(this).is(':not(":first-child")')) {
queryParams.set('t', $(this).html())
window.history.replaceState({}, '', `${location.pathname}?${queryParams}${anchor}`);
} else {
queryParams.delete('t')
window.history.replaceState({}, '', `${location.pathname}${anchor}`);
}
})
};
//////////////////// Activate Tab with Cookie or Query Param ///////////////////
/**
* Activate code-tabs based on the cookie then override with query param.
**/
var tab = getApiLibPreference();
(['.code-tabs']).forEach(selector => activateTabs(selector, tab));
tab = getTabQueryParam();
(['.tabs', '.code-tabs']).forEach(selector => activateTabs(selector, tab));
/////////////////////////////// Truncate Content ///////////////////////////////