bootstrap-dropdown.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* ============================================================
  2. * bootstrap-dropdown.js v2.0.4
  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. , selector
  36. , isActive
  37. if ($this.is('.disabled, :disabled')) return
  38. selector = $this.attr('data-target')
  39. if (!selector) {
  40. selector = $this.attr('href')
  41. selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
  42. }
  43. $parent = $(selector)
  44. $parent.length || ($parent = $this.parent())
  45. isActive = $parent.hasClass('open')
  46. clearMenus()
  47. if (!isActive) $parent.toggleClass('open')
  48. return false
  49. }
  50. }
  51. function clearMenus() {
  52. $(toggle).parent().removeClass('open')
  53. }
  54. /* DROPDOWN PLUGIN DEFINITION
  55. * ========================== */
  56. $.fn.dropdown = function (option) {
  57. return this.each(function () {
  58. var $this = $(this)
  59. , data = $this.data('dropdown')
  60. if (!data) $this.data('dropdown', (data = new Dropdown(this)))
  61. if (typeof option == 'string') data[option].call($this)
  62. })
  63. }
  64. $.fn.dropdown.Constructor = Dropdown
  65. /* APPLY TO STANDARD DROPDOWN ELEMENTS
  66. * =================================== */
  67. $(function () {
  68. $('html').on('click.dropdown.data-api', clearMenus)
  69. $('body')
  70. .on('click.dropdown', '.dropdown form', function (e) { e.stopPropagation() })
  71. .on('click.dropdown.data-api', toggle, Dropdown.prototype.toggle)
  72. })
  73. }(window.jQuery);