modal.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* ========================================================================
  2. * Bootstrap: modal.js v3.3.4
  3. * http://getbootstrap.com/javascript/#modals
  4. * ========================================================================
  5. * Copyright 2011-2015 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.$dialog = this.$element.find('.modal-dialog')
  17. this.$backdrop = null
  18. this.isShown = null
  19. this.originalBodyPad = null
  20. this.scrollbarWidth = 0
  21. this.ignoreBackdropClick = false
  22. if (this.options.remote) {
  23. this.$element
  24. .find('.modal-content')
  25. .load(this.options.remote, $.proxy(function () {
  26. this.$element.trigger('loaded.bs.modal')
  27. }, this))
  28. }
  29. }
  30. Modal.VERSION = '3.3.4'
  31. Modal.TRANSITION_DURATION = 300
  32. Modal.BACKDROP_TRANSITION_DURATION = 150
  33. Modal.DEFAULTS = {
  34. backdrop: true,
  35. keyboard: true,
  36. show: true
  37. }
  38. Modal.prototype.toggle = function (_relatedTarget) {
  39. return this.isShown ? this.hide() : this.show(_relatedTarget)
  40. }
  41. Modal.prototype.show = function (_relatedTarget) {
  42. var that = this
  43. var e = $.Event('show.bs.modal', { relatedTarget: _relatedTarget })
  44. this.$element.trigger(e)
  45. if (this.isShown || e.isDefaultPrevented()) return
  46. this.isShown = true
  47. this.checkScrollbar()
  48. this.setScrollbar()
  49. this.$body.addClass('modal-open')
  50. this.escape()
  51. this.resize()
  52. this.$element.on('click.dismiss.bs.modal', '[data-dismiss="modal"]', $.proxy(this.hide, this))
  53. this.$dialog.on('mousedown.dismiss.bs.modal', function () {
  54. that.$element.one('mouseup.dismiss.bs.modal', function (e) {
  55. if ($(e.target).is(that.$element)) that.ignoreBackdropClick = true
  56. })
  57. })
  58. this.backdrop(function () {
  59. var transition = $.support.transition && that.$element.hasClass('fade')
  60. if (!that.$element.parent().length) {
  61. that.$element.appendTo(that.$body) // don't move modals dom position
  62. }
  63. that.$element
  64. .show()
  65. .scrollTop(0)
  66. that.adjustDialog()
  67. if (transition) {
  68. that.$element[0].offsetWidth // force reflow
  69. }
  70. that.$element
  71. .addClass('in')
  72. .attr('aria-hidden', false)
  73. that.enforceFocus()
  74. var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget })
  75. transition ?
  76. that.$dialog // wait for modal to slide in
  77. .one('bsTransitionEnd', function () {
  78. that.$element.trigger('focus').trigger(e)
  79. })
  80. .emulateTransitionEnd(Modal.TRANSITION_DURATION) :
  81. that.$element.trigger('focus').trigger(e)
  82. })
  83. }
  84. Modal.prototype.hide = function (e) {
  85. if (e) e.preventDefault()
  86. e = $.Event('hide.bs.modal')
  87. this.$element.trigger(e)
  88. if (!this.isShown || e.isDefaultPrevented()) return
  89. this.isShown = false
  90. this.escape()
  91. this.resize()
  92. $(document).off('focusin.bs.modal')
  93. this.$element
  94. .removeClass('in')
  95. .attr('aria-hidden', true)
  96. .off('click.dismiss.bs.modal')
  97. .off('mouseup.dismiss.bs.modal')
  98. this.$dialog.off('mousedown.dismiss.bs.modal')
  99. $.support.transition && this.$element.hasClass('fade') ?
  100. this.$element
  101. .one('bsTransitionEnd', $.proxy(this.hideModal, this))
  102. .emulateTransitionEnd(Modal.TRANSITION_DURATION) :
  103. this.hideModal()
  104. }
  105. Modal.prototype.enforceFocus = function () {
  106. $(document)
  107. .off('focusin.bs.modal') // guard against infinite focus loop
  108. .on('focusin.bs.modal', $.proxy(function (e) {
  109. if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
  110. this.$element.trigger('focus')
  111. }
  112. }, this))
  113. }
  114. Modal.prototype.escape = function () {
  115. if (this.isShown && this.options.keyboard) {
  116. this.$element.on('keydown.dismiss.bs.modal', $.proxy(function (e) {
  117. e.which == 27 && this.hide()
  118. }, this))
  119. } else if (!this.isShown) {
  120. this.$element.off('keydown.dismiss.bs.modal')
  121. }
  122. }
  123. Modal.prototype.resize = function () {
  124. if (this.isShown) {
  125. $(window).on('resize.bs.modal', $.proxy(this.handleUpdate, this))
  126. } else {
  127. $(window).off('resize.bs.modal')
  128. }
  129. }
  130. Modal.prototype.hideModal = function () {
  131. var that = this
  132. this.$element.hide()
  133. this.backdrop(function () {
  134. that.$body.removeClass('modal-open')
  135. that.resetAdjustments()
  136. that.resetScrollbar()
  137. that.$element.trigger('hidden.bs.modal')
  138. })
  139. }
  140. Modal.prototype.removeBackdrop = function () {
  141. this.$backdrop && this.$backdrop.remove()
  142. this.$backdrop = null
  143. }
  144. Modal.prototype.backdrop = function (callback) {
  145. var that = this
  146. var animate = this.$element.hasClass('fade') ? 'fade' : ''
  147. if (this.isShown && this.options.backdrop) {
  148. var doAnimate = $.support.transition && animate
  149. this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
  150. .appendTo(this.$body)
  151. this.$element.on('click.dismiss.bs.modal', $.proxy(function (e) {
  152. if (this.ignoreBackdropClick) {
  153. this.ignoreBackdropClick = false
  154. return
  155. }
  156. if (e.target !== e.currentTarget) return
  157. this.options.backdrop == 'static'
  158. ? this.$element[0].focus()
  159. : this.hide()
  160. }, this))
  161. if (doAnimate) this.$backdrop[0].offsetWidth // force reflow
  162. this.$backdrop.addClass('in')
  163. if (!callback) return
  164. doAnimate ?
  165. this.$backdrop
  166. .one('bsTransitionEnd', callback)
  167. .emulateTransitionEnd(Modal.BACKDROP_TRANSITION_DURATION) :
  168. callback()
  169. } else if (!this.isShown && this.$backdrop) {
  170. this.$backdrop.removeClass('in')
  171. var callbackRemove = function () {
  172. that.removeBackdrop()
  173. callback && callback()
  174. }
  175. $.support.transition && this.$element.hasClass('fade') ?
  176. this.$backdrop
  177. .one('bsTransitionEnd', callbackRemove)
  178. .emulateTransitionEnd(Modal.BACKDROP_TRANSITION_DURATION) :
  179. callbackRemove()
  180. } else if (callback) {
  181. callback()
  182. }
  183. }
  184. // these following methods are used to handle overflowing modals
  185. Modal.prototype.handleUpdate = function () {
  186. this.adjustDialog()
  187. }
  188. Modal.prototype.adjustDialog = function () {
  189. var modalIsOverflowing = this.$element[0].scrollHeight > document.documentElement.clientHeight
  190. this.$element.css({
  191. paddingLeft: !this.bodyIsOverflowing && modalIsOverflowing ? this.scrollbarWidth : '',
  192. paddingRight: this.bodyIsOverflowing && !modalIsOverflowing ? this.scrollbarWidth : ''
  193. })
  194. }
  195. Modal.prototype.resetAdjustments = function () {
  196. this.$element.css({
  197. paddingLeft: '',
  198. paddingRight: ''
  199. })
  200. }
  201. Modal.prototype.checkScrollbar = function () {
  202. var fullWindowWidth = window.innerWidth
  203. if (!fullWindowWidth) { // workaround for missing window.innerWidth in IE8
  204. var documentElementRect = document.documentElement.getBoundingClientRect()
  205. fullWindowWidth = documentElementRect.right - Math.abs(documentElementRect.left)
  206. }
  207. this.bodyIsOverflowing = document.body.clientWidth < fullWindowWidth
  208. this.scrollbarWidth = this.measureScrollbar()
  209. }
  210. Modal.prototype.setScrollbar = function () {
  211. var bodyPad = parseInt((this.$body.css('padding-right') || 0), 10)
  212. this.originalBodyPad = document.body.style.paddingRight || ''
  213. if (this.bodyIsOverflowing) this.$body.css('padding-right', bodyPad + this.scrollbarWidth)
  214. }
  215. Modal.prototype.resetScrollbar = function () {
  216. this.$body.css('padding-right', this.originalBodyPad)
  217. }
  218. Modal.prototype.measureScrollbar = function () { // thx walsh
  219. var scrollDiv = document.createElement('div')
  220. scrollDiv.className = 'modal-scrollbar-measure'
  221. this.$body.append(scrollDiv)
  222. var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth
  223. this.$body[0].removeChild(scrollDiv)
  224. return scrollbarWidth
  225. }
  226. // MODAL PLUGIN DEFINITION
  227. // =======================
  228. function Plugin(option, _relatedTarget) {
  229. return this.each(function () {
  230. var $this = $(this)
  231. var data = $this.data('bs.modal')
  232. var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option)
  233. if (!data) $this.data('bs.modal', (data = new Modal(this, options)))
  234. if (typeof option == 'string') data[option](_relatedTarget)
  235. else if (options.show) data.show(_relatedTarget)
  236. })
  237. }
  238. var old = $.fn.modal
  239. $.fn.modal = Plugin
  240. $.fn.modal.Constructor = Modal
  241. // MODAL NO CONFLICT
  242. // =================
  243. $.fn.modal.noConflict = function () {
  244. $.fn.modal = old
  245. return this
  246. }
  247. // MODAL DATA-API
  248. // ==============
  249. $(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
  250. var $this = $(this)
  251. var href = $this.attr('href')
  252. var $target = $($this.attr('data-target') || (href && href.replace(/.*(?=#[^\s]+$)/, ''))) // strip for ie7
  253. var option = $target.data('bs.modal') ? 'toggle' : $.extend({ remote: !/#/.test(href) && href }, $target.data(), $this.data())
  254. if ($this.is('a')) e.preventDefault()
  255. $target.one('show.bs.modal', function (showEvent) {
  256. if (showEvent.isDefaultPrevented()) return // only register focus restorer if modal will actually get shown
  257. $target.one('hidden.bs.modal', function () {
  258. $this.is(':visible') && $this.trigger('focus')
  259. })
  260. })
  261. Plugin.call($target, option, this)
  262. })
  263. }(jQuery);