vertical-tabs.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. (function ($) {
  2. /**
  3. * This script transforms a set of fieldsets into a stack of vertical
  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. * 'verticalTabCallback' (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.verticalTabs = {
  14. attach: function (context) {
  15. $('.vertical-tabs-panes', context).once('vertical-tabs', function () {
  16. var focusID = $(':hidden.vertical-tabs-active-tab', this).val();
  17. var tab_focus;
  18. // Check if there are some fieldsets that can be converted to vertical-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="vertical-tabs-list"></ul>');
  25. $(this).wrap('<div class="vertical-tabs clearfix"></div>').before(tab_list);
  26. // Transform each fieldset into a tab.
  27. $fieldsets.each(function () {
  28. var vertical_tab = new Drupal.verticalTab({
  29. title: $('> legend', this).text(),
  30. fieldset: $(this)
  31. });
  32. tab_list.append(vertical_tab.item);
  33. $(this)
  34. .removeClass('collapsible collapsed')
  35. .addClass('vertical-tabs-pane')
  36. .data('verticalTab', vertical_tab);
  37. if (this.id == focusID) {
  38. tab_focus = $(this);
  39. }
  40. });
  41. $('> li:first', tab_list).addClass('first');
  42. $('> li:last', tab_list).addClass('last');
  43. if (!tab_focus) {
  44. // If the current URL has a fragment and one of the tabs contains an
  45. // element that matches the URL fragment, activate that tab.
  46. // Remove any characters that jquery does not accept in find().
  47. var hash = window.location.hash.replace(/[=%;,\/]/g, "");
  48. if (hash !== '#' && $(this).find(hash).length) {
  49. tab_focus = $(this).find(hash).closest('.vertical-tabs-pane');
  50. }
  51. else {
  52. tab_focus = $('> .vertical-tabs-pane:first', this);
  53. }
  54. }
  55. if (tab_focus.length) {
  56. tab_focus.data('verticalTab').focus();
  57. }
  58. });
  59. }
  60. };
  61. /**
  62. * The vertical 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.verticalTab = function (settings) {
  70. var self = this;
  71. $.extend(this, settings, Drupal.theme('verticalTab', 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.vertical-tabs-pane :input:visible:enabled:first").focus();
  83. return false;
  84. }
  85. });
  86. this.fieldset
  87. .bind('summaryUpdated', function () {
  88. self.updateSummary();
  89. })
  90. .trigger('summaryUpdated');
  91. };
  92. Drupal.verticalTab.prototype = {
  93. /**
  94. * Displays the tab's content pane.
  95. */
  96. focus: function () {
  97. this.fieldset
  98. .siblings('fieldset.vertical-tabs-pane')
  99. .each(function () {
  100. var tab = $(this).data('verticalTab');
  101. tab.fieldset.hide();
  102. tab.item.removeClass('selected');
  103. })
  104. .end()
  105. .show()
  106. .siblings(':hidden.vertical-tabs-active-tab')
  107. .val(this.fieldset.attr('id'));
  108. this.item.addClass('selected');
  109. // Mark the active tab for screen readers.
  110. $('#active-vertical-tab').remove();
  111. this.link.append('<span id="active-vertical-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 vertical tab pane.
  121. */
  122. tabShow: function () {
  123. // Display the tab.
  124. this.item.show();
  125. // Show the vertical tabs.
  126. this.item.closest('.vertical-tabs').show();
  127. // Update .first marker for items. We need recurse from parent to retain the
  128. // actual DOM element order as jQuery implements sortOrder, but not as public
  129. // method.
  130. this.item.parent().children('.vertical-tab-button').removeClass('first')
  131. .filter(':visible:first').addClass('first');
  132. // Display the fieldset.
  133. this.fieldset.removeClass('vertical-tab-hidden').show();
  134. // Focus this tab.
  135. this.focus();
  136. return this;
  137. },
  138. /**
  139. * Hides a vertical tab pane.
  140. */
  141. tabHide: function () {
  142. // Hide this tab.
  143. this.item.hide();
  144. // Update .first marker for items. We need recurse from parent to retain the
  145. // actual DOM element order as jQuery implements sortOrder, but not as public
  146. // method.
  147. this.item.parent().children('.vertical-tab-button').removeClass('first')
  148. .filter(':visible:first').addClass('first');
  149. // Hide the fieldset.
  150. this.fieldset.addClass('vertical-tab-hidden').hide();
  151. // Focus the first visible tab (if there is one).
  152. var $firstTab = this.fieldset.siblings('.vertical-tabs-pane:not(.vertical-tab-hidden):first');
  153. if ($firstTab.length) {
  154. $firstTab.data('verticalTab').focus();
  155. }
  156. // Hide the vertical tabs (if no tabs remain).
  157. else {
  158. this.item.closest('.vertical-tabs').hide();
  159. }
  160. return this;
  161. }
  162. };
  163. /**
  164. * Theme function for a vertical tab.
  165. *
  166. * @param settings
  167. * An object with the following keys:
  168. * - title: The name of the tab.
  169. * @return
  170. * This function has to return an object with at least these keys:
  171. * - item: The root tab jQuery element
  172. * - link: The anchor tag that acts as the clickable area of the tab
  173. * (jQuery version)
  174. * - summary: The jQuery element that contains the tab summary
  175. */
  176. Drupal.theme.prototype.verticalTab = function (settings) {
  177. var tab = {};
  178. tab.item = $('<li class="vertical-tab-button" tabindex="-1"></li>')
  179. .append(tab.link = $('<a href="#"></a>')
  180. .append(tab.title = $('<strong></strong>').text(settings.title))
  181. .append(tab.summary = $('<span class="summary"></span>')
  182. )
  183. );
  184. return tab;
  185. };
  186. })(jQuery);