Layout von Websiten mit Bootstrap und Foundation
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.

modal.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /**
  2. * --------------------------------------------------------------------------
  3. * Bootstrap (v4.5.0): modal.js
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  5. * --------------------------------------------------------------------------
  6. */
  7. import $ from 'jquery'
  8. import Util from './util'
  9. /**
  10. * ------------------------------------------------------------------------
  11. * Constants
  12. * ------------------------------------------------------------------------
  13. */
  14. const NAME = 'modal'
  15. const VERSION = '4.5.0'
  16. const DATA_KEY = 'bs.modal'
  17. const EVENT_KEY = `.${DATA_KEY}`
  18. const DATA_API_KEY = '.data-api'
  19. const JQUERY_NO_CONFLICT = $.fn[NAME]
  20. const ESCAPE_KEYCODE = 27 // KeyboardEvent.which value for Escape (Esc) key
  21. const Default = {
  22. backdrop : true,
  23. keyboard : true,
  24. focus : true,
  25. show : true
  26. }
  27. const DefaultType = {
  28. backdrop : '(boolean|string)',
  29. keyboard : 'boolean',
  30. focus : 'boolean',
  31. show : 'boolean'
  32. }
  33. const EVENT_HIDE = `hide${EVENT_KEY}`
  34. const EVENT_HIDE_PREVENTED = `hidePrevented${EVENT_KEY}`
  35. const EVENT_HIDDEN = `hidden${EVENT_KEY}`
  36. const EVENT_SHOW = `show${EVENT_KEY}`
  37. const EVENT_SHOWN = `shown${EVENT_KEY}`
  38. const EVENT_FOCUSIN = `focusin${EVENT_KEY}`
  39. const EVENT_RESIZE = `resize${EVENT_KEY}`
  40. const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`
  41. const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`
  42. const EVENT_MOUSEUP_DISMISS = `mouseup.dismiss${EVENT_KEY}`
  43. const EVENT_MOUSEDOWN_DISMISS = `mousedown.dismiss${EVENT_KEY}`
  44. const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
  45. const CLASS_NAME_SCROLLABLE = 'modal-dialog-scrollable'
  46. const CLASS_NAME_SCROLLBAR_MEASURER = 'modal-scrollbar-measure'
  47. const CLASS_NAME_BACKDROP = 'modal-backdrop'
  48. const CLASS_NAME_OPEN = 'modal-open'
  49. const CLASS_NAME_FADE = 'fade'
  50. const CLASS_NAME_SHOW = 'show'
  51. const CLASS_NAME_STATIC = 'modal-static'
  52. const SELECTOR_DIALOG = '.modal-dialog'
  53. const SELECTOR_MODAL_BODY = '.modal-body'
  54. const SELECTOR_DATA_TOGGLE = '[data-toggle="modal"]'
  55. const SELECTOR_DATA_DISMISS = '[data-dismiss="modal"]'
  56. const SELECTOR_FIXED_CONTENT = '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top'
  57. const SELECTOR_STICKY_CONTENT = '.sticky-top'
  58. /**
  59. * ------------------------------------------------------------------------
  60. * Class Definition
  61. * ------------------------------------------------------------------------
  62. */
  63. class Modal {
  64. constructor(element, config) {
  65. this._config = this._getConfig(config)
  66. this._element = element
  67. this._dialog = element.querySelector(SELECTOR_DIALOG)
  68. this._backdrop = null
  69. this._isShown = false
  70. this._isBodyOverflowing = false
  71. this._ignoreBackdropClick = false
  72. this._isTransitioning = false
  73. this._scrollbarWidth = 0
  74. }
  75. // Getters
  76. static get VERSION() {
  77. return VERSION
  78. }
  79. static get Default() {
  80. return Default
  81. }
  82. // Public
  83. toggle(relatedTarget) {
  84. return this._isShown ? this.hide() : this.show(relatedTarget)
  85. }
  86. show(relatedTarget) {
  87. if (this._isShown || this._isTransitioning) {
  88. return
  89. }
  90. if ($(this._element).hasClass(CLASS_NAME_FADE)) {
  91. this._isTransitioning = true
  92. }
  93. const showEvent = $.Event(EVENT_SHOW, {
  94. relatedTarget
  95. })
  96. $(this._element).trigger(showEvent)
  97. if (this._isShown || showEvent.isDefaultPrevented()) {
  98. return
  99. }
  100. this._isShown = true
  101. this._checkScrollbar()
  102. this._setScrollbar()
  103. this._adjustDialog()
  104. this._setEscapeEvent()
  105. this._setResizeEvent()
  106. $(this._element).on(
  107. EVENT_CLICK_DISMISS,
  108. SELECTOR_DATA_DISMISS,
  109. (event) => this.hide(event)
  110. )
  111. $(this._dialog).on(EVENT_MOUSEDOWN_DISMISS, () => {
  112. $(this._element).one(EVENT_MOUSEUP_DISMISS, (event) => {
  113. if ($(event.target).is(this._element)) {
  114. this._ignoreBackdropClick = true
  115. }
  116. })
  117. })
  118. this._showBackdrop(() => this._showElement(relatedTarget))
  119. }
  120. hide(event) {
  121. if (event) {
  122. event.preventDefault()
  123. }
  124. if (!this._isShown || this._isTransitioning) {
  125. return
  126. }
  127. const hideEvent = $.Event(EVENT_HIDE)
  128. $(this._element).trigger(hideEvent)
  129. if (!this._isShown || hideEvent.isDefaultPrevented()) {
  130. return
  131. }
  132. this._isShown = false
  133. const transition = $(this._element).hasClass(CLASS_NAME_FADE)
  134. if (transition) {
  135. this._isTransitioning = true
  136. }
  137. this._setEscapeEvent()
  138. this._setResizeEvent()
  139. $(document).off(EVENT_FOCUSIN)
  140. $(this._element).removeClass(CLASS_NAME_SHOW)
  141. $(this._element).off(EVENT_CLICK_DISMISS)
  142. $(this._dialog).off(EVENT_MOUSEDOWN_DISMISS)
  143. if (transition) {
  144. const transitionDuration = Util.getTransitionDurationFromElement(this._element)
  145. $(this._element)
  146. .one(Util.TRANSITION_END, (event) => this._hideModal(event))
  147. .emulateTransitionEnd(transitionDuration)
  148. } else {
  149. this._hideModal()
  150. }
  151. }
  152. dispose() {
  153. [window, this._element, this._dialog]
  154. .forEach((htmlElement) => $(htmlElement).off(EVENT_KEY))
  155. /**
  156. * `document` has 2 events `EVENT_FOCUSIN` and `EVENT_CLICK_DATA_API`
  157. * Do not move `document` in `htmlElements` array
  158. * It will remove `EVENT_CLICK_DATA_API` event that should remain
  159. */
  160. $(document).off(EVENT_FOCUSIN)
  161. $.removeData(this._element, DATA_KEY)
  162. this._config = null
  163. this._element = null
  164. this._dialog = null
  165. this._backdrop = null
  166. this._isShown = null
  167. this._isBodyOverflowing = null
  168. this._ignoreBackdropClick = null
  169. this._isTransitioning = null
  170. this._scrollbarWidth = null
  171. }
  172. handleUpdate() {
  173. this._adjustDialog()
  174. }
  175. // Private
  176. _getConfig(config) {
  177. config = {
  178. ...Default,
  179. ...config
  180. }
  181. Util.typeCheckConfig(NAME, config, DefaultType)
  182. return config
  183. }
  184. _triggerBackdropTransition() {
  185. if (this._config.backdrop === 'static') {
  186. const hideEventPrevented = $.Event(EVENT_HIDE_PREVENTED)
  187. $(this._element).trigger(hideEventPrevented)
  188. if (hideEventPrevented.defaultPrevented) {
  189. return
  190. }
  191. this._element.classList.add(CLASS_NAME_STATIC)
  192. const modalTransitionDuration = Util.getTransitionDurationFromElement(this._element)
  193. $(this._element).one(Util.TRANSITION_END, () => {
  194. this._element.classList.remove(CLASS_NAME_STATIC)
  195. })
  196. .emulateTransitionEnd(modalTransitionDuration)
  197. this._element.focus()
  198. } else {
  199. this.hide()
  200. }
  201. }
  202. _showElement(relatedTarget) {
  203. const transition = $(this._element).hasClass(CLASS_NAME_FADE)
  204. const modalBody = this._dialog ? this._dialog.querySelector(SELECTOR_MODAL_BODY) : null
  205. if (!this._element.parentNode ||
  206. this._element.parentNode.nodeType !== Node.ELEMENT_NODE) {
  207. // Don't move modal's DOM position
  208. document.body.appendChild(this._element)
  209. }
  210. this._element.style.display = 'block'
  211. this._element.removeAttribute('aria-hidden')
  212. this._element.setAttribute('aria-modal', true)
  213. if ($(this._dialog).hasClass(CLASS_NAME_SCROLLABLE) && modalBody) {
  214. modalBody.scrollTop = 0
  215. } else {
  216. this._element.scrollTop = 0
  217. }
  218. if (transition) {
  219. Util.reflow(this._element)
  220. }
  221. $(this._element).addClass(CLASS_NAME_SHOW)
  222. if (this._config.focus) {
  223. this._enforceFocus()
  224. }
  225. const shownEvent = $.Event(EVENT_SHOWN, {
  226. relatedTarget
  227. })
  228. const transitionComplete = () => {
  229. if (this._config.focus) {
  230. this._element.focus()
  231. }
  232. this._isTransitioning = false
  233. $(this._element).trigger(shownEvent)
  234. }
  235. if (transition) {
  236. const transitionDuration = Util.getTransitionDurationFromElement(this._dialog)
  237. $(this._dialog)
  238. .one(Util.TRANSITION_END, transitionComplete)
  239. .emulateTransitionEnd(transitionDuration)
  240. } else {
  241. transitionComplete()
  242. }
  243. }
  244. _enforceFocus() {
  245. $(document)
  246. .off(EVENT_FOCUSIN) // Guard against infinite focus loop
  247. .on(EVENT_FOCUSIN, (event) => {
  248. if (document !== event.target &&
  249. this._element !== event.target &&
  250. $(this._element).has(event.target).length === 0) {
  251. this._element.focus()
  252. }
  253. })
  254. }
  255. _setEscapeEvent() {
  256. if (this._isShown) {
  257. $(this._element).on(EVENT_KEYDOWN_DISMISS, (event) => {
  258. if (this._config.keyboard && event.which === ESCAPE_KEYCODE) {
  259. event.preventDefault()
  260. this.hide()
  261. } else if (!this._config.keyboard && event.which === ESCAPE_KEYCODE) {
  262. this._triggerBackdropTransition()
  263. }
  264. })
  265. } else if (!this._isShown) {
  266. $(this._element).off(EVENT_KEYDOWN_DISMISS)
  267. }
  268. }
  269. _setResizeEvent() {
  270. if (this._isShown) {
  271. $(window).on(EVENT_RESIZE, (event) => this.handleUpdate(event))
  272. } else {
  273. $(window).off(EVENT_RESIZE)
  274. }
  275. }
  276. _hideModal() {
  277. this._element.style.display = 'none'
  278. this._element.setAttribute('aria-hidden', true)
  279. this._element.removeAttribute('aria-modal')
  280. this._isTransitioning = false
  281. this._showBackdrop(() => {
  282. $(document.body).removeClass(CLASS_NAME_OPEN)
  283. this._resetAdjustments()
  284. this._resetScrollbar()
  285. $(this._element).trigger(EVENT_HIDDEN)
  286. })
  287. }
  288. _removeBackdrop() {
  289. if (this._backdrop) {
  290. $(this._backdrop).remove()
  291. this._backdrop = null
  292. }
  293. }
  294. _showBackdrop(callback) {
  295. const animate = $(this._element).hasClass(CLASS_NAME_FADE)
  296. ? CLASS_NAME_FADE : ''
  297. if (this._isShown && this._config.backdrop) {
  298. this._backdrop = document.createElement('div')
  299. this._backdrop.className = CLASS_NAME_BACKDROP
  300. if (animate) {
  301. this._backdrop.classList.add(animate)
  302. }
  303. $(this._backdrop).appendTo(document.body)
  304. $(this._element).on(EVENT_CLICK_DISMISS, (event) => {
  305. if (this._ignoreBackdropClick) {
  306. this._ignoreBackdropClick = false
  307. return
  308. }
  309. if (event.target !== event.currentTarget) {
  310. return
  311. }
  312. this._triggerBackdropTransition()
  313. })
  314. if (animate) {
  315. Util.reflow(this._backdrop)
  316. }
  317. $(this._backdrop).addClass(CLASS_NAME_SHOW)
  318. if (!callback) {
  319. return
  320. }
  321. if (!animate) {
  322. callback()
  323. return
  324. }
  325. const backdropTransitionDuration = Util.getTransitionDurationFromElement(this._backdrop)
  326. $(this._backdrop)
  327. .one(Util.TRANSITION_END, callback)
  328. .emulateTransitionEnd(backdropTransitionDuration)
  329. } else if (!this._isShown && this._backdrop) {
  330. $(this._backdrop).removeClass(CLASS_NAME_SHOW)
  331. const callbackRemove = () => {
  332. this._removeBackdrop()
  333. if (callback) {
  334. callback()
  335. }
  336. }
  337. if ($(this._element).hasClass(CLASS_NAME_FADE)) {
  338. const backdropTransitionDuration = Util.getTransitionDurationFromElement(this._backdrop)
  339. $(this._backdrop)
  340. .one(Util.TRANSITION_END, callbackRemove)
  341. .emulateTransitionEnd(backdropTransitionDuration)
  342. } else {
  343. callbackRemove()
  344. }
  345. } else if (callback) {
  346. callback()
  347. }
  348. }
  349. // ----------------------------------------------------------------------
  350. // the following methods are used to handle overflowing modals
  351. // todo (fat): these should probably be refactored out of modal.js
  352. // ----------------------------------------------------------------------
  353. _adjustDialog() {
  354. const isModalOverflowing =
  355. this._element.scrollHeight > document.documentElement.clientHeight
  356. if (!this._isBodyOverflowing && isModalOverflowing) {
  357. this._element.style.paddingLeft = `${this._scrollbarWidth}px`
  358. }
  359. if (this._isBodyOverflowing && !isModalOverflowing) {
  360. this._element.style.paddingRight = `${this._scrollbarWidth}px`
  361. }
  362. }
  363. _resetAdjustments() {
  364. this._element.style.paddingLeft = ''
  365. this._element.style.paddingRight = ''
  366. }
  367. _checkScrollbar() {
  368. const rect = document.body.getBoundingClientRect()
  369. this._isBodyOverflowing = Math.round(rect.left + rect.right) < window.innerWidth
  370. this._scrollbarWidth = this._getScrollbarWidth()
  371. }
  372. _setScrollbar() {
  373. if (this._isBodyOverflowing) {
  374. // Note: DOMNode.style.paddingRight returns the actual value or '' if not set
  375. // while $(DOMNode).css('padding-right') returns the calculated value or 0 if not set
  376. const fixedContent = [].slice.call(document.querySelectorAll(SELECTOR_FIXED_CONTENT))
  377. const stickyContent = [].slice.call(document.querySelectorAll(SELECTOR_STICKY_CONTENT))
  378. // Adjust fixed content padding
  379. $(fixedContent).each((index, element) => {
  380. const actualPadding = element.style.paddingRight
  381. const calculatedPadding = $(element).css('padding-right')
  382. $(element)
  383. .data('padding-right', actualPadding)
  384. .css('padding-right', `${parseFloat(calculatedPadding) + this._scrollbarWidth}px`)
  385. })
  386. // Adjust sticky content margin
  387. $(stickyContent).each((index, element) => {
  388. const actualMargin = element.style.marginRight
  389. const calculatedMargin = $(element).css('margin-right')
  390. $(element)
  391. .data('margin-right', actualMargin)
  392. .css('margin-right', `${parseFloat(calculatedMargin) - this._scrollbarWidth}px`)
  393. })
  394. // Adjust body padding
  395. const actualPadding = document.body.style.paddingRight
  396. const calculatedPadding = $(document.body).css('padding-right')
  397. $(document.body)
  398. .data('padding-right', actualPadding)
  399. .css('padding-right', `${parseFloat(calculatedPadding) + this._scrollbarWidth}px`)
  400. }
  401. $(document.body).addClass(CLASS_NAME_OPEN)
  402. }
  403. _resetScrollbar() {
  404. // Restore fixed content padding
  405. const fixedContent = [].slice.call(document.querySelectorAll(SELECTOR_FIXED_CONTENT))
  406. $(fixedContent).each((index, element) => {
  407. const padding = $(element).data('padding-right')
  408. $(element).removeData('padding-right')
  409. element.style.paddingRight = padding ? padding : ''
  410. })
  411. // Restore sticky content
  412. const elements = [].slice.call(document.querySelectorAll(`${SELECTOR_STICKY_CONTENT}`))
  413. $(elements).each((index, element) => {
  414. const margin = $(element).data('margin-right')
  415. if (typeof margin !== 'undefined') {
  416. $(element).css('margin-right', margin).removeData('margin-right')
  417. }
  418. })
  419. // Restore body padding
  420. const padding = $(document.body).data('padding-right')
  421. $(document.body).removeData('padding-right')
  422. document.body.style.paddingRight = padding ? padding : ''
  423. }
  424. _getScrollbarWidth() { // thx d.walsh
  425. const scrollDiv = document.createElement('div')
  426. scrollDiv.className = CLASS_NAME_SCROLLBAR_MEASURER
  427. document.body.appendChild(scrollDiv)
  428. const scrollbarWidth = scrollDiv.getBoundingClientRect().width - scrollDiv.clientWidth
  429. document.body.removeChild(scrollDiv)
  430. return scrollbarWidth
  431. }
  432. // Static
  433. static _jQueryInterface(config, relatedTarget) {
  434. return this.each(function () {
  435. let data = $(this).data(DATA_KEY)
  436. const _config = {
  437. ...Default,
  438. ...$(this).data(),
  439. ...typeof config === 'object' && config ? config : {}
  440. }
  441. if (!data) {
  442. data = new Modal(this, _config)
  443. $(this).data(DATA_KEY, data)
  444. }
  445. if (typeof config === 'string') {
  446. if (typeof data[config] === 'undefined') {
  447. throw new TypeError(`No method named "${config}"`)
  448. }
  449. data[config](relatedTarget)
  450. } else if (_config.show) {
  451. data.show(relatedTarget)
  452. }
  453. })
  454. }
  455. }
  456. /**
  457. * ------------------------------------------------------------------------
  458. * Data Api implementation
  459. * ------------------------------------------------------------------------
  460. */
  461. $(document).on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
  462. let target
  463. const selector = Util.getSelectorFromElement(this)
  464. if (selector) {
  465. target = document.querySelector(selector)
  466. }
  467. const config = $(target).data(DATA_KEY)
  468. ? 'toggle' : {
  469. ...$(target).data(),
  470. ...$(this).data()
  471. }
  472. if (this.tagName === 'A' || this.tagName === 'AREA') {
  473. event.preventDefault()
  474. }
  475. const $target = $(target).one(EVENT_SHOW, (showEvent) => {
  476. if (showEvent.isDefaultPrevented()) {
  477. // Only register focus restorer if modal will actually get shown
  478. return
  479. }
  480. $target.one(EVENT_HIDDEN, () => {
  481. if ($(this).is(':visible')) {
  482. this.focus()
  483. }
  484. })
  485. })
  486. Modal._jQueryInterface.call($(target), config, this)
  487. })
  488. /**
  489. * ------------------------------------------------------------------------
  490. * jQuery
  491. * ------------------------------------------------------------------------
  492. */
  493. $.fn[NAME] = Modal._jQueryInterface
  494. $.fn[NAME].Constructor = Modal
  495. $.fn[NAME].noConflict = () => {
  496. $.fn[NAME] = JQUERY_NO_CONFLICT
  497. return Modal._jQueryInterface
  498. }
  499. export default Modal