bootstrap-dropdown.js 3.88 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ============================================================
Mark Otto's avatar
Mark Otto committed
2
 * bootstrap-dropdown.js v2.1.1
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
!function ($) {

  "use strict"; // jshint ;_;
24
25


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

29
  var toggle = '[data-toggle=dropdown]'
30
    , Dropdown = function (element) {
Jacob Thornton's avatar
Jacob Thornton committed
31
32
33
34
        var $el = $(element).on('click.dropdown.data-api', this.toggle)
        $('html').on('click.dropdown.data-api', function () {
          $el.parent().removeClass('open')
        })
35
      }
36

37
38
  Dropdown.prototype = {

39
40
    constructor: Dropdown

41
  , toggle: function (e) {
42
      var $this = $(this)
Jacob Thornton's avatar
Jacob Thornton committed
43
        , $parent
44
        , isActive
45

46
47
      if ($this.is('.disabled, :disabled')) return

48
      $parent = getParent($this)
49

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

52
      clearMenus()
53

54
55
56
      if (!isActive) {
        $parent.toggleClass('open')
        $this.focus()
Jacob Thornton's avatar
Jacob Thornton committed
57
58
      }

59
60
      return false
    }
61

62
  , keydown: function (e) {
63
      var $this
64
65
66
        , $items
        , $active
        , $parent
67
        , isActive
68
69
        , index

70
      if (!/(38|40|27)/.test(e.keyCode)) return
71

72
      $this = $(this)
73
74
75
76
77
78
79

      e.preventDefault()
      e.stopPropagation()

      if ($this.is('.disabled, :disabled')) return

      $parent = getParent($this)
Jacob Thornton's avatar
Jacob Thornton committed
80

81
      isActive = $parent.hasClass('open')
82

83
      if (!isActive || (isActive && e.keyCode == 27)) return $this.click()
84

85
      $items = $('[role=menu] li:not(.divider) a', $parent)
86

87
88
      if (!$items.length) return

89
      index = $items.index($items.filter(':focus'))
90
91

      if (e.keyCode == 38 && index > 0) index--                                        // up
92
      if (e.keyCode == 40 && index < $items.length - 1) index++                        // down
93
94
95
96
      if (!~index) index = 0

      $items
        .eq(index)
97
        .focus()
98
    }
99

100
101
102
  }

  function clearMenus() {
103
104
105
106
107
108
109
110
111
112
    getParent($(toggle))
      .removeClass('open')
  }

  function getParent($this) {
    var selector = $this.attr('data-target')
      , $parent

    if (!selector) {
      selector = $this.attr('href')
113
      selector = selector && /#/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
114
115
116
117
118
119
    }

    $parent = $(selector)
    $parent.length || ($parent = $this.parent())

    return $parent
120
121
122
  }


123
124
125
  /* DROPDOWN PLUGIN DEFINITION
   * ========================== */

126
  $.fn.dropdown = function (option) {
127
    return this.each(function () {
128
129
130
131
      var $this = $(this)
        , data = $this.data('dropdown')
      if (!data) $this.data('dropdown', (data = new Dropdown(this)))
      if (typeof option == 'string') data[option].call($this)
132
133
    })
  }
Jacob Thornton's avatar
Jacob Thornton committed
134

135
136
  $.fn.dropdown.Constructor = Dropdown

137

138
139
  /* APPLY TO STANDARD DROPDOWN ELEMENTS
   * =================================== */
140

141
  $(function () {
142
    $('html')
143
      .on('click.dropdown.data-api touchstart.dropdown.data-api', clearMenus)
144
    $('body')
145
      .on('click.dropdown touchstart.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() })
146
147
      .on('click.dropdown.data-api touchstart.dropdown.data-api'  , toggle, Dropdown.prototype.toggle)
      .on('keydown.dropdown.data-api touchstart.dropdown.data-api', toggle + ', [role=menu]' , Dropdown.prototype.keydown)
148
149
  })

150
}(window.jQuery);