dropbutton.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal) {
  8. Drupal.behaviors.dropButton = {
  9. attach: function attach(context, settings) {
  10. var $dropbuttons = $(context).find('.dropbutton-wrapper').once('dropbutton');
  11. if ($dropbuttons.length) {
  12. var $body = $('body').once('dropbutton-click');
  13. if ($body.length) {
  14. $body.on('click', '.dropbutton-toggle', dropbuttonClickHandler);
  15. }
  16. var il = $dropbuttons.length;
  17. for (var i = 0; i < il; i++) {
  18. DropButton.dropbuttons.push(new DropButton($dropbuttons[i], settings.dropbutton));
  19. }
  20. }
  21. }
  22. };
  23. function dropbuttonClickHandler(e) {
  24. e.preventDefault();
  25. $(e.target).closest('.dropbutton-wrapper').toggleClass('open');
  26. }
  27. function DropButton(dropbutton, settings) {
  28. var options = $.extend({ title: Drupal.t('List additional actions') }, settings);
  29. var $dropbutton = $(dropbutton);
  30. this.$dropbutton = $dropbutton;
  31. this.$list = $dropbutton.find('.dropbutton');
  32. this.$actions = this.$list.find('li').addClass('dropbutton-action');
  33. if (this.$actions.length > 1) {
  34. var $primary = this.$actions.slice(0, 1);
  35. var $secondary = this.$actions.slice(1);
  36. $secondary.addClass('secondary-action');
  37. $primary.after(Drupal.theme('dropbuttonToggle', options));
  38. this.$dropbutton.addClass('dropbutton-multiple').on({
  39. 'mouseleave.dropbutton': $.proxy(this.hoverOut, this),
  40. 'mouseenter.dropbutton': $.proxy(this.hoverIn, this),
  41. 'focusout.dropbutton': $.proxy(this.focusOut, this),
  42. 'focusin.dropbutton': $.proxy(this.focusIn, this)
  43. });
  44. } else {
  45. this.$dropbutton.addClass('dropbutton-single');
  46. }
  47. }
  48. $.extend(DropButton, {
  49. dropbuttons: []
  50. });
  51. $.extend(DropButton.prototype, {
  52. toggle: function toggle(show) {
  53. var isBool = typeof show === 'boolean';
  54. show = isBool ? show : !this.$dropbutton.hasClass('open');
  55. this.$dropbutton.toggleClass('open', show);
  56. },
  57. hoverIn: function hoverIn() {
  58. if (this.timerID) {
  59. window.clearTimeout(this.timerID);
  60. }
  61. },
  62. hoverOut: function hoverOut() {
  63. this.timerID = window.setTimeout($.proxy(this, 'close'), 500);
  64. },
  65. open: function open() {
  66. this.toggle(true);
  67. },
  68. close: function close() {
  69. this.toggle(false);
  70. },
  71. focusOut: function focusOut(e) {
  72. this.hoverOut.call(this, e);
  73. },
  74. focusIn: function focusIn(e) {
  75. this.hoverIn.call(this, e);
  76. }
  77. });
  78. $.extend(Drupal.theme, {
  79. dropbuttonToggle: function dropbuttonToggle(options) {
  80. return '<li class="dropbutton-toggle"><button type="button"><span class="dropbutton-arrow"><span class="visually-hidden">' + options.title + '</span></span></button></li>';
  81. }
  82. });
  83. Drupal.DropButton = DropButton;
  84. })(jQuery, Drupal);