collapse.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*!
  2. * Bootstrap collapse.js v5.0.2 (https://getbootstrap.com/)
  3. * Copyright 2011-2021 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('./dom/selector-engine.js'), require('./dom/data.js'), require('./dom/event-handler.js'), require('./dom/manipulator.js'), require('./base-component.js')) :
  8. typeof define === 'function' && define.amd ? define(['./dom/selector-engine', './dom/data', './dom/event-handler', './dom/manipulator', './base-component'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Collapse = factory(global.SelectorEngine, global.Data, global.EventHandler, global.Manipulator, global.Base));
  10. }(this, (function (SelectorEngine, Data, EventHandler, Manipulator, BaseComponent) { 'use strict';
  11. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  12. var SelectorEngine__default = /*#__PURE__*/_interopDefaultLegacy(SelectorEngine);
  13. var Data__default = /*#__PURE__*/_interopDefaultLegacy(Data);
  14. var EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
  15. var Manipulator__default = /*#__PURE__*/_interopDefaultLegacy(Manipulator);
  16. var BaseComponent__default = /*#__PURE__*/_interopDefaultLegacy(BaseComponent);
  17. const toType = obj => {
  18. if (obj === null || obj === undefined) {
  19. return `${obj}`;
  20. }
  21. return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
  22. };
  23. const getSelector = element => {
  24. let selector = element.getAttribute('data-bs-target');
  25. if (!selector || selector === '#') {
  26. let hrefAttr = element.getAttribute('href'); // The only valid content that could double as a selector are IDs or classes,
  27. // so everything starting with `#` or `.`. If a "real" URL is used as the selector,
  28. // `document.querySelector` will rightfully complain it is invalid.
  29. // See https://github.com/twbs/bootstrap/issues/32273
  30. if (!hrefAttr || !hrefAttr.includes('#') && !hrefAttr.startsWith('.')) {
  31. return null;
  32. } // Just in case some CMS puts out a full URL with the anchor appended
  33. if (hrefAttr.includes('#') && !hrefAttr.startsWith('#')) {
  34. hrefAttr = `#${hrefAttr.split('#')[1]}`;
  35. }
  36. selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null;
  37. }
  38. return selector;
  39. };
  40. const getSelectorFromElement = element => {
  41. const selector = getSelector(element);
  42. if (selector) {
  43. return document.querySelector(selector) ? selector : null;
  44. }
  45. return null;
  46. };
  47. const getElementFromSelector = element => {
  48. const selector = getSelector(element);
  49. return selector ? document.querySelector(selector) : null;
  50. };
  51. const isElement = obj => {
  52. if (!obj || typeof obj !== 'object') {
  53. return false;
  54. }
  55. if (typeof obj.jquery !== 'undefined') {
  56. obj = obj[0];
  57. }
  58. return typeof obj.nodeType !== 'undefined';
  59. };
  60. const getElement = obj => {
  61. if (isElement(obj)) {
  62. // it's a jQuery object or a node element
  63. return obj.jquery ? obj[0] : obj;
  64. }
  65. if (typeof obj === 'string' && obj.length > 0) {
  66. return SelectorEngine__default['default'].findOne(obj);
  67. }
  68. return null;
  69. };
  70. const typeCheckConfig = (componentName, config, configTypes) => {
  71. Object.keys(configTypes).forEach(property => {
  72. const expectedTypes = configTypes[property];
  73. const value = config[property];
  74. const valueType = value && isElement(value) ? 'element' : toType(value);
  75. if (!new RegExp(expectedTypes).test(valueType)) {
  76. throw new TypeError(`${componentName.toUpperCase()}: Option "${property}" provided type "${valueType}" but expected type "${expectedTypes}".`);
  77. }
  78. });
  79. };
  80. const reflow = element => element.offsetHeight;
  81. const getjQuery = () => {
  82. const {
  83. jQuery
  84. } = window;
  85. if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
  86. return jQuery;
  87. }
  88. return null;
  89. };
  90. const DOMContentLoadedCallbacks = [];
  91. const onDOMContentLoaded = callback => {
  92. if (document.readyState === 'loading') {
  93. // add listener on the first call when the document is in loading state
  94. if (!DOMContentLoadedCallbacks.length) {
  95. document.addEventListener('DOMContentLoaded', () => {
  96. DOMContentLoadedCallbacks.forEach(callback => callback());
  97. });
  98. }
  99. DOMContentLoadedCallbacks.push(callback);
  100. } else {
  101. callback();
  102. }
  103. };
  104. const defineJQueryPlugin = plugin => {
  105. onDOMContentLoaded(() => {
  106. const $ = getjQuery();
  107. /* istanbul ignore if */
  108. if ($) {
  109. const name = plugin.NAME;
  110. const JQUERY_NO_CONFLICT = $.fn[name];
  111. $.fn[name] = plugin.jQueryInterface;
  112. $.fn[name].Constructor = plugin;
  113. $.fn[name].noConflict = () => {
  114. $.fn[name] = JQUERY_NO_CONFLICT;
  115. return plugin.jQueryInterface;
  116. };
  117. }
  118. });
  119. };
  120. /**
  121. * --------------------------------------------------------------------------
  122. * Bootstrap (v5.0.2): collapse.js
  123. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  124. * --------------------------------------------------------------------------
  125. */
  126. /**
  127. * ------------------------------------------------------------------------
  128. * Constants
  129. * ------------------------------------------------------------------------
  130. */
  131. const NAME = 'collapse';
  132. const DATA_KEY = 'bs.collapse';
  133. const EVENT_KEY = `.${DATA_KEY}`;
  134. const DATA_API_KEY = '.data-api';
  135. const Default = {
  136. toggle: true,
  137. parent: ''
  138. };
  139. const DefaultType = {
  140. toggle: 'boolean',
  141. parent: '(string|element)'
  142. };
  143. const EVENT_SHOW = `show${EVENT_KEY}`;
  144. const EVENT_SHOWN = `shown${EVENT_KEY}`;
  145. const EVENT_HIDE = `hide${EVENT_KEY}`;
  146. const EVENT_HIDDEN = `hidden${EVENT_KEY}`;
  147. const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`;
  148. const CLASS_NAME_SHOW = 'show';
  149. const CLASS_NAME_COLLAPSE = 'collapse';
  150. const CLASS_NAME_COLLAPSING = 'collapsing';
  151. const CLASS_NAME_COLLAPSED = 'collapsed';
  152. const WIDTH = 'width';
  153. const HEIGHT = 'height';
  154. const SELECTOR_ACTIVES = '.show, .collapsing';
  155. const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="collapse"]';
  156. /**
  157. * ------------------------------------------------------------------------
  158. * Class Definition
  159. * ------------------------------------------------------------------------
  160. */
  161. class Collapse extends BaseComponent__default['default'] {
  162. constructor(element, config) {
  163. super(element);
  164. this._isTransitioning = false;
  165. this._config = this._getConfig(config);
  166. this._triggerArray = SelectorEngine__default['default'].find(`${SELECTOR_DATA_TOGGLE}[href="#${this._element.id}"],` + `${SELECTOR_DATA_TOGGLE}[data-bs-target="#${this._element.id}"]`);
  167. const toggleList = SelectorEngine__default['default'].find(SELECTOR_DATA_TOGGLE);
  168. for (let i = 0, len = toggleList.length; i < len; i++) {
  169. const elem = toggleList[i];
  170. const selector = getSelectorFromElement(elem);
  171. const filterElement = SelectorEngine__default['default'].find(selector).filter(foundElem => foundElem === this._element);
  172. if (selector !== null && filterElement.length) {
  173. this._selector = selector;
  174. this._triggerArray.push(elem);
  175. }
  176. }
  177. this._parent = this._config.parent ? this._getParent() : null;
  178. if (!this._config.parent) {
  179. this._addAriaAndCollapsedClass(this._element, this._triggerArray);
  180. }
  181. if (this._config.toggle) {
  182. this.toggle();
  183. }
  184. } // Getters
  185. static get Default() {
  186. return Default;
  187. }
  188. static get NAME() {
  189. return NAME;
  190. } // Public
  191. toggle() {
  192. if (this._element.classList.contains(CLASS_NAME_SHOW)) {
  193. this.hide();
  194. } else {
  195. this.show();
  196. }
  197. }
  198. show() {
  199. if (this._isTransitioning || this._element.classList.contains(CLASS_NAME_SHOW)) {
  200. return;
  201. }
  202. let actives;
  203. let activesData;
  204. if (this._parent) {
  205. actives = SelectorEngine__default['default'].find(SELECTOR_ACTIVES, this._parent).filter(elem => {
  206. if (typeof this._config.parent === 'string') {
  207. return elem.getAttribute('data-bs-parent') === this._config.parent;
  208. }
  209. return elem.classList.contains(CLASS_NAME_COLLAPSE);
  210. });
  211. if (actives.length === 0) {
  212. actives = null;
  213. }
  214. }
  215. const container = SelectorEngine__default['default'].findOne(this._selector);
  216. if (actives) {
  217. const tempActiveData = actives.find(elem => container !== elem);
  218. activesData = tempActiveData ? Collapse.getInstance(tempActiveData) : null;
  219. if (activesData && activesData._isTransitioning) {
  220. return;
  221. }
  222. }
  223. const startEvent = EventHandler__default['default'].trigger(this._element, EVENT_SHOW);
  224. if (startEvent.defaultPrevented) {
  225. return;
  226. }
  227. if (actives) {
  228. actives.forEach(elemActive => {
  229. if (container !== elemActive) {
  230. Collapse.collapseInterface(elemActive, 'hide');
  231. }
  232. if (!activesData) {
  233. Data__default['default'].set(elemActive, DATA_KEY, null);
  234. }
  235. });
  236. }
  237. const dimension = this._getDimension();
  238. this._element.classList.remove(CLASS_NAME_COLLAPSE);
  239. this._element.classList.add(CLASS_NAME_COLLAPSING);
  240. this._element.style[dimension] = 0;
  241. if (this._triggerArray.length) {
  242. this._triggerArray.forEach(element => {
  243. element.classList.remove(CLASS_NAME_COLLAPSED);
  244. element.setAttribute('aria-expanded', true);
  245. });
  246. }
  247. this.setTransitioning(true);
  248. const complete = () => {
  249. this._element.classList.remove(CLASS_NAME_COLLAPSING);
  250. this._element.classList.add(CLASS_NAME_COLLAPSE, CLASS_NAME_SHOW);
  251. this._element.style[dimension] = '';
  252. this.setTransitioning(false);
  253. EventHandler__default['default'].trigger(this._element, EVENT_SHOWN);
  254. };
  255. const capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1);
  256. const scrollSize = `scroll${capitalizedDimension}`;
  257. this._queueCallback(complete, this._element, true);
  258. this._element.style[dimension] = `${this._element[scrollSize]}px`;
  259. }
  260. hide() {
  261. if (this._isTransitioning || !this._element.classList.contains(CLASS_NAME_SHOW)) {
  262. return;
  263. }
  264. const startEvent = EventHandler__default['default'].trigger(this._element, EVENT_HIDE);
  265. if (startEvent.defaultPrevented) {
  266. return;
  267. }
  268. const dimension = this._getDimension();
  269. this._element.style[dimension] = `${this._element.getBoundingClientRect()[dimension]}px`;
  270. reflow(this._element);
  271. this._element.classList.add(CLASS_NAME_COLLAPSING);
  272. this._element.classList.remove(CLASS_NAME_COLLAPSE, CLASS_NAME_SHOW);
  273. const triggerArrayLength = this._triggerArray.length;
  274. if (triggerArrayLength > 0) {
  275. for (let i = 0; i < triggerArrayLength; i++) {
  276. const trigger = this._triggerArray[i];
  277. const elem = getElementFromSelector(trigger);
  278. if (elem && !elem.classList.contains(CLASS_NAME_SHOW)) {
  279. trigger.classList.add(CLASS_NAME_COLLAPSED);
  280. trigger.setAttribute('aria-expanded', false);
  281. }
  282. }
  283. }
  284. this.setTransitioning(true);
  285. const complete = () => {
  286. this.setTransitioning(false);
  287. this._element.classList.remove(CLASS_NAME_COLLAPSING);
  288. this._element.classList.add(CLASS_NAME_COLLAPSE);
  289. EventHandler__default['default'].trigger(this._element, EVENT_HIDDEN);
  290. };
  291. this._element.style[dimension] = '';
  292. this._queueCallback(complete, this._element, true);
  293. }
  294. setTransitioning(isTransitioning) {
  295. this._isTransitioning = isTransitioning;
  296. } // Private
  297. _getConfig(config) {
  298. config = { ...Default,
  299. ...config
  300. };
  301. config.toggle = Boolean(config.toggle); // Coerce string values
  302. typeCheckConfig(NAME, config, DefaultType);
  303. return config;
  304. }
  305. _getDimension() {
  306. return this._element.classList.contains(WIDTH) ? WIDTH : HEIGHT;
  307. }
  308. _getParent() {
  309. let {
  310. parent
  311. } = this._config;
  312. parent = getElement(parent);
  313. const selector = `${SELECTOR_DATA_TOGGLE}[data-bs-parent="${parent}"]`;
  314. SelectorEngine__default['default'].find(selector, parent).forEach(element => {
  315. const selected = getElementFromSelector(element);
  316. this._addAriaAndCollapsedClass(selected, [element]);
  317. });
  318. return parent;
  319. }
  320. _addAriaAndCollapsedClass(element, triggerArray) {
  321. if (!element || !triggerArray.length) {
  322. return;
  323. }
  324. const isOpen = element.classList.contains(CLASS_NAME_SHOW);
  325. triggerArray.forEach(elem => {
  326. if (isOpen) {
  327. elem.classList.remove(CLASS_NAME_COLLAPSED);
  328. } else {
  329. elem.classList.add(CLASS_NAME_COLLAPSED);
  330. }
  331. elem.setAttribute('aria-expanded', isOpen);
  332. });
  333. } // Static
  334. static collapseInterface(element, config) {
  335. let data = Collapse.getInstance(element);
  336. const _config = { ...Default,
  337. ...Manipulator__default['default'].getDataAttributes(element),
  338. ...(typeof config === 'object' && config ? config : {})
  339. };
  340. if (!data && _config.toggle && typeof config === 'string' && /show|hide/.test(config)) {
  341. _config.toggle = false;
  342. }
  343. if (!data) {
  344. data = new Collapse(element, _config);
  345. }
  346. if (typeof config === 'string') {
  347. if (typeof data[config] === 'undefined') {
  348. throw new TypeError(`No method named "${config}"`);
  349. }
  350. data[config]();
  351. }
  352. }
  353. static jQueryInterface(config) {
  354. return this.each(function () {
  355. Collapse.collapseInterface(this, config);
  356. });
  357. }
  358. }
  359. /**
  360. * ------------------------------------------------------------------------
  361. * Data Api implementation
  362. * ------------------------------------------------------------------------
  363. */
  364. EventHandler__default['default'].on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
  365. // preventDefault only for <a> elements (which change the URL) not inside the collapsible element
  366. if (event.target.tagName === 'A' || event.delegateTarget && event.delegateTarget.tagName === 'A') {
  367. event.preventDefault();
  368. }
  369. const triggerData = Manipulator__default['default'].getDataAttributes(this);
  370. const selector = getSelectorFromElement(this);
  371. const selectorElements = SelectorEngine__default['default'].find(selector);
  372. selectorElements.forEach(element => {
  373. const data = Collapse.getInstance(element);
  374. let config;
  375. if (data) {
  376. // update parent attribute
  377. if (data._parent === null && typeof triggerData.parent === 'string') {
  378. data._config.parent = triggerData.parent;
  379. data._parent = data._getParent();
  380. }
  381. config = 'toggle';
  382. } else {
  383. config = triggerData;
  384. }
  385. Collapse.collapseInterface(element, config);
  386. });
  387. });
  388. /**
  389. * ------------------------------------------------------------------------
  390. * jQuery
  391. * ------------------------------------------------------------------------
  392. * add .Collapse to jQuery only if jQuery is present
  393. */
  394. defineJQueryPlugin(Collapse);
  395. return Collapse;
  396. })));
  397. //# sourceMappingURL=collapse.js.map