diff options
Diffstat (limited to 'js/src/scrollspy.js')
-rw-r--r-- | js/src/scrollspy.js | 51 |
1 files changed, 27 insertions, 24 deletions
diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js index 900d245c64..df9a14e220 100644 --- a/js/src/scrollspy.js +++ b/js/src/scrollspy.js @@ -111,7 +111,7 @@ class ScrollSpy extends BaseComponent { const targets = SelectorEngine.find(SELECTOR_LINK_ITEMS, this._config.target) - targets.map(element => { + for (const item of targets.map(element => { const targetSelector = getSelectorFromElement(element) const target = targetSelector ? SelectorEngine.findOne(targetSelector) : null @@ -128,11 +128,10 @@ class ScrollSpy extends BaseComponent { return null }) .filter(item => item) - .sort((a, b) => a[0] - b[0]) - .forEach(item => { - this._offsets.push(item[0]) - this._targets.push(item[1]) - }) + .sort((a, b) => a[0] - b[0])) { + this._offsets.push(item[0]) + this._targets.push(item[1]) + } } dispose() { @@ -226,20 +225,20 @@ class ScrollSpy extends BaseComponent { SelectorEngine.findOne(SELECTOR_DROPDOWN_TOGGLE, link.closest(SELECTOR_DROPDOWN)) .classList.add(CLASS_NAME_ACTIVE) } else { - SelectorEngine.parents(link, SELECTOR_NAV_LIST_GROUP) - .forEach(listGroup => { - // Set triggered links parents as active - // With both <ul> and <nav> markup a parent is the previous sibling of any nav ancestor - SelectorEngine.prev(listGroup, `${SELECTOR_NAV_LINKS}, ${SELECTOR_LIST_ITEMS}`) - .forEach(item => item.classList.add(CLASS_NAME_ACTIVE)) - - // Handle special case when .nav-link is inside .nav-item - SelectorEngine.prev(listGroup, SELECTOR_NAV_ITEMS) - .forEach(navItem => { - SelectorEngine.children(navItem, SELECTOR_NAV_LINKS) - .forEach(item => item.classList.add(CLASS_NAME_ACTIVE)) - }) - }) + for (const listGroup of SelectorEngine.parents(link, SELECTOR_NAV_LIST_GROUP)) { + // Set triggered links parents as active + // With both <ul> and <nav> markup a parent is the previous sibling of any nav ancestor + for (const item of SelectorEngine.prev(listGroup, `${SELECTOR_NAV_LINKS}, ${SELECTOR_LIST_ITEMS}`)) { + item.classList.add(CLASS_NAME_ACTIVE) + } + + // Handle special case when .nav-link is inside .nav-item + for (const navItem of SelectorEngine.prev(listGroup, SELECTOR_NAV_ITEMS)) { + for (const item of SelectorEngine.children(navItem, SELECTOR_NAV_LINKS)) { + item.classList.add(CLASS_NAME_ACTIVE) + } + } + } } EventHandler.trigger(this._scrollElement, EVENT_ACTIVATE, { @@ -248,9 +247,12 @@ class ScrollSpy extends BaseComponent { } _clear() { - SelectorEngine.find(SELECTOR_LINK_ITEMS, this._config.target) + const activeNodes = SelectorEngine.find(SELECTOR_LINK_ITEMS, this._config.target) .filter(node => node.classList.contains(CLASS_NAME_ACTIVE)) - .forEach(node => node.classList.remove(CLASS_NAME_ACTIVE)) + + for (const node of activeNodes) { + node.classList.remove(CLASS_NAME_ACTIVE) + } } // Static @@ -279,8 +281,9 @@ class ScrollSpy extends BaseComponent { */ EventHandler.on(window, EVENT_LOAD_DATA_API, () => { - SelectorEngine.find(SELECTOR_DATA_SPY) - .forEach(spy => new ScrollSpy(spy)) + for (const spy of SelectorEngine.find(SELECTOR_DATA_SPY)) { + new ScrollSpy(spy) // eslint-disable-line no-new + } }) /** |