multipage.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 clearfix"></div>');
  23. $(this).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 === undefined) {
  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 !== '#' && $(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 !== undefined) {
  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. var controls = Drupal.theme('multipage', settings);
  69. $.extend(self, settings, controls);
  70. this.nextLink.click(function () {
  71. self.nextPage();
  72. return false;
  73. });
  74. this.previousLink.click(function () {
  75. self.previousPage();
  76. return false;
  77. });
  78. /*
  79. // Keyboard events added:
  80. // Pressing the Enter key will open the tab pane.
  81. this.nextLink.keydown(function(event) {
  82. if (event.keyCode == 13) {
  83. self.focus();
  84. // Set focus on the first input field of the visible wrapper/tab pane.
  85. $("div.multipage-pane :input:visible:enabled:first").focus();
  86. return false;
  87. }
  88. });
  89. // Pressing the Enter key lets you leave the tab again.
  90. this.wrapper.keydown(function(event) {
  91. // Enter key should not trigger inside <textarea> to allow for multi-line entries.
  92. if (event.keyCode == 13 && event.target.nodeName != "TEXTAREA") {
  93. // Set focus on the selected tab button again.
  94. $(".multipage-tab-button.selected a").focus();
  95. return false;
  96. }
  97. });
  98. */
  99. };
  100. Drupal.multipageControl.prototype = {
  101. /**
  102. * Displays the tab's content pane.
  103. */
  104. focus: function () {
  105. this.wrapper
  106. .show()
  107. .siblings('div.multipage-pane')
  108. .each(function () {
  109. var tab = $(this).data('multipageControl');
  110. tab.wrapper.hide();
  111. })
  112. .end()
  113. .siblings(':hidden.multipage-active-control')
  114. .val(this.wrapper.attr('id'));
  115. // Mark the active control for screen readers.
  116. $('#active-multipage-control').remove();
  117. this.nextLink.after('<span id="active-multipage-control" class="element-invisible">' + Drupal.t('(active page)') + '</span>');
  118. },
  119. /**
  120. * Continues to the next page or step in the form.
  121. */
  122. nextPage: function () {
  123. this.wrapper.next().data('multipageControl').focus();
  124. $('html, body').scrollTop(this.wrapper.parents('.field-group-multipage-group-wrapper').offset().top);
  125. },
  126. /**
  127. * Returns to the previous page or step in the form.
  128. */
  129. previousPage: function () {
  130. this.wrapper.prev().data('multipageControl').focus();
  131. $('html, body').scrollTop(this.wrapper.parents('.field-group-multipage-group-wrapper').offset().top);
  132. },
  133. /**
  134. * Shows a horizontal tab pane.
  135. */
  136. tabShow: function () {
  137. // Display the tab.
  138. this.item.show();
  139. // Update .first marker for items. We need recurse from parent to retain the
  140. // actual DOM element order as jQuery implements sortOrder, but not as public
  141. // method.
  142. this.item.parent().children('.multipage-control').removeClass('first')
  143. .filter(':visible:first').addClass('first');
  144. // Display the wrapper.
  145. this.wrapper.removeClass('multipage-control-hidden').show();
  146. // Focus this tab.
  147. this.focus();
  148. return this;
  149. },
  150. /**
  151. * Hides a horizontal tab pane.
  152. */
  153. tabHide: function () {
  154. // Hide this tab.
  155. this.item.hide();
  156. // Update .first marker for items. We need recurse from parent to retain the
  157. // actual DOM element order as jQuery implements sortOrder, but not as public
  158. // method.
  159. this.item.parent().children('.multipage-control').removeClass('first')
  160. .filter(':visible:first').addClass('first');
  161. // Hide the wrapper.
  162. this.wrapper.addClass('horizontal-tab-hidden').hide();
  163. // Focus the first visible tab (if there is one).
  164. var $firstTab = this.wrapper.siblings('.multipage-pane:not(.multipage-control-hidden):first');
  165. if ($firstTab.length) {
  166. $firstTab.data('multipageControl').focus();
  167. }
  168. return this;
  169. }
  170. };
  171. /**
  172. * Theme function for a multipage control.
  173. *
  174. * @param settings
  175. * An object with the following keys:
  176. * - title: The name of the tab.
  177. * @return
  178. * This function has to return an object with at least these keys:
  179. * - item: The root tab jQuery element
  180. * - nextLink: The anchor tag that acts as the clickable area of the control
  181. * - nextTitle: The jQuery element that contains the group title
  182. * - previousLink: The anchor tag that acts as the clickable area of the control
  183. * - previousTitle: The jQuery element that contains the group title
  184. */
  185. Drupal.theme.prototype.multipage = function (settings) {
  186. var controls = {};
  187. controls.item = $('<span class="multipage-button"></span>');
  188. controls.previousLink = $('<input type="button" class="form-submit multipage-link-previous" value="" />');
  189. controls.previousTitle = Drupal.t('Previous page');
  190. controls.item.append(controls.previousLink.val(controls.previousTitle));
  191. controls.nextLink = $('<input type="button" class="form-submit multipage-link-next" value="" />');
  192. controls.nextTitle = Drupal.t('Next page');
  193. controls.item.append(controls.nextLink.val(controls.nextTitle));
  194. if (!settings.has_next) {
  195. controls.nextLink.hide();
  196. }
  197. if (!settings.has_previous) {
  198. controls.previousLink.hide();
  199. }
  200. return controls;
  201. };
  202. Drupal.FieldGroup = Drupal.FieldGroup || {};
  203. Drupal.FieldGroup.Effects = Drupal.FieldGroup.Effects || {};
  204. /**
  205. * Implements Drupal.FieldGroup.processHook().
  206. */
  207. Drupal.FieldGroup.Effects.processMultipage = {
  208. execute: function (context, settings, type) {
  209. if (type == 'form') {
  210. var $firstErrorItem = false;
  211. // Add required fields mark to any element containing required fields
  212. $('div.multipage-pane').each(function(i){
  213. if ($('.error', $(this)).length) {
  214. // Save first error item, for focussing it.
  215. if (!$firstErrorItem) {
  216. $firstErrorItem = $(this).data('multipageControl');
  217. }
  218. Drupal.FieldGroup.setGroupWithfocus($(this));
  219. $(this).data('multipageControl').focus();
  220. }
  221. });
  222. // Focus on first multipage that has an error.
  223. if ($firstErrorItem) {
  224. $firstErrorItem.focus();
  225. }
  226. }
  227. }
  228. }
  229. })(jQuery);