Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't use push state and Turbolinks. Turbolinks can't follow our push…
…es backward
- Loading branch information
Showing
1 changed file
with
28 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,36 @@ | ||
// This code is to implement the tabs on the home page | ||
|
||
// navigate to the selected tab or the first tab | ||
function tabNavigation(e) { | ||
var activeTab = $('[href="' + location.hash + '"]'); | ||
if (activeTab.length) { | ||
activeTab.tab('show'); | ||
} else { | ||
var firstTab = $('.nav-tabs a:first'); | ||
// select the first tab if it has an id and is expected to be selected | ||
if ((firstTab[0] !== undefined) && (firstTab[0].id != "")){ | ||
$(firstTab).tab('show'); | ||
} | ||
} | ||
} | ||
|
||
Blacklight.onLoad(function () { | ||
// When we visit a link to a tab, open that tab. | ||
var url = document.location.toString(); | ||
if (url.match('#')) { | ||
$('.nav-tabs a[href="#' + url.split('#')[1] + '"]').tab('show'); | ||
} | ||
|
||
// Change the url when a tab is clicked. | ||
$('a[data-toggle="tab"]').on('click', function(e) { | ||
history.pushState(null, null, $(this).attr('href')); | ||
}); | ||
// navigate to a tab when the history changes (back button) | ||
window.addEventListener("popstate", tabNavigation); | ||
}); | ||
|
||
if (typeof Turbolinks === "undefined") { | ||
// navigate to the selected tab or the first tab | ||
function tabNavigation(e) { | ||
var activeTab = $('[href="' + location.hash + '"]'); | ||
if (activeTab.length) { | ||
activeTab.tab('show'); | ||
} else { | ||
var firstTab = $('.nav-tabs a:first'); | ||
// select the first tab if it has an id and is expected to be selected | ||
if ((firstTab[0] !== undefined) && (firstTab[0].id != "")){ | ||
$(firstTab).tab('show'); | ||
} | ||
} | ||
} | ||
|
||
// Messing with the push state means that turbolinks will be unable to handle | ||
// the back button because the event will not have been a turbolinks event. | ||
// See https://github.com/turbolinks/turbolinks/blob/c73e134731ad12b2ee987080f4c905aaacdebba1/src/turbolinks/history.coffee#L28 | ||
Blacklight.onLoad(function () { | ||
// Change the url when a tab is clicked. | ||
$('a[data-toggle="tab"]').on('click', function(e) { | ||
history.pushState(null, null, $(this).attr('href')); | ||
}); | ||
// navigate to a tab when the history changes (back button) | ||
window.addEventListener("popstate", tabNavigation); | ||
}) | ||
} |