dropdown.js 4.7 KB

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