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

299 lines
9.0 KiB
JavaScript
Raw Normal View History

2021-03-23 17:26:54 +01:00
/*!
* Bootstrap offcanvas.js v5.2.1 (https://getbootstrap.com/)
2022-05-13 08:07:23 +02:00
* Copyright 2011-2022 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2021-03-23 17:26:54 +01:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
*/
(function (global, factory) {
2022-05-13 08:07:23 +02:00
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./util/index'), require('./util/scrollbar'), require('./dom/event-handler'), require('./base-component'), require('./dom/selector-engine'), require('./util/backdrop'), require('./util/focustrap'), require('./util/component-functions')) :
typeof define === 'function' && define.amd ? define(['./util/index', './util/scrollbar', './dom/event-handler', './base-component', './dom/selector-engine', './util/backdrop', './util/focustrap', './util/component-functions'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Offcanvas = factory(global.Index, global.Scrollbar, global.EventHandler, global.BaseComponent, global.SelectorEngine, global.Backdrop, global.Focustrap, global.ComponentFunctions));
})(this, (function (index, ScrollBarHelper, EventHandler, BaseComponent, SelectorEngine, Backdrop, FocusTrap, componentFunctions) { 'use strict';
2021-03-23 17:26:54 +01:00
2021-10-05 17:50:18 +02:00
const _interopDefaultLegacy = e => e && typeof e === 'object' && 'default' in e ? e : { default: e };
2021-03-23 17:26:54 +01:00
2022-05-13 08:07:23 +02:00
const ScrollBarHelper__default = /*#__PURE__*/_interopDefaultLegacy(ScrollBarHelper);
2021-10-05 17:50:18 +02:00
const EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
const BaseComponent__default = /*#__PURE__*/_interopDefaultLegacy(BaseComponent);
2022-05-13 08:07:23 +02:00
const SelectorEngine__default = /*#__PURE__*/_interopDefaultLegacy(SelectorEngine);
const Backdrop__default = /*#__PURE__*/_interopDefaultLegacy(Backdrop);
const FocusTrap__default = /*#__PURE__*/_interopDefaultLegacy(FocusTrap);
2021-03-23 17:26:54 +01:00
2021-08-04 17:41:51 +02:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v5.2.1): offcanvas.js
2021-10-05 17:50:18 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2021-03-23 17:26:54 +01:00
* --------------------------------------------------------------------------
*/
/**
* Constants
*/
const NAME = 'offcanvas';
const DATA_KEY = 'bs.offcanvas';
const EVENT_KEY = `.${DATA_KEY}`;
const DATA_API_KEY = '.data-api';
const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`;
const ESCAPE_KEY = 'Escape';
const CLASS_NAME_SHOW = 'show';
2022-05-13 08:07:23 +02:00
const CLASS_NAME_SHOWING = 'showing';
const CLASS_NAME_HIDING = 'hiding';
2021-08-04 17:41:51 +02:00
const CLASS_NAME_BACKDROP = 'offcanvas-backdrop';
2021-03-23 17:26:54 +01:00
const OPEN_SELECTOR = '.offcanvas.show';
const EVENT_SHOW = `show${EVENT_KEY}`;
const EVENT_SHOWN = `shown${EVENT_KEY}`;
const EVENT_HIDE = `hide${EVENT_KEY}`;
2022-05-13 08:07:23 +02:00
const EVENT_HIDE_PREVENTED = `hidePrevented${EVENT_KEY}`;
2021-03-23 17:26:54 +01:00
const EVENT_HIDDEN = `hidden${EVENT_KEY}`;
2022-05-13 08:07:23 +02:00
const EVENT_RESIZE = `resize${EVENT_KEY}`;
2021-03-23 17:26:54 +01:00
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`;
const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`;
2021-03-23 17:26:54 +01:00
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="offcanvas"]';
2022-05-13 08:07:23 +02:00
const Default = {
backdrop: true,
keyboard: true,
scroll: false
};
const DefaultType = {
backdrop: '(boolean|string)',
keyboard: 'boolean',
scroll: 'boolean'
};
2021-03-23 17:26:54 +01:00
/**
2022-05-13 08:07:23 +02:00
* Class definition
2021-03-23 17:26:54 +01:00
*/
2021-10-05 17:50:18 +02:00
class Offcanvas 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-03-23 17:26:54 +01:00
this._isShown = false;
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._addEventListeners();
} // Getters
static get Default() {
return Default;
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
toggle(relatedTarget) {
return this._isShown ? this.hide() : this.show(relatedTarget);
}
show(relatedTarget) {
if (this._isShown) {
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
});
if (showEvent.defaultPrevented) {
return;
}
this._isShown = true;
this._backdrop.show();
2021-03-23 17:26:54 +01:00
if (!this._config.scroll) {
2022-05-13 08:07:23 +02:00
new ScrollBarHelper__default.default().hide();
}
2021-03-23 17:26:54 +01:00
this._element.setAttribute('aria-modal', true);
this._element.setAttribute('role', 'dialog');
2022-05-13 08:07:23 +02:00
this._element.classList.add(CLASS_NAME_SHOWING);
2021-03-23 17:26:54 +01:00
const completeCallBack = () => {
if (!this._config.scroll || this._config.backdrop) {
2021-08-04 17:41:51 +02:00
this._focustrap.activate();
}
2022-05-13 08:07:23 +02:00
this._element.classList.add(CLASS_NAME_SHOW);
this._element.classList.remove(CLASS_NAME_SHOWING);
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
});
};
this._queueCallback(completeCallBack, this._element, true);
2021-03-23 17:26:54 +01:00
}
hide() {
if (!this._isShown) {
return;
}
2021-10-05 17:50:18 +02:00
const hideEvent = EventHandler__default.default.trigger(this._element, EVENT_HIDE);
2021-03-23 17:26:54 +01:00
if (hideEvent.defaultPrevented) {
return;
}
2021-08-04 17:41:51 +02:00
this._focustrap.deactivate();
2021-03-23 17:26:54 +01:00
this._element.blur();
this._isShown = false;
2022-05-13 08:07:23 +02:00
this._element.classList.add(CLASS_NAME_HIDING);
2021-03-23 17:26:54 +01:00
this._backdrop.hide();
2021-03-23 17:26:54 +01:00
const completeCallback = () => {
2022-05-13 08:07:23 +02:00
this._element.classList.remove(CLASS_NAME_SHOW, CLASS_NAME_HIDING);
2021-03-23 17:26:54 +01:00
this._element.removeAttribute('aria-modal');
this._element.removeAttribute('role');
if (!this._config.scroll) {
2022-05-13 08:07:23 +02:00
new ScrollBarHelper__default.default().reset();
2021-03-23 17:26:54 +01:00
}
2021-10-05 17:50:18 +02:00
EventHandler__default.default.trigger(this._element, EVENT_HIDDEN);
2021-03-23 17:26:54 +01:00
};
this._queueCallback(completeCallback, this._element, true);
}
dispose() {
this._backdrop.dispose();
2021-08-04 17:41:51 +02:00
this._focustrap.deactivate();
super.dispose();
2021-03-23 17:26:54 +01:00
} // Private
_initializeBackDrop() {
2022-05-13 08:07:23 +02:00
const clickCallback = () => {
if (this._config.backdrop === 'static') {
EventHandler__default.default.trigger(this._element, EVENT_HIDE_PREVENTED);
return;
}
this.hide();
}; // 'static' option will be translated to true, and booleans will keep their value
const isVisible = Boolean(this._config.backdrop);
return new Backdrop__default.default({
2021-08-04 17:41:51 +02:00
className: CLASS_NAME_BACKDROP,
2022-05-13 08:07:23 +02:00
isVisible,
isAnimated: true,
rootElement: this._element.parentNode,
2022-05-13 08:07:23 +02:00
clickCallback: isVisible ? clickCallback : null
});
}
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
});
}
_addEventListeners() {
2021-10-05 17:50:18 +02:00
EventHandler__default.default.on(this._element, EVENT_KEYDOWN_DISMISS, event => {
2022-05-13 08:07:23 +02:00
if (event.key !== ESCAPE_KEY) {
return;
2021-03-23 17:26:54 +01:00
}
2022-05-13 08:07:23 +02:00
if (!this._config.keyboard) {
EventHandler__default.default.trigger(this._element, EVENT_HIDE_PREVENTED);
return;
}
this.hide();
2021-03-23 17:26:54 +01:00
});
} // Static
static jQueryInterface(config) {
return this.each(function () {
const data = Offcanvas.getOrCreateInstance(this, config);
2021-03-23 17:26:54 +01:00
if (typeof config !== 'string') {
return;
}
if (data[config] === undefined || config.startsWith('_') || config === 'constructor') {
throw new TypeError(`No method named "${config}"`);
}
data[config](this);
});
}
}
/**
2022-05-13 08:07:23 +02:00
* Data API implementation
2021-03-23 17:26:54 +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);
2021-03-23 17:26:54 +01:00
if (['A', 'AREA'].includes(this.tagName)) {
event.preventDefault();
}
2022-05-13 08:07:23 +02:00
if (index.isDisabled(this)) {
2021-03-23 17:26:54 +01:00
return;
}
2021-10-05 17:50:18 +02:00
EventHandler__default.default.one(target, EVENT_HIDDEN, () => {
2021-03-23 17:26:54 +01:00
// focus on trigger when it is closed
2022-05-13 08:07:23 +02:00
if (index.isVisible(this)) {
2021-03-23 17:26:54 +01:00
this.focus();
}
}); // avoid conflict when clicking a toggler of an offcanvas, while another is open
2022-05-13 08:07:23 +02:00
const alreadyOpen = SelectorEngine__default.default.findOne(OPEN_SELECTOR);
2021-03-23 17:26:54 +01:00
2022-05-13 08:07:23 +02:00
if (alreadyOpen && alreadyOpen !== target) {
Offcanvas.getInstance(alreadyOpen).hide();
2021-03-23 17:26:54 +01:00
}
const data = Offcanvas.getOrCreateInstance(target);
2021-03-23 17:26:54 +01:00
data.toggle(this);
});
2022-05-13 08:07:23 +02:00
EventHandler__default.default.on(window, EVENT_LOAD_DATA_API, () => {
for (const selector of SelectorEngine__default.default.find(OPEN_SELECTOR)) {
Offcanvas.getOrCreateInstance(selector).show();
}
});
EventHandler__default.default.on(window, EVENT_RESIZE, () => {
for (const element of SelectorEngine__default.default.find('[aria-modal][class*=show][class*=offcanvas-]')) {
if (getComputedStyle(element).position !== 'fixed') {
Offcanvas.getOrCreateInstance(element).hide();
}
}
});
componentFunctions.enableDismissTrigger(Offcanvas);
2021-03-23 17:26:54 +01:00
/**
* jQuery
*/
2022-05-13 08:07:23 +02:00
index.defineJQueryPlugin(Offcanvas);
2021-03-23 17:26:54 +01:00
return Offcanvas;
2021-10-05 17:50:18 +02:00
}));
2021-03-23 17:26:54 +01:00
//# sourceMappingURL=offcanvas.js.map