Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,5 +274,5 @@ gem push jekyll-tabs-{version_here}.gem
### Run the tests

```bash
npm test
npm run test
```
2 changes: 1 addition & 1 deletion docs/tabs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions js/jekyllTabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ interface Configuration {
copyToClipboardSettings: CopyToClipboardSettings;
}

const init = (overriddenConfiguration: Partial<Configuration> = {}) => {
const init = (
overriddenConfiguration: Partial<Configuration> = {},
windowObject: Window = window,
historyObject: History = history,
) => {

const defaultConfiguration: Configuration = {
syncTabsWithSameLabels: false,
Expand Down Expand Up @@ -53,7 +57,7 @@ const init = (overriddenConfiguration: Partial<Configuration> = {}) => {
handleTabClicked(link);

if (configuration.activateTabFromUrl) {
updateUrlWithActiveTab(link);
updateUrlWithActiveTab(link, windowObject, historyObject);
}

if (configuration.syncTabsWithSameLabels) {
Expand All @@ -73,7 +77,7 @@ const init = (overriddenConfiguration: Partial<Configuration> = {}) => {
}

if (configuration.activateTabFromUrl) {
activateTabFromUrl();
activateTabFromUrl(windowObject);
}
};

Expand Down
18 changes: 11 additions & 7 deletions js/tabsHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ const copyToClipboard = (text: string, callBack: () => void) => {
* For example, considering url http://your-jekyll-website.com/some-page/?active_tab=tab-2#my_tabs
* Then the tabs with name 'my_tabs' would see tab with label 'tab 2' automatically open.
*/
const activateTabFromUrl = () => {
const tabsAnchor = window.location.hash?.substring(1);
const activateTabFromUrl = (windowObject: Window) => {
const tabsAnchor = windowObject.location.hash?.substring(1);

if (!tabsAnchor) {
return;
Expand All @@ -98,7 +98,7 @@ const activateTabFromUrl = () => {
return;
}

const urlParams = new URLSearchParams(window.location.search);
const urlParams = new URLSearchParams(windowObject.location.search);
const tabIdToActivate = urlParams.get('active_tab');

if (!tabIdToActivate) {
Expand All @@ -117,15 +117,19 @@ const activateTabFromUrl = () => {
/**
* Update the url when clicking on a tab. See method activateTabFromUrl above.
*/
const updateUrlWithActiveTab = (link: HTMLAnchorElement) => {
const updateUrlWithActiveTab = (
link: HTMLAnchorElement,
windowObject: Window,
historyObject: History,
) => {
const liTab = link.parentNode as HTMLLIElement;
const ulTab = liTab.parentNode as HTMLUListElement;

const searchParams = new URLSearchParams(window.location.search);
const searchParams = new URLSearchParams(windowObject.location.search);
searchParams.set('active_tab', liTab.id);

const updatedUrl = window.location.pathname + '?' + searchParams.toString() + '#' + ulTab.id;
history.replaceState(null, '', updatedUrl);
const updatedUrl = windowObject.location.pathname + '?' + searchParams.toString() + '#' + ulTab.id;
historyObject.replaceState(null, '', updatedUrl);
};

/**
Expand Down
Loading
Loading