collapse.js 13 KB

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