diff options
Diffstat (limited to 'vendor/twbs/bootstrap/js/src/util/backdrop.js')
-rw-r--r-- | vendor/twbs/bootstrap/js/src/util/backdrop.js | 91 |
1 files changed, 55 insertions, 36 deletions
diff --git a/vendor/twbs/bootstrap/js/src/util/backdrop.js b/vendor/twbs/bootstrap/js/src/util/backdrop.js index 04c763518..26c413a83 100644 --- a/vendor/twbs/bootstrap/js/src/util/backdrop.js +++ b/vendor/twbs/bootstrap/js/src/util/backdrop.js @@ -1,41 +1,65 @@ /** * -------------------------------------------------------------------------- - * Bootstrap (v5.1.3): util/backdrop.js + * Bootstrap (v5.2.0): util/backdrop.js * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * -------------------------------------------------------------------------- */ import EventHandler from '../dom/event-handler' -import { execute, executeAfterTransition, getElement, reflow, typeCheckConfig } from './index' +import { execute, executeAfterTransition, getElement, reflow } from './index' +import Config from './config' + +/** + * Constants + */ + +const NAME = 'backdrop' +const CLASS_NAME_FADE = 'fade' +const CLASS_NAME_SHOW = 'show' +const EVENT_MOUSEDOWN = `mousedown.bs.${NAME}` const Default = { className: 'modal-backdrop', - isVisible: true, // if false, we use the backdrop helper without adding any element to the dom + clickCallback: null, isAnimated: false, - rootElement: 'body', // give the choice to place backdrop under different elements - clickCallback: null + isVisible: true, // if false, we use the backdrop helper without adding any element to the dom + rootElement: 'body' // give the choice to place backdrop under different elements } const DefaultType = { className: 'string', - isVisible: 'boolean', + clickCallback: '(function|null)', isAnimated: 'boolean', - rootElement: '(element|string)', - clickCallback: '(function|null)' + isVisible: 'boolean', + rootElement: '(element|string)' } -const NAME = 'backdrop' -const CLASS_NAME_FADE = 'fade' -const CLASS_NAME_SHOW = 'show' -const EVENT_MOUSEDOWN = `mousedown.bs.${NAME}` +/** + * Class definition + */ -class Backdrop { +class Backdrop extends Config { constructor(config) { + super() this._config = this._getConfig(config) this._isAppended = false this._element = null } + // Getters + static get Default() { + return Default + } + + static get DefaultType() { + return DefaultType + } + + static get NAME() { + return NAME + } + + // Public show(callback) { if (!this._config.isVisible) { execute(callback) @@ -44,11 +68,12 @@ class Backdrop { this._append() + const element = this._getElement() if (this._config.isAnimated) { - reflow(this._getElement()) + reflow(element) } - this._getElement().classList.add(CLASS_NAME_SHOW) + element.classList.add(CLASS_NAME_SHOW) this._emulateAnimation(() => { execute(callback) @@ -69,8 +94,18 @@ class Backdrop { }) } - // Private + dispose() { + if (!this._isAppended) { + return + } + + EventHandler.off(this._element, EVENT_MOUSEDOWN) + this._element.remove() + this._isAppended = false + } + + // Private _getElement() { if (!this._element) { const backdrop = document.createElement('div') @@ -85,15 +120,9 @@ class Backdrop { return this._element } - _getConfig(config) { - config = { - ...Default, - ...(typeof config === 'object' ? config : {}) - } - + _configAfterMerge(config) { // use getElement() with the default "body" to get a fresh Element on each instantiation config.rootElement = getElement(config.rootElement) - typeCheckConfig(NAME, config, DefaultType) return config } @@ -102,26 +131,16 @@ class Backdrop { return } - this._config.rootElement.append(this._getElement()) + const element = this._getElement() + this._config.rootElement.append(element) - EventHandler.on(this._getElement(), EVENT_MOUSEDOWN, () => { + EventHandler.on(element, EVENT_MOUSEDOWN, () => { execute(this._config.clickCallback) }) this._isAppended = true } - dispose() { - if (!this._isAppended) { - return - } - - EventHandler.off(this._element, EVENT_MOUSEDOWN) - - this._element.remove() - this._isAppended = false - } - _emulateAnimation(callback) { executeAfterTransition(callback, this._getElement(), this._config.isAnimated) } |