dropdown.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* ========================================================================
  2. * Bootstrap: dropdown.js v3.0.3
  3. * http://getbootstrap.com/javascript/#dropdowns
  4. * ========================================================================
  5. * Copyright 2013 Twitter, Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ======================================================================== */
  19. +function ($) { "use strict";
  20. // DROPDOWN CLASS DEFINITION
  21. // =========================
  22. var backdrop = '.dropdown-backdrop'
  23. var toggle = '[data-toggle=dropdown]'
  24. var Dropdown = function (element) {
  25. $(element).on('click.bs.dropdown', this.toggle)
  26. }
  27. Dropdown.prototype.toggle = function (e) {
  28. var $this = $(this)
  29. if ($this.is('.disabled, :disabled')) return
  30. var $parent = getParent($this)
  31. var isActive = $parent.hasClass('open')
  32. clearMenus()
  33. if (!isActive) {
  34. if ('ontouchstart' in document.documentElement && !$parent.closest('.navbar-nav').length) {
  35. // if mobile we use a backdrop because click events don't delegate
  36. $('<div class="dropdown-backdrop"/>').insertAfter($(this)).on('click', clearMenus)
  37. }
  38. $parent.trigger(e = $.Event('show.bs.dropdown'))
  39. if (e.isDefaultPrevented()) return
  40. $parent
  41. .toggleClass('open')
  42. .trigger('shown.bs.dropdown')
  43. $this.focus()
  44. }
  45. return false
  46. }
  47. Dropdown.prototype.keydown = function (e) {
  48. if (!/(38|40|27)/.test(e.keyCode)) return
  49. var $this = $(this)
  50. e.preventDefault()
  51. e.stopPropagation()
  52. if ($this.is('.disabled, :disabled')) return
  53. var $parent = getParent($this)
  54. var isActive = $parent.hasClass('open')
  55. if (!isActive || (isActive && e.keyCode == 27)) {
  56. if (e.which == 27) $parent.find(toggle).focus()
  57. return $this.click()
  58. }
  59. var $items = $('[role=menu] li:not(.divider):visible a', $parent)
  60. if (!$items.length) return
  61. var index = $items.index($items.filter(':focus'))
  62. if (e.keyCode == 38 && index > 0) index-- // up
  63. if (e.keyCode == 40 && index < $items.length - 1) index++ // down
  64. if (!~index) index=0
  65. $items.eq(index).focus()
  66. }
  67. function clearMenus() {
  68. $(backdrop).remove()
  69. $(toggle).each(function (e) {
  70. var $parent = getParent($(this))
  71. if (!$parent.hasClass('open')) return
  72. $parent.trigger(e = $.Event('hide.bs.dropdown'))
  73. if (e.isDefaultPrevented()) return
  74. $parent.removeClass('open').trigger('hidden.bs.dropdown')
  75. })
  76. }
  77. function getParent($this) {
  78. var selector = $this.attr('data-target')
  79. if (!selector) {
  80. selector = $this.attr('href')
  81. selector = selector && /#/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
  82. }
  83. var $parent = selector && $(selector)
  84. return $parent && $parent.length ? $parent : $this.parent()
  85. }
  86. // DROPDOWN PLUGIN DEFINITION
  87. // ==========================
  88. var old = $.fn.dropdown
  89. $.fn.dropdown = function (option) {
  90. return this.each(function () {
  91. var $this = $(this)
  92. var data = $this.data('bs.dropdown')
  93. if (!data) $this.data('bs.dropdown', (data = new Dropdown(this)))
  94. if (typeof option == 'string') data[option].call($this)
  95. })
  96. }
  97. $.fn.dropdown.Constructor = Dropdown
  98. // DROPDOWN NO CONFLICT
  99. // ====================
  100. $.fn.dropdown.noConflict = function () {
  101. $.fn.dropdown = old
  102. return this
  103. }
  104. // APPLY TO STANDARD DROPDOWN ELEMENTS
  105. // ===================================
  106. $(document)
  107. .on('click.bs.dropdown.data-api', clearMenus)
  108. .on('click.bs.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() })
  109. .on('click.bs.dropdown.data-api' , toggle, Dropdown.prototype.toggle)
  110. .on('keydown.bs.dropdown.data-api', toggle + ', [role=menu]' , Dropdown.prototype.keydown)
  111. }(jQuery);