multipage.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. (function ($) {
  2. /**
  3. * This script transforms a set of wrappers into a stack of multipage pages.
  4. * Another pane can be entered by clicking next/previous.
  5. *
  6. */
  7. Drupal.behaviors.MultiPage = {
  8. attach: function (context) {
  9. $('.multipage-panes', context).once('multipage', function () {
  10. var focusID = $(':hidden.multipage-active-control', this).val();
  11. var paneWithFocus;
  12. // Check if there are some wrappers that can be converted to multipages.
  13. var $panes = $('> div.field-group-multipage', this);
  14. var $form = $panes.parents('form');
  15. if ($panes.length == 0) {
  16. return;
  17. }
  18. // Create the next/previous controls.
  19. var $controls;
  20. // Transform each div.multipage-pane into a multipage with controls.
  21. $panes.each(function () {
  22. $controls = $('<div class="multipage-controls-list"></div>');
  23. $(this).append('<div class="multipage-controls clearfix"></div>').append($controls);
  24. // Check if the submit button needs to move to the latest pane.
  25. if (Drupal.settings.field_group.multipage_move_submit && $('.form-actions').length) {
  26. $('.form-actions', $form).remove().appendTo($($controls, $panes.last()));
  27. }
  28. var multipageControl = new Drupal.multipageControl({
  29. title: $('> .multipage-pane-title', this).text(),
  30. wrapper: $(this),
  31. has_next: $(this).next().length,
  32. has_previous: $(this).prev().length
  33. });
  34. $controls.append(multipageControl.item);
  35. $(this)
  36. .addClass('multipage-pane')
  37. .data('multipageControl', multipageControl);
  38. if (this.id == focusID) {
  39. paneWithFocus = $(this);
  40. }
  41. });
  42. if (!paneWithFocus) {
  43. // If the current URL has a fragment and one of the tabs contains an
  44. // element that matches the URL fragment, activate that tab.
  45. if (window.location.hash && $(window.location.hash, this).length) {
  46. paneWithFocus = $(window.location.hash, this).closest('.multipage-pane');
  47. }
  48. else {
  49. paneWithFocus = $('multipage-open', this).length ? $('multipage-open', this) : $('> .multipage-pane:first', this);
  50. }
  51. }
  52. if (paneWithFocus.length) {
  53. paneWithFocus.data('multipageControl').focus();
  54. }
  55. });
  56. }
  57. };
  58. /**
  59. * The multipagePane object represents a single div as a page.
  60. *
  61. * @param settings
  62. * An object with the following keys:
  63. * - title: The name of the tab.
  64. * - wrapper: The jQuery object of the <div> that is the tab pane.
  65. */
  66. Drupal.multipageControl = function (settings) {
  67. var self = this;
  68. $.extend(this, settings, Drupal.theme('multipage', settings));
  69. this.nextLink.click(function () {
  70. self.nextPage();
  71. return false;
  72. });
  73. this.previousLink.click(function () {
  74. self.previousPage();
  75. return false;
  76. });
  77. /*
  78. // Keyboard events added:
  79. // Pressing the Enter key will open the tab pane.
  80. this.nextLink.keydown(function(event) {
  81. if (event.keyCode == 13) {
  82. self.focus();
  83. // Set focus on the first input field of the visible wrapper/tab pane.
  84. $("div.multipage-pane :input:visible:enabled:first").focus();
  85. return false;
  86. }
  87. });
  88. // Pressing the Enter key lets you leave the tab again.
  89. this.wrapper.keydown(function(event) {
  90. // Enter key should not trigger inside <textarea> to allow for multi-line entries.
  91. if (event.keyCode == 13 && event.target.nodeName != "TEXTAREA") {
  92. // Set focus on the selected tab button again.
  93. $(".multipage-tab-button.selected a").focus();
  94. return false;
  95. }
  96. });
  97. */
  98. };
  99. Drupal.multipageControl.prototype = {
  100. /**
  101. * Displays the tab's content pane.
  102. */
  103. focus: function () {
  104. this.wrapper
  105. .show()
  106. .siblings('div.multipage-pane')
  107. .each(function () {
  108. var tab = $(this).data('multipageControl');
  109. tab.wrapper.hide();
  110. })
  111. .end()
  112. .siblings(':hidden.multipage-active-control')
  113. .val(this.wrapper.attr('id'));
  114. // Mark the active control for screen readers.
  115. $('#active-multipage-control').remove();
  116. this.nextLink.append('<span id="active-multipage-control" class="element-invisible">' + Drupal.t('(active page)') + '</span>');
  117. },
  118. /**
  119. * Continues to the next page or step in the form.
  120. */
  121. nextPage: function () {
  122. this.wrapper.next().data('multipageControl').focus();
  123. },
  124. /**
  125. * Returns to the previous page or step in the form.
  126. */
  127. previousPage: function () {
  128. this.wrapper.prev().data('multipageControl').focus();
  129. },
  130. /**
  131. * Shows a horizontal tab pane.
  132. */
  133. tabShow: function () {
  134. // Display the tab.
  135. this.item.show();
  136. // Update .first marker for items. We need recurse from parent to retain the
  137. // actual DOM element order as jQuery implements sortOrder, but not as public
  138. // method.
  139. this.item.parent().children('.multipage-control').removeClass('first')
  140. .filter(':visible:first').addClass('first');
  141. // Display the wrapper.
  142. this.wrapper.removeClass('multipage-control-hidden').show();
  143. // Focus this tab.
  144. this.focus();
  145. return this;
  146. },
  147. /**
  148. * Hides a horizontal tab pane.
  149. */
  150. tabHide: function () {
  151. // Hide this tab.
  152. this.item.hide();
  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('.multipage-control').removeClass('first')
  157. .filter(':visible:first').addClass('first');
  158. // Hide the wrapper.
  159. this.wrapper.addClass('horizontal-tab-hidden').hide();
  160. // Focus the first visible tab (if there is one).
  161. var $firstTab = this.wrapper.siblings('.multipage-pane:not(.multipage-control-hidden):first');
  162. if ($firstTab.length) {
  163. $firstTab.data('multipageControl').focus();
  164. }
  165. return this;
  166. }
  167. };
  168. /**
  169. * Theme function for a multipage control.
  170. *
  171. * @param settings
  172. * An object with the following keys:
  173. * - title: The name of the tab.
  174. * @return
  175. * This function has to return an object with at least these keys:
  176. * - item: The root tab jQuery element
  177. * - nextLink: The anchor tag that acts as the clickable area of the control
  178. * - nextTitle: The jQuery element that contains the group title
  179. * - previousLink: The anchor tag that acts as the clickable area of the control
  180. * - previousTitle: The jQuery element that contains the group title
  181. */
  182. Drupal.theme.prototype.multipage = function (settings) {
  183. var controls = {};
  184. controls.item = $('<span class="multipage-button"></span>');
  185. controls.item.append(controls.nextLink = $('<input type="button" class="form-submit multipage-link-next" value="" />').val(controls.nextTitle = Drupal.t('Next page')));
  186. controls.item.append(controls.previousLink = $('<input type="button" class="form-submit multipage-link-previous" value="" />').val(controls.nextTitle = Drupal.t('Previous')));
  187. if (!settings.has_next) {
  188. controls.nextLink.hide();
  189. }
  190. if (!settings.has_previous) {
  191. controls.previousLink.hide();
  192. }
  193. return controls;
  194. };
  195. Drupal.FieldGroup = Drupal.FieldGroup || {};
  196. Drupal.FieldGroup.Effects = Drupal.FieldGroup.Effects || {};
  197. /**
  198. * Implements Drupal.FieldGroup.processHook().
  199. */
  200. Drupal.FieldGroup.Effects.processMultipage = {
  201. execute: function (context, settings, type) {
  202. if (type == 'form') {
  203. // Add required fields mark to any element containing required fields
  204. $('div.multipage-pane').each(function(i){
  205. if ($('.error', $(this)).length) {
  206. Drupal.FieldGroup.setGroupWithfocus($(this));
  207. $(this).data('multipageControl').focus();
  208. }
  209. });
  210. }
  211. }
  212. }
  213. })(jQuery);