766f721a1234790cc5f77f66e1d003b9dae1ea8d.svn-base 12 KB

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