You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
9.8 KiB

3 years ago
  1. /* ========================================================================
  2. * Bootstrap: modal.js v3.3.7
  3. * http://getbootstrap.com/javascript/#modals
  4. * ========================================================================
  5. * Copyright 2011-2016 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.7'
  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.addClass('in')
  71. that.enforceFocus()
  72. var e = $.Event('shown.bs.modal', { relatedTarget: _relatedTarget })
  73. transition ?
  74. that.$dialog // wait for modal to slide in
  75. .one('bsTransitionEnd', function () {
  76. that.$element.trigger('focus').trigger(e)
  77. })
  78. .emulateTransitionEnd(Modal.TRANSITION_DURATION) :
  79. that.$element.trigger('focus').trigger(e)
  80. })
  81. }
  82. Modal.prototype.hide = function (e) {
  83. if (e) e.preventDefault()
  84. e = $.Event('hide.bs.modal')
  85. this.$element.trigger(e)
  86. if (!this.isShown || e.isDefaultPrevented()) return
  87. this.isShown = false
  88. this.escape()
  89. this.resize()
  90. $(document).off('focusin.bs.modal')
  91. this.$element
  92. .removeClass('in')
  93. .off('click.dismiss.bs.modal')
  94. .off('mouseup.dismiss.bs.modal')
  95. this.$dialog.off('mousedown.dismiss.bs.modal')
  96. $.support.transition && this.$element.hasClass('fade') ?
  97. this.$element
  98. .one('bsTransitionEnd', $.proxy(this.hideModal, this))
  99. .emulateTransitionEnd(Modal.TRANSITION_DURATION) :
  100. this.hideModal()
  101. }
  102. Modal.prototype.enforceFocus = function () {
  103. $(document)
  104. .off('focusin.bs.modal') // guard against infinite focus loop
  105. .on('focusin.bs.modal', $.proxy(function (e) {
  106. if (document !== e.target &&
  107. this.$element[0] !== e.target &&
  108. !this.$element.has(e.target).length) {
  109. this.$element.trigger('focus')
  110. }
  111. }, this))
  112. }
  113. Modal.prototype.escape = function () {
  114. if (this.isShown && this.options.keyboard) {
  115. this.$element.on('keydown.dismiss.bs.modal', $.proxy(function (e) {
  116. e.which == 27 && this.hide()
  117. }, this))
  118. } else if (!this.isShown) {
  119. this.$element.off('keydown.dismiss.bs.modal')
  120. }
  121. }
  122. Modal.prototype.resize = function () {
  123. if (this.isShown) {
  124. $(window).on('resize.bs.modal', $.proxy(this.handleUpdate, this))
  125. } else {
  126. $(window).off('resize.bs.modal')
  127. }
  128. }
  129. Modal.prototype.hideModal = function () {
  130. var that = this
  131. this.$element.hide()
  132. this.backdrop(function () {
  133. that.$body.removeClass('modal-open')
  134. that.resetAdjustments()
  135. that.resetScrollbar()
  136. that.$element.trigger('hidden.bs.modal')
  137. })
  138. }
  139. Modal.prototype.removeBackdrop = function () {
  140. this.$backdrop && this.$backdrop.remove()
  141. this.$backdrop = null
  142. }
  143. Modal.prototype.backdrop = function (callback) {
  144. var that = this
  145. var animate = this.$element.hasClass('fade') ? 'fade' : ''
  146. if (this.isShown && this.options.backdrop) {
  147. var doAnimate = $.support.transition && animate
  148. this.$backdrop = $(document.createElement('div'))
  149. .addClass('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);