chat.js 7.4 KB

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