modal.js 10 KB

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