collapse.js 13 KB

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