]> git.openstreetmap.org Git - rails.git/blob - vendor/assets/bootstrap/bootstrap.dropdown.js
Compact user menu
[rails.git] / vendor / assets / bootstrap / bootstrap.dropdown.js
1 /* ============================================================
2  * bootstrap-dropdown.js v2.3.2
3  * http://getbootstrap.com/2.3.2/javascript.html#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 ($) {
22
23   "use strict"; // jshint ;_;
24
25
26  /* DROPDOWN CLASS DEFINITION
27   * ========================= */
28
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')
34         })
35       }
36
37   Dropdown.prototype = {
38
39     constructor: Dropdown
40
41   , toggle: function (e) {
42       var $this = $(this)
43         , $parent
44         , isActive
45
46       if ($this.is('.disabled, :disabled')) return
47
48       $parent = getParent($this)
49
50       isActive = $parent.hasClass('open')
51
52       clearMenus()
53
54       if (!isActive) {
55         if ('ontouchstart' in document.documentElement) {
56           // if mobile we we use a backdrop because click events don't delegate
57           $('<div class="dropdown-backdrop"/>').insertBefore($(this)).on('click', clearMenus)
58         }
59         $parent.toggleClass('open')
60       }
61
62       $this.focus()
63
64       return false
65     }
66
67   , keydown: function (e) {
68       var $this
69         , $items
70         , $active
71         , $parent
72         , isActive
73         , index
74
75       if (!/(38|40|27)/.test(e.keyCode)) return
76
77       $this = $(this)
78
79       e.preventDefault()
80       e.stopPropagation()
81
82       if ($this.is('.disabled, :disabled')) return
83
84       $parent = getParent($this)
85
86       isActive = $parent.hasClass('open')
87
88       if (!isActive || (isActive && e.keyCode == 27)) {
89         if (e.which == 27) $parent.find(toggle).focus()
90         return $this.click()
91       }
92
93       $items = $('[role=menu] li:not(.divider):visible a', $parent)
94
95       if (!$items.length) return
96
97       index = $items.index($items.filter(':focus'))
98
99       if (e.keyCode == 38 && index > 0) index--                                        // up
100       if (e.keyCode == 40 && index < $items.length - 1) index++                        // down
101       if (!~index) index = 0
102
103       $items
104         .eq(index)
105         .focus()
106     }
107
108   }
109
110   function clearMenus() {
111     $('.dropdown-backdrop').remove()
112     $(toggle).each(function () {
113       getParent($(this)).removeClass('open')
114     })
115   }
116
117   function getParent($this) {
118     var selector = $this.attr('data-target')
119       , $parent
120
121     if (!selector) {
122       selector = $this.attr('href')
123       selector = selector && /#/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
124     }
125
126     $parent = selector && $(selector)
127
128     if (!$parent || !$parent.length) $parent = $this.parent()
129
130     return $parent
131   }
132
133
134   /* DROPDOWN PLUGIN DEFINITION
135    * ========================== */
136
137   var old = $.fn.dropdown
138
139   $.fn.dropdown = function (option) {
140     return this.each(function () {
141       var $this = $(this)
142         , data = $this.data('dropdown')
143       if (!data) $this.data('dropdown', (data = new Dropdown(this)))
144       if (typeof option == 'string') data[option].call($this)
145     })
146   }
147
148   $.fn.dropdown.Constructor = Dropdown
149
150
151  /* DROPDOWN NO CONFLICT
152   * ==================== */
153
154   $.fn.dropdown.noConflict = function () {
155     $.fn.dropdown = old
156     return this
157   }
158
159
160   /* APPLY TO STANDARD DROPDOWN ELEMENTS
161    * =================================== */
162
163   $(document)
164     .on('click.dropdown.data-api', clearMenus)
165     .on('click.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() })
166     .on('click.dropdown.data-api'  , toggle, Dropdown.prototype.toggle)
167     .on('keydown.dropdown.data-api', toggle + ', [role=menu]' , Dropdown.prototype.keydown)
168
169 }(window.jQuery);