horizontal-tabs.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. if (window.location.hash && window.location.hash !== '#' && $(window.location.hash, this).length) {
  48. tab_focus = $(window.location.hash, this).closest('.horizontal-tabs-pane');
  49. }
  50. else {
  51. tab_focus = $('> .horizontal-tabs-pane:first', this);
  52. }
  53. }
  54. if (tab_focus.length) {
  55. tab_focus.data('horizontalTab').focus();
  56. }
  57. });
  58. }
  59. };
  60. /**
  61. * The horizontal tab object represents a single tab within a tab group.
  62. *
  63. * @param settings
  64. * An object with the following keys:
  65. * - title: The name of the tab.
  66. * - fieldset: The jQuery object of the fieldset that is the tab pane.
  67. */
  68. Drupal.horizontalTab = function (settings) {
  69. var self = this;
  70. $.extend(this, settings, Drupal.theme('horizontalTab', settings));
  71. this.link.click(function () {
  72. self.focus();
  73. return false;
  74. });
  75. // Keyboard events added:
  76. // Pressing the Enter key will open the tab pane.
  77. this.link.keydown(function(event) {
  78. if (event.keyCode == 13) {
  79. self.focus();
  80. // Set focus on the first input field of the visible fieldset/tab pane.
  81. $("fieldset.horizontal-tabs-pane :input:visible:enabled:first").focus();
  82. return false;
  83. }
  84. });
  85. // Only bind update summary on forms.
  86. if (this.fieldset.drupalGetSummary) {
  87. this.fieldset.bind('summaryUpdated', function() {
  88. self.updateSummary();
  89. }).trigger('summaryUpdated');
  90. }
  91. };
  92. Drupal.horizontalTab.prototype = {
  93. /**
  94. * Displays the tab's content pane.
  95. */
  96. focus: function () {
  97. this.fieldset
  98. .removeClass('horizontal-tab-hidden')
  99. .siblings('fieldset.horizontal-tabs-pane')
  100. .each(function () {
  101. var tab = $(this).data('horizontalTab');
  102. tab.fieldset.addClass('horizontal-tab-hidden');
  103. tab.item.removeClass('selected');
  104. })
  105. .end()
  106. .siblings(':hidden.horizontal-tabs-active-tab')
  107. .val(this.fieldset.attr('id'));
  108. this.item.addClass('selected');
  109. // Mark the active tab for screen readers.
  110. $('#active-horizontal-tab').remove();
  111. this.link.append('<span id="active-horizontal-tab" class="element-invisible">' + Drupal.t('(active tab)') + '</span>');
  112. },
  113. /**
  114. * Updates the tab's summary.
  115. */
  116. updateSummary: function () {
  117. this.summary.html(this.fieldset.drupalGetSummary());
  118. },
  119. /**
  120. * Shows a horizontal tab pane.
  121. */
  122. tabShow: function () {
  123. // Display the tab.
  124. this.item.removeClass('horizontal-tab-hidden');
  125. // Update .first marker for items. We need recurse from parent to retain the
  126. // actual DOM element order as jQuery implements sortOrder, but not as public
  127. // method.
  128. this.item.parent().children('.horizontal-tab-button').removeClass('first')
  129. .filter(':visible:first').addClass('first');
  130. // Display the fieldset.
  131. this.fieldset.removeClass('horizontal-tab-hidden');
  132. // Focus this tab.
  133. this.focus();
  134. return this;
  135. },
  136. /**
  137. * Hides a horizontal tab pane.
  138. */
  139. tabHide: function () {
  140. // Hide this tab.
  141. this.item.addClass('horizontal-tab-hidden');
  142. // Update .first marker for items. We need recurse from parent to retain the
  143. // actual DOM element order as jQuery implements sortOrder, but not as public
  144. // method.
  145. this.item.parent().children('.horizontal-tab-button').removeClass('first')
  146. .filter(':visible:first').addClass('first');
  147. // Hide the fieldset.
  148. this.fieldset.addClass('horizontal-tab-hidden');
  149. // Focus the first visible tab (if there is one).
  150. var $firstTab = this.fieldset.siblings('.horizontal-tabs-pane:not(.horizontal-tab-hidden):first');
  151. if ($firstTab.length) {
  152. $firstTab.data('horizontalTab').focus();
  153. }
  154. return this;
  155. }
  156. };
  157. /**
  158. * Theme function for a horizontal tab.
  159. *
  160. * @param settings
  161. * An object with the following keys:
  162. * - title: The name of the tab.
  163. * @return
  164. * This function has to return an object with at least these keys:
  165. * - item: The root tab jQuery element
  166. * - link: The anchor tag that acts as the clickable area of the tab
  167. * (jQuery version)
  168. * - summary: The jQuery element that contains the tab summary
  169. */
  170. Drupal.theme.prototype.horizontalTab = function (settings) {
  171. var tab = {};
  172. var idAttr = settings.fieldset.attr('id');
  173. tab.item = $('<li class="horizontal-tab-button" tabindex="-1"></li>')
  174. .append(tab.link = $('<a href="#' + idAttr + '"></a>')
  175. .append(tab.title = $('<strong></strong>').text(settings.title))
  176. );
  177. // No need to add summary on frontend.
  178. if (settings.fieldset.drupalGetSummary) {
  179. tab.link.append(tab.summary = $('<span class="summary"></span>'))
  180. }
  181. return tab;
  182. };
  183. })(jQuery);