bootstrap-dropdown.js 2.5 KB

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