bootstrap-collapse.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import jQuery from 'jquery';
  2. /* ========================================================================
  3. * Bootstrap: collapse.js v3.4.0
  4. * http://getbootstrap.com/javascript/#collapse
  5. * ========================================================================
  6. * Copyright 2011-2016 Twitter, Inc.
  7. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  8. * ======================================================================== */
  9. /* jshint latedef: false */
  10. +(function($) {
  11. 'use strict';
  12. // COLLAPSE PUBLIC CLASS DEFINITION
  13. // ================================
  14. var Collapse = function(element, options) {
  15. this.$element = $(element);
  16. this.options = $.extend({}, Collapse.DEFAULTS, options);
  17. this.$trigger = $('[data-toggle="collapse"][href="#' + element.id + '"],' +
  18. '[data-toggle="collapse"][data-target="#' + element.id + '"]');
  19. this.transitioning = null;
  20. if (this.options.parent) {
  21. this.$parent = this.getParent();
  22. } else {
  23. this.addAriaAndCollapsedClass(this.$element, this.$trigger);
  24. }
  25. if (this.options.toggle) this.toggle();
  26. };
  27. Collapse.VERSION = '3.4.0';
  28. Collapse.TRANSITION_DURATION = 350;
  29. Collapse.DEFAULTS = {
  30. toggle: true
  31. };
  32. Collapse.prototype.dimension = function() {
  33. var hasWidth = this.$element.hasClass('width');
  34. return hasWidth ? 'width' : 'height';
  35. };
  36. Collapse.prototype.show = function() {
  37. if (this.transitioning || this.$element.hasClass('in')) return;
  38. var activesData;
  39. var actives = this.$parent && this.$parent.children('.panel').children('.in, .collapsing');
  40. if (actives && actives.length) {
  41. activesData = actives.data('bs.collapse');
  42. if (activesData && activesData.transitioning) return;
  43. }
  44. var startEvent = $.Event('show.bs.collapse');
  45. this.$element.trigger(startEvent);
  46. if (startEvent.isDefaultPrevented()) return;
  47. if (actives && actives.length) {
  48. Plugin.call(actives, 'hide');
  49. activesData || actives.data('bs.collapse', null);
  50. }
  51. var dimension = this.dimension();
  52. this.$element
  53. .removeClass('collapse')
  54. .addClass('collapsing')[dimension](0)
  55. .attr('aria-expanded', true);
  56. this.$trigger
  57. .removeClass('collapsed')
  58. .attr('aria-expanded', true);
  59. this.transitioning = 1;
  60. var complete = function() {
  61. this.$element
  62. .removeClass('collapsing')
  63. .addClass('collapse in')[dimension]('');
  64. this.transitioning = 0;
  65. this.$element
  66. .trigger('shown.bs.collapse');
  67. };
  68. if (!$.support.transition) return complete.call(this);
  69. var scrollSize = $.camelCase(['scroll', dimension].join('-'));
  70. this.$element
  71. .one('bsTransitionEnd', $.proxy(complete, this))
  72. .emulateTransitionEnd(Collapse.TRANSITION_DURATION)[dimension](this.$element[0][scrollSize]);
  73. };
  74. Collapse.prototype.hide = function() {
  75. if (this.transitioning || !this.$element.hasClass('in')) return;
  76. var startEvent = $.Event('hide.bs.collapse');
  77. this.$element.trigger(startEvent);
  78. if (startEvent.isDefaultPrevented()) return;
  79. var dimension = this.dimension();
  80. this.$element[dimension](this.$element[dimension]())[0].offsetHeight;
  81. this.$element
  82. .addClass('collapsing')
  83. .removeClass('collapse in')
  84. .attr('aria-expanded', false);
  85. this.$trigger
  86. .addClass('collapsed')
  87. .attr('aria-expanded', false);
  88. this.transitioning = 1;
  89. var complete = function() {
  90. this.transitioning = 0;
  91. this.$element
  92. .removeClass('collapsing')
  93. .addClass('collapse')
  94. .trigger('hidden.bs.collapse');
  95. };
  96. if (!$.support.transition) return complete.call(this);
  97. this.$element[dimension](0)
  98. .one('bsTransitionEnd', $.proxy(complete, this))
  99. .emulateTransitionEnd(Collapse.TRANSITION_DURATION);
  100. };
  101. Collapse.prototype.toggle = function() {
  102. this[this.$element.hasClass('in') ? 'hide' : 'show']();
  103. };
  104. Collapse.prototype.getParent = function() {
  105. return $(this.options.parent)
  106. .find('[data-toggle="collapse"][data-parent="' + this.options.parent + '"]')
  107. .each($.proxy(function(i, element) {
  108. var $element = $(element);
  109. this.addAriaAndCollapsedClass(getTargetFromTrigger($element), $element);
  110. }, this))
  111. .end();
  112. };
  113. Collapse.prototype.addAriaAndCollapsedClass = function($element, $trigger) {
  114. var isOpen = $element.hasClass('in');
  115. $element.attr('aria-expanded', isOpen);
  116. $trigger
  117. .toggleClass('collapsed', !isOpen)
  118. .attr('aria-expanded', isOpen);
  119. };
  120. function getTargetFromTrigger($trigger) {
  121. var href;
  122. var target = $trigger.attr('data-target') ||
  123. (href = $trigger.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, ''); // strip for ie7
  124. return $(target);
  125. }
  126. // COLLAPSE PLUGIN DEFINITION
  127. // ==========================
  128. function Plugin(option) {
  129. return this.each(function() {
  130. var $this = $(this);
  131. var data = $this.data('bs.collapse');
  132. var options = $.extend({}, Collapse.DEFAULTS, $this.data(), typeof option === 'object' && option);
  133. if (!data && options.toggle && /show|hide/.test(option)) options.toggle = false;
  134. if (!data) $this.data('bs.collapse', (data = new Collapse(this, options)));
  135. if (typeof option === 'string') data[option]();
  136. });
  137. }
  138. var old = $.fn.collapse;
  139. $.fn.collapse = Plugin;
  140. $.fn.collapse.Constructor = Collapse;
  141. // COLLAPSE NO CONFLICT
  142. // ====================
  143. $.fn.collapse.noConflict = function() {
  144. $.fn.collapse = old;
  145. return this;
  146. };
  147. // COLLAPSE DATA-API
  148. // =================
  149. $(document).on('click.bs.collapse.data-api', '[data-toggle="collapse"]', function(e) {
  150. var $this = $(this);
  151. if (!$this.attr('data-target')) e.preventDefault();
  152. var $target = getTargetFromTrigger($this);
  153. var data = $target.data('bs.collapse');
  154. var option = data ? 'toggle' : $this.data();
  155. Plugin.call($target, option);
  156. });
  157. }(jQuery));