Skip to content

Commit

Permalink
Don't use push state and Turbolinks. Turbolinks can't follow our push…
Browse files Browse the repository at this point in the history
…es backward
  • Loading branch information
jcoyne committed Apr 7, 2017
1 parent 7798501 commit 21a540a
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions app/assets/javascripts/hyrax/tabs.js
@@ -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);
})
}

0 comments on commit 21a540a

Please sign in to comment.