diff --git a/misc/vertical-tabs.js b/misc/vertical-tabs.js index 3aa0f6f..bb88211 100644 --- a/misc/vertical-tabs.js +++ b/misc/vertical-tabs.js @@ -74,10 +74,15 @@ Drupal.behaviors.verticalTabs = { */ Drupal.verticalTab = function (settings) { var self = this; + // Get the ID of the tab pane element. + var fieldsetId = settings.fieldset.attr('id'); $.extend(this, settings, Drupal.theme('verticalTab', settings)); + this.link.attr('href', '#' + fieldsetId); + this.link.click(function () { self.focus(); + self.updateHash(fieldsetId); return false; }); @@ -86,6 +91,7 @@ Drupal.verticalTab = function (settings) { this.link.keydown(function(event) { if (event.keyCode == 13) { self.focus(); + self.updateHash(fieldsetId); // Set focus on the first input field of the visible fieldset/tab pane. $("fieldset.vertical-tabs-pane :input:visible:enabled:first").focus(); return false; @@ -171,6 +177,27 @@ Drupal.verticalTab.prototype = { this.item.closest('.vertical-tabs').hide(); } return this; + }, + + /** + * Updates the URL to have the ID of tab pane element in the hash. + * + * This is used whenever we click a tab pane element, for instance, so as to + * allow users to link a specific tab. + * + * @var string detailsId + * The ID of the tab pane element we want to set the hash to. + */ + updateHash: function (fieldsetId) { + // Get the URL before the hash. + var currentUrl = window.location.href.split('#')[0]; + // Save the numbers of pixels that we are scrolled upwards so we can + // prevent the page from scrolling after replacing the URL. + var yScroll = document.body.scrollTop; + // Update the URL bar with the hash. + window.location.replace(currentUrl + '#' + fieldsetId); + // Prevent the page from scrolling when the URL is replaced. + document.body.scrollTop = yScroll; } };