modal.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* ========================================================================
  2. * Bootstrap: modal.js v3.2.0
  3. * http://getbootstrap.com/javascript/#modals
  4. * ========================================================================
  5. * Copyright 2011-2014 Twitter, Inc.
  6. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  7. * ======================================================================== */
  8. +function ($) {
  9. 'use strict';
  10. // MODAL CLASS DEFINITION
  11. // ======================
  12. var Modal = function (element, options) {
  13. this.options = options
  14. this.$body = $(document.body)
  15. this.$element = $(element)
  16. this.$backdrop =
  17. this.isShown = null
  18. this.scrollbarWidth = 0
  19. if (this.options.remote) {
  20. this.$element
  21. .find('.modal-content')
  22. .load(this.options.remote, $.proxy(function () {
  23. this.$element.trigger('loaded.bs.modal')
  24. }, this))
  25. }
  26. }
  27. Modal.VERSION = '3.2.0'
  28. Modal.DEFAULTS = {
  29. backdrop: true,
  30. keyboard: true,
  31. show: true
  32. }
  33. Modal.prototype.toggle = function (_relatedTarget) {
  34. return this.isShown ? this.hide() : this.show(_relatedTarget)
  35. }
  36. Modal.prototype.show = function (_relatedTarget) {
  37. var that = this
  38. var e = $.Event('show.bs.modal', { relatedTarget: _relatedTarget })
  39. this.$element.trigger(e)
  40. if (this.isShown || e.isDefaultPrevented()) return
  41. this.isShown = true
  42. this.checkScrollbar()
  43. this.$body.addClass('modal-open')
  44. this.setScrollbar()
  45. this.escape()
  46. this.$element.on('click.dismiss.bs.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))
  47. this.backdrop(function () {
  48. var transition = $.support.transition && that.$element.hasClass('fade')
  49. if (!that.$element.parent().length) {
  50. that.$element.appendTo(that.$body) // don't move modals dom position
  51. }
  52. that.$element
  53. .show()
  54. .scrollTop(0)
  55. if (transition) {
  56. that.$element[0].offsetWidth // force reflow
  57. }
  58. that.$element
  59. .addClass('in')
  60. .attr('aria-hidden', false)
  61. that.enforceFocus()
  62. var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget })
  63. transition ?
  64. that.$element.find('.modal-dialog') // wait for modal to slide in
  65. .one('bsTransitionEnd', function () {
  66. that.$element.trigger('focus').trigger(e)
  67. })
  68. .emulateTransitionEnd(300) :
  69. that.$element.trigger('focus').trigger(e)
  70. })
  71. }
  72. Modal.prototype.hide = function (e) {
  73. if (e) e.preventDefault()
  74. e = $.Event('hide.bs.modal')
  75. this.$element.trigger(e)
  76. if (!this.isShown || e.isDefaultPrevented()) return
  77. this.isShown = false
  78. this.$body.removeClass('modal-open')
  79. this.resetScrollbar()
  80. this.escape()
  81. $(document).off('focusin.bs.modal')
  82. this.$element
  83. .removeClass('in')
  84. .attr('aria-hidden', true)
  85. .off('click.dismiss.bs.modal')
  86. $.support.transition && this.$element.hasClass('fade') ?
  87. this.$element
  88. .one('bsTransitionEnd', $.proxy(this.hideModal, this))
  89. .emulateTransitionEnd(300) :
  90. this.hideModal()
  91. }
  92. Modal.prototype.enforceFocus = function () {
  93. $(document)
  94. .off('focusin.bs.modal') // guard against infinite focus loop
  95. .on('focusin.bs.modal', $.proxy(function (e) {
  96. if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
  97. this.$element.trigger('focus')
  98. }
  99. }, this))
  100. }
  101. Modal.prototype.escape = function () {
  102. if (this.isShown && this.options.keyboard) {
  103. this.$element.on('keyup.dismiss.bs.modal', $.proxy(function (e) {
  104. e.which == 27 && this.hide()
  105. }, this))
  106. } else if (!this.isShown) {
  107. this.$element.off('keyup.dismiss.bs.modal')
  108. }
  109. }
  110. Modal.prototype.hideModal = function () {
  111. var that = this
  112. this.$element.hide()
  113. this.backdrop(function () {
  114. that.$element.trigger('hidden.bs.modal')
  115. })
  116. }
  117. Modal.prototype.removeBackdrop = function () {
  118. this.$backdrop && this.$backdrop.remove()
  119. this.$backdrop = null
  120. }
  121. Modal.prototype.backdrop = function (callback) {
  122. var that = this
  123. var animate = this.$element.hasClass('fade') ? 'fade' : ''
  124. if (this.isShown && this.options.backdrop) {
  125. var doAnimate = $.support.transition && animate
  126. this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
  127. .appendTo(this.$body)
  128. this.$element.on('click.dismiss.bs.modal', $.proxy(function (e) {
  129. if (e.target !== e.currentTarget) return
  130. this.options.backdrop == 'static'
  131. ? this.$element[0].focus.call(this.$element[0])
  132. : this.hide.call(this)
  133. }, this))
  134. if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
  135. this.$backdrop.addClass('in')
  136. if (!callback) return
  137. doAnimate ?
  138. this.$backdrop
  139. .one('bsTransitionEnd', callback)
  140. .emulateTransitionEnd(150) :
  141. callback()
  142. } else if (!this.isShown && this.$backdrop) {
  143. this.$backdrop.removeClass('in')
  144. var callbackRemove = function () {
  145. that.removeBackdrop()
  146. callback && callback()
  147. }
  148. $.support.transition && this.$element.hasClass('fade') ?
  149. this.$backdrop
  150. .one('bsTransitionEnd', callbackRemove)
  151. .emulateTransitionEnd(150) :
  152. callbackRemove()
  153. } else if (callback) {
  154. callback()
  155. }
  156. }
  157. Modal.prototype.checkScrollbar = function () {
  158. if (document.body.clientWidth >= window.innerWidth) return
  159. this.scrollbarWidth = this.scrollbarWidth || this.measureScrollbar()
  160. }
  161. Modal.prototype.setScrollbar = function () {
  162. var bodyPad = parseInt((this.$body.css('padding-right') || 0), 10)
  163. if (this.scrollbarWidth) this.$body.css('padding-right', bodyPad + this.scrollbarWidth)
  164. }
  165. Modal.prototype.resetScrollbar = function () {
  166. this.$body.css('padding-right', '')
  167. }
  168. Modal.prototype.measureScrollbar = function () { // thx walsh
  169. var scrollDiv = document.createElement('div')
  170. scrollDiv.className = 'modal-scrollbar-measure'
  171. this.$body.append(scrollDiv)
  172. var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth
  173. this.$body[0].removeChild(scrollDiv)
  174. return scrollbarWidth
  175. }
  176. // MODAL PLUGIN DEFINITION
  177. // =======================
  178. function Plugin(option, _relatedTarget) {
  179. return this.each(function () {
  180. var $this = $(this)
  181. var data = $this.data('bs.modal')
  182. var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)
  183. if (!data) $this.data('bs.modal', (data = new Modal(this, options)))
  184. if (typeof option == 'string') data[option](_relatedTarget)
  185. else if (options.show) data.show(_relatedTarget)
  186. })
  187. }
  188. var old = $.fn.modal
  189. $.fn.modal = Plugin
  190. $.fn.modal.Constructor = Modal
  191. // MODAL NO CONFLICT
  192. // =================
  193. $.fn.modal.noConflict = function () {
  194. $.fn.modal = old
  195. return this
  196. }
  197. // MODAL DATA-API
  198. // ==============
  199. $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
  200. var $this = $(this)
  201. var href = $this.attr('href')
  202. var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) // strip for ie7
  203. var option = $target.data('bs.modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())
  204. if ($this.is('a')) e.preventDefault()
  205. $target.one('show.bs.modal', function (showEvent) {
  206. if (showEvent.isDefaultPrevented()) return // only register focus restorer if modal will actually get shown
  207. $target.one('hidden.bs.modal', function () {
  208. $this.is(':visible') && $this.trigger('focus')
  209. })
  210. })
  211. Plugin.call($target, option, this)
  212. })
  213. }(jQuery);