Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

370 строки
12 KiB

  1. /**
  2. * --------------------------------------------------------------------------
  3. * Bootstrap (v5.1.3): collapse.js and base-component.js
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  5. * --------------------------------------------------------------------------
  6. */
  7. define([
  8. "jquery",
  9. "./util/index",
  10. "./dom/data",
  11. "./dom/event-handler",
  12. "./dom/manipulator",
  13. "./dom/selector-engine"
  14. ], function($, Util, Data, EventHandler, Manipulator, SelectorEngine) {
  15. 'use strict';
  16. const defineJQueryPlugin = Util.defineJQueryPlugin;
  17. const executeAfterTransition = Util.executeAfterTransition;
  18. const getElement = Util.getElement;
  19. const getSelectorFromElement = Util.getSelectorFromElement;
  20. const getElementFromSelector = Util.getElementFromSelector;
  21. const reflow = Util.reflow;
  22. const typeCheckConfig = Util.typeCheckConfig;
  23. /**
  24. * ------------------------------------------------------------------------
  25. * Constants
  26. * ------------------------------------------------------------------------
  27. */
  28. const VERSION = '5.1.3';
  29. const NAME = 'collapse';
  30. const DATA_KEY = 'bs.collapse';
  31. const EVENT_KEY = `.${DATA_KEY}`;
  32. const DATA_API_KEY = '.data-api';
  33. const Default = {
  34. toggle: true,
  35. parent: null
  36. };
  37. const DefaultType = {
  38. toggle: 'boolean',
  39. parent: '(null|element)'
  40. };
  41. const EVENT_SHOW = `show${EVENT_KEY}`;
  42. const EVENT_SHOWN = `shown${EVENT_KEY}`;
  43. const EVENT_HIDE = `hide${EVENT_KEY}`;
  44. const EVENT_HIDDEN = `hidden${EVENT_KEY}`;
  45. const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`;
  46. const CLASS_NAME_SHOW = 'show';
  47. const CLASS_NAME_COLLAPSE = 'collapse';
  48. const CLASS_NAME_COLLAPSING = 'collapsing';
  49. const CLASS_NAME_COLLAPSED = 'collapsed';
  50. const CLASS_NAME_DEEPER_CHILDREN = `:scope .${CLASS_NAME_COLLAPSE} .${CLASS_NAME_COLLAPSE}`;
  51. const CLASS_NAME_HORIZONTAL = 'collapse-horizontal';
  52. const WIDTH = 'width';
  53. const HEIGHT = 'height';
  54. const SELECTOR_ACTIVES = '.collapse.show, .collapse.collapsing';
  55. const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="collapse"]';
  56. /**
  57. * ------------------------------------------------------------------------
  58. * Class Definition
  59. * ------------------------------------------------------------------------
  60. */
  61. var Collapse = function(element, config) {
  62. element = getElement(element);
  63. if (!element) {
  64. return;
  65. }
  66. this._element = element;
  67. Data.set(this._element, DATA_KEY, this);
  68. this._isTransitioning = false;
  69. this._config = this._getConfig(config);
  70. this._triggerArray = [];
  71. const toggleList = SelectorEngine.find(SELECTOR_DATA_TOGGLE);
  72. for (let i = 0, len = toggleList.length; i < len; i++) {
  73. const elem = toggleList[i];
  74. const selector = getSelectorFromElement(elem);
  75. const filterElement = SelectorEngine.find(selector)
  76. .filter(foundElem => foundElem === this._element);
  77. if (selector !== null && filterElement.length) {
  78. this._selector = selector;
  79. this._triggerArray.push(elem);
  80. }
  81. }
  82. this._initializeChildren();
  83. if (!this._config.parent) {
  84. this._addAriaAndCollapsedClass(this._triggerArray, this._isShown());
  85. }
  86. if (this._config.toggle) {
  87. this.toggle();
  88. }
  89. }
  90. // Getters
  91. Collapse.VERSION = VERSION;
  92. Collapse.Default = Default;
  93. Collapse.NAME = NAME;
  94. Collapse.DATA_KEY = 'bs.' + Collapse.NAME;
  95. Collapse.EVENT_KEY = '.' + Collapse.DATA_KEY;
  96. // Public
  97. Collapse.prototype.dispose = function() {
  98. Data.remove(this._element, this.constructor.DATA_KEY);
  99. EventHandler.off(this._element, this.constructor.EVENT_KEY);
  100. Object.getOwnPropertyNames(this).forEach(propertyName => {
  101. this[propertyName] = null;
  102. })
  103. }
  104. Collapse.prototype._queueCallback = function(callback, element, isAnimated = true) {
  105. executeAfterTransition(callback, element, isAnimated);
  106. }
  107. Collapse.prototype.toggle = function() {
  108. if (this._isShown()) {
  109. this.hide();
  110. } else {
  111. this.show();
  112. }
  113. }
  114. Collapse.prototype.show = function() {
  115. if (this._isTransitioning || this._isShown()) {
  116. return;
  117. }
  118. let actives = [];
  119. let activesData;
  120. if (this._config.parent) {
  121. const children = SelectorEngine.find(CLASS_NAME_DEEPER_CHILDREN, this._config.parent);
  122. actives = SelectorEngine.find(SELECTOR_ACTIVES, this._config.parent).filter(elem => !children.includes(elem)); // remove children if greater depth
  123. }
  124. const container = SelectorEngine.findOne(this._selector);
  125. if (actives.length) {
  126. const tempActiveData = actives.find(elem => container !== elem);
  127. activesData = tempActiveData ? Collapse.getInstance(tempActiveData) : null;
  128. if (activesData && activesData._isTransitioning) {
  129. return;
  130. }
  131. }
  132. const startEvent = EventHandler.trigger(this._element, EVENT_SHOW);
  133. if (startEvent.defaultPrevented) {
  134. return;
  135. }
  136. actives.forEach(elemActive => {
  137. if (container !== elemActive) {
  138. Collapse.getOrCreateInstance(elemActive, {toggle: false}).hide();
  139. }
  140. if (!activesData) {
  141. Data.set(elemActive, DATA_KEY, null);
  142. }
  143. })
  144. const dimension = this._getDimension();
  145. this._element.classList.remove(CLASS_NAME_COLLAPSE);
  146. this._element.classList.add(CLASS_NAME_COLLAPSING);
  147. this._element.style[dimension] = 0;
  148. this._addAriaAndCollapsedClass(this._triggerArray, true);
  149. this._isTransitioning = true;
  150. const complete = () => {
  151. this._isTransitioning = false;
  152. this._element.classList.remove(CLASS_NAME_COLLAPSING);
  153. this._element.classList.add(CLASS_NAME_COLLAPSE, CLASS_NAME_SHOW);
  154. this._element.style[dimension] = '';
  155. EventHandler.trigger(this._element, EVENT_SHOWN);
  156. }
  157. const capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1);
  158. const scrollSize = `scroll${capitalizedDimension}`;
  159. this._queueCallback(complete, this._element, true);
  160. this._element.style[dimension] = `${this._element[scrollSize]}px`;
  161. }
  162. Collapse.prototype.hide = function() {
  163. if (this._isTransitioning || !this._isShown()) {
  164. return;
  165. }
  166. const startEvent = EventHandler.trigger(this._element, EVENT_HIDE);
  167. if (startEvent.defaultPrevented) {
  168. return;
  169. }
  170. const dimension = this._getDimension();
  171. this._element.style[dimension] = `${this._element.getBoundingClientRect()[dimension]}px`;
  172. reflow(this._element);
  173. this._element.classList.add(CLASS_NAME_COLLAPSING);
  174. this._element.classList.remove(CLASS_NAME_COLLAPSE, CLASS_NAME_SHOW);
  175. const triggerArrayLength = this._triggerArray.length;
  176. for (let i = 0; i < triggerArrayLength; i++) {
  177. const trigger = this._triggerArray[i];
  178. const elem = getElementFromSelector(trigger);
  179. if (elem && !this._isShown(elem)) {
  180. this._addAriaAndCollapsedClass([trigger], false);
  181. }
  182. }
  183. this._isTransitioning = true;
  184. const complete = () => {
  185. this._isTransitioning = false;
  186. this._element.classList.remove(CLASS_NAME_COLLAPSING);
  187. this._element.classList.add(CLASS_NAME_COLLAPSE);
  188. EventHandler.trigger(this._element, EVENT_HIDDEN);
  189. }
  190. this._element.style[dimension] = '';
  191. this._queueCallback(complete, this._element, true);
  192. }
  193. Collapse.prototype._isShown = function(element = this._element) {
  194. return element.classList.contains(CLASS_NAME_SHOW);
  195. }
  196. // Private
  197. Collapse.prototype._getConfig = function(config) {
  198. config = {
  199. ...Default,
  200. ...Manipulator.getDataAttributes(this._element),
  201. ...config
  202. };
  203. config.toggle = Boolean(config.toggle); // Coerce string values
  204. config.parent = getElement(config.parent);
  205. typeCheckConfig(NAME, config, DefaultType);
  206. return config;
  207. }
  208. Collapse.prototype._getDimension = function() {
  209. return this._element.classList.contains(CLASS_NAME_HORIZONTAL) ? WIDTH : HEIGHT;
  210. }
  211. Collapse.prototype._initializeChildren = function() {
  212. if (!this._config.parent) {
  213. return;
  214. }
  215. const children = SelectorEngine.find(CLASS_NAME_DEEPER_CHILDREN, this._config.parent);
  216. SelectorEngine.find(SELECTOR_DATA_TOGGLE, this._config.parent).filter(elem => !children.includes(elem))
  217. .forEach(element => {
  218. const selected = getElementFromSelector(element);
  219. if (selected) {
  220. this._addAriaAndCollapsedClass([element], this._isShown(selected));
  221. }
  222. })
  223. }
  224. Collapse.prototype._addAriaAndCollapsedClass = function(triggerArray, isOpen) {
  225. if (!triggerArray.length) {
  226. return;
  227. }
  228. triggerArray.forEach(elem => {
  229. if (isOpen) {
  230. elem.classList.remove(CLASS_NAME_COLLAPSED);
  231. } else {
  232. elem.classList.add(CLASS_NAME_COLLAPSED);
  233. }
  234. elem.setAttribute('aria-expanded', isOpen);
  235. })
  236. }
  237. // Static
  238. Collapse.getInstance = function(element) {
  239. return Data.get(getElement(element), this.DATA_KEY);
  240. }
  241. Collapse.getOrCreateInstance = function(element, config = {}) {
  242. return this.getInstance(element) || new this(element, typeof config === 'object' ? config : null);
  243. }
  244. Collapse.jQueryInterface = function(config) {
  245. return this.each(function () {
  246. const _config = {};
  247. if (typeof config === 'string' && /show|hide/.test(config)) {
  248. _config.toggle = false;
  249. }
  250. const data = Collapse.getOrCreateInstance(this, _config);
  251. if (typeof config === 'string') {
  252. if (typeof data[config] === 'undefined') {
  253. throw new TypeError(`No method named "${config}"`);
  254. }
  255. data[config]();
  256. }
  257. })
  258. }
  259. /**
  260. * ------------------------------------------------------------------------
  261. * Data Api implementation
  262. * ------------------------------------------------------------------------
  263. */
  264. EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
  265. // preventDefault only for <a> elements (which change the URL) not inside the collapsible element
  266. if (event.target.tagName === 'A' || (event.delegateTarget && event.delegateTarget.tagName === 'A')) {
  267. event.preventDefault();
  268. }
  269. const selector = getSelectorFromElement(this);
  270. const selectorElements = SelectorEngine.find(selector);
  271. selectorElements.forEach(element => {
  272. Collapse.getOrCreateInstance(element, {toggle: false}).toggle();
  273. })
  274. })
  275. /**
  276. * ------------------------------------------------------------------------
  277. * jQuery
  278. * ------------------------------------------------------------------------
  279. * add .Collapse to jQuery only if jQuery is present
  280. */
  281. defineJQueryPlugin(Collapse);
  282. return Collapse;
  283. });