bootstrap-modal.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* =========================================================
  2. * bootstrap-modal.js v2.0.4
  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. !function ($) {
  20. "use strict"; // jshint ;_;
  21. /* MODAL CLASS DEFINITION
  22. * ====================== */
  23. var Modal = function (content, options) {
  24. this.options = options
  25. this.$element = $(content)
  26. .delegate('[data-dismiss="modal"]', 'click.dismiss.modal', $.proxy(this.hide, this))
  27. }
  28. Modal.prototype = {
  29. constructor: Modal
  30. , toggle: function () {
  31. return this[!this.isShown ? 'show' : 'hide']()
  32. }
  33. , show: function () {
  34. var that = this
  35. , e = $.Event('show')
  36. this.$element.trigger(e)
  37. if (this.isShown || e.isDefaultPrevented()) return
  38. $('body').addClass('modal-open')
  39. this.isShown = true
  40. escape.call(this)
  41. backdrop.call(this, function () {
  42. var transition = $.support.transition && that.$element.hasClass('fade')
  43. if (!that.$element.parent().length) {
  44. that.$element.appendTo(document.body) //don't move modals dom position
  45. }
  46. that.$element
  47. .show()
  48. if (transition) {
  49. that.$element[0].offsetWidth // force reflow
  50. }
  51. that.$element.addClass('in')
  52. transition ?
  53. that.$element.one($.support.transition.end, function () { that.$element.trigger('shown') }) :
  54. that.$element.trigger('shown')
  55. })
  56. }
  57. , hide: function (e) {
  58. e && e.preventDefault()
  59. var that = this
  60. e = $.Event('hide')
  61. this.$element.trigger(e)
  62. if (!this.isShown || e.isDefaultPrevented()) return
  63. this.isShown = false
  64. $('body').removeClass('modal-open')
  65. escape.call(this)
  66. this.$element.removeClass('in')
  67. $.support.transition && this.$element.hasClass('fade') ?
  68. hideWithTransition.call(this) :
  69. hideModal.call(this)
  70. }
  71. }
  72. /* MODAL PRIVATE METHODS
  73. * ===================== */
  74. function hideWithTransition() {
  75. var that = this
  76. , timeout = setTimeout(function () {
  77. that.$element.off($.support.transition.end)
  78. hideModal.call(that)
  79. }, 500)
  80. this.$element.one($.support.transition.end, function () {
  81. clearTimeout(timeout)
  82. hideModal.call(that)
  83. })
  84. }
  85. function hideModal(that) {
  86. this.$element
  87. .hide()
  88. .trigger('hidden')
  89. backdrop.call(this)
  90. }
  91. function backdrop(callback) {
  92. var that = this
  93. , animate = this.$element.hasClass('fade') ? 'fade' : ''
  94. if (this.isShown && this.options.backdrop) {
  95. var doAnimate = $.support.transition && animate
  96. this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
  97. .appendTo(document.body)
  98. if (this.options.backdrop != 'static') {
  99. this.$backdrop.click($.proxy(this.hide, this))
  100. }
  101. if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
  102. this.$backdrop.addClass('in')
  103. doAnimate ?
  104. this.$backdrop.one($.support.transition.end, callback) :
  105. callback()
  106. } else if (!this.isShown && this.$backdrop) {
  107. this.$backdrop.removeClass('in')
  108. $.support.transition && this.$element.hasClass('fade')?
  109. this.$backdrop.one($.support.transition.end, $.proxy(removeBackdrop, this)) :
  110. removeBackdrop.call(this)
  111. } else if (callback) {
  112. callback()
  113. }
  114. }
  115. function removeBackdrop() {
  116. this.$backdrop.remove()
  117. this.$backdrop = null
  118. }
  119. function escape() {
  120. var that = this
  121. if (this.isShown && this.options.keyboard) {
  122. $(document).on('keyup.dismiss.modal', function ( e ) {
  123. e.which == 27 && that.hide()
  124. })
  125. } else if (!this.isShown) {
  126. $(document).off('keyup.dismiss.modal')
  127. }
  128. }
  129. /* MODAL PLUGIN DEFINITION
  130. * ======================= */
  131. $.fn.modal = function (option) {
  132. return this.each(function () {
  133. var $this = $(this)
  134. , data = $this.data('modal')
  135. , options = $.extend({}, $.fn.modal.defaults, $this.data(), typeof option == 'object' && option)
  136. if (!data) $this.data('modal', (data = new Modal(this, options)))
  137. if (typeof option == 'string') data[option]()
  138. else if (options.show) data.show()
  139. })
  140. }
  141. $.fn.modal.defaults = {
  142. backdrop: true
  143. , keyboard: true
  144. , show: true
  145. }
  146. $.fn.modal.Constructor = Modal
  147. /* MODAL DATA-API
  148. * ============== */
  149. $(function () {
  150. $('body').on('click.modal.data-api', '[data-toggle="modal"]', function ( e ) {
  151. var $this = $(this), href
  152. , $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
  153. , option = $target.data('modal') ? 'toggle' : $.extend({}, $target.data(), $this.data())
  154. e.preventDefault()
  155. $target.modal(option)
  156. })
  157. })
  158. }(window.jQuery);