0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-03 15:24:19 +01:00
Bootstrap/js/dist/modal.js

321 lines
11 KiB
JavaScript
Raw Normal View History

2018-11-13 07:41:12 +01:00
/*!
* Bootstrap modal.js v5.3.0-alpha2 (https://getbootstrap.com/)
* Copyright 2011-2023 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2020-06-16 20:50:01 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2018-11-13 07:41:12 +01:00
*/
2018-07-24 02:51:14 +02:00
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./util/index.js'), require('./dom/event-handler.js'), require('./dom/selector-engine.js'), require('./util/scrollbar.js'), require('./base-component.js'), require('./util/backdrop.js'), require('./util/focustrap.js'), require('./util/component-functions.js')) :
2022-05-13 08:07:23 +02:00
typeof define === 'function' && define.amd ? define(['./util/index', './dom/event-handler', './dom/selector-engine', './util/scrollbar', './base-component', './util/backdrop', './util/focustrap', './util/component-functions'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Modal = factory(global.Index, global.EventHandler, global.SelectorEngine, global.Scrollbar, global.BaseComponent, global.Backdrop, global.Focustrap, global.ComponentFunctions));
})(this, (function (index_js, EventHandler, SelectorEngine, ScrollBarHelper, BaseComponent, Backdrop, FocusTrap, componentFunctions_js) { 'use strict';
2021-08-04 17:41:51 +02:00
/**
* --------------------------------------------------------------------------
* Bootstrap modal.js
2021-08-04 17:41:51 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
2021-08-04 17:41:51 +02:00
/**
2020-12-03 15:18:59 +01:00
* Constants
*/
2021-03-23 17:26:54 +01:00
const NAME = 'modal';
const DATA_KEY = 'bs.modal';
const EVENT_KEY = `.${DATA_KEY}`;
const DATA_API_KEY = '.data-api';
const ESCAPE_KEY = 'Escape';
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_RESIZE = `resize${EVENT_KEY}`;
const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`;
const EVENT_MOUSEDOWN_DISMISS = `mousedown.dismiss${EVENT_KEY}`;
2021-03-23 17:26:54 +01:00
const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`;
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`;
const CLASS_NAME_OPEN = 'modal-open';
const CLASS_NAME_FADE = 'fade';
const CLASS_NAME_SHOW = 'show';
const CLASS_NAME_STATIC = 'modal-static';
const OPEN_SELECTOR = '.modal.show';
2021-03-23 17:26:54 +01:00
const SELECTOR_DIALOG = '.modal-dialog';
const SELECTOR_MODAL_BODY = '.modal-body';
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="modal"]';
2022-05-13 08:07:23 +02:00
const Default = {
backdrop: true,
focus: true,
keyboard: true
2022-05-13 08:07:23 +02:00
};
const DefaultType = {
backdrop: '(boolean|string)',
focus: 'boolean',
keyboard: 'boolean'
2022-05-13 08:07:23 +02:00
};
2019-10-08 08:39:10 +02:00
/**
2022-05-13 08:07:23 +02:00
* Class definition
2019-10-08 08:39:10 +02:00
*/
2018-11-13 07:41:12 +01:00
class Modal extends BaseComponent {
2021-03-23 17:26:54 +01:00
constructor(element, config) {
2022-05-13 08:07:23 +02:00
super(element, config);
this._dialog = SelectorEngine.findOne(SELECTOR_DIALOG, this._element);
this._backdrop = this._initializeBackDrop();
2021-08-04 17:41:51 +02:00
this._focustrap = this._initializeFocusTrap();
2021-03-23 17:26:54 +01:00
this._isShown = false;
this._isTransitioning = false;
this._scrollBar = new ScrollBarHelper();
2022-05-13 08:07:23 +02:00
this._addEventListeners();
}
2018-11-13 07:41:12 +01:00
// Getters
2021-03-23 17:26:54 +01:00
static get Default() {
return Default;
}
2022-05-13 08:07:23 +02:00
static get DefaultType() {
return DefaultType;
}
static get NAME() {
return NAME;
}
2015-08-13 06:12:03 +02:00
// Public
2021-03-23 17:26:54 +01:00
toggle(relatedTarget) {
return this._isShown ? this.hide() : this.show(relatedTarget);
}
show(relatedTarget) {
2018-11-13 07:41:12 +01:00
if (this._isShown || this._isTransitioning) {
return;
}
const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, {
2021-03-23 17:26:54 +01:00
relatedTarget
2018-11-13 07:41:12 +01:00
});
if (showEvent.defaultPrevented) {
2018-11-13 07:41:12 +01:00
return;
}
this._isShown = true;
2022-05-13 08:07:23 +02:00
this._isTransitioning = true;
this._scrollBar.hide();
document.body.classList.add(CLASS_NAME_OPEN);
2018-11-13 07:41:12 +01:00
this._adjustDialog();
2022-05-13 08:07:23 +02:00
this._backdrop.show(() => this._showElement(relatedTarget));
2021-03-23 17:26:54 +01:00
}
2021-08-04 17:41:51 +02:00
hide() {
2018-11-13 07:41:12 +01:00
if (!this._isShown || this._isTransitioning) {
return;
}
const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE);
if (hideEvent.defaultPrevented) {
2018-11-13 07:41:12 +01:00
return;
}
this._isShown = false;
2022-05-13 08:07:23 +02:00
this._isTransitioning = true;
2021-08-04 17:41:51 +02:00
this._focustrap.deactivate();
2020-03-28 11:29:08 +01:00
this._element.classList.remove(CLASS_NAME_SHOW);
2022-05-13 08:07:23 +02:00
this._queueCallback(() => this._hideModal(), this._element, this._isAnimated());
2021-03-23 17:26:54 +01:00
}
dispose() {
2022-05-13 08:07:23 +02:00
for (const htmlElement of [window, this._dialog]) {
EventHandler.off(htmlElement, EVENT_KEY);
2022-05-13 08:07:23 +02:00
}
this._backdrop.dispose();
2021-08-04 17:41:51 +02:00
this._focustrap.deactivate();
super.dispose();
2021-03-23 17:26:54 +01:00
}
handleUpdate() {
2018-11-13 07:41:12 +01:00
this._adjustDialog();
}
2021-03-23 17:26:54 +01:00
// Private
_initializeBackDrop() {
return new Backdrop({
isVisible: Boolean(this._config.backdrop),
2022-05-13 08:07:23 +02:00
// 'static' option will be translated to true, and booleans will keep their value,
isAnimated: this._isAnimated()
});
}
2021-08-04 17:41:51 +02:00
_initializeFocusTrap() {
return new FocusTrap({
2021-08-04 17:41:51 +02:00
trapElement: this._element
});
}
2021-03-23 17:26:54 +01:00
_showElement(relatedTarget) {
2022-05-13 08:07:23 +02:00
// try to append dynamic modal
if (!document.body.contains(this._element)) {
2021-08-04 17:41:51 +02:00
document.body.append(this._element);
2018-11-13 07:41:12 +01:00
}
this._element.style.display = 'block';
this._element.removeAttribute('aria-hidden');
2018-12-16 00:13:22 +01:00
this._element.setAttribute('aria-modal', true);
2020-06-14 00:40:28 +02:00
this._element.setAttribute('role', 'dialog');
2020-05-13 20:53:43 +02:00
this._element.scrollTop = 0;
const modalBody = SelectorEngine.findOne(SELECTOR_MODAL_BODY, this._dialog);
2020-05-13 20:53:43 +02:00
if (modalBody) {
2019-08-27 15:03:21 +02:00
modalBody.scrollTop = 0;
}
index_js.reflow(this._element);
2020-03-28 11:29:08 +01:00
this._element.classList.add(CLASS_NAME_SHOW);
2021-03-23 17:26:54 +01:00
const transitionComplete = () => {
if (this._config.focus) {
2021-08-04 17:41:51 +02:00
this._focustrap.activate();
2017-09-06 06:05:12 +02:00
}
2021-03-23 17:26:54 +01:00
this._isTransitioning = false;
EventHandler.trigger(this._element, EVENT_SHOWN, {
2021-03-23 17:26:54 +01:00
relatedTarget
2019-03-01 17:31:34 +01:00
});
2017-09-30 23:28:03 +02:00
};
2022-05-13 08:07:23 +02:00
this._queueCallback(transitionComplete, this._dialog, this._isAnimated());
2021-03-23 17:26:54 +01:00
}
2022-05-13 08:07:23 +02:00
_addEventListeners() {
EventHandler.on(this._element, EVENT_KEYDOWN_DISMISS, event => {
2022-05-13 08:07:23 +02:00
if (event.key !== ESCAPE_KEY) {
return;
}
if (this._config.keyboard) {
this.hide();
return;
}
this._triggerBackdropTransition();
});
EventHandler.on(window, EVENT_RESIZE, () => {
2022-05-13 08:07:23 +02:00
if (this._isShown && !this._isTransitioning) {
this._adjustDialog();
}
});
EventHandler.on(this._element, EVENT_MOUSEDOWN_DISMISS, event => {
// a bad trick to segregate clicks that may start inside dialog but end outside, and avoid listen to scrollbar clicks
EventHandler.one(this._element, EVENT_CLICK_DISMISS, event2 => {
if (this._element !== event.target || this._element !== event2.target) {
return;
}
if (this._config.backdrop === 'static') {
this._triggerBackdropTransition();
return;
}
if (this._config.backdrop) {
this.hide();
}
});
2022-05-13 08:07:23 +02:00
});
2021-03-23 17:26:54 +01:00
}
_hideModal() {
2018-11-13 07:41:12 +01:00
this._element.style.display = 'none';
this._element.setAttribute('aria-hidden', true);
2018-12-16 00:13:22 +01:00
this._element.removeAttribute('aria-modal');
2020-06-14 00:40:28 +02:00
this._element.removeAttribute('role');
2018-11-13 07:41:12 +01:00
this._isTransitioning = false;
this._backdrop.hide(() => {
2020-03-28 11:29:08 +01:00
document.body.classList.remove(CLASS_NAME_OPEN);
2021-03-23 17:26:54 +01:00
this._resetAdjustments();
this._scrollBar.reset();
EventHandler.trigger(this._element, EVENT_HIDDEN);
2018-11-13 07:41:12 +01:00
});
2021-03-23 17:26:54 +01:00
}
_isAnimated() {
return this._element.classList.contains(CLASS_NAME_FADE);
}
_triggerBackdropTransition() {
const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE_PREVENTED);
2020-11-23 14:17:16 +01:00
if (hideEvent.defaultPrevented) {
return;
}
2022-05-13 08:07:23 +02:00
const isModalOverflowing = this._element.scrollHeight > document.documentElement.clientHeight;
const initialOverflowY = this._element.style.overflowY;
// return if the following background transition hasn't yet completed
2022-05-13 08:07:23 +02:00
if (initialOverflowY === 'hidden' || this._element.classList.contains(CLASS_NAME_STATIC)) {
return;
}
2020-11-23 14:17:16 +01:00
if (!isModalOverflowing) {
2022-05-13 08:07:23 +02:00
this._element.style.overflowY = 'hidden';
2020-11-23 14:17:16 +01:00
}
2022-05-13 08:07:23 +02:00
this._element.classList.add(CLASS_NAME_STATIC);
this._queueCallback(() => {
2022-05-13 08:07:23 +02:00
this._element.classList.remove(CLASS_NAME_STATIC);
this._queueCallback(() => {
this._element.style.overflowY = initialOverflowY;
}, this._dialog);
}, this._dialog);
2020-11-23 14:17:16 +01:00
this._element.focus();
2022-05-13 08:07:23 +02:00
}
2022-05-13 08:07:23 +02:00
/**
* The following methods are used to handle overflowing modals
*/
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
_adjustDialog() {
const isModalOverflowing = this._element.scrollHeight > document.documentElement.clientHeight;
const scrollbarWidth = this._scrollBar.getWidth();
const isBodyOverflowing = scrollbarWidth > 0;
2022-05-13 08:07:23 +02:00
if (isBodyOverflowing && !isModalOverflowing) {
const property = index_js.isRTL() ? 'paddingLeft' : 'paddingRight';
2022-05-13 08:07:23 +02:00
this._element.style[property] = `${scrollbarWidth}px`;
2018-11-13 07:41:12 +01:00
}
2022-05-13 08:07:23 +02:00
if (!isBodyOverflowing && isModalOverflowing) {
const property = index_js.isRTL() ? 'paddingRight' : 'paddingLeft';
2022-05-13 08:07:23 +02:00
this._element.style[property] = `${scrollbarWidth}px`;
2018-11-13 07:41:12 +01:00
}
2021-03-23 17:26:54 +01:00
}
_resetAdjustments() {
2018-11-13 07:41:12 +01:00
this._element.style.paddingLeft = '';
this._element.style.paddingRight = '';
}
2016-10-10 02:26:51 +02:00
// Static
2021-03-23 17:26:54 +01:00
static jQueryInterface(config, relatedTarget) {
return this.each(function () {
const data = Modal.getOrCreateInstance(this, config);
if (typeof config !== 'string') {
return;
2018-11-13 07:41:12 +01:00
}
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`);
2018-07-24 02:51:14 +02:00
}
data[config](relatedTarget);
2018-11-13 07:41:12 +01:00
});
2021-03-23 17:26:54 +01:00
}
}
2018-11-13 07:41:12 +01:00
/**
2022-05-13 08:07:23 +02:00
* Data API implementation
2018-11-13 07:41:12 +01:00
*/
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
const target = SelectorEngine.getElementFromSelector(this);
if (['A', 'AREA'].includes(this.tagName)) {
2018-11-13 07:41:12 +01:00
event.preventDefault();
}
EventHandler.one(target, EVENT_SHOW, showEvent => {
2019-03-01 17:31:34 +01:00
if (showEvent.defaultPrevented) {
// only register focus restorer if modal will actually get shown
2018-11-13 07:41:12 +01:00
return;
}
EventHandler.one(target, EVENT_HIDDEN, () => {
if (index_js.isVisible(this)) {
2021-03-23 17:26:54 +01:00
this.focus();
}
});
});
// avoid conflict when clicking modal toggler while another one is open
const alreadyOpen = SelectorEngine.findOne(OPEN_SELECTOR);
2022-05-13 08:07:23 +02:00
if (alreadyOpen) {
Modal.getInstance(alreadyOpen).hide();
}
const data = Modal.getOrCreateInstance(target);
data.toggle(this);
2018-11-13 07:41:12 +01:00
});
componentFunctions_js.enableDismissTrigger(Modal);
2018-11-13 07:41:12 +01:00
/**
* jQuery
*/
index_js.defineJQueryPlugin(Modal);
return Modal;
2018-07-24 02:51:14 +02:00
2021-10-05 17:50:18 +02:00
}));
2018-07-24 02:51:14 +02:00
//# sourceMappingURL=modal.js.map