Mercurial > hg > Members > nobuyasu > TwitterBootstrap
comparison assets/js/bootstrap-dropdown.js @ 3:902636e4a800 draft
move some files
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 19 Sep 2012 22:33:03 +0900 |
parents | js/bootstrap-dropdown.js@cfa3e2e51af7 |
children |
comparison
equal
deleted
inserted
replaced
2:144c93c1c71c | 3:902636e4a800 |
---|---|
1 /* ============================================================ | |
2 * bootstrap-dropdown.js v2.1.1 | |
3 * http://twitter.github.com/bootstrap/javascript.html#dropdowns | |
4 * ============================================================ | |
5 * Copyright 2012 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 $parent.toggleClass('open') | |
56 $this.focus() | |
57 } | |
58 | |
59 return false | |
60 } | |
61 | |
62 , keydown: function (e) { | |
63 var $this | |
64 , $items | |
65 , $active | |
66 , $parent | |
67 , isActive | |
68 , index | |
69 | |
70 if (!/(38|40|27)/.test(e.keyCode)) return | |
71 | |
72 $this = $(this) | |
73 | |
74 e.preventDefault() | |
75 e.stopPropagation() | |
76 | |
77 if ($this.is('.disabled, :disabled')) return | |
78 | |
79 $parent = getParent($this) | |
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 if (!$items.length) return | |
88 | |
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 if (!~index) index = 0 | |
94 | |
95 $items | |
96 .eq(index) | |
97 .focus() | |
98 } | |
99 | |
100 } | |
101 | |
102 function clearMenus() { | |
103 getParent($(toggle)) | |
104 .removeClass('open') | |
105 } | |
106 | |
107 function getParent($this) { | |
108 var selector = $this.attr('data-target') | |
109 , $parent | |
110 | |
111 if (!selector) { | |
112 selector = $this.attr('href') | |
113 selector = selector && /#/.test(selector) && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7 | |
114 } | |
115 | |
116 $parent = $(selector) | |
117 $parent.length || ($parent = $this.parent()) | |
118 | |
119 return $parent | |
120 } | |
121 | |
122 | |
123 /* DROPDOWN PLUGIN DEFINITION | |
124 * ========================== */ | |
125 | |
126 $.fn.dropdown = function (option) { | |
127 return this.each(function () { | |
128 var $this = $(this) | |
129 , data = $this.data('dropdown') | |
130 if (!data) $this.data('dropdown', (data = new Dropdown(this))) | |
131 if (typeof option == 'string') data[option].call($this) | |
132 }) | |
133 } | |
134 | |
135 $.fn.dropdown.Constructor = Dropdown | |
136 | |
137 | |
138 /* APPLY TO STANDARD DROPDOWN ELEMENTS | |
139 * =================================== */ | |
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 .on('click.dropdown.data-api touchstart.dropdown.data-api' , toggle, Dropdown.prototype.toggle) | |
147 .on('keydown.dropdown.data-api touchstart.dropdown.data-api', toggle + ', [role=menu]' , Dropdown.prototype.keydown) | |
148 }) | |
149 | |
150 }(window.jQuery); |