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