horizontal-tabs.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. (function ($) {
  2. /**
  3. * This script transforms a set of fieldsets into a stack of horizontal
  4. * tabs. Another tab pane can be selected by clicking on the respective
  5. * tab.
  6. *
  7. * Each tab may have a summary which can be updated by another
  8. * script. For that to work, each fieldset has an associated
  9. * 'horizontalTabCallback' (with jQuery.data() attached to the fieldset),
  10. * which is called every time the user performs an update to a form
  11. * element inside the tab pane.
  12. */
  13. Drupal.behaviors.horizontalTabs = {
  14. attach: function (context) {
  15. $('.horizontal-tabs-panes', context).once('horizontal-tabs', function () {
  16. var focusID = $(':hidden.horizontal-tabs-active-tab', this).val();
  17. var tab_focus;
  18. // Check if there are some fieldsets that can be converted to horizontal-tabs
  19. var $fieldsets = $('> fieldset', this);
  20. if ($fieldsets.length == 0) {
  21. return;
  22. }
  23. // Create the tab column.
  24. var tab_list = $('<ul class="horizontal-tabs-list"></ul>');
  25. $(this).wrap('<div class="horizontal-tabs clearfix"></div>').before(tab_list);
  26. // Transform each fieldset into a tab.
  27. $fieldsets.each(function (i) {
  28. var horizontal_tab = new Drupal.horizontalTab({
  29. title: $('> legend', this).text(),
  30. fieldset: $(this)
  31. });
  32. horizontal_tab.item.addClass('horizontal-tab-button-' + i);
  33. tab_list.append(horizontal_tab.item);
  34. $(this)
  35. .removeClass('collapsible collapsed')
  36. .addClass('horizontal-tabs-pane')
  37. .data('horizontalTab', horizontal_tab);
  38. if (this.id == focusID) {
  39. tab_focus = $(this);
  40. }
  41. });
  42. $('> li:first', tab_list).addClass('first');
  43. $('> li:last', tab_list).addClass('last');
  44. if (!tab_focus) {
  45. // If the current URL has a fragment and one of the tabs contains an
  46. // element that matches the URL fragment, activate that tab.
  47. var hash = window.location.hash.replace(/[=%;,\/]/g, "");
  48. if (hash !== '#' && $(hash, this).length) {
  49. tab_focus = $(window.location.hash, this).closest('.horizontal-tabs-pane');
  50. }
  51. else {
  52. tab_focus = $('> .horizontal-tabs-pane:first', this);
  53. }
  54. }
  55. if (tab_focus.length) {
  56. tab_focus.data('horizontalTab').focus();
  57. }
  58. });
  59. }
  60. };
  61. /**
  62. * The horizontal tab object represents a single tab within a tab group.
  63. *
  64. * @param settings
  65. * An object with the following keys:
  66. * - title: The name of the tab.
  67. * - fieldset: The jQuery object of the fieldset that is the tab pane.
  68. */
  69. Drupal.horizontalTab = function (settings) {
  70. var self = this;
  71. $.extend(this, settings, Drupal.theme('horizontalTab', settings));
  72. this.link.click(function () {
  73. self.focus();
  74. return false;
  75. });
  76. // Keyboard events added:
  77. // Pressing the Enter key will open the tab pane.
  78. this.link.keydown(function(event) {
  79. if (event.keyCode == 13) {
  80. self.focus();
  81. // Set focus on the first input field of the visible fieldset/tab pane.
  82. $("fieldset.horizontal-tabs-pane :input:visible:enabled:first").focus();
  83. return false;
  84. }
  85. });
  86. // Only bind update summary on forms.
  87. if (this.fieldset.drupalGetSummary) {
  88. this.fieldset.bind('summaryUpdated', function() {
  89. self.updateSummary();
  90. }).trigger('summaryUpdated');
  91. }
  92. };
  93. Drupal.horizontalTab.prototype = {
  94. /**
  95. * Displays the tab's content pane.
  96. */
  97. focus: function () {
  98. this.fieldset
  99. .removeClass('horizontal-tab-hidden')
  100. .siblings('fieldset.horizontal-tabs-pane')
  101. .each(function () {
  102. var tab = $(this).data('horizontalTab');
  103. tab.fieldset.addClass('horizontal-tab-hidden');
  104. tab.item.removeClass('selected');
  105. })
  106. .end()
  107. .siblings(':hidden.horizontal-tabs-active-tab')
  108. .val(this.fieldset.attr('id'));
  109. this.item.addClass('selected');
  110. // Mark the active tab for screen readers.
  111. $('#active-horizontal-tab').remove();
  112. this.link.append('<span id="active-horizontal-tab" class="element-invisible">' + Drupal.t('(active tab)') + '</span>');
  113. },
  114. /**
  115. * Updates the tab's summary.
  116. */
  117. updateSummary: function () {
  118. this.summary.html(this.fieldset.drupalGetSummary());
  119. },
  120. /**
  121. * Shows a horizontal tab pane.
  122. */
  123. tabShow: function () {
  124. // Display the tab.
  125. this.item.removeClass('horizontal-tab-hidden');
  126. // Update .first marker for items. We need recurse from parent to retain the
  127. // actual DOM element order as jQuery implements sortOrder, but not as public
  128. // method.
  129. this.item.parent().children('.horizontal-tab-button').removeClass('first')
  130. .filter(':visible:first').addClass('first');
  131. // Display the fieldset.
  132. this.fieldset.removeClass('horizontal-tab-hidden');
  133. // Focus this tab.
  134. this.focus();
  135. return this;
  136. },
  137. /**
  138. * Hides a horizontal tab pane.
  139. */
  140. tabHide: function () {
  141. // Hide this tab.
  142. this.item.addClass('horizontal-tab-hidden');
  143. // Update .first marker for items. We need recurse from parent to retain the
  144. // actual DOM element order as jQuery implements sortOrder, but not as public
  145. // method.
  146. this.item.parent().children('.horizontal-tab-button').removeClass('first')
  147. .filter(':visible:first').addClass('first');
  148. // Hide the fieldset.
  149. this.fieldset.addClass('horizontal-tab-hidden');
  150. // Focus the first visible tab (if there is one).
  151. var $firstTab = this.fieldset.siblings('.horizontal-tabs-pane:not(.horizontal-tab-hidden):first');
  152. if ($firstTab.length) {
  153. $firstTab.data('horizontalTab').focus();
  154. }
  155. return this;
  156. }
  157. };
  158. /**
  159. * Theme function for a horizontal tab.
  160. *
  161. * @param settings
  162. * An object with the following keys:
  163. * - title: The name of the tab.
  164. * @return
  165. * This function has to return an object with at least these keys:
  166. * - item: The root tab jQuery element
  167. * - link: The anchor tag that acts as the clickable area of the tab
  168. * (jQuery version)
  169. * - summary: The jQuery element that contains the tab summary
  170. */
  171. Drupal.theme.prototype.horizontalTab = function (settings) {
  172. var tab = {};
  173. var idAttr = settings.fieldset.attr('id');
  174. tab.item = $('<li class="horizontal-tab-button" tabindex="-1"></li>')
  175. .append(tab.link = $('<a href="#' + idAttr + '"></a>')
  176. .append(tab.title = $('<strong></strong>').text(settings.title))
  177. );
  178. // No need to add summary on frontend.
  179. if (settings.fieldset.drupalGetSummary) {
  180. tab.link.append(tab.summary = $('<span class="summary"></span>'))
  181. }
  182. return tab;
  183. };
  184. })(jQuery);