dropbutton.es6.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * @file
  3. * Dropbutton feature.
  4. */
  5. (function ($, Drupal) {
  6. /**
  7. * Process elements with the .dropbutton class on page load.
  8. *
  9. * @type {Drupal~behavior}
  10. *
  11. * @prop {Drupal~behaviorAttach} attach
  12. * Attaches dropButton behaviors.
  13. */
  14. Drupal.behaviors.dropButton = {
  15. attach(context, settings) {
  16. const $dropbuttons = $(context).find('.dropbutton-wrapper').once('dropbutton');
  17. if ($dropbuttons.length) {
  18. // Adds the delegated handler that will toggle dropdowns on click.
  19. const $body = $('body').once('dropbutton-click');
  20. if ($body.length) {
  21. $body.on('click', '.dropbutton-toggle', dropbuttonClickHandler);
  22. }
  23. // Initialize all buttons.
  24. const il = $dropbuttons.length;
  25. for (let i = 0; i < il; i++) {
  26. DropButton.dropbuttons.push(new DropButton($dropbuttons[i], settings.dropbutton));
  27. }
  28. }
  29. },
  30. };
  31. /**
  32. * Delegated callback for opening and closing dropbutton secondary actions.
  33. *
  34. * @function Drupal.DropButton~dropbuttonClickHandler
  35. *
  36. * @param {jQuery.Event} e
  37. * The event triggered.
  38. */
  39. function dropbuttonClickHandler(e) {
  40. e.preventDefault();
  41. $(e.target).closest('.dropbutton-wrapper').toggleClass('open');
  42. }
  43. /**
  44. * A DropButton presents an HTML list as a button with a primary action.
  45. *
  46. * All secondary actions beyond the first in the list are presented in a
  47. * dropdown list accessible through a toggle arrow associated with the button.
  48. *
  49. * @constructor Drupal.DropButton
  50. *
  51. * @param {HTMLElement} dropbutton
  52. * A DOM element.
  53. * @param {object} settings
  54. * A list of options including:
  55. * @param {string} settings.title
  56. * The text inside the toggle link element. This text is hidden
  57. * from visual UAs.
  58. */
  59. function DropButton(dropbutton, settings) {
  60. // Merge defaults with settings.
  61. const options = $.extend({ title: Drupal.t('List additional actions') }, settings);
  62. const $dropbutton = $(dropbutton);
  63. /**
  64. * @type {jQuery}
  65. */
  66. this.$dropbutton = $dropbutton;
  67. /**
  68. * @type {jQuery}
  69. */
  70. this.$list = $dropbutton.find('.dropbutton');
  71. /**
  72. * Find actions and mark them.
  73. *
  74. * @type {jQuery}
  75. */
  76. this.$actions = this.$list.find('li').addClass('dropbutton-action');
  77. // Add the special dropdown only if there are hidden actions.
  78. if (this.$actions.length > 1) {
  79. // Identify the first element of the collection.
  80. const $primary = this.$actions.slice(0, 1);
  81. // Identify the secondary actions.
  82. const $secondary = this.$actions.slice(1);
  83. $secondary.addClass('secondary-action');
  84. // Add toggle link.
  85. $primary.after(Drupal.theme('dropbuttonToggle', options));
  86. // Bind mouse events.
  87. this.$dropbutton
  88. .addClass('dropbutton-multiple')
  89. .on({
  90. /**
  91. * Adds a timeout to close the dropdown on mouseleave.
  92. *
  93. * @ignore
  94. */
  95. 'mouseleave.dropbutton': $.proxy(this.hoverOut, this),
  96. /**
  97. * Clears timeout when mouseout of the dropdown.
  98. *
  99. * @ignore
  100. */
  101. 'mouseenter.dropbutton': $.proxy(this.hoverIn, this),
  102. /**
  103. * Similar to mouseleave/mouseenter, but for keyboard navigation.
  104. *
  105. * @ignore
  106. */
  107. 'focusout.dropbutton': $.proxy(this.focusOut, this),
  108. /**
  109. * @ignore
  110. */
  111. 'focusin.dropbutton': $.proxy(this.focusIn, this),
  112. });
  113. }
  114. else {
  115. this.$dropbutton.addClass('dropbutton-single');
  116. }
  117. }
  118. /**
  119. * Extend the DropButton constructor.
  120. */
  121. $.extend(DropButton, /** @lends Drupal.DropButton */{
  122. /**
  123. * Store all processed DropButtons.
  124. *
  125. * @type {Array.<Drupal.DropButton>}
  126. */
  127. dropbuttons: [],
  128. });
  129. /**
  130. * Extend the DropButton prototype.
  131. */
  132. $.extend(DropButton.prototype, /** @lends Drupal.DropButton# */{
  133. /**
  134. * Toggle the dropbutton open and closed.
  135. *
  136. * @param {bool} [show]
  137. * Force the dropbutton to open by passing true or to close by
  138. * passing false.
  139. */
  140. toggle(show) {
  141. const isBool = typeof show === 'boolean';
  142. show = isBool ? show : !this.$dropbutton.hasClass('open');
  143. this.$dropbutton.toggleClass('open', show);
  144. },
  145. /**
  146. * @method
  147. */
  148. hoverIn() {
  149. // Clear any previous timer we were using.
  150. if (this.timerID) {
  151. window.clearTimeout(this.timerID);
  152. }
  153. },
  154. /**
  155. * @method
  156. */
  157. hoverOut() {
  158. // Wait half a second before closing.
  159. this.timerID = window.setTimeout($.proxy(this, 'close'), 500);
  160. },
  161. /**
  162. * @method
  163. */
  164. open() {
  165. this.toggle(true);
  166. },
  167. /**
  168. * @method
  169. */
  170. close() {
  171. this.toggle(false);
  172. },
  173. /**
  174. * @param {jQuery.Event} e
  175. * The event triggered.
  176. */
  177. focusOut(e) {
  178. this.hoverOut.call(this, e);
  179. },
  180. /**
  181. * @param {jQuery.Event} e
  182. * The event triggered.
  183. */
  184. focusIn(e) {
  185. this.hoverIn.call(this, e);
  186. },
  187. });
  188. $.extend(Drupal.theme, /** @lends Drupal.theme */{
  189. /**
  190. * A toggle is an interactive element often bound to a click handler.
  191. *
  192. * @param {object} options
  193. * Options object.
  194. * @param {string} [options.title]
  195. * The HTML anchor title attribute and text for the inner span element.
  196. *
  197. * @return {string}
  198. * A string representing a DOM fragment.
  199. */
  200. dropbuttonToggle(options) {
  201. return `<li class="dropbutton-toggle"><button type="button"><span class="dropbutton-arrow"><span class="visually-hidden">${options.title}</span></span></button></li>`;
  202. },
  203. });
  204. // Expose constructor in the public space.
  205. Drupal.DropButton = DropButton;
  206. }(jQuery, Drupal));