dropdown.js 4.1 KB

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