0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-04 16:24:22 +01:00
Bootstrap/js/dist/collapse.js

429 lines
13 KiB
JavaScript
Raw Normal View History

2018-11-13 07:41:12 +01:00
/*!
* Bootstrap collapse.js v4.3.0 (https://getbootstrap.com/)
2019-01-04 17:29:45 +01:00
* Copyright 2011-2019 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
2018-11-13 07:41:12 +01:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/
2018-07-24 02:51:14 +02:00
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery'), require('./util.js')) :
typeof define === 'function' && define.amd ? define(['jquery', './util.js'], factory) :
2019-01-04 17:29:45 +01:00
(global = global || self, global.Collapse = factory(global.jQuery, global.Util));
}(this, function ($, Util) { 'use strict';
2018-07-24 02:51:14 +02:00
$ = $ && $.hasOwnProperty('default') ? $['default'] : $;
Util = Util && Util.hasOwnProperty('default') ? Util['default'] : Util;
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);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
2018-03-31 22:59:37 +02:00
2018-07-24 02:51:14 +02:00
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
2017-12-23 01:21:54 +01:00
2018-07-24 02:51:14 +02:00
if (typeof Object.getOwnPropertySymbols === 'function') {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function (key) {
_defineProperty(target, key, source[key]);
});
}
2017-09-06 06:05:12 +02:00
2018-07-24 02:51:14 +02:00
return target;
}
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
var NAME = 'collapse';
var VERSION = '4.3.0';
2018-11-13 07:41:12 +01:00
var DATA_KEY = 'bs.collapse';
var EVENT_KEY = "." + DATA_KEY;
var DATA_API_KEY = '.data-api';
var JQUERY_NO_CONFLICT = $.fn[NAME];
var Default = {
toggle: true,
parent: ''
};
var DefaultType = {
toggle: 'boolean',
parent: '(string|element)'
};
var Event = {
SHOW: "show" + EVENT_KEY,
SHOWN: "shown" + EVENT_KEY,
HIDE: "hide" + EVENT_KEY,
HIDDEN: "hidden" + EVENT_KEY,
CLICK_DATA_API: "click" + EVENT_KEY + DATA_API_KEY
};
var ClassName = {
SHOW: 'show',
COLLAPSE: 'collapse',
COLLAPSING: 'collapsing',
COLLAPSED: 'collapsed'
};
var Dimension = {
WIDTH: 'width',
HEIGHT: 'height'
};
var Selector = {
ACTIVES: '.show, .collapsing',
DATA_TOGGLE: '[data-toggle="collapse"]'
2017-09-13 07:24:15 +02:00
/**
* ------------------------------------------------------------------------
2018-11-13 07:41:12 +01:00
* Class Definition
2017-09-13 07:24:15 +02:00
* ------------------------------------------------------------------------
*/
2018-01-04 01:03:22 +01:00
2018-11-13 07:41:12 +01:00
};
var Collapse =
/*#__PURE__*/
function () {
function Collapse(element, config) {
this._isTransitioning = false;
this._element = element;
this._config = this._getConfig(config);
2018-11-24 17:22:59 +01:00
this._triggerArray = [].slice.call(document.querySelectorAll("[data-toggle=\"collapse\"][href=\"#" + element.id + "\"]," + ("[data-toggle=\"collapse\"][data-target=\"#" + element.id + "\"]")));
2018-11-13 07:41:12 +01:00
var toggleList = [].slice.call(document.querySelectorAll(Selector.DATA_TOGGLE));
for (var i = 0, len = toggleList.length; i < len; i++) {
var elem = toggleList[i];
var selector = Util.getSelectorFromElement(elem);
var filterElement = [].slice.call(document.querySelectorAll(selector)).filter(function (foundElem) {
return foundElem === element;
});
2017-05-16 09:59:44 +02:00
2018-11-13 07:41:12 +01:00
if (selector !== null && filterElement.length > 0) {
this._selector = selector;
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
this._triggerArray.push(elem);
2018-07-24 02:51:14 +02:00
}
2018-11-13 07:41:12 +01:00
}
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
this._parent = this._config.parent ? this._getParent() : null;
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
if (!this._config.parent) {
this._addAriaAndCollapsedClass(this._element, this._triggerArray);
}
2015-08-13 06:12:03 +02:00
2018-11-13 07:41:12 +01:00
if (this._config.toggle) {
this.toggle();
}
} // Getters
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
var _proto = Collapse.prototype;
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
// Public
_proto.toggle = function toggle() {
if ($(this._element).hasClass(ClassName.SHOW)) {
this.hide();
} else {
this.show();
}
};
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
_proto.show = function show() {
var _this = this;
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
if (this._isTransitioning || $(this._element).hasClass(ClassName.SHOW)) {
return;
}
2018-09-17 21:34:34 +02:00
2018-11-13 07:41:12 +01:00
var actives;
var activesData;
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
if (this._parent) {
actives = [].slice.call(this._parent.querySelectorAll(Selector.ACTIVES)).filter(function (elem) {
if (typeof _this._config.parent === 'string') {
return elem.getAttribute('data-parent') === _this._config.parent;
2018-07-24 02:51:14 +02:00
}
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
return elem.classList.contains(ClassName.COLLAPSE);
});
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
if (actives.length === 0) {
actives = null;
2017-09-06 06:05:12 +02:00
}
2018-11-13 07:41:12 +01:00
}
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
if (actives) {
activesData = $(actives).not(this._selector).data(DATA_KEY);
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
if (activesData && activesData._isTransitioning) {
2018-07-24 02:51:14 +02:00
return;
}
2018-11-13 07:41:12 +01:00
}
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
var startEvent = $.Event(Event.SHOW);
$(this._element).trigger(startEvent);
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
if (startEvent.isDefaultPrevented()) {
return;
}
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
if (actives) {
Collapse._jQueryInterface.call($(actives).not(this._selector), 'hide');
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
if (!activesData) {
$(actives).data(DATA_KEY, null);
2018-07-24 02:51:14 +02:00
}
2018-11-13 07:41:12 +01:00
}
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
var dimension = this._getDimension();
$(this._element).removeClass(ClassName.COLLAPSE).addClass(ClassName.COLLAPSING);
this._element.style[dimension] = 0;
if (this._triggerArray.length) {
$(this._triggerArray).removeClass(ClassName.COLLAPSED).attr('aria-expanded', true);
}
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
this.setTransitioning(true);
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
var complete = function complete() {
$(_this._element).removeClass(ClassName.COLLAPSING).addClass(ClassName.COLLAPSE).addClass(ClassName.SHOW);
_this._element.style[dimension] = '';
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
_this.setTransitioning(false);
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
$(_this._element).trigger(Event.SHOWN);
2018-07-24 02:51:14 +02:00
};
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
var capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1);
var scrollSize = "scroll" + capitalizedDimension;
var transitionDuration = Util.getTransitionDurationFromElement(this._element);
$(this._element).one(Util.TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
this._element.style[dimension] = this._element[scrollSize] + "px";
};
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
_proto.hide = function hide() {
var _this2 = this;
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
if (this._isTransitioning || !$(this._element).hasClass(ClassName.SHOW)) {
return;
}
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
var startEvent = $.Event(Event.HIDE);
$(this._element).trigger(startEvent);
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
if (startEvent.isDefaultPrevented()) {
return;
}
var dimension = this._getDimension();
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
this._element.style[dimension] = this._element.getBoundingClientRect()[dimension] + "px";
Util.reflow(this._element);
$(this._element).addClass(ClassName.COLLAPSING).removeClass(ClassName.COLLAPSE).removeClass(ClassName.SHOW);
var triggerArrayLength = this._triggerArray.length;
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
if (triggerArrayLength > 0) {
for (var i = 0; i < triggerArrayLength; i++) {
var trigger = this._triggerArray[i];
var selector = Util.getSelectorFromElement(trigger);
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
if (selector !== null) {
var $elem = $([].slice.call(document.querySelectorAll(selector)));
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
if (!$elem.hasClass(ClassName.SHOW)) {
$(trigger).addClass(ClassName.COLLAPSED).attr('aria-expanded', false);
2017-09-06 06:05:12 +02:00
}
}
}
2018-11-13 07:41:12 +01:00
}
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
this.setTransitioning(true);
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
var complete = function complete() {
_this2.setTransitioning(false);
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
$(_this2._element).removeClass(ClassName.COLLAPSING).addClass(ClassName.COLLAPSE).trigger(Event.HIDDEN);
2018-07-24 02:51:14 +02:00
};
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
this._element.style[dimension] = '';
var transitionDuration = Util.getTransitionDurationFromElement(this._element);
$(this._element).one(Util.TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
};
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
_proto.setTransitioning = function setTransitioning(isTransitioning) {
this._isTransitioning = isTransitioning;
};
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
_proto.dispose = function dispose() {
$.removeData(this._element, DATA_KEY);
this._config = null;
this._parent = null;
this._element = null;
this._triggerArray = null;
this._isTransitioning = null;
2019-01-04 17:29:45 +01:00
} // Private
;
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
_proto._getConfig = function _getConfig(config) {
config = _objectSpread({}, Default, config);
config.toggle = Boolean(config.toggle); // Coerce string values
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
Util.typeCheckConfig(NAME, config, DefaultType);
return config;
};
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
_proto._getDimension = function _getDimension() {
var hasWidth = $(this._element).hasClass(Dimension.WIDTH);
return hasWidth ? Dimension.WIDTH : Dimension.HEIGHT;
};
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
_proto._getParent = function _getParent() {
var _this3 = this;
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
var parent;
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
if (Util.isElement(this._config.parent)) {
parent = this._config.parent; // It's a jQuery object
if (typeof this._config.parent.jquery !== 'undefined') {
parent = this._config.parent[0];
2015-05-10 08:00:59 +02:00
}
2018-11-13 07:41:12 +01:00
} else {
parent = document.querySelector(this._config.parent);
}
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
var selector = "[data-toggle=\"collapse\"][data-parent=\"" + this._config.parent + "\"]";
var children = [].slice.call(parent.querySelectorAll(selector));
$(children).each(function (i, element) {
_this3._addAriaAndCollapsedClass(Collapse._getTargetFromElement(element), [element]);
});
return parent;
};
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
_proto._addAriaAndCollapsedClass = function _addAriaAndCollapsedClass(element, triggerArray) {
var isOpen = $(element).hasClass(ClassName.SHOW);
2016-10-10 02:26:51 +02:00
2018-11-13 07:41:12 +01:00
if (triggerArray.length) {
$(triggerArray).toggleClass(ClassName.COLLAPSED, !isOpen).attr('aria-expanded', isOpen);
}
2019-01-04 17:29:45 +01:00
} // Static
;
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
Collapse._getTargetFromElement = function _getTargetFromElement(element) {
var selector = Util.getSelectorFromElement(element);
return selector ? document.querySelector(selector) : null;
};
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
Collapse._jQueryInterface = function _jQueryInterface(config) {
return this.each(function () {
var $this = $(this);
var data = $this.data(DATA_KEY);
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
var _config = _objectSpread({}, Default, $this.data(), typeof config === 'object' && config ? config : {});
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
if (!data && _config.toggle && /show|hide/.test(config)) {
_config.toggle = false;
}
2018-07-24 02:51:14 +02:00
2018-11-13 07:41:12 +01:00
if (!data) {
data = new Collapse(this, _config);
$this.data(DATA_KEY, data);
}
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError("No method named \"" + config + "\"");
2017-09-06 06:05:12 +02:00
}
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
data[config]();
2018-07-24 02:51:14 +02:00
}
2018-11-13 07:41:12 +01:00
});
};
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
_createClass(Collapse, null, [{
key: "VERSION",
get: function get() {
return VERSION;
}
}, {
key: "Default",
get: function get() {
return Default;
}
}]);
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
return Collapse;
}();
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2017-09-30 23:28:03 +02:00
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
$(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
// preventDefault only for <a> elements (which change the URL) not inside the collapsible element
if (event.currentTarget.tagName === 'A') {
event.preventDefault();
}
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
var $trigger = $(this);
var selector = Util.getSelectorFromElement(this);
var selectors = [].slice.call(document.querySelectorAll(selector));
$(selectors).each(function () {
var $target = $(this);
var data = $target.data(DATA_KEY);
var config = data ? 'toggle' : $trigger.data();
2018-07-24 02:51:14 +02:00
2018-11-13 07:41:12 +01:00
Collapse._jQueryInterface.call($target, config);
});
});
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
2015-05-10 08:00:59 +02:00
2018-11-13 07:41:12 +01:00
$.fn[NAME] = Collapse._jQueryInterface;
$.fn[NAME].Constructor = Collapse;
2017-09-30 23:28:03 +02:00
2018-11-13 07:41:12 +01:00
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return Collapse._jQueryInterface;
};
2015-05-10 08:00:59 +02:00
return Collapse;
2018-07-24 02:51:14 +02:00
2019-01-04 17:29:45 +01:00
}));
2018-07-24 02:51:14 +02:00
//# sourceMappingURL=collapse.js.map