collapse.js 14 KB

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