Mercurial > hg > Members > nobuyasu > TwitterBootstrap
comparison js/bootstrap-modal.js @ 0:cfa3e2e51af7 draft
add some files
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 19 Sep 2012 22:11:23 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:cfa3e2e51af7 |
---|---|
1 /* ========================================================= | |
2 * bootstrap-modal.js v2.1.1 | |
3 * http://twitter.github.com/bootstrap/javascript.html#modals | |
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 /* MODAL CLASS DEFINITION | |
27 * ====================== */ | |
28 | |
29 var Modal = function (element, options) { | |
30 this.options = options | |
31 this.$element = $(element) | |
32 .delegate('[data-dismiss="modal"]', 'click.dismiss.modal', $.proxy(this.hide, this)) | |
33 this.options.remote && this.$element.find('.modal-body').load(this.options.remote) | |
34 } | |
35 | |
36 Modal.prototype = { | |
37 | |
38 constructor: Modal | |
39 | |
40 , toggle: function () { | |
41 return this[!this.isShown ? 'show' : 'hide']() | |
42 } | |
43 | |
44 , show: function () { | |
45 var that = this | |
46 , e = $.Event('show') | |
47 | |
48 this.$element.trigger(e) | |
49 | |
50 if (this.isShown || e.isDefaultPrevented()) return | |
51 | |
52 $('body').addClass('modal-open') | |
53 | |
54 this.isShown = true | |
55 | |
56 this.escape() | |
57 | |
58 this.backdrop(function () { | |
59 var transition = $.support.transition && that.$element.hasClass('fade') | |
60 | |
61 if (!that.$element.parent().length) { | |
62 that.$element.appendTo(document.body) //don't move modals dom position | |
63 } | |
64 | |
65 that.$element | |
66 .show() | |
67 | |
68 if (transition) { | |
69 that.$element[0].offsetWidth // force reflow | |
70 } | |
71 | |
72 that.$element | |
73 .addClass('in') | |
74 .attr('aria-hidden', false) | |
75 .focus() | |
76 | |
77 that.enforceFocus() | |
78 | |
79 transition ? | |
80 that.$element.one($.support.transition.end, function () { that.$element.trigger('shown') }) : | |
81 that.$element.trigger('shown') | |
82 | |
83 }) | |
84 } | |
85 | |
86 , hide: function (e) { | |
87 e && e.preventDefault() | |
88 | |
89 var that = this | |
90 | |
91 e = $.Event('hide') | |
92 | |
93 this.$element.trigger(e) | |
94 | |
95 if (!this.isShown || e.isDefaultPrevented()) return | |
96 | |
97 this.isShown = false | |
98 | |
99 $('body').removeClass('modal-open') | |
100 | |
101 this.escape() | |
102 | |
103 $(document).off('focusin.modal') | |
104 | |
105 this.$element | |
106 .removeClass('in') | |
107 .attr('aria-hidden', true) | |
108 | |
109 $.support.transition && this.$element.hasClass('fade') ? | |
110 this.hideWithTransition() : | |
111 this.hideModal() | |
112 } | |
113 | |
114 , enforceFocus: function () { | |
115 var that = this | |
116 $(document).on('focusin.modal', function (e) { | |
117 if (that.$element[0] !== e.target && !that.$element.has(e.target).length) { | |
118 that.$element.focus() | |
119 } | |
120 }) | |
121 } | |
122 | |
123 , escape: function () { | |
124 var that = this | |
125 if (this.isShown && this.options.keyboard) { | |
126 this.$element.on('keyup.dismiss.modal', function ( e ) { | |
127 e.which == 27 && that.hide() | |
128 }) | |
129 } else if (!this.isShown) { | |
130 this.$element.off('keyup.dismiss.modal') | |
131 } | |
132 } | |
133 | |
134 , hideWithTransition: function () { | |
135 var that = this | |
136 , timeout = setTimeout(function () { | |
137 that.$element.off($.support.transition.end) | |
138 that.hideModal() | |
139 }, 500) | |
140 | |
141 this.$element.one($.support.transition.end, function () { | |
142 clearTimeout(timeout) | |
143 that.hideModal() | |
144 }) | |
145 } | |
146 | |
147 , hideModal: function (that) { | |
148 this.$element | |
149 .hide() | |
150 .trigger('hidden') | |
151 | |
152 this.backdrop() | |
153 } | |
154 | |
155 , removeBackdrop: function () { | |
156 this.$backdrop.remove() | |
157 this.$backdrop = null | |
158 } | |
159 | |
160 , backdrop: function (callback) { | |
161 var that = this | |
162 , animate = this.$element.hasClass('fade') ? 'fade' : '' | |
163 | |
164 if (this.isShown && this.options.backdrop) { | |
165 var doAnimate = $.support.transition && animate | |
166 | |
167 this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />') | |
168 .appendTo(document.body) | |
169 | |
170 if (this.options.backdrop != 'static') { | |
171 this.$backdrop.click($.proxy(this.hide, this)) | |
172 } | |
173 | |
174 if (doAnimate) this.$backdrop[0].offsetWidth // force reflow | |
175 | |
176 this.$backdrop.addClass('in') | |
177 | |
178 doAnimate ? | |
179 this.$backdrop.one($.support.transition.end, callback) : | |
180 callback() | |
181 | |
182 } else if (!this.isShown && this.$backdrop) { | |
183 this.$backdrop.removeClass('in') | |
184 | |
185 $.support.transition && this.$element.hasClass('fade')? | |
186 this.$backdrop.one($.support.transition.end, $.proxy(this.removeBackdrop, this)) : | |
187 this.removeBackdrop() | |
188 | |
189 } else if (callback) { | |
190 callback() | |
191 } | |
192 } | |
193 } | |
194 | |
195 | |
196 /* MODAL PLUGIN DEFINITION | |
197 * ======================= */ | |
198 | |
199 $.fn.modal = function (option) { | |
200 return this.each(function () { | |
201 var $this = $(this) | |
202 , data = $this.data('modal') | |
203 , options = $.extend({}, $.fn.modal.defaults, $this.data(), typeof option == 'object' && option) | |
204 if (!data) $this.data('modal', (data = new Modal(this, options))) | |
205 if (typeof option == 'string') data[option]() | |
206 else if (options.show) data.show() | |
207 }) | |
208 } | |
209 | |
210 $.fn.modal.defaults = { | |
211 backdrop: true | |
212 , keyboard: true | |
213 , show: true | |
214 } | |
215 | |
216 $.fn.modal.Constructor = Modal | |
217 | |
218 | |
219 /* MODAL DATA-API | |
220 * ============== */ | |
221 | |
222 $(function () { | |
223 $('body').on('click.modal.data-api', '[data-toggle="modal"]', function ( e ) { | |
224 var $this = $(this) | |
225 , href = $this.attr('href') | |
226 , $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) //strip for ie7 | |
227 , option = $target.data('modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data()) | |
228 | |
229 e.preventDefault() | |
230 | |
231 $target | |
232 .modal(option) | |
233 .one('hide', function () { | |
234 $this.focus() | |
235 }) | |
236 }) | |
237 }) | |
238 | |
239 }(window.jQuery); |