horizontal-tabs.js 8.2 KB

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