diff --git a/js/quicktabs.js b/js/quicktabs.js index b4e2db2..d4c1720 100644 --- a/js/quicktabs.js +++ b/js/quicktabs.js @@ -16,6 +16,47 @@ Drupal.behaviors.quicktabs = { } } +// Enable tab memory. +// Relies on the jQuery Cookie plugin. +// @see http://plugins.jquery.com/cookie +Drupal.behaviors.quicktabsTabMemory = { + attach: function(context, settings) { + // The .each() is in case there is more than one quicktab on a page. + $('.quicktabs-wrapper', context).each(function () { + + var $this = $(this); + var wrapperID = $this.attr('id'); + + var $activeTabLink = $('.quicktabs-tabs li.active a', context); + var activeTabLinkID = $activeTabLink.attr('id'); + + // Use a unique cookie namespace for each set of Quicktabs, which + // should allow for more than one set on a page. + var cookieName = 'Drupal-quicktabs-active-tab-id-' + wrapperID; + // Default cookie options. + var cookieOptions = {path: '/'}; + + // Click the tab ID if a cookie exists otherwise set a cookie for + // the default active tab. + var $cookieValue = $.cookie(cookieName); + if ($cookieValue) { + $('#' + $cookieValue).click(); + } + else { + $.cookie(cookieName, activeTabLinkID, cookieOptions); + } + + // Set the click handler for all tabs, this updates the cookie on every + // tab click. + $this.find('ul.quicktabs-tabs a').click(function quicktabsCookieClickHandler() { + $activeTabLink = $this.find('.quicktabs-tabs li.active a', context); + activeTabLinkID = $activeTabLink.attr('id'); + $.cookie(cookieName, activeTabLinkID, cookieOptions); + }); + }); + } +}; + // Setting up the inital behaviours Drupal.quicktabs.prepare = function(el) { // el.id format: "quicktabs-$name" diff --git a/quicktabs.module b/quicktabs.module index 182a89c..74e0941 100644 --- a/quicktabs.module +++ b/quicktabs.module @@ -287,6 +287,7 @@ function quicktabs_load($name) { * Load the quicktabs data. */ function quicktabs_load_multiple($names = array()) { + drupal_add_library('system', 'jquery.cookie'); ctools_include('export'); $defaults = empty($names) ? ctools_export_load_object('quicktabs', 'all') : ctools_export_load_object('quicktabs', 'names', $names); return $defaults;