dropbutton.es6.js 5.9 KB

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