bootstrap-dropdown.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import jQuery from 'jquery';
  2. /* ========================================================================
  3. * Bootstrap: dropdown.js v3.4.1
  4. * https://getbootstrap.com/docs/3.4/javascript/#dropdowns
  5. * ========================================================================
  6. * Copyright 2011-2019 Twitter, Inc.
  7. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/v3-dev/LICENSE)
  8. * ======================================================================== */
  9. +(function($) {
  10. 'use strict';
  11. // DROPDOWN CLASS DEFINITION
  12. // =========================
  13. const backdrop = '.dropdown-backdrop';
  14. const toggle = '[data-toggle="dropdown"]';
  15. const Dropdown = function(element) {
  16. $(element).on('click.bs.dropdown', this.toggle);
  17. };
  18. Dropdown.VERSION = '3.4.1';
  19. function getParent($this) {
  20. let selector = $this.attr('data-target');
  21. if (!selector) {
  22. selector = $this.attr('href');
  23. selector = selector && /#[A-Za-z]/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, ''); // strip for ie7
  24. }
  25. const $parent = selector !== '#' ? $(document).find(selector) : null;
  26. return $parent && $parent.length ? $parent : $this.parent();
  27. }
  28. function clearMenus(e) {
  29. if (e && e.which === 3) { return; }
  30. $(backdrop).remove();
  31. $(toggle).each(function() {
  32. const $this = $(this);
  33. const $parent = getParent($this);
  34. const relatedTarget = { relatedTarget: this };
  35. if (!$parent.hasClass('open')) { return; }
  36. if (e && e.type === 'click' && /input|textarea/i.test(e.target.tagName) && $.contains($parent[0], e.target)) { return; }
  37. $parent.trigger(e = $.Event('hide.bs.dropdown', relatedTarget));
  38. if (e.isDefaultPrevented()) { return; }
  39. $this.attr('aria-expanded', 'false');
  40. $parent.removeClass('open').trigger($.Event('hidden.bs.dropdown', relatedTarget));
  41. });
  42. }
  43. Dropdown.prototype.toggle = function(e) {
  44. const $this = $(this);
  45. if ($this.is('.disabled, :disabled')) { return; }
  46. const $parent = getParent($this);
  47. const isActive = $parent.hasClass('open');
  48. clearMenus();
  49. if (!isActive) {
  50. if ('ontouchstart' in document.documentElement && !$parent.closest('.navbar-nav').length) {
  51. // if mobile we use a backdrop because click events don't delegate
  52. $(document.createElement('div'))
  53. .addClass('dropdown-backdrop')
  54. .insertAfter($(this))
  55. .on('click', clearMenus);
  56. }
  57. const relatedTarget = { relatedTarget: this };
  58. $parent.trigger(e = $.Event('show.bs.dropdown', relatedTarget));
  59. if (e.isDefaultPrevented()) { return; }
  60. $this
  61. .trigger('focus')
  62. .attr('aria-expanded', 'true');
  63. $parent
  64. .toggleClass('open')
  65. .trigger($.Event('shown.bs.dropdown', relatedTarget));
  66. }
  67. return false;
  68. };
  69. Dropdown.prototype.keydown = function(e) {
  70. if (!/(38|40|27|32)/.test(e.which) || /input|textarea/i.test(e.target.tagName)) return;
  71. const $this = $(this);
  72. e.preventDefault();
  73. e.stopPropagation();
  74. if ($this.is('.disabled, :disabled')) {
  75. return;
  76. }
  77. const $parent = getParent($this);
  78. const isActive = $parent.hasClass('open');
  79. if (!isActive && e.which !== 27 || isActive && e.which === 27) {
  80. if (e.which === 27) {
  81. $parent.find(toggle).trigger('focus');
  82. }
  83. return $this.trigger('click');
  84. }
  85. const desc = ' li:not(.disabled):visible a';
  86. const $items = $parent.find('.dropdown-menu' + desc);
  87. if (!$items.length) {
  88. return;
  89. }
  90. let index = $items.index(e.target);
  91. if (e.which === 38 && index > 0) { index--; } // up
  92. if (e.which === 40 && index < $items.length - 1) { index++; } // down
  93. if (!~index) { index = 0; }
  94. $items.eq(index).trigger('focus');
  95. };
  96. // DROPDOWN PLUGIN DEFINITION
  97. // ==========================
  98. function Plugin(option) {
  99. return this.each(function() {
  100. const $this = $(this);
  101. let data = $this.data('bs.dropdown');
  102. if (!data) $this.data('bs.dropdown', (data = new Dropdown(this)));
  103. if (typeof option === 'string') data[option].call($this);
  104. });
  105. }
  106. const old = $.fn.dropdown;
  107. $.fn.dropdown = Plugin;
  108. $.fn.dropdown.Constructor = Dropdown;
  109. // DROPDOWN NO CONFLICT
  110. // ====================
  111. $.fn.dropdown.noConflict = function() {
  112. $.fn.dropdown = old;
  113. return this;
  114. };
  115. // APPLY TO STANDARD DROPDOWN ELEMENTS
  116. // ===================================
  117. $(document)
  118. .on('click.bs.dropdown.data-api', clearMenus)
  119. .on('click.bs.dropdown.data-api', '.dropdown form', function(e) { e.stopPropagation(); })
  120. .on('click.bs.dropdown.data-api', toggle, Dropdown.prototype.toggle)
  121. .on('keydown.bs.dropdown.data-api', toggle, Dropdown.prototype.keydown)
  122. .on('keydown.bs.dropdown.data-api', '.dropdown-menu', Dropdown.prototype.keydown);
  123. }(jQuery));