You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
8.1 KiB

8 months ago
  1. /*!
  2. * Bootstrap offcanvas.js v5.3.3 (https://getbootstrap.com/)
  3. * Copyright 2011-2024 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./base-component.js'), require('./dom/event-handler.js'), require('./dom/selector-engine.js'), require('./util/backdrop.js'), require('./util/component-functions.js'), require('./util/focustrap.js'), require('./util/index.js'), require('./util/scrollbar.js')) :
  8. typeof define === 'function' && define.amd ? define(['./base-component', './dom/event-handler', './dom/selector-engine', './util/backdrop', './util/component-functions', './util/focustrap', './util/index', './util/scrollbar'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Offcanvas = factory(global.BaseComponent, global.EventHandler, global.SelectorEngine, global.Backdrop, global.ComponentFunctions, global.Focustrap, global.Index, global.Scrollbar));
  10. })(this, (function (BaseComponent, EventHandler, SelectorEngine, Backdrop, componentFunctions_js, FocusTrap, index_js, ScrollBarHelper) { 'use strict';
  11. /**
  12. * --------------------------------------------------------------------------
  13. * Bootstrap offcanvas.js
  14. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  15. * --------------------------------------------------------------------------
  16. */
  17. /**
  18. * Constants
  19. */
  20. const NAME = 'offcanvas';
  21. const DATA_KEY = 'bs.offcanvas';
  22. const EVENT_KEY = `.${DATA_KEY}`;
  23. const DATA_API_KEY = '.data-api';
  24. const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`;
  25. const ESCAPE_KEY = 'Escape';
  26. const CLASS_NAME_SHOW = 'show';
  27. const CLASS_NAME_SHOWING = 'showing';
  28. const CLASS_NAME_HIDING = 'hiding';
  29. const CLASS_NAME_BACKDROP = 'offcanvas-backdrop';
  30. const OPEN_SELECTOR = '.offcanvas.show';
  31. const EVENT_SHOW = `show${EVENT_KEY}`;
  32. const EVENT_SHOWN = `shown${EVENT_KEY}`;
  33. const EVENT_HIDE = `hide${EVENT_KEY}`;
  34. const EVENT_HIDE_PREVENTED = `hidePrevented${EVENT_KEY}`;
  35. const EVENT_HIDDEN = `hidden${EVENT_KEY}`;
  36. const EVENT_RESIZE = `resize${EVENT_KEY}`;
  37. const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`;
  38. const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`;
  39. const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="offcanvas"]';
  40. const Default = {
  41. backdrop: true,
  42. keyboard: true,
  43. scroll: false
  44. };
  45. const DefaultType = {
  46. backdrop: '(boolean|string)',
  47. keyboard: 'boolean',
  48. scroll: 'boolean'
  49. };
  50. /**
  51. * Class definition
  52. */
  53. class Offcanvas extends BaseComponent {
  54. constructor(element, config) {
  55. super(element, config);
  56. this._isShown = false;
  57. this._backdrop = this._initializeBackDrop();
  58. this._focustrap = this._initializeFocusTrap();
  59. this._addEventListeners();
  60. }
  61. // Getters
  62. static get Default() {
  63. return Default;
  64. }
  65. static get DefaultType() {
  66. return DefaultType;
  67. }
  68. static get NAME() {
  69. return NAME;
  70. }
  71. // Public
  72. toggle(relatedTarget) {
  73. return this._isShown ? this.hide() : this.show(relatedTarget);
  74. }
  75. show(relatedTarget) {
  76. if (this._isShown) {
  77. return;
  78. }
  79. const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, {
  80. relatedTarget
  81. });
  82. if (showEvent.defaultPrevented) {
  83. return;
  84. }
  85. this._isShown = true;
  86. this._backdrop.show();
  87. if (!this._config.scroll) {
  88. new ScrollBarHelper().hide();
  89. }
  90. this._element.setAttribute('aria-modal', true);
  91. this._element.setAttribute('role', 'dialog');
  92. this._element.classList.add(CLASS_NAME_SHOWING);
  93. const completeCallBack = () => {
  94. if (!this._config.scroll || this._config.backdrop) {
  95. this._focustrap.activate();
  96. }
  97. this._element.classList.add(CLASS_NAME_SHOW);
  98. this._element.classList.remove(CLASS_NAME_SHOWING);
  99. EventHandler.trigger(this._element, EVENT_SHOWN, {
  100. relatedTarget
  101. });
  102. };
  103. this._queueCallback(completeCallBack, this._element, true);
  104. }
  105. hide() {
  106. if (!this._isShown) {
  107. return;
  108. }
  109. const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE);
  110. if (hideEvent.defaultPrevented) {
  111. return;
  112. }
  113. this._focustrap.deactivate();
  114. this._element.blur();
  115. this._isShown = false;
  116. this._element.classList.add(CLASS_NAME_HIDING);
  117. this._backdrop.hide();
  118. const completeCallback = () => {
  119. this._element.classList.remove(CLASS_NAME_SHOW, CLASS_NAME_HIDING);
  120. this._element.removeAttribute('aria-modal');
  121. this._element.removeAttribute('role');
  122. if (!this._config.scroll) {
  123. new ScrollBarHelper().reset();
  124. }
  125. EventHandler.trigger(this._element, EVENT_HIDDEN);
  126. };
  127. this._queueCallback(completeCallback, this._element, true);
  128. }
  129. dispose() {
  130. this._backdrop.dispose();
  131. this._focustrap.deactivate();
  132. super.dispose();
  133. }
  134. // Private
  135. _initializeBackDrop() {
  136. const clickCallback = () => {
  137. if (this._config.backdrop === 'static') {
  138. EventHandler.trigger(this._element, EVENT_HIDE_PREVENTED);
  139. return;
  140. }
  141. this.hide();
  142. };
  143. // 'static' option will be translated to true, and booleans will keep their value
  144. const isVisible = Boolean(this._config.backdrop);
  145. return new Backdrop({
  146. className: CLASS_NAME_BACKDROP,
  147. isVisible,
  148. isAnimated: true,
  149. rootElement: this._element.parentNode,
  150. clickCallback: isVisible ? clickCallback : null
  151. });
  152. }
  153. _initializeFocusTrap() {
  154. return new FocusTrap({
  155. trapElement: this._element
  156. });
  157. }
  158. _addEventListeners() {
  159. EventHandler.on(this._element, EVENT_KEYDOWN_DISMISS, event => {
  160. if (event.key !== ESCAPE_KEY) {
  161. return;
  162. }
  163. if (this._config.keyboard) {
  164. this.hide();
  165. return;
  166. }
  167. EventHandler.trigger(this._element, EVENT_HIDE_PREVENTED);
  168. });
  169. }
  170. // Static
  171. static jQueryInterface(config) {
  172. return this.each(function () {
  173. const data = Offcanvas.getOrCreateInstance(this, config);
  174. if (typeof config !== 'string') {
  175. return;
  176. }
  177. if (data[config] === undefined || config.startsWith('_') || config === 'constructor') {
  178. throw new TypeError(`No method named "${config}"`);
  179. }
  180. data[config](this);
  181. });
  182. }
  183. }
  184. /**
  185. * Data API implementation
  186. */
  187. EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
  188. const target = SelectorEngine.getElementFromSelector(this);
  189. if (['A', 'AREA'].includes(this.tagName)) {
  190. event.preventDefault();
  191. }
  192. if (index_js.isDisabled(this)) {
  193. return;
  194. }
  195. EventHandler.one(target, EVENT_HIDDEN, () => {
  196. // focus on trigger when it is closed
  197. if (index_js.isVisible(this)) {
  198. this.focus();
  199. }
  200. });
  201. // avoid conflict when clicking a toggler of an offcanvas, while another is open
  202. const alreadyOpen = SelectorEngine.findOne(OPEN_SELECTOR);
  203. if (alreadyOpen && alreadyOpen !== target) {
  204. Offcanvas.getInstance(alreadyOpen).hide();
  205. }
  206. const data = Offcanvas.getOrCreateInstance(target);
  207. data.toggle(this);
  208. });
  209. EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
  210. for (const selector of SelectorEngine.find(OPEN_SELECTOR)) {
  211. Offcanvas.getOrCreateInstance(selector).show();
  212. }
  213. });
  214. EventHandler.on(window, EVENT_RESIZE, () => {
  215. for (const element of SelectorEngine.find('[aria-modal][class*=show][class*=offcanvas-]')) {
  216. if (getComputedStyle(element).position !== 'fixed') {
  217. Offcanvas.getOrCreateInstance(element).hide();
  218. }
  219. }
  220. });
  221. componentFunctions_js.enableDismissTrigger(Offcanvas);
  222. /**
  223. * jQuery
  224. */
  225. index_js.defineJQueryPlugin(Offcanvas);
  226. return Offcanvas;
  227. }));
  228. //# sourceMappingURL=offcanvas.js.map