mirror of
https://github.com/twbs/bootstrap.git
synced 2025-08-08 14:46:34 +02:00
Scrollspy: minor refactoring (#35512)
* reorder variables * join lines * use `filter(Boolean)` since it's clearer * use `for...of`
This commit is contained in:
@@ -5,11 +5,7 @@
|
|||||||
* --------------------------------------------------------------------------
|
* --------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {
|
import { defineJQueryPlugin, getElement, getSelectorFromElement } from './util/index'
|
||||||
defineJQueryPlugin,
|
|
||||||
getElement,
|
|
||||||
getSelectorFromElement
|
|
||||||
} from './util/index'
|
|
||||||
import EventHandler from './dom/event-handler'
|
import EventHandler from './dom/event-handler'
|
||||||
import Manipulator from './dom/manipulator'
|
import Manipulator from './dom/manipulator'
|
||||||
import SelectorEngine from './dom/selector-engine'
|
import SelectorEngine from './dom/selector-engine'
|
||||||
@@ -89,45 +85,34 @@ class ScrollSpy extends BaseComponent {
|
|||||||
|
|
||||||
// Public
|
// Public
|
||||||
refresh() {
|
refresh() {
|
||||||
const autoMethod = this._scrollElement === this._scrollElement.window ?
|
|
||||||
METHOD_OFFSET :
|
|
||||||
METHOD_POSITION
|
|
||||||
|
|
||||||
const offsetMethod = this._config.method === 'auto' ?
|
|
||||||
autoMethod :
|
|
||||||
this._config.method
|
|
||||||
|
|
||||||
const offsetBase = offsetMethod === METHOD_POSITION ?
|
|
||||||
this._getScrollTop() :
|
|
||||||
0
|
|
||||||
|
|
||||||
this._offsets = []
|
this._offsets = []
|
||||||
this._targets = []
|
this._targets = []
|
||||||
this._scrollHeight = this._getScrollHeight()
|
this._scrollHeight = this._getScrollHeight()
|
||||||
|
|
||||||
|
const autoMethod = this._scrollElement === this._scrollElement.window ? METHOD_OFFSET : METHOD_POSITION
|
||||||
|
const offsetMethod = this._config.method === 'auto' ? autoMethod : this._config.method
|
||||||
|
const offsetBase = offsetMethod === METHOD_POSITION ? this._getScrollTop() : 0
|
||||||
const targets = SelectorEngine.find(SELECTOR_LINK_ITEMS, this._config.target)
|
const targets = SelectorEngine.find(SELECTOR_LINK_ITEMS, this._config.target)
|
||||||
.map(element => {
|
.map(element => {
|
||||||
const targetSelector = getSelectorFromElement(element)
|
const targetSelector = getSelectorFromElement(element)
|
||||||
const target = targetSelector ? SelectorEngine.findOne(targetSelector) : null
|
const target = targetSelector ? SelectorEngine.findOne(targetSelector) : null
|
||||||
|
|
||||||
if (target) {
|
if (!target) {
|
||||||
const targetBCR = target.getBoundingClientRect()
|
return null
|
||||||
if (targetBCR.width || targetBCR.height) {
|
|
||||||
return [
|
|
||||||
Manipulator[offsetMethod](target).top + offsetBase,
|
|
||||||
targetSelector
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
const targetBCR = target.getBoundingClientRect()
|
||||||
|
|
||||||
|
return targetBCR.width || targetBCR.height ?
|
||||||
|
[Manipulator[offsetMethod](target).top + offsetBase, targetSelector] :
|
||||||
|
null
|
||||||
})
|
})
|
||||||
.filter(item => item)
|
.filter(Boolean)
|
||||||
.sort((a, b) => a[0] - b[0])
|
.sort((a, b) => a[0] - b[0])
|
||||||
|
|
||||||
for (const item of targets) {
|
for (const target of targets) {
|
||||||
this._offsets.push(item[0])
|
this._offsets.push(target[0])
|
||||||
this._targets.push(item[1])
|
this._targets.push(target[1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,7 +173,7 @@ class ScrollSpy extends BaseComponent {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = this._offsets.length; i--;) {
|
for (const i of this._offsets.keys()) {
|
||||||
const isActiveTarget = this._activeTarget !== this._targets[i] &&
|
const isActiveTarget = this._activeTarget !== this._targets[i] &&
|
||||||
scrollTop >= this._offsets[i] &&
|
scrollTop >= this._offsets[i] &&
|
||||||
(typeof this._offsets[i + 1] === 'undefined' || scrollTop < this._offsets[i + 1])
|
(typeof this._offsets[i + 1] === 'undefined' || scrollTop < this._offsets[i + 1])
|
||||||
|
Reference in New Issue
Block a user