bootstrap-dropdown.js 2.36 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ============================================================
2
 * bootstrap-dropdown.js v2.0.0
Jon Stevens's avatar
Jon Stevens committed
3
 * http://twitter.github.com/bootstrap/javascript.html#dropdowns
Jacob Thornton's avatar
Jacob Thornton committed
4
 * ============================================================
Mark Otto's avatar
Mark Otto committed
5
 * Copyright 2012 Twitter, Inc.
Jacob Thornton's avatar
Jacob Thornton committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============================================================ */

Jacob Thornton's avatar
Jacob Thornton committed
20

21
22
23
24
!function( $ ){

  "use strict"

25
26
 /* DROPDOWN CLASS DEFINITION
  * ========================= */
27

28
29
30
31
  var toggle = '[data-toggle="dropdown"]'
    , Dropdown = function ( element ) {
        $(element).bind('click', this.toggle)
      }
32

33
34
  Dropdown.prototype = {

35
36
37
    constructor: Dropdown

  , toggle: function ( e ) {
38
      var $this = $(this)
Jacob Thornton's avatar
Jacob Thornton committed
39
40
        , selector = $this.attr('data-target')
        , $parent
41
        , isActive
42

Jacob Thornton's avatar
Jacob Thornton committed
43
44
45
46
47
48
      if (!selector) {
        selector = $this.attr('href')
        selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
      }

      $parent = $(selector)
49
      $parent.length || ($parent = $this.parent())
Jacob Thornton's avatar
Jacob Thornton committed
50

51
      isActive = $parent.hasClass('open')
52

53
      clearMenus()
54

55
      !isActive && $parent.toggleClass('open')
56

57
58
      return false
    }
59

60
61
62
  }

  function clearMenus() {
63
    $(toggle).parent().removeClass('open')
64
65
66
  }


67
68
69
  /* DROPDOWN PLUGIN DEFINITION
   * ========================== */

70
  $.fn.dropdown = function ( option ) {
71
    return this.each(function () {
72
73
74
75
      var $this = $(this)
        , data = $this.data('dropdown')
      if (!data) $this.data('dropdown', (data = new Dropdown(this)))
      if (typeof option == 'string') data[option].call($this)
76
77
    })
  }
Jacob Thornton's avatar
Jacob Thornton committed
78

79
80
  $.fn.dropdown.Constructor = Dropdown

81

82
83
  /* APPLY TO STANDARD DROPDOWN ELEMENTS
   * =================================== */
84

85
  $(function () {
Jacob Thornton's avatar
Jacob Thornton committed
86
    $('html').on('click.dropdown.data-api', clearMenus)
87
    $('body').on('click.dropdown.data-api', toggle, Dropdown.prototype.toggle)
88
89
  })

90
}( window.jQuery )