3.0.0 -> 3.0.1
[bootswatch] / bower_components / bootstrap / js / dropdown.js
1 /* ========================================================================
2  * Bootstrap: dropdown.js v3.0.0
3  * http://getbootstrap.com/javascript/#dropdowns
4  * ========================================================================
5  * Copyright 2013 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
20
21 +function ($) { "use strict";
22
23   // DROPDOWN CLASS DEFINITION
24   // =========================
25
26   var backdrop = '.dropdown-backdrop'
27   var toggle   = '[data-toggle=dropdown]'
28   var Dropdown = function (element) {
29     var $el = $(element).on('click.bs.dropdown', this.toggle)
30   }
31
32   Dropdown.prototype.toggle = function (e) {
33     var $this = $(this)
34
35     if ($this.is('.disabled, :disabled')) return
36
37     var $parent  = getParent($this)
38     var isActive = $parent.hasClass('open')
39
40     clearMenus()
41
42     if (!isActive) {
43       if ('ontouchstart' in document.documentElement && !$parent.closest('.navbar-nav').length) {
44         // if mobile we we use a backdrop because click events don't delegate
45         $('<div class="dropdown-backdrop"/>').insertAfter($(this)).on('click', clearMenus)
46       }
47
48       $parent.trigger(e = $.Event('show.bs.dropdown'))
49
50       if (e.isDefaultPrevented()) return
51
52       $parent
53         .toggleClass('open')
54         .trigger('shown.bs.dropdown')
55
56       $this.focus()
57     }
58
59     return false
60   }
61
62   Dropdown.prototype.keydown = function (e) {
63     if (!/(38|40|27)/.test(e.keyCode)) return
64
65     var $this = $(this)
66
67     e.preventDefault()
68     e.stopPropagation()
69
70     if ($this.is('.disabled, :disabled')) return
71
72     var $parent  = getParent($this)
73     var isActive = $parent.hasClass('open')
74
75     if (!isActive || (isActive && e.keyCode == 27)) {
76       if (e.which == 27) $parent.find(toggle).focus()
77       return $this.click()
78     }
79
80     var $items = $('[role=menu] li:not(.divider):visible a', $parent)
81
82     if (!$items.length) return
83
84     var index = $items.index($items.filter(':focus'))
85
86     if (e.keyCode == 38 && index > 0)                 index--                        // up
87     if (e.keyCode == 40 && index < $items.length - 1) index++                        // down
88     if (!~index)                                      index=0
89
90     $items.eq(index).focus()
91   }
92
93   function clearMenus() {
94     $(backdrop).remove()
95     $(toggle).each(function (e) {
96       var $parent = getParent($(this))
97       if (!$parent.hasClass('open')) return
98       $parent.trigger(e = $.Event('hide.bs.dropdown'))
99       if (e.isDefaultPrevented()) return
100       $parent.removeClass('open').trigger('hidden.bs.dropdown')
101     })
102   }
103
104   function getParent($this) {
105     var selector = $this.attr('data-target')
106
107     if (!selector) {
108       selector = $this.attr('href')
109       selector = selector && /#/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
110     }
111
112     var $parent = selector && $(selector)
113
114     return $parent && $parent.length ? $parent : $this.parent()
115   }
116
117
118   // DROPDOWN PLUGIN DEFINITION
119   // ==========================
120
121   var old = $.fn.dropdown
122
123   $.fn.dropdown = function (option) {
124     return this.each(function () {
125       var $this = $(this)
126       var data  = $this.data('dropdown')
127
128       if (!data) $this.data('dropdown', (data = new Dropdown(this)))
129       if (typeof option == 'string') data[option].call($this)
130     })
131   }
132
133   $.fn.dropdown.Constructor = Dropdown
134
135
136   // DROPDOWN NO CONFLICT
137   // ====================
138
139   $.fn.dropdown.noConflict = function () {
140     $.fn.dropdown = old
141     return this
142   }
143
144
145   // APPLY TO STANDARD DROPDOWN ELEMENTS
146   // ===================================
147
148   $(document)
149     .on('click.bs.dropdown.data-api', clearMenus)
150     .on('click.bs.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() })
151     .on('click.bs.dropdown.data-api'  , toggle, Dropdown.prototype.toggle)
152     .on('keydown.bs.dropdown.data-api', toggle + ', [role=menu]' , Dropdown.prototype.keydown)
153
154 }(window.jQuery);