collapse.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /**
  5. * --------------------------------------------------------------------------
  6. * Bootstrap (v4.0.0-alpha.6): collapse.js
  7. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  8. * --------------------------------------------------------------------------
  9. */
  10. var Collapse = function ($) {
  11. /**
  12. * ------------------------------------------------------------------------
  13. * Constants
  14. * ------------------------------------------------------------------------
  15. */
  16. var NAME = 'collapse';
  17. var VERSION = '4.0.0-alpha.6';
  18. var DATA_KEY = 'bs.collapse';
  19. var EVENT_KEY = '.' + DATA_KEY;
  20. var DATA_API_KEY = '.data-api';
  21. var JQUERY_NO_CONFLICT = $.fn[NAME];
  22. var TRANSITION_DURATION = 600;
  23. var Default = {
  24. toggle: true,
  25. parent: ''
  26. };
  27. var DefaultType = {
  28. toggle: 'boolean',
  29. parent: 'string'
  30. };
  31. var Event = {
  32. SHOW: 'show' + EVENT_KEY,
  33. SHOWN: 'shown' + EVENT_KEY,
  34. HIDE: 'hide' + EVENT_KEY,
  35. HIDDEN: 'hidden' + EVENT_KEY,
  36. CLICK_DATA_API: 'click' + EVENT_KEY + DATA_API_KEY
  37. };
  38. var ClassName = {
  39. SHOW: 'show',
  40. COLLAPSE: 'collapse',
  41. COLLAPSING: 'collapsing',
  42. COLLAPSED: 'collapsed'
  43. };
  44. var Dimension = {
  45. WIDTH: 'width',
  46. HEIGHT: 'height'
  47. };
  48. var Selector = {
  49. ACTIVES: '.card > .show, .card > .collapsing',
  50. DATA_TOGGLE: '[data-toggle="collapse"]'
  51. };
  52. /**
  53. * ------------------------------------------------------------------------
  54. * Class Definition
  55. * ------------------------------------------------------------------------
  56. */
  57. var Collapse = function () {
  58. function Collapse(element, config) {
  59. _classCallCheck(this, Collapse);
  60. this._isTransitioning = false;
  61. this._element = element;
  62. this._config = this._getConfig(config);
  63. this._triggerArray = $.makeArray($('[data-toggle="collapse"][href="#' + element.id + '"],' + ('[data-toggle="collapse"][data-target="#' + element.id + '"]')));
  64. this._parent = this._config.parent ? this._getParent() : null;
  65. if (!this._config.parent) {
  66. this._addAriaAndCollapsedClass(this._element, this._triggerArray);
  67. }
  68. if (this._config.toggle) {
  69. this.toggle();
  70. }
  71. }
  72. // getters
  73. // public
  74. Collapse.prototype.toggle = function toggle() {
  75. if ($(this._element).hasClass(ClassName.SHOW)) {
  76. this.hide();
  77. } else {
  78. this.show();
  79. }
  80. };
  81. Collapse.prototype.show = function show() {
  82. var _this = this;
  83. if (this._isTransitioning) {
  84. throw new Error('Collapse is transitioning');
  85. }
  86. if ($(this._element).hasClass(ClassName.SHOW)) {
  87. return;
  88. }
  89. var actives = void 0;
  90. var activesData = void 0;
  91. if (this._parent) {
  92. actives = $.makeArray($(this._parent).find(Selector.ACTIVES));
  93. if (!actives.length) {
  94. actives = null;
  95. }
  96. }
  97. if (actives) {
  98. activesData = $(actives).data(DATA_KEY);
  99. if (activesData && activesData._isTransitioning) {
  100. return;
  101. }
  102. }
  103. var startEvent = $.Event(Event.SHOW);
  104. $(this._element).trigger(startEvent);
  105. if (startEvent.isDefaultPrevented()) {
  106. return;
  107. }
  108. if (actives) {
  109. Collapse._jQueryInterface.call($(actives), 'hide');
  110. if (!activesData) {
  111. $(actives).data(DATA_KEY, null);
  112. }
  113. }
  114. var dimension = this._getDimension();
  115. $(this._element).removeClass(ClassName.COLLAPSE).addClass(ClassName.COLLAPSING);
  116. this._element.style[dimension] = 0;
  117. this._element.setAttribute('aria-expanded', true);
  118. if (this._triggerArray.length) {
  119. $(this._triggerArray).removeClass(ClassName.COLLAPSED).attr('aria-expanded', true);
  120. }
  121. this.setTransitioning(true);
  122. var complete = function complete() {
  123. $(_this._element).removeClass(ClassName.COLLAPSING).addClass(ClassName.COLLAPSE).addClass(ClassName.SHOW);
  124. _this._element.style[dimension] = '';
  125. _this.setTransitioning(false);
  126. $(_this._element).trigger(Event.SHOWN);
  127. };
  128. if (!Util.supportsTransitionEnd()) {
  129. complete();
  130. return;
  131. }
  132. var capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1);
  133. var scrollSize = 'scroll' + capitalizedDimension;
  134. $(this._element).one(Util.TRANSITION_END, complete).emulateTransitionEnd(TRANSITION_DURATION);
  135. this._element.style[dimension] = this._element[scrollSize] + 'px';
  136. };
  137. Collapse.prototype.hide = function hide() {
  138. var _this2 = this;
  139. if (this._isTransitioning) {
  140. throw new Error('Collapse is transitioning');
  141. }
  142. if (!$(this._element).hasClass(ClassName.SHOW)) {
  143. return;
  144. }
  145. var startEvent = $.Event(Event.HIDE);
  146. $(this._element).trigger(startEvent);
  147. if (startEvent.isDefaultPrevented()) {
  148. return;
  149. }
  150. var dimension = this._getDimension();
  151. var offsetDimension = dimension === Dimension.WIDTH ? 'offsetWidth' : 'offsetHeight';
  152. this._element.style[dimension] = this._element[offsetDimension] + 'px';
  153. Util.reflow(this._element);
  154. $(this._element).addClass(ClassName.COLLAPSING).removeClass(ClassName.COLLAPSE).removeClass(ClassName.SHOW);
  155. this._element.setAttribute('aria-expanded', false);
  156. if (this._triggerArray.length) {
  157. $(this._triggerArray).addClass(ClassName.COLLAPSED).attr('aria-expanded', false);
  158. }
  159. this.setTransitioning(true);
  160. var complete = function complete() {
  161. _this2.setTransitioning(false);
  162. $(_this2._element).removeClass(ClassName.COLLAPSING).addClass(ClassName.COLLAPSE).trigger(Event.HIDDEN);
  163. };
  164. this._element.style[dimension] = '';
  165. if (!Util.supportsTransitionEnd()) {
  166. complete();
  167. return;
  168. }
  169. $(this._element).one(Util.TRANSITION_END, complete).emulateTransitionEnd(TRANSITION_DURATION);
  170. };
  171. Collapse.prototype.setTransitioning = function setTransitioning(isTransitioning) {
  172. this._isTransitioning = isTransitioning;
  173. };
  174. Collapse.prototype.dispose = function dispose() {
  175. $.removeData(this._element, DATA_KEY);
  176. this._config = null;
  177. this._parent = null;
  178. this._element = null;
  179. this._triggerArray = null;
  180. this._isTransitioning = null;
  181. };
  182. // private
  183. Collapse.prototype._getConfig = function _getConfig(config) {
  184. config = $.extend({}, Default, config);
  185. config.toggle = Boolean(config.toggle); // coerce string values
  186. Util.typeCheckConfig(NAME, config, DefaultType);
  187. return config;
  188. };
  189. Collapse.prototype._getDimension = function _getDimension() {
  190. var hasWidth = $(this._element).hasClass(Dimension.WIDTH);
  191. return hasWidth ? Dimension.WIDTH : Dimension.HEIGHT;
  192. };
  193. Collapse.prototype._getParent = function _getParent() {
  194. var _this3 = this;
  195. var parent = $(this._config.parent)[0];
  196. var selector = '[data-toggle="collapse"][data-parent="' + this._config.parent + '"]';
  197. $(parent).find(selector).each(function (i, element) {
  198. _this3._addAriaAndCollapsedClass(Collapse._getTargetFromElement(element), [element]);
  199. });
  200. return parent;
  201. };
  202. Collapse.prototype._addAriaAndCollapsedClass = function _addAriaAndCollapsedClass(element, triggerArray) {
  203. if (element) {
  204. var isOpen = $(element).hasClass(ClassName.SHOW);
  205. element.setAttribute('aria-expanded', isOpen);
  206. if (triggerArray.length) {
  207. $(triggerArray).toggleClass(ClassName.COLLAPSED, !isOpen).attr('aria-expanded', isOpen);
  208. }
  209. }
  210. };
  211. // static
  212. Collapse._getTargetFromElement = function _getTargetFromElement(element) {
  213. var selector = Util.getSelectorFromElement(element);
  214. return selector ? $(selector)[0] : null;
  215. };
  216. Collapse._jQueryInterface = function _jQueryInterface(config) {
  217. return this.each(function () {
  218. var $this = $(this);
  219. var data = $this.data(DATA_KEY);
  220. var _config = $.extend({}, Default, $this.data(), (typeof config === 'undefined' ? 'undefined' : _typeof(config)) === 'object' && config);
  221. if (!data && _config.toggle && /show|hide/.test(config)) {
  222. _config.toggle = false;
  223. }
  224. if (!data) {
  225. data = new Collapse(this, _config);
  226. $this.data(DATA_KEY, data);
  227. }
  228. if (typeof config === 'string') {
  229. if (data[config] === undefined) {
  230. throw new Error('No method named "' + config + '"');
  231. }
  232. data[config]();
  233. }
  234. });
  235. };
  236. _createClass(Collapse, null, [{
  237. key: 'VERSION',
  238. get: function get() {
  239. return VERSION;
  240. }
  241. }, {
  242. key: 'Default',
  243. get: function get() {
  244. return Default;
  245. }
  246. }]);
  247. return Collapse;
  248. }();
  249. /**
  250. * ------------------------------------------------------------------------
  251. * Data Api implementation
  252. * ------------------------------------------------------------------------
  253. */
  254. $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
  255. event.preventDefault();
  256. var target = Collapse._getTargetFromElement(this);
  257. var data = $(target).data(DATA_KEY);
  258. var config = data ? 'toggle' : $(this).data();
  259. Collapse._jQueryInterface.call($(target), config);
  260. });
  261. /**
  262. * ------------------------------------------------------------------------
  263. * jQuery
  264. * ------------------------------------------------------------------------
  265. */
  266. $.fn[NAME] = Collapse._jQueryInterface;
  267. $.fn[NAME].Constructor = Collapse;
  268. $.fn[NAME].noConflict = function () {
  269. $.fn[NAME] = JQUERY_NO_CONFLICT;
  270. return Collapse._jQueryInterface;
  271. };
  272. return Collapse;
  273. }(jQuery);
  274. //# sourceMappingURL=collapse.js.map