dropdown.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* ========================================================================
  2. * Bootstrap: dropdown.js v3.2.0
  3. * http://getbootstrap.com/javascript/#dropdowns
  4. * ========================================================================
  5. * Copyright 2011-2014 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.2.0'
  18. Dropdown.prototype.toggle = function (e) {
  19. var $this = $(this)
  20. if ($this.is('.disabled, :disabled')) return
  21. var $parent = getParent($this)
  22. var isActive = $parent.hasClass('open')
  23. clearMenus()
  24. if (!isActive) {
  25. if ('ontouchstart' in document.documentElement && !$parent.closest('.navbar-nav').length) {
  26. // if mobile we use a backdrop because click events don't delegate
  27. $('<div class="dropdown-backdrop"/>').insertAfter($(this)).on('click', clearMenus)
  28. }
  29. var relatedTarget = { relatedTarget: this }
  30. $parent.trigger(e = $.Event('show.bs.dropdown', relatedTarget))
  31. if (e.isDefaultPrevented()) return
  32. $this.trigger('focus')
  33. $parent
  34. .toggleClass('open')
  35. .trigger('shown.bs.dropdown', relatedTarget)
  36. }
  37. return false
  38. }
  39. Dropdown.prototype.keydown = function (e) {
  40. if (!/(38|40|27)/.test(e.keyCode)) return
  41. var $this = $(this)
  42. e.preventDefault()
  43. e.stopPropagation()
  44. if ($this.is('.disabled, :disabled')) return
  45. var $parent = getParent($this)
  46. var isActive = $parent.hasClass('open')
  47. if (!isActive || (isActive && e.keyCode == 27)) {
  48. if (e.which == 27) $parent.find(toggle).trigger('focus')
  49. return $this.trigger('click')
  50. }
  51. var desc = ' li:not(.divider):visible a'
  52. var $items = $parent.find('[role="menu"]' + desc + ', [role="listbox"]' + desc)
  53. if (!$items.length) return
  54. var index = $items.index($items.filter(':focus'))
  55. if (e.keyCode == 38 && index > 0) index-- // up
  56. if (e.keyCode == 40 && index < $items.length - 1) index++ // down
  57. if (!~index) index = 0
  58. $items.eq(index).trigger('focus')
  59. }
  60. function clearMenus(e) {
  61. if (e && e.which === 3) return
  62. $(backdrop).remove()
  63. $(toggle).each(function () {
  64. var $parent = getParent($(this))
  65. var relatedTarget = { relatedTarget: this }
  66. if (!$parent.hasClass('open')) return
  67. $parent.trigger(e = $.Event('hide.bs.dropdown', relatedTarget))
  68. if (e.isDefaultPrevented()) return
  69. $parent.removeClass('open').trigger('hidden.bs.dropdown', relatedTarget)
  70. })
  71. }
  72. function getParent($this) {
  73. var selector = $this.attr('data-target')
  74. if (!selector) {
  75. selector = $this.attr('href')
  76. selector = selector && /#[A-Za-z]/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') // strip for ie7
  77. }
  78. var $parent = selector && $(selector)
  79. return $parent && $parent.length ? $parent : $this.parent()
  80. }
  81. // DROPDOWN PLUGIN DEFINITION
  82. // ==========================
  83. function Plugin(option) {
  84. return this.each(function () {
  85. var $this = $(this)
  86. var data = $this.data('bs.dropdown')
  87. if (!data) $this.data('bs.dropdown', (data = new Dropdown(this)))
  88. if (typeof option == 'string') data[option].call($this)
  89. })
  90. }
  91. var old = $.fn.dropdown
  92. $.fn.dropdown = Plugin
  93. $.fn.dropdown.Constructor = Dropdown
  94. // DROPDOWN NO CONFLICT
  95. // ====================
  96. $.fn.dropdown.noConflict = function () {
  97. $.fn.dropdown = old
  98. return this
  99. }
  100. // APPLY TO STANDARD DROPDOWN ELEMENTS
  101. // ===================================
  102. $(document)
  103. .on('click.bs.dropdown.data-api', clearMenus)
  104. .on('click.bs.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() })
  105. .on('click.bs.dropdown.data-api', toggle, Dropdown.prototype.toggle)
  106. .on('keydown.bs.dropdown.data-api', toggle + ', [role="menu"], [role="listbox"]', Dropdown.prototype.keydown)
  107. }(jQuery);