[d565449] | 1 | /*!
|
---|
| 2 | * Bootstrap collapse.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/index.js')) :
|
---|
| 8 | typeof define === 'function' && define.amd ? define(['./base-component', './dom/event-handler', './dom/selector-engine', './util/index'], factory) :
|
---|
| 9 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Collapse = factory(global.BaseComponent, global.EventHandler, global.SelectorEngine, global.Index));
|
---|
| 10 | })(this, (function (BaseComponent, EventHandler, SelectorEngine, index_js) { 'use strict';
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * --------------------------------------------------------------------------
|
---|
| 14 | * Bootstrap collapse.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 = 'collapse';
|
---|
| 25 | const DATA_KEY = 'bs.collapse';
|
---|
| 26 | const EVENT_KEY = `.${DATA_KEY}`;
|
---|
| 27 | const DATA_API_KEY = '.data-api';
|
---|
| 28 | const EVENT_SHOW = `show${EVENT_KEY}`;
|
---|
| 29 | const EVENT_SHOWN = `shown${EVENT_KEY}`;
|
---|
| 30 | const EVENT_HIDE = `hide${EVENT_KEY}`;
|
---|
| 31 | const EVENT_HIDDEN = `hidden${EVENT_KEY}`;
|
---|
| 32 | const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`;
|
---|
| 33 | const CLASS_NAME_SHOW = 'show';
|
---|
| 34 | const CLASS_NAME_COLLAPSE = 'collapse';
|
---|
| 35 | const CLASS_NAME_COLLAPSING = 'collapsing';
|
---|
| 36 | const CLASS_NAME_COLLAPSED = 'collapsed';
|
---|
| 37 | const CLASS_NAME_DEEPER_CHILDREN = `:scope .${CLASS_NAME_COLLAPSE} .${CLASS_NAME_COLLAPSE}`;
|
---|
| 38 | const CLASS_NAME_HORIZONTAL = 'collapse-horizontal';
|
---|
| 39 | const WIDTH = 'width';
|
---|
| 40 | const HEIGHT = 'height';
|
---|
| 41 | const SELECTOR_ACTIVES = '.collapse.show, .collapse.collapsing';
|
---|
| 42 | const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="collapse"]';
|
---|
| 43 | const Default = {
|
---|
| 44 | parent: null,
|
---|
| 45 | toggle: true
|
---|
| 46 | };
|
---|
| 47 | const DefaultType = {
|
---|
| 48 | parent: '(null|element)',
|
---|
| 49 | toggle: 'boolean'
|
---|
| 50 | };
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * Class definition
|
---|
| 54 | */
|
---|
| 55 |
|
---|
| 56 | class Collapse extends BaseComponent {
|
---|
| 57 | constructor(element, config) {
|
---|
| 58 | super(element, config);
|
---|
| 59 | this._isTransitioning = false;
|
---|
| 60 | this._triggerArray = [];
|
---|
| 61 | const toggleList = SelectorEngine.find(SELECTOR_DATA_TOGGLE);
|
---|
| 62 | for (const elem of toggleList) {
|
---|
| 63 | const selector = SelectorEngine.getSelectorFromElement(elem);
|
---|
| 64 | const filterElement = SelectorEngine.find(selector).filter(foundElement => foundElement === this._element);
|
---|
| 65 | if (selector !== null && filterElement.length) {
|
---|
| 66 | this._triggerArray.push(elem);
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | this._initializeChildren();
|
---|
| 70 | if (!this._config.parent) {
|
---|
| 71 | this._addAriaAndCollapsedClass(this._triggerArray, this._isShown());
|
---|
| 72 | }
|
---|
| 73 | if (this._config.toggle) {
|
---|
| 74 | this.toggle();
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | // Getters
|
---|
| 79 | static get Default() {
|
---|
| 80 | return Default;
|
---|
| 81 | }
|
---|
| 82 | static get DefaultType() {
|
---|
| 83 | return DefaultType;
|
---|
| 84 | }
|
---|
| 85 | static get NAME() {
|
---|
| 86 | return NAME;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | // Public
|
---|
| 90 | toggle() {
|
---|
| 91 | if (this._isShown()) {
|
---|
| 92 | this.hide();
|
---|
| 93 | } else {
|
---|
| 94 | this.show();
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | show() {
|
---|
| 98 | if (this._isTransitioning || this._isShown()) {
|
---|
| 99 | return;
|
---|
| 100 | }
|
---|
| 101 | let activeChildren = [];
|
---|
| 102 |
|
---|
| 103 | // find active children
|
---|
| 104 | if (this._config.parent) {
|
---|
| 105 | activeChildren = this._getFirstLevelChildren(SELECTOR_ACTIVES).filter(element => element !== this._element).map(element => Collapse.getOrCreateInstance(element, {
|
---|
| 106 | toggle: false
|
---|
| 107 | }));
|
---|
| 108 | }
|
---|
| 109 | if (activeChildren.length && activeChildren[0]._isTransitioning) {
|
---|
| 110 | return;
|
---|
| 111 | }
|
---|
| 112 | const startEvent = EventHandler.trigger(this._element, EVENT_SHOW);
|
---|
| 113 | if (startEvent.defaultPrevented) {
|
---|
| 114 | return;
|
---|
| 115 | }
|
---|
| 116 | for (const activeInstance of activeChildren) {
|
---|
| 117 | activeInstance.hide();
|
---|
| 118 | }
|
---|
| 119 | const dimension = this._getDimension();
|
---|
| 120 | this._element.classList.remove(CLASS_NAME_COLLAPSE);
|
---|
| 121 | this._element.classList.add(CLASS_NAME_COLLAPSING);
|
---|
| 122 | this._element.style[dimension] = 0;
|
---|
| 123 | this._addAriaAndCollapsedClass(this._triggerArray, true);
|
---|
| 124 | this._isTransitioning = true;
|
---|
| 125 | const complete = () => {
|
---|
| 126 | this._isTransitioning = false;
|
---|
| 127 | this._element.classList.remove(CLASS_NAME_COLLAPSING);
|
---|
| 128 | this._element.classList.add(CLASS_NAME_COLLAPSE, CLASS_NAME_SHOW);
|
---|
| 129 | this._element.style[dimension] = '';
|
---|
| 130 | EventHandler.trigger(this._element, EVENT_SHOWN);
|
---|
| 131 | };
|
---|
| 132 | const capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1);
|
---|
| 133 | const scrollSize = `scroll${capitalizedDimension}`;
|
---|
| 134 | this._queueCallback(complete, this._element, true);
|
---|
| 135 | this._element.style[dimension] = `${this._element[scrollSize]}px`;
|
---|
| 136 | }
|
---|
| 137 | hide() {
|
---|
| 138 | if (this._isTransitioning || !this._isShown()) {
|
---|
| 139 | return;
|
---|
| 140 | }
|
---|
| 141 | const startEvent = EventHandler.trigger(this._element, EVENT_HIDE);
|
---|
| 142 | if (startEvent.defaultPrevented) {
|
---|
| 143 | return;
|
---|
| 144 | }
|
---|
| 145 | const dimension = this._getDimension();
|
---|
| 146 | this._element.style[dimension] = `${this._element.getBoundingClientRect()[dimension]}px`;
|
---|
| 147 | index_js.reflow(this._element);
|
---|
| 148 | this._element.classList.add(CLASS_NAME_COLLAPSING);
|
---|
| 149 | this._element.classList.remove(CLASS_NAME_COLLAPSE, CLASS_NAME_SHOW);
|
---|
| 150 | for (const trigger of this._triggerArray) {
|
---|
| 151 | const element = SelectorEngine.getElementFromSelector(trigger);
|
---|
| 152 | if (element && !this._isShown(element)) {
|
---|
| 153 | this._addAriaAndCollapsedClass([trigger], false);
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | this._isTransitioning = true;
|
---|
| 157 | const complete = () => {
|
---|
| 158 | this._isTransitioning = false;
|
---|
| 159 | this._element.classList.remove(CLASS_NAME_COLLAPSING);
|
---|
| 160 | this._element.classList.add(CLASS_NAME_COLLAPSE);
|
---|
| 161 | EventHandler.trigger(this._element, EVENT_HIDDEN);
|
---|
| 162 | };
|
---|
| 163 | this._element.style[dimension] = '';
|
---|
| 164 | this._queueCallback(complete, this._element, true);
|
---|
| 165 | }
|
---|
| 166 | _isShown(element = this._element) {
|
---|
| 167 | return element.classList.contains(CLASS_NAME_SHOW);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | // Private
|
---|
| 171 | _configAfterMerge(config) {
|
---|
| 172 | config.toggle = Boolean(config.toggle); // Coerce string values
|
---|
| 173 | config.parent = index_js.getElement(config.parent);
|
---|
| 174 | return config;
|
---|
| 175 | }
|
---|
| 176 | _getDimension() {
|
---|
| 177 | return this._element.classList.contains(CLASS_NAME_HORIZONTAL) ? WIDTH : HEIGHT;
|
---|
| 178 | }
|
---|
| 179 | _initializeChildren() {
|
---|
| 180 | if (!this._config.parent) {
|
---|
| 181 | return;
|
---|
| 182 | }
|
---|
| 183 | const children = this._getFirstLevelChildren(SELECTOR_DATA_TOGGLE);
|
---|
| 184 | for (const element of children) {
|
---|
| 185 | const selected = SelectorEngine.getElementFromSelector(element);
|
---|
| 186 | if (selected) {
|
---|
| 187 | this._addAriaAndCollapsedClass([element], this._isShown(selected));
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | _getFirstLevelChildren(selector) {
|
---|
| 192 | const children = SelectorEngine.find(CLASS_NAME_DEEPER_CHILDREN, this._config.parent);
|
---|
| 193 | // remove children if greater depth
|
---|
| 194 | return SelectorEngine.find(selector, this._config.parent).filter(element => !children.includes(element));
|
---|
| 195 | }
|
---|
| 196 | _addAriaAndCollapsedClass(triggerArray, isOpen) {
|
---|
| 197 | if (!triggerArray.length) {
|
---|
| 198 | return;
|
---|
| 199 | }
|
---|
| 200 | for (const element of triggerArray) {
|
---|
| 201 | element.classList.toggle(CLASS_NAME_COLLAPSED, !isOpen);
|
---|
| 202 | element.setAttribute('aria-expanded', isOpen);
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | // Static
|
---|
| 207 | static jQueryInterface(config) {
|
---|
| 208 | const _config = {};
|
---|
| 209 | if (typeof config === 'string' && /show|hide/.test(config)) {
|
---|
| 210 | _config.toggle = false;
|
---|
| 211 | }
|
---|
| 212 | return this.each(function () {
|
---|
| 213 | const data = Collapse.getOrCreateInstance(this, _config);
|
---|
| 214 | if (typeof config === 'string') {
|
---|
| 215 | if (typeof data[config] === 'undefined') {
|
---|
| 216 | throw new TypeError(`No method named "${config}"`);
|
---|
| 217 | }
|
---|
| 218 | data[config]();
|
---|
| 219 | }
|
---|
| 220 | });
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | /**
|
---|
| 225 | * Data API implementation
|
---|
| 226 | */
|
---|
| 227 |
|
---|
| 228 | EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
|
---|
| 229 | // preventDefault only for <a> elements (which change the URL) not inside the collapsible element
|
---|
| 230 | if (event.target.tagName === 'A' || event.delegateTarget && event.delegateTarget.tagName === 'A') {
|
---|
| 231 | event.preventDefault();
|
---|
| 232 | }
|
---|
| 233 | for (const element of SelectorEngine.getMultipleElementsFromSelector(this)) {
|
---|
| 234 | Collapse.getOrCreateInstance(element, {
|
---|
| 235 | toggle: false
|
---|
| 236 | }).toggle();
|
---|
| 237 | }
|
---|
| 238 | });
|
---|
| 239 |
|
---|
| 240 | /**
|
---|
| 241 | * jQuery
|
---|
| 242 | */
|
---|
| 243 |
|
---|
| 244 | index_js.defineJQueryPlugin(Collapse);
|
---|
| 245 |
|
---|
| 246 | return Collapse;
|
---|
| 247 |
|
---|
| 248 | }));
|
---|
| 249 | //# sourceMappingURL=collapse.js.map
|
---|