collapse.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*!
  2. * Bootstrap collapse.js v4.6.2 (https://getbootstrap.com/)
  3. * Copyright 2011-2022 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('jquery'), require('./util.js')) :
  8. typeof define === 'function' && define.amd ? define(['jquery', './util'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Collapse = factory(global.jQuery, global.Util));
  10. })(this, (function ($, Util) { 'use strict';
  11. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  12. var $__default = /*#__PURE__*/_interopDefaultLegacy($);
  13. var Util__default = /*#__PURE__*/_interopDefaultLegacy(Util);
  14. function _defineProperties(target, props) {
  15. for (var i = 0; i < props.length; i++) {
  16. var descriptor = props[i];
  17. descriptor.enumerable = descriptor.enumerable || false;
  18. descriptor.configurable = true;
  19. if ("value" in descriptor) descriptor.writable = true;
  20. Object.defineProperty(target, descriptor.key, descriptor);
  21. }
  22. }
  23. function _createClass(Constructor, protoProps, staticProps) {
  24. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  25. if (staticProps) _defineProperties(Constructor, staticProps);
  26. Object.defineProperty(Constructor, "prototype", {
  27. writable: false
  28. });
  29. return Constructor;
  30. }
  31. function _extends() {
  32. _extends = Object.assign ? Object.assign.bind() : function (target) {
  33. for (var i = 1; i < arguments.length; i++) {
  34. var source = arguments[i];
  35. for (var key in source) {
  36. if (Object.prototype.hasOwnProperty.call(source, key)) {
  37. target[key] = source[key];
  38. }
  39. }
  40. }
  41. return target;
  42. };
  43. return _extends.apply(this, arguments);
  44. }
  45. /**
  46. * Constants
  47. */
  48. var NAME = 'collapse';
  49. var VERSION = '4.6.2';
  50. var DATA_KEY = 'bs.collapse';
  51. var EVENT_KEY = "." + DATA_KEY;
  52. var DATA_API_KEY = '.data-api';
  53. var JQUERY_NO_CONFLICT = $__default["default"].fn[NAME];
  54. var CLASS_NAME_SHOW = 'show';
  55. var CLASS_NAME_COLLAPSE = 'collapse';
  56. var CLASS_NAME_COLLAPSING = 'collapsing';
  57. var CLASS_NAME_COLLAPSED = 'collapsed';
  58. var DIMENSION_WIDTH = 'width';
  59. var DIMENSION_HEIGHT = 'height';
  60. var EVENT_SHOW = "show" + EVENT_KEY;
  61. var EVENT_SHOWN = "shown" + EVENT_KEY;
  62. var EVENT_HIDE = "hide" + EVENT_KEY;
  63. var EVENT_HIDDEN = "hidden" + EVENT_KEY;
  64. var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY;
  65. var SELECTOR_ACTIVES = '.show, .collapsing';
  66. var SELECTOR_DATA_TOGGLE = '[data-toggle="collapse"]';
  67. var Default = {
  68. toggle: true,
  69. parent: ''
  70. };
  71. var DefaultType = {
  72. toggle: 'boolean',
  73. parent: '(string|element)'
  74. };
  75. /**
  76. * Class definition
  77. */
  78. var Collapse = /*#__PURE__*/function () {
  79. function Collapse(element, config) {
  80. this._isTransitioning = false;
  81. this._element = element;
  82. this._config = this._getConfig(config);
  83. this._triggerArray = [].slice.call(document.querySelectorAll("[data-toggle=\"collapse\"][href=\"#" + element.id + "\"]," + ("[data-toggle=\"collapse\"][data-target=\"#" + element.id + "\"]")));
  84. var toggleList = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLE));
  85. for (var i = 0, len = toggleList.length; i < len; i++) {
  86. var elem = toggleList[i];
  87. var selector = Util__default["default"].getSelectorFromElement(elem);
  88. var filterElement = [].slice.call(document.querySelectorAll(selector)).filter(function (foundElem) {
  89. return foundElem === element;
  90. });
  91. if (selector !== null && filterElement.length > 0) {
  92. this._selector = selector;
  93. this._triggerArray.push(elem);
  94. }
  95. }
  96. this._parent = this._config.parent ? this._getParent() : null;
  97. if (!this._config.parent) {
  98. this._addAriaAndCollapsedClass(this._element, this._triggerArray);
  99. }
  100. if (this._config.toggle) {
  101. this.toggle();
  102. }
  103. } // Getters
  104. var _proto = Collapse.prototype;
  105. // Public
  106. _proto.toggle = function toggle() {
  107. if ($__default["default"](this._element).hasClass(CLASS_NAME_SHOW)) {
  108. this.hide();
  109. } else {
  110. this.show();
  111. }
  112. };
  113. _proto.show = function show() {
  114. var _this = this;
  115. if (this._isTransitioning || $__default["default"](this._element).hasClass(CLASS_NAME_SHOW)) {
  116. return;
  117. }
  118. var actives;
  119. var activesData;
  120. if (this._parent) {
  121. actives = [].slice.call(this._parent.querySelectorAll(SELECTOR_ACTIVES)).filter(function (elem) {
  122. if (typeof _this._config.parent === 'string') {
  123. return elem.getAttribute('data-parent') === _this._config.parent;
  124. }
  125. return elem.classList.contains(CLASS_NAME_COLLAPSE);
  126. });
  127. if (actives.length === 0) {
  128. actives = null;
  129. }
  130. }
  131. if (actives) {
  132. activesData = $__default["default"](actives).not(this._selector).data(DATA_KEY);
  133. if (activesData && activesData._isTransitioning) {
  134. return;
  135. }
  136. }
  137. var startEvent = $__default["default"].Event(EVENT_SHOW);
  138. $__default["default"](this._element).trigger(startEvent);
  139. if (startEvent.isDefaultPrevented()) {
  140. return;
  141. }
  142. if (actives) {
  143. Collapse._jQueryInterface.call($__default["default"](actives).not(this._selector), 'hide');
  144. if (!activesData) {
  145. $__default["default"](actives).data(DATA_KEY, null);
  146. }
  147. }
  148. var dimension = this._getDimension();
  149. $__default["default"](this._element).removeClass(CLASS_NAME_COLLAPSE).addClass(CLASS_NAME_COLLAPSING);
  150. this._element.style[dimension] = 0;
  151. if (this._triggerArray.length) {
  152. $__default["default"](this._triggerArray).removeClass(CLASS_NAME_COLLAPSED).attr('aria-expanded', true);
  153. }
  154. this.setTransitioning(true);
  155. var complete = function complete() {
  156. $__default["default"](_this._element).removeClass(CLASS_NAME_COLLAPSING).addClass(CLASS_NAME_COLLAPSE + " " + CLASS_NAME_SHOW);
  157. _this._element.style[dimension] = '';
  158. _this.setTransitioning(false);
  159. $__default["default"](_this._element).trigger(EVENT_SHOWN);
  160. };
  161. var capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1);
  162. var scrollSize = "scroll" + capitalizedDimension;
  163. var transitionDuration = Util__default["default"].getTransitionDurationFromElement(this._element);
  164. $__default["default"](this._element).one(Util__default["default"].TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
  165. this._element.style[dimension] = this._element[scrollSize] + "px";
  166. };
  167. _proto.hide = function hide() {
  168. var _this2 = this;
  169. if (this._isTransitioning || !$__default["default"](this._element).hasClass(CLASS_NAME_SHOW)) {
  170. return;
  171. }
  172. var startEvent = $__default["default"].Event(EVENT_HIDE);
  173. $__default["default"](this._element).trigger(startEvent);
  174. if (startEvent.isDefaultPrevented()) {
  175. return;
  176. }
  177. var dimension = this._getDimension();
  178. this._element.style[dimension] = this._element.getBoundingClientRect()[dimension] + "px";
  179. Util__default["default"].reflow(this._element);
  180. $__default["default"](this._element).addClass(CLASS_NAME_COLLAPSING).removeClass(CLASS_NAME_COLLAPSE + " " + CLASS_NAME_SHOW);
  181. var triggerArrayLength = this._triggerArray.length;
  182. if (triggerArrayLength > 0) {
  183. for (var i = 0; i < triggerArrayLength; i++) {
  184. var trigger = this._triggerArray[i];
  185. var selector = Util__default["default"].getSelectorFromElement(trigger);
  186. if (selector !== null) {
  187. var $elem = $__default["default"]([].slice.call(document.querySelectorAll(selector)));
  188. if (!$elem.hasClass(CLASS_NAME_SHOW)) {
  189. $__default["default"](trigger).addClass(CLASS_NAME_COLLAPSED).attr('aria-expanded', false);
  190. }
  191. }
  192. }
  193. }
  194. this.setTransitioning(true);
  195. var complete = function complete() {
  196. _this2.setTransitioning(false);
  197. $__default["default"](_this2._element).removeClass(CLASS_NAME_COLLAPSING).addClass(CLASS_NAME_COLLAPSE).trigger(EVENT_HIDDEN);
  198. };
  199. this._element.style[dimension] = '';
  200. var transitionDuration = Util__default["default"].getTransitionDurationFromElement(this._element);
  201. $__default["default"](this._element).one(Util__default["default"].TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
  202. };
  203. _proto.setTransitioning = function setTransitioning(isTransitioning) {
  204. this._isTransitioning = isTransitioning;
  205. };
  206. _proto.dispose = function dispose() {
  207. $__default["default"].removeData(this._element, DATA_KEY);
  208. this._config = null;
  209. this._parent = null;
  210. this._element = null;
  211. this._triggerArray = null;
  212. this._isTransitioning = null;
  213. } // Private
  214. ;
  215. _proto._getConfig = function _getConfig(config) {
  216. config = _extends({}, Default, config);
  217. config.toggle = Boolean(config.toggle); // Coerce string values
  218. Util__default["default"].typeCheckConfig(NAME, config, DefaultType);
  219. return config;
  220. };
  221. _proto._getDimension = function _getDimension() {
  222. var hasWidth = $__default["default"](this._element).hasClass(DIMENSION_WIDTH);
  223. return hasWidth ? DIMENSION_WIDTH : DIMENSION_HEIGHT;
  224. };
  225. _proto._getParent = function _getParent() {
  226. var _this3 = this;
  227. var parent;
  228. if (Util__default["default"].isElement(this._config.parent)) {
  229. parent = this._config.parent; // It's a jQuery object
  230. if (typeof this._config.parent.jquery !== 'undefined') {
  231. parent = this._config.parent[0];
  232. }
  233. } else {
  234. parent = document.querySelector(this._config.parent);
  235. }
  236. var selector = "[data-toggle=\"collapse\"][data-parent=\"" + this._config.parent + "\"]";
  237. var children = [].slice.call(parent.querySelectorAll(selector));
  238. $__default["default"](children).each(function (i, element) {
  239. _this3._addAriaAndCollapsedClass(Collapse._getTargetFromElement(element), [element]);
  240. });
  241. return parent;
  242. };
  243. _proto._addAriaAndCollapsedClass = function _addAriaAndCollapsedClass(element, triggerArray) {
  244. var isOpen = $__default["default"](element).hasClass(CLASS_NAME_SHOW);
  245. if (triggerArray.length) {
  246. $__default["default"](triggerArray).toggleClass(CLASS_NAME_COLLAPSED, !isOpen).attr('aria-expanded', isOpen);
  247. }
  248. } // Static
  249. ;
  250. Collapse._getTargetFromElement = function _getTargetFromElement(element) {
  251. var selector = Util__default["default"].getSelectorFromElement(element);
  252. return selector ? document.querySelector(selector) : null;
  253. };
  254. Collapse._jQueryInterface = function _jQueryInterface(config) {
  255. return this.each(function () {
  256. var $element = $__default["default"](this);
  257. var data = $element.data(DATA_KEY);
  258. var _config = _extends({}, Default, $element.data(), typeof config === 'object' && config ? config : {});
  259. if (!data && _config.toggle && typeof config === 'string' && /show|hide/.test(config)) {
  260. _config.toggle = false;
  261. }
  262. if (!data) {
  263. data = new Collapse(this, _config);
  264. $element.data(DATA_KEY, data);
  265. }
  266. if (typeof config === 'string') {
  267. if (typeof data[config] === 'undefined') {
  268. throw new TypeError("No method named \"" + config + "\"");
  269. }
  270. data[config]();
  271. }
  272. });
  273. };
  274. _createClass(Collapse, null, [{
  275. key: "VERSION",
  276. get: function get() {
  277. return VERSION;
  278. }
  279. }, {
  280. key: "Default",
  281. get: function get() {
  282. return Default;
  283. }
  284. }]);
  285. return Collapse;
  286. }();
  287. /**
  288. * Data API implementation
  289. */
  290. $__default["default"](document).on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
  291. // preventDefault only for <a> elements (which change the URL) not inside the collapsible element
  292. if (event.currentTarget.tagName === 'A') {
  293. event.preventDefault();
  294. }
  295. var $trigger = $__default["default"](this);
  296. var selector = Util__default["default"].getSelectorFromElement(this);
  297. var selectors = [].slice.call(document.querySelectorAll(selector));
  298. $__default["default"](selectors).each(function () {
  299. var $target = $__default["default"](this);
  300. var data = $target.data(DATA_KEY);
  301. var config = data ? 'toggle' : $trigger.data();
  302. Collapse._jQueryInterface.call($target, config);
  303. });
  304. });
  305. /**
  306. * jQuery
  307. */
  308. $__default["default"].fn[NAME] = Collapse._jQueryInterface;
  309. $__default["default"].fn[NAME].Constructor = Collapse;
  310. $__default["default"].fn[NAME].noConflict = function () {
  311. $__default["default"].fn[NAME] = JQUERY_NO_CONFLICT;
  312. return Collapse._jQueryInterface;
  313. };
  314. return Collapse;
  315. }));
  316. //# sourceMappingURL=collapse.js.map