0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-13 01:08:58 +01:00
Bootstrap/js/dist/modal.js

401 lines
12 KiB
JavaScript
Raw Normal View History

2018-11-13 07:41:12 +01:00
/*!
* Bootstrap modal.js v5.2.2 (https://getbootstrap.com/)
2022-05-13 08:07:23 +02:00
* Copyright 2011-2022 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) {
2022-05-13 08:07:23 +02:00
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./util/index'), require('./dom/event-handler'), require('./dom/selector-engine'), require('./util/scrollbar'), require('./base-component'), require('./util/backdrop'), require('./util/focustrap'), require('./util/component-functions')) :
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, EventHandler, SelectorEngine, ScrollBarHelper, BaseComponent, Backdrop, FocusTrap, componentFunctions) { 'use strict';
2018-07-24 02:51:14 +02:00
2021-10-05 17:50:18 +02:00
const _interopDefaultLegacy = e => e && typeof e === 'object' && 'default' in e ? e : { default: e };
2020-09-14 17:12:06 +02:00
2021-10-05 17:50:18 +02:00
const EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
const SelectorEngine__default = /*#__PURE__*/_interopDefaultLegacy(SelectorEngine);
2022-05-13 08:07:23 +02:00
const ScrollBarHelper__default = /*#__PURE__*/_interopDefaultLegacy(ScrollBarHelper);
2021-10-05 17:50:18 +02:00
const BaseComponent__default = /*#__PURE__*/_interopDefaultLegacy(BaseComponent);
2022-05-13 08:07:23 +02:00
const Backdrop__default = /*#__PURE__*/_interopDefaultLegacy(Backdrop);
const FocusTrap__default = /*#__PURE__*/_interopDefaultLegacy(FocusTrap);
2021-08-04 17:41:51 +02:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.2.2): modal.js
2021-08-04 17:41:51 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
/**
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
2021-10-05 17:50:18 +02:00
class Modal extends BaseComponent__default.default {
2021-03-23 17:26:54 +01:00
constructor(element, config) {
2022-05-13 08:07:23 +02:00
super(element, config);
2021-10-05 17:50:18 +02:00
this._dialog = SelectorEngine__default.default.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;
2022-05-13 08:07:23 +02:00
this._scrollBar = new ScrollBarHelper__default.default();
this._addEventListeners();
2018-11-13 07:41:12 +01:00
} // Getters
2021-03-23 17:26:54 +01:00
static get Default() {
return Default;
}
2018-11-13 07:41:12 +01:00
2022-05-13 08:07:23 +02:00
static get DefaultType() {
return DefaultType;
}
static get NAME() {
return NAME;
2021-03-23 17:26:54 +01:00
} // Public
2015-08-13 06:12:03 +02:00
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;
}
2021-10-05 17:50:18 +02:00
const showEvent = EventHandler__default.default.trigger(this._element, EVENT_SHOW, {
2021-03-23 17:26:54 +01:00
relatedTarget
2018-11-13 07:41:12 +01:00
});
2016-10-10 02:26:51 +02:00
if (showEvent.defaultPrevented) {
2018-11-13 07:41:12 +01:00
return;
}
2018-11-13 07:41:12 +01:00
this._isShown = true;
2022-05-13 08:07:23 +02:00
this._isTransitioning = true;
this._scrollBar.hide();
document.body.classList.add(CLASS_NAME_OPEN);
2016-12-02 19:13:36 +01:00
2018-11-13 07:41:12 +01:00
this._adjustDialog();
2017-04-02 04:18:29 +02:00
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;
}
2021-10-05 17:50:18 +02:00
const hideEvent = EventHandler__default.default.trigger(this._element, EVENT_HIDE);
if (hideEvent.defaultPrevented) {
2018-11-13 07:41:12 +01:00
return;
}
2017-10-16 00:51:44 +02:00
2018-11-13 07:41:12 +01:00
this._isShown = false;
2022-05-13 08:07:23 +02:00
this._isTransitioning = true;
2016-10-10 02:26:51 +02:00
2021-08-04 17:41:51 +02:00
this._focustrap.deactivate();
2019-03-01 17:31:34 +01:00
2020-03-28 11:29:08 +01:00
this._element.classList.remove(CLASS_NAME_SHOW);
2019-03-01 17:31:34 +01:00
2022-05-13 08:07:23 +02:00
this._queueCallback(() => this._hideModal(), this._element, this._isAnimated());
2021-03-23 17:26:54 +01:00
}
2020-12-03 15:18:59 +01:00
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__default.default.off(htmlElement, EVENT_KEY);
}
this._backdrop.dispose();
2021-08-04 17:41:51 +02:00
this._focustrap.deactivate();
2017-03-26 20:26:31 +02:00
2021-08-04 17:41:51 +02:00
super.dispose();
2021-03-23 17:26:54 +01:00
}
2016-10-10 02:26:51 +02:00
2021-03-23 17:26:54 +01:00
handleUpdate() {
2018-11-13 07:41:12 +01:00
this._adjustDialog();
2019-01-04 17:29:45 +01:00
} // Private
2016-10-10 02:26:51 +02:00
2021-03-23 17:26:54 +01:00
_initializeBackDrop() {
2022-05-13 08:07:23 +02:00
return new Backdrop__default.default({
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() {
2022-05-13 08:07:23 +02:00
return new FocusTrap__default.default({
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
}
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
this._element.style.display = 'block';
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
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;
2022-05-13 08:07:23 +02:00
const modalBody = SelectorEngine__default.default.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;
}
2022-05-13 08:07:23 +02:00
index.reflow(this._element);
2020-03-28 11:29:08 +01:00
this._element.classList.add(CLASS_NAME_SHOW);
2018-07-24 02:51:14 +02:00
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;
2021-10-05 17:50:18 +02:00
EventHandler__default.default.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__default.default.on(this._element, EVENT_KEYDOWN_DISMISS, event => {
if (event.key !== ESCAPE_KEY) {
return;
}
2022-05-13 08:07:23 +02:00
if (this._config.keyboard) {
event.preventDefault();
this.hide();
return;
}
this._triggerBackdropTransition();
});
EventHandler__default.default.on(window, EVENT_RESIZE, () => {
if (this._isShown && !this._isTransitioning) {
this._adjustDialog();
}
});
EventHandler__default.default.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__default.default.one(this._element, EVENT_CLICK_DISMISS, event2 => {
if (this._element !== event.target || this._element !== event2.target) {
return;
}
2022-05-13 08:07:23 +02:00
if (this._config.backdrop === 'static') {
this._triggerBackdropTransition();
2022-05-13 08:07:23 +02:00
return;
}
2022-05-13 08:07:23 +02:00
if (this._config.backdrop) {
this.hide();
}
});
2022-05-13 08:07:23 +02:00
});
2021-03-23 17:26:54 +01:00
}
2021-03-23 17:26:54 +01:00
_hideModal() {
2018-11-13 07:41:12 +01:00
this._element.style.display = 'none';
2018-11-13 07:41:12 +01:00
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;
2017-09-30 23:28:03 +02:00
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();
2017-09-30 23:28:03 +02:00
this._scrollBar.reset();
2021-10-05 17:50:18 +02:00
EventHandler__default.default.trigger(this._element, EVENT_HIDDEN);
2018-11-13 07:41:12 +01:00
});
2021-03-23 17:26:54 +01:00
}
2017-09-30 23:28:03 +02:00
2021-03-23 17:26:54 +01:00
_isAnimated() {
return this._element.classList.contains(CLASS_NAME_FADE);
}
2019-11-08 09:11:23 +01:00
2021-03-23 17:26:54 +01:00
_triggerBackdropTransition() {
2021-10-05 17:50:18 +02:00
const hideEvent = EventHandler__default.default.trigger(this._element, EVENT_HIDE_PREVENTED);
2019-11-08 09:11:23 +01:00
2020-11-23 14:17:16 +01:00
if (hideEvent.defaultPrevented) {
return;
}
2019-11-08 09:11:23 +01:00
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-09-14 17:12:06 +02:00
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
}
2020-09-14 17:12:06 +02:00
2022-05-13 08:07:23 +02:00
this._element.classList.add(CLASS_NAME_STATIC);
2019-11-08 09:11:23 +01:00
this._queueCallback(() => {
2022-05-13 08:07:23 +02:00
this._element.classList.remove(CLASS_NAME_STATIC);
2020-09-14 17:12:06 +02:00
2022-05-13 08:07:23 +02:00
this._queueCallback(() => {
this._element.style.overflowY = initialOverflowY;
}, this._dialog);
}, this._dialog);
2019-11-08 09:11:23 +01:00
2020-11-23 14:17:16 +01:00
this._element.focus();
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;
2021-03-23 17:26:54 +01:00
2022-05-13 08:07:23 +02:00
if (isBodyOverflowing && !isModalOverflowing) {
const property = index.isRTL() ? 'paddingLeft' : 'paddingRight';
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.isRTL() ? 'paddingRight' : 'paddingLeft';
this._element.style[property] = `${scrollbarWidth}px`;
2018-11-13 07:41:12 +01:00
}
2021-03-23 17:26:54 +01:00
}
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 = '';
2019-01-04 17:29:45 +01:00
} // Static
2016-10-10 02:26:51 +02:00
2021-03-23 17:26:54 +01:00
static jQueryInterface(config, relatedTarget) {
return this.each(function () {
const data = Modal.getOrCreateInstance(this, config);
2017-09-30 23:28:03 +02:00
if (typeof config !== 'string') {
return;
2018-11-13 07:41:12 +01:00
}
2017-09-30 23:28:03 +02: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
}
2017-09-30 23:28:03 +02: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
*/
2021-10-05 17:50:18 +02:00
EventHandler__default.default.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
2022-05-13 08:07:23 +02:00
const target = index.getElementFromSelector(this);
2018-11-13 07:41:12 +01:00
if (['A', 'AREA'].includes(this.tagName)) {
2018-11-13 07:41:12 +01:00
event.preventDefault();
}
2021-10-05 17:50:18 +02:00
EventHandler__default.default.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;
}
2021-10-05 17:50:18 +02:00
EventHandler__default.default.one(target, EVENT_HIDDEN, () => {
2022-05-13 08:07:23 +02:00
if (index.isVisible(this)) {
2021-03-23 17:26:54 +01:00
this.focus();
}
});
2022-05-13 08:07:23 +02:00
}); // avoid conflict when clicking modal toggler while another one is open
2022-05-13 08:07:23 +02:00
const alreadyOpen = SelectorEngine__default.default.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
});
2022-05-13 08:07:23 +02:00
componentFunctions.enableDismissTrigger(Modal);
2018-11-13 07:41:12 +01:00
/**
* jQuery
*/
2022-05-13 08:07:23 +02:00
index.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