1 /* ============================================================
2 * bootstrap-dropdown.js v2.3.1
3 * http://twitter.github.com/bootstrap/javascript.html#dropdowns
4 * ============================================================
5 * Copyright 2012 Twitter, Inc.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============================================================ */
23 "use strict"; // jshint ;_;
26 /* DROPDOWN CLASS DEFINITION
27 * ========================= */
29 var toggle = '[data-toggle=dropdown]'
30 , Dropdown = function (element) {
31 var $el = $(element).on('click.dropdown.data-api', this.toggle)
32 $('html').on('click.dropdown.data-api', function () {
33 $el.parent().removeClass('open')
37 Dropdown.prototype = {
41 , toggle: function (e) {
46 if ($this.is('.disabled, :disabled')) return
48 $parent = getParent($this)
50 isActive = $parent.hasClass('open')
55 $parent.toggleClass('open')
63 , keydown: function (e) {
71 if (!/(38|40|27)/.test(e.keyCode)) return
78 if ($this.is('.disabled, :disabled')) return
80 $parent = getParent($this)
82 isActive = $parent.hasClass('open')
84 if (!isActive || (isActive && e.keyCode == 27)) {
85 if (e.which == 27) $parent.find(toggle).focus()
89 $items = $('[role=menu] li:not(.divider):visible a', $parent)
91 if (!$items.length) return
93 index = $items.index($items.filter(':focus'))
95 if (e.keyCode == 38 && index > 0) index-- // up
96 if (e.keyCode == 40 && index < $items.length - 1) index++ // down
97 if (!~index) index = 0
106 function clearMenus() {
107 $(toggle).each(function () {
108 getParent($(this)).removeClass('open')
112 function getParent($this) {
113 var selector = $this.attr('data-target')
117 selector = $this.attr('href')
118 selector = selector && /#/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
121 $parent = selector && $(selector)
123 if (!$parent || !$parent.length) $parent = $this.parent()
129 /* DROPDOWN PLUGIN DEFINITION
130 * ========================== */
132 var old = $.fn.dropdown
134 $.fn.dropdown = function (option) {
135 return this.each(function () {
137 , data = $this.data('dropdown')
138 if (!data) $this.data('dropdown', (data = new Dropdown(this)))
139 if (typeof option == 'string') data[option].call($this)
143 $.fn.dropdown.Constructor = Dropdown
146 /* DROPDOWN NO CONFLICT
147 * ==================== */
149 $.fn.dropdown.noConflict = function () {
155 /* APPLY TO STANDARD DROPDOWN ELEMENTS
156 * =================================== */
159 .on('click.dropdown.data-api', clearMenus)
160 .on('click.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() })
161 .on('click.dropdown-menu', function (e) { e.stopPropagation() })
162 .on('click.dropdown.data-api' , toggle, Dropdown.prototype.toggle)
163 .on('keydown.dropdown.data-api', toggle + ', [role=menu]' , Dropdown.prototype.keydown)