diff options
Diffstat (limited to 'vendor/twbs/bootstrap/js/src/modal.js')
-rw-r--r-- | vendor/twbs/bootstrap/js/src/modal.js | 209 |
1 files changed, 121 insertions, 88 deletions
diff --git a/vendor/twbs/bootstrap/js/src/modal.js b/vendor/twbs/bootstrap/js/src/modal.js index 99fe1bf2d..b67507b1e 100644 --- a/vendor/twbs/bootstrap/js/src/modal.js +++ b/vendor/twbs/bootstrap/js/src/modal.js @@ -1,7 +1,7 @@ /** * -------------------------------------------------------------------------- - * Bootstrap (v4.3.1): modal.js - * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) + * Bootstrap (v4.5.2): modal.js + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * -------------------------------------------------------------------------- */ @@ -15,7 +15,7 @@ import Util from './util' */ const NAME = 'modal' -const VERSION = '4.3.1' +const VERSION = '4.5.2' const DATA_KEY = 'bs.modal' const EVENT_KEY = `.${DATA_KEY}` const DATA_API_KEY = '.data-api' @@ -36,37 +36,33 @@ const DefaultType = { show : 'boolean' } -const Event = { - HIDE : `hide${EVENT_KEY}`, - HIDDEN : `hidden${EVENT_KEY}`, - SHOW : `show${EVENT_KEY}`, - SHOWN : `shown${EVENT_KEY}`, - FOCUSIN : `focusin${EVENT_KEY}`, - RESIZE : `resize${EVENT_KEY}`, - CLICK_DISMISS : `click.dismiss${EVENT_KEY}`, - KEYDOWN_DISMISS : `keydown.dismiss${EVENT_KEY}`, - MOUSEUP_DISMISS : `mouseup.dismiss${EVENT_KEY}`, - MOUSEDOWN_DISMISS : `mousedown.dismiss${EVENT_KEY}`, - CLICK_DATA_API : `click${EVENT_KEY}${DATA_API_KEY}` -} - -const ClassName = { - SCROLLABLE : 'modal-dialog-scrollable', - SCROLLBAR_MEASURER : 'modal-scrollbar-measure', - BACKDROP : 'modal-backdrop', - OPEN : 'modal-open', - FADE : 'fade', - SHOW : 'show' -} - -const Selector = { - DIALOG : '.modal-dialog', - MODAL_BODY : '.modal-body', - DATA_TOGGLE : '[data-toggle="modal"]', - DATA_DISMISS : '[data-dismiss="modal"]', - FIXED_CONTENT : '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top', - STICKY_CONTENT : '.sticky-top' -} +const EVENT_HIDE = `hide${EVENT_KEY}` +const EVENT_HIDE_PREVENTED = `hidePrevented${EVENT_KEY}` +const EVENT_HIDDEN = `hidden${EVENT_KEY}` +const EVENT_SHOW = `show${EVENT_KEY}` +const EVENT_SHOWN = `shown${EVENT_KEY}` +const EVENT_FOCUSIN = `focusin${EVENT_KEY}` +const EVENT_RESIZE = `resize${EVENT_KEY}` +const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}` +const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}` +const EVENT_MOUSEUP_DISMISS = `mouseup.dismiss${EVENT_KEY}` +const EVENT_MOUSEDOWN_DISMISS = `mousedown.dismiss${EVENT_KEY}` +const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}` + +const CLASS_NAME_SCROLLABLE = 'modal-dialog-scrollable' +const CLASS_NAME_SCROLLBAR_MEASURER = 'modal-scrollbar-measure' +const CLASS_NAME_BACKDROP = 'modal-backdrop' +const CLASS_NAME_OPEN = 'modal-open' +const CLASS_NAME_FADE = 'fade' +const CLASS_NAME_SHOW = 'show' +const CLASS_NAME_STATIC = 'modal-static' + +const SELECTOR_DIALOG = '.modal-dialog' +const SELECTOR_MODAL_BODY = '.modal-body' +const SELECTOR_DATA_TOGGLE = '[data-toggle="modal"]' +const SELECTOR_DATA_DISMISS = '[data-dismiss="modal"]' +const SELECTOR_FIXED_CONTENT = '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top' +const SELECTOR_STICKY_CONTENT = '.sticky-top' /** * ------------------------------------------------------------------------ @@ -78,7 +74,7 @@ class Modal { constructor(element, config) { this._config = this._getConfig(config) this._element = element - this._dialog = element.querySelector(Selector.DIALOG) + this._dialog = element.querySelector(SELECTOR_DIALOG) this._backdrop = null this._isShown = false this._isBodyOverflowing = false @@ -108,11 +104,11 @@ class Modal { return } - if ($(this._element).hasClass(ClassName.FADE)) { + if ($(this._element).hasClass(CLASS_NAME_FADE)) { this._isTransitioning = true } - const showEvent = $.Event(Event.SHOW, { + const showEvent = $.Event(EVENT_SHOW, { relatedTarget }) @@ -133,13 +129,13 @@ class Modal { this._setResizeEvent() $(this._element).on( - Event.CLICK_DISMISS, - Selector.DATA_DISMISS, + EVENT_CLICK_DISMISS, + SELECTOR_DATA_DISMISS, (event) => this.hide(event) ) - $(this._dialog).on(Event.MOUSEDOWN_DISMISS, () => { - $(this._element).one(Event.MOUSEUP_DISMISS, (event) => { + $(this._dialog).on(EVENT_MOUSEDOWN_DISMISS, () => { + $(this._element).one(EVENT_MOUSEUP_DISMISS, (event) => { if ($(event.target).is(this._element)) { this._ignoreBackdropClick = true } @@ -158,7 +154,7 @@ class Modal { return } - const hideEvent = $.Event(Event.HIDE) + const hideEvent = $.Event(EVENT_HIDE) $(this._element).trigger(hideEvent) @@ -167,7 +163,7 @@ class Modal { } this._isShown = false - const transition = $(this._element).hasClass(ClassName.FADE) + const transition = $(this._element).hasClass(CLASS_NAME_FADE) if (transition) { this._isTransitioning = true @@ -176,13 +172,12 @@ class Modal { this._setEscapeEvent() this._setResizeEvent() - $(document).off(Event.FOCUSIN) - - $(this._element).removeClass(ClassName.SHOW) + $(document).off(EVENT_FOCUSIN) - $(this._element).off(Event.CLICK_DISMISS) - $(this._dialog).off(Event.MOUSEDOWN_DISMISS) + $(this._element).removeClass(CLASS_NAME_SHOW) + $(this._element).off(EVENT_CLICK_DISMISS) + $(this._dialog).off(EVENT_MOUSEDOWN_DISMISS) if (transition) { const transitionDuration = Util.getTransitionDurationFromElement(this._element) @@ -200,11 +195,11 @@ class Modal { .forEach((htmlElement) => $(htmlElement).off(EVENT_KEY)) /** - * `document` has 2 events `Event.FOCUSIN` and `Event.CLICK_DATA_API` + * `document` has 2 events `EVENT_FOCUSIN` and `EVENT_CLICK_DATA_API` * Do not move `document` in `htmlElements` array - * It will remove `Event.CLICK_DATA_API` event that should remain + * It will remove `EVENT_CLICK_DATA_API` event that should remain */ - $(document).off(Event.FOCUSIN) + $(document).off(EVENT_FOCUSIN) $.removeData(this._element, DATA_KEY) @@ -234,8 +229,45 @@ class Modal { return config } + _triggerBackdropTransition() { + if (this._config.backdrop === 'static') { + const hideEventPrevented = $.Event(EVENT_HIDE_PREVENTED) + + $(this._element).trigger(hideEventPrevented) + if (hideEventPrevented.defaultPrevented) { + return + } + + const isModalOverflowing = this._element.scrollHeight > document.documentElement.clientHeight + + if (!isModalOverflowing) { + this._element.style.overflowY = 'hidden' + } + + this._element.classList.add(CLASS_NAME_STATIC) + + const modalTransitionDuration = Util.getTransitionDurationFromElement(this._dialog) + $(this._element).off(Util.TRANSITION_END) + + $(this._element).one(Util.TRANSITION_END, () => { + this._element.classList.remove(CLASS_NAME_STATIC) + if (!isModalOverflowing) { + $(this._element).one(Util.TRANSITION_END, () => { + this._element.style.overflowY = '' + }) + .emulateTransitionEnd(this._element, modalTransitionDuration) + } + }) + .emulateTransitionEnd(modalTransitionDuration) + this._element.focus() + } else { + this.hide() + } + } + _showElement(relatedTarget) { - const transition = $(this._element).hasClass(ClassName.FADE) + const transition = $(this._element).hasClass(CLASS_NAME_FADE) + const modalBody = this._dialog ? this._dialog.querySelector(SELECTOR_MODAL_BODY) : null if (!this._element.parentNode || this._element.parentNode.nodeType !== Node.ELEMENT_NODE) { @@ -246,9 +278,10 @@ class Modal { this._element.style.display = 'block' this._element.removeAttribute('aria-hidden') this._element.setAttribute('aria-modal', true) + this._element.setAttribute('role', 'dialog') - if ($(this._dialog).hasClass(ClassName.SCROLLABLE)) { - this._dialog.querySelector(Selector.MODAL_BODY).scrollTop = 0 + if ($(this._dialog).hasClass(CLASS_NAME_SCROLLABLE) && modalBody) { + modalBody.scrollTop = 0 } else { this._element.scrollTop = 0 } @@ -257,13 +290,13 @@ class Modal { Util.reflow(this._element) } - $(this._element).addClass(ClassName.SHOW) + $(this._element).addClass(CLASS_NAME_SHOW) if (this._config.focus) { this._enforceFocus() } - const shownEvent = $.Event(Event.SHOWN, { + const shownEvent = $.Event(EVENT_SHOWN, { relatedTarget }) @@ -288,8 +321,8 @@ class Modal { _enforceFocus() { $(document) - .off(Event.FOCUSIN) // Guard against infinite focus loop - .on(Event.FOCUSIN, (event) => { + .off(EVENT_FOCUSIN) // Guard against infinite focus loop + .on(EVENT_FOCUSIN, (event) => { if (document !== event.target && this._element !== event.target && $(this._element).has(event.target).length === 0) { @@ -299,23 +332,25 @@ class Modal { } _setEscapeEvent() { - if (this._isShown && this._config.keyboard) { - $(this._element).on(Event.KEYDOWN_DISMISS, (event) => { - if (event.which === ESCAPE_KEYCODE) { + if (this._isShown) { + $(this._element).on(EVENT_KEYDOWN_DISMISS, (event) => { + if (this._config.keyboard && event.which === ESCAPE_KEYCODE) { event.preventDefault() this.hide() + } else if (!this._config.keyboard && event.which === ESCAPE_KEYCODE) { + this._triggerBackdropTransition() } }) } else if (!this._isShown) { - $(this._element).off(Event.KEYDOWN_DISMISS) + $(this._element).off(EVENT_KEYDOWN_DISMISS) } } _setResizeEvent() { if (this._isShown) { - $(window).on(Event.RESIZE, (event) => this.handleUpdate(event)) + $(window).on(EVENT_RESIZE, (event) => this.handleUpdate(event)) } else { - $(window).off(Event.RESIZE) + $(window).off(EVENT_RESIZE) } } @@ -323,12 +358,13 @@ class Modal { this._element.style.display = 'none' this._element.setAttribute('aria-hidden', true) this._element.removeAttribute('aria-modal') + this._element.removeAttribute('role') this._isTransitioning = false this._showBackdrop(() => { - $(document.body).removeClass(ClassName.OPEN) + $(document.body).removeClass(CLASS_NAME_OPEN) this._resetAdjustments() this._resetScrollbar() - $(this._element).trigger(Event.HIDDEN) + $(this._element).trigger(EVENT_HIDDEN) }) } @@ -340,12 +376,12 @@ class Modal { } _showBackdrop(callback) { - const animate = $(this._element).hasClass(ClassName.FADE) - ? ClassName.FADE : '' + const animate = $(this._element).hasClass(CLASS_NAME_FADE) + ? CLASS_NAME_FADE : '' if (this._isShown && this._config.backdrop) { this._backdrop = document.createElement('div') - this._backdrop.className = ClassName.BACKDROP + this._backdrop.className = CLASS_NAME_BACKDROP if (animate) { this._backdrop.classList.add(animate) @@ -353,7 +389,7 @@ class Modal { $(this._backdrop).appendTo(document.body) - $(this._element).on(Event.CLICK_DISMISS, (event) => { + $(this._element).on(EVENT_CLICK_DISMISS, (event) => { if (this._ignoreBackdropClick) { this._ignoreBackdropClick = false return @@ -361,18 +397,15 @@ class Modal { if (event.target !== event.currentTarget) { return } - if (this._config.backdrop === 'static') { - this._element.focus() - } else { - this.hide() - } + + this._triggerBackdropTransition() }) if (animate) { Util.reflow(this._backdrop) } - $(this._backdrop).addClass(ClassName.SHOW) + $(this._backdrop).addClass(CLASS_NAME_SHOW) if (!callback) { return @@ -389,7 +422,7 @@ class Modal { .one(Util.TRANSITION_END, callback) .emulateTransitionEnd(backdropTransitionDuration) } else if (!this._isShown && this._backdrop) { - $(this._backdrop).removeClass(ClassName.SHOW) + $(this._backdrop).removeClass(CLASS_NAME_SHOW) const callbackRemove = () => { this._removeBackdrop() @@ -398,7 +431,7 @@ class Modal { } } - if ($(this._element).hasClass(ClassName.FADE)) { + if ($(this._element).hasClass(CLASS_NAME_FADE)) { const backdropTransitionDuration = Util.getTransitionDurationFromElement(this._backdrop) $(this._backdrop) @@ -437,7 +470,7 @@ class Modal { _checkScrollbar() { const rect = document.body.getBoundingClientRect() - this._isBodyOverflowing = rect.left + rect.right < window.innerWidth + this._isBodyOverflowing = Math.round(rect.left + rect.right) < window.innerWidth this._scrollbarWidth = this._getScrollbarWidth() } @@ -445,8 +478,8 @@ class Modal { if (this._isBodyOverflowing) { // Note: DOMNode.style.paddingRight returns the actual value or '' if not set // while $(DOMNode).css('padding-right') returns the calculated value or 0 if not set - const fixedContent = [].slice.call(document.querySelectorAll(Selector.FIXED_CONTENT)) - const stickyContent = [].slice.call(document.querySelectorAll(Selector.STICKY_CONTENT)) + const fixedContent = [].slice.call(document.querySelectorAll(SELECTOR_FIXED_CONTENT)) + const stickyContent = [].slice.call(document.querySelectorAll(SELECTOR_STICKY_CONTENT)) // Adjust fixed content padding $(fixedContent).each((index, element) => { @@ -474,12 +507,12 @@ class Modal { .css('padding-right', `${parseFloat(calculatedPadding) + this._scrollbarWidth}px`) } - $(document.body).addClass(ClassName.OPEN) + $(document.body).addClass(CLASS_NAME_OPEN) } _resetScrollbar() { // Restore fixed content padding - const fixedContent = [].slice.call(document.querySelectorAll(Selector.FIXED_CONTENT)) + const fixedContent = [].slice.call(document.querySelectorAll(SELECTOR_FIXED_CONTENT)) $(fixedContent).each((index, element) => { const padding = $(element).data('padding-right') $(element).removeData('padding-right') @@ -487,7 +520,7 @@ class Modal { }) // Restore sticky content - const elements = [].slice.call(document.querySelectorAll(`${Selector.STICKY_CONTENT}`)) + const elements = [].slice.call(document.querySelectorAll(`${SELECTOR_STICKY_CONTENT}`)) $(elements).each((index, element) => { const margin = $(element).data('margin-right') if (typeof margin !== 'undefined') { @@ -503,7 +536,7 @@ class Modal { _getScrollbarWidth() { // thx d.walsh const scrollDiv = document.createElement('div') - scrollDiv.className = ClassName.SCROLLBAR_MEASURER + scrollDiv.className = CLASS_NAME_SCROLLBAR_MEASURER document.body.appendChild(scrollDiv) const scrollbarWidth = scrollDiv.getBoundingClientRect().width - scrollDiv.clientWidth document.body.removeChild(scrollDiv) @@ -544,7 +577,7 @@ class Modal { * ------------------------------------------------------------------------ */ -$(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) { +$(document).on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) { let target const selector = Util.getSelectorFromElement(this) @@ -562,13 +595,13 @@ $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) { event.preventDefault() } - const $target = $(target).one(Event.SHOW, (showEvent) => { + const $target = $(target).one(EVENT_SHOW, (showEvent) => { if (showEvent.isDefaultPrevented()) { // Only register focus restorer if modal will actually get shown return } - $target.one(Event.HIDDEN, () => { + $target.one(EVENT_HIDDEN, () => { if ($(this).is(':visible')) { this.focus() } |