bootstrap-dropdown.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* ============================================================
  2. * bootstrap-dropdown.js v2.1.1
  3. * http://twitter.github.com/bootstrap/javascript.html#dropdowns
  4. * ============================================================
  5. * Copyright 2012 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 ($) {
  20. "use strict"; // jshint ;_;
  21. /* DROPDOWN CLASS DEFINITION
  22. * ========================= */
  23. var toggle = '[data-toggle=dropdown]'
  24. , Dropdown = function (element) {
  25. var $el = $(element).on('click.dropdown.data-api', this.toggle)
  26. $('html').on('click.dropdown.data-api', function () {
  27. $el.parent().removeClass('open')
  28. })
  29. }
  30. Dropdown.prototype = {
  31. constructor: Dropdown
  32. , toggle: function (e) {
  33. var $this = $(this)
  34. , $parent
  35. , isActive
  36. if ($this.is('.disabled, :disabled')) return
  37. $parent = getParent($this)
  38. isActive = $parent.hasClass('open')
  39. clearMenus()
  40. if (!isActive) {
  41. $parent.toggleClass('open')
  42. $this.focus()
  43. }
  44. return false
  45. }
  46. , keydown: function (e) {
  47. var $this
  48. , $items
  49. , $active
  50. , $parent
  51. , isActive
  52. , index
  53. if (!/(38|40|27)/.test(e.keyCode)) return
  54. $this = $(this)
  55. e.preventDefault()
  56. e.stopPropagation()
  57. if ($this.is('.disabled, :disabled')) return
  58. $parent = getParent($this)
  59. isActive = $parent.hasClass('open')
  60. if (!isActive || (isActive && e.keyCode == 27)) return $this.click()
  61. $items = $('[role=menu] li:not(.divider) a', $parent)
  62. if (!$items.length) return
  63. index = $items.index($items.filter(':focus'))
  64. if (e.keyCode == 38 && index > 0) index-- // up
  65. if (e.keyCode == 40 && index < $items.length - 1) index++ // down
  66. if (!~index) index = 0
  67. $items
  68. .eq(index)
  69. .focus()
  70. }
  71. }
  72. function clearMenus() {
  73. getParent($(toggle))
  74. .removeClass('open')
  75. }
  76. function getParent($this) {
  77. var selector = $this.attr('data-target')
  78. , $parent
  79. if (!selector) {
  80. selector = $this.attr('href')
  81. selector = selector && /#/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
  82. }
  83. $parent = $(selector)
  84. $parent.length || ($parent = $this.parent())
  85. return $parent
  86. }
  87. /* DROPDOWN PLUGIN DEFINITION
  88. * ========================== */
  89. $.fn.dropdown = function (option) {
  90. return this.each(function () {
  91. var $this = $(this)
  92. , data = $this.data('dropdown')
  93. if (!data) $this.data('dropdown', (data = new Dropdown(this)))
  94. if (typeof option == 'string') data[option].call($this)
  95. })
  96. }
  97. $.fn.dropdown.Constructor = Dropdown
  98. /* APPLY TO STANDARD DROPDOWN ELEMENTS
  99. * =================================== */
  100. $(function () {
  101. $('html')
  102. .on('click.dropdown.data-api touchstart.dropdown.data-api', clearMenus)
  103. $('body')
  104. .on('click.dropdown touchstart.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() })
  105. .on('click.dropdown.data-api touchstart.dropdown.data-api' , toggle, Dropdown.prototype.toggle)
  106. .on('keydown.dropdown.data-api touchstart.dropdown.data-api', toggle + ', [role=menu]' , Dropdown.prototype.keydown)
  107. })
  108. }(window.jQuery);