From 1a6fdfae6be09b09eaced8f0e442ca6f7680a61e Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Sat, 9 Oct 2021 09:33:12 +0300 Subject: Bump version to 5.1.3. --- js/src/collapse.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'js/src/collapse.js') diff --git a/js/src/collapse.js b/js/src/collapse.js index 4ed0dadd6..33d5674eb 100644 --- a/js/src/collapse.js +++ b/js/src/collapse.js @@ -1,6 +1,6 @@ /** * -------------------------------------------------------------------------- - * Bootstrap (v5.1.2): collapse.js + * Bootstrap (v5.1.3): collapse.js * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * -------------------------------------------------------------------------- */ -- cgit v1.2.3 From e8f702666f285a3e69866ed1f8d29fa6eaaaeabb Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Wed, 13 Oct 2021 15:19:28 +0300 Subject: JS: minor refactoring (#35183) * add missing comments * shorten block comments * reorder constants * reorder public/private methods * sort exports alphabetically in util/index.js * fix a couple of typos --- js/src/collapse.js | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) (limited to 'js/src/collapse.js') diff --git a/js/src/collapse.js b/js/src/collapse.js index b32ce0186..39093c7a2 100644 --- a/js/src/collapse.js +++ b/js/src/collapse.js @@ -20,9 +20,7 @@ import SelectorEngine from './dom/selector-engine' import BaseComponent from './base-component' /** - * ------------------------------------------------------------------------ * Constants - * ------------------------------------------------------------------------ */ const NAME = 'collapse' @@ -30,16 +28,6 @@ const DATA_KEY = 'bs.collapse' const EVENT_KEY = `.${DATA_KEY}` const DATA_API_KEY = '.data-api' -const Default = { - toggle: true, - parent: null -} - -const DefaultType = { - toggle: 'boolean', - parent: '(null|element)' -} - const EVENT_SHOW = `show${EVENT_KEY}` const EVENT_SHOWN = `shown${EVENT_KEY}` const EVENT_HIDE = `hide${EVENT_KEY}` @@ -59,10 +47,18 @@ const HEIGHT = 'height' const SELECTOR_ACTIVES = '.collapse.show, .collapse.collapsing' const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="collapse"]' +const Default = { + toggle: true, + parent: null +} + +const DefaultType = { + toggle: 'boolean', + parent: '(null|element)' +} + /** - * ------------------------------------------------------------------------ - * Class Definition - * ------------------------------------------------------------------------ + * Class definition */ class Collapse extends BaseComponent { @@ -98,7 +94,6 @@ class Collapse extends BaseComponent { } // Getters - static get Default() { return Default } @@ -108,7 +103,6 @@ class Collapse extends BaseComponent { } // Public - toggle() { if (this._isShown()) { this.hide() @@ -230,7 +224,6 @@ class Collapse extends BaseComponent { } // Private - _getConfig(config) { config = { ...Default, @@ -281,7 +274,6 @@ class Collapse extends BaseComponent { } // Static - static jQueryInterface(config) { return this.each(function () { const _config = {} @@ -303,9 +295,7 @@ class Collapse extends BaseComponent { } /** - * ------------------------------------------------------------------------ - * Data Api implementation - * ------------------------------------------------------------------------ + * Data API implementation */ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) { @@ -323,10 +313,7 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function ( }) /** - * ------------------------------------------------------------------------ * jQuery - * ------------------------------------------------------------------------ - * add .Collapse to jQuery only if jQuery is present */ defineJQueryPlugin(Collapse) -- cgit v1.2.3 From 1eea13286671d19bdd5c0ec52769b6d75789a928 Mon Sep 17 00:00:00 2001 From: GeoSot Date: Sun, 10 Oct 2021 12:36:18 +0300 Subject: collapse: extract duplicate code to a function --- js/src/collapse.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'js/src/collapse.js') diff --git a/js/src/collapse.js b/js/src/collapse.js index 39093c7a2..f4fa11de4 100644 --- a/js/src/collapse.js +++ b/js/src/collapse.js @@ -8,8 +8,8 @@ import { defineJQueryPlugin, getElement, - getSelectorFromElement, getElementFromSelector, + getSelectorFromElement, reflow, typeCheckConfig } from './util/index' @@ -120,9 +120,7 @@ class Collapse extends BaseComponent { let activesData if (this._config.parent) { - const children = SelectorEngine.find(CLASS_NAME_DEEPER_CHILDREN, this._config.parent) - // remove children if greater depth - actives = SelectorEngine.find(SELECTOR_ACTIVES, this._config.parent).filter(elem => !children.includes(elem)) + actives = this._getFirstLevelChildren(SELECTOR_ACTIVES) } const container = SelectorEngine.findOne(this._selector) @@ -245,10 +243,9 @@ class Collapse extends BaseComponent { return } - const children = SelectorEngine.find(CLASS_NAME_DEEPER_CHILDREN, this._config.parent) - const elements = SelectorEngine.find(SELECTOR_DATA_TOGGLE, this._config.parent).filter(elem => !children.includes(elem)) + const children = this._getFirstLevelChildren(SELECTOR_DATA_TOGGLE) - for (const element of elements) { + for (const element of children) { const selected = getElementFromSelector(element) if (selected) { @@ -257,6 +254,12 @@ class Collapse extends BaseComponent { } } + _getFirstLevelChildren(selector) { + const children = SelectorEngine.find(CLASS_NAME_DEEPER_CHILDREN, this._config.parent) + // remove children if greater depth + return SelectorEngine.find(selector, this._config.parent).filter(elem => !children.includes(elem)) + } + _addAriaAndCollapsedClass(triggerArray, isOpen) { if (!triggerArray.length) { return -- cgit v1.2.3 From 9640e2d5dd4ace95d4fd9f03af160b4329e97282 Mon Sep 17 00:00:00 2001 From: GeoSot Date: Sun, 10 Oct 2021 14:35:52 +0300 Subject: Change the way collapse handles its children on opening --- js/src/collapse.js | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) (limited to 'js/src/collapse.js') diff --git a/js/src/collapse.js b/js/src/collapse.js index f4fa11de4..642f7e840 100644 --- a/js/src/collapse.js +++ b/js/src/collapse.js @@ -13,7 +13,6 @@ import { reflow, typeCheckConfig } from './util/index' -import Data from './dom/data' import EventHandler from './dom/event-handler' import Manipulator from './dom/manipulator' import SelectorEngine from './dom/selector-engine' @@ -77,7 +76,6 @@ class Collapse extends BaseComponent { .filter(foundElem => foundElem === this._element) if (selector !== null && filterElement.length) { - this._selector = selector this._triggerArray.push(elem) } } @@ -116,21 +114,17 @@ class Collapse extends BaseComponent { return } - let actives = [] - let activesData + let activeChildren = [] + // find active children if (this._config.parent) { - actives = this._getFirstLevelChildren(SELECTOR_ACTIVES) + activeChildren = this._getFirstLevelChildren(SELECTOR_ACTIVES) + .filter(element => element !== this._element) + .map(element => Collapse.getOrCreateInstance(element, { toggle: false })) } - const container = SelectorEngine.findOne(this._selector) - if (actives.length) { - const tempActiveData = actives.find(elem => container !== elem) - activesData = tempActiveData ? Collapse.getInstance(tempActiveData) : null - - if (activesData && activesData._isTransitioning) { - return - } + if (activeChildren.length && activeChildren[0]._isTransitioning) { + return } const startEvent = EventHandler.trigger(this._element, EVENT_SHOW) @@ -138,14 +132,8 @@ class Collapse extends BaseComponent { return } - for (const elemActive of actives) { - if (container !== elemActive) { - Collapse.getOrCreateInstance(elemActive, { toggle: false }).hide() - } - - if (!activesData) { - Data.set(elemActive, DATA_KEY, null) - } + for (const activeInstance of activeChildren) { + activeInstance.hide() } const dimension = this._getDimension() -- cgit v1.2.3