0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-10 03:46:13 +01:00
Bootstrap/js/src/scrollspy.js

300 lines
8.0 KiB
JavaScript
Raw Normal View History

2015-05-11 21:05:35 +02:00
/**
* --------------------------------------------------------------------------
2021-10-09 08:33:12 +02:00
* Bootstrap (v5.1.3): scrollspy.js
2020-06-16 20:41:47 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2015-05-11 21:05:35 +02:00
* --------------------------------------------------------------------------
*/
import {
defineJQueryPlugin,
getElement,
getSelectorFromElement,
typeCheckConfig
} from './util/index'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
import SelectorEngine from './dom/selector-engine'
2019-09-04 16:58:29 +02:00
import BaseComponent from './base-component'
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2015-05-11 21:05:35 +02:00
2019-02-26 12:20:34 +01:00
const NAME = 'scrollspy'
const DATA_KEY = 'bs.scrollspy'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
2018-09-26 10:39:01 +02:00
const Default = {
2019-02-26 12:20:34 +01:00
offset: 10,
method: 'auto',
target: ''
2018-09-26 10:39:01 +02:00
}
const DefaultType = {
2019-02-26 12:20:34 +01:00
offset: 'number',
method: 'string',
target: '(string|element)'
2018-09-26 10:39:01 +02:00
}
const EVENT_ACTIVATE = `activate${EVENT_KEY}`
const EVENT_SCROLL = `scroll${EVENT_KEY}`
const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`
2018-09-26 10:39:01 +02:00
const CLASS_NAME_DROPDOWN_ITEM = 'dropdown-item'
const CLASS_NAME_ACTIVE = 'active'
2018-09-26 10:39:01 +02:00
const SELECTOR_DATA_SPY = '[data-bs-spy="scroll"]'
const SELECTOR_NAV_LIST_GROUP = '.nav, .list-group'
const SELECTOR_NAV_LINKS = '.nav-link'
const SELECTOR_NAV_ITEMS = '.nav-item'
const SELECTOR_LIST_ITEMS = '.list-group-item'
const SELECTOR_LINK_ITEMS = `${SELECTOR_NAV_LINKS}, ${SELECTOR_LIST_ITEMS}, .${CLASS_NAME_DROPDOWN_ITEM}`
const SELECTOR_DROPDOWN = '.dropdown'
const SELECTOR_DROPDOWN_TOGGLE = '.dropdown-toggle'
2018-09-26 10:39:01 +02:00
const METHOD_OFFSET = 'offset'
const METHOD_POSITION = 'position'
2015-05-13 23:46:50 +02:00
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
2015-05-11 21:05:35 +02:00
2019-09-04 16:58:29 +02:00
class ScrollSpy extends BaseComponent {
2018-09-26 10:39:01 +02:00
constructor(element, config) {
2019-09-04 16:58:29 +02:00
super(element)
this._scrollElement = this._element.tagName === 'BODY' ? window : this._element
2019-02-26 12:20:34 +01:00
this._config = this._getConfig(config)
this._offsets = []
this._targets = []
this._activeTarget = null
this._scrollHeight = 0
2018-09-26 10:39:01 +02:00
EventHandler.on(this._scrollElement, EVENT_SCROLL, () => this._process())
2018-09-26 10:39:01 +02:00
this.refresh()
this._process()
2015-05-11 21:05:35 +02:00
}
2018-09-26 10:39:01 +02:00
// Getters
2015-05-11 21:05:35 +02:00
2018-09-26 10:39:01 +02:00
static get Default() {
return Default
}
2015-05-11 21:05:35 +02:00
static get NAME() {
return NAME
2019-09-04 16:58:29 +02:00
}
2018-09-26 10:39:01 +02:00
// Public
2015-05-11 21:05:35 +02:00
2018-09-26 10:39:01 +02:00
refresh() {
2019-02-26 12:20:34 +01:00
const autoMethod = this._scrollElement === this._scrollElement.window ?
METHOD_OFFSET :
METHOD_POSITION
2019-02-26 12:20:34 +01:00
const offsetMethod = this._config.method === 'auto' ?
autoMethod :
this._config.method
2015-05-11 21:05:35 +02:00
const offsetBase = offsetMethod === METHOD_POSITION ?
2019-02-26 12:20:34 +01:00
this._getScrollTop() :
0
2015-05-11 21:05:35 +02:00
2018-09-26 10:39:01 +02:00
this._offsets = []
this._targets = []
this._scrollHeight = this._getScrollHeight()
2015-05-11 21:05:35 +02:00
const targets = SelectorEngine.find(SELECTOR_LINK_ITEMS, this._config.target)
2021-10-08 11:28:05 +02:00
.map(element => {
const targetSelector = getSelectorFromElement(element)
const target = targetSelector ? SelectorEngine.findOne(targetSelector) : null
if (target) {
const targetBCR = target.getBoundingClientRect()
if (targetBCR.width || targetBCR.height) {
return [
Manipulator[offsetMethod](target).top + offsetBase,
targetSelector
]
}
2015-05-13 22:43:56 +02:00
}
2019-02-26 12:20:34 +01:00
2021-10-08 11:28:05 +02:00
return null
})
.filter(item => item)
.sort((a, b) => a[0] - b[0])
for (const item of targets) {
this._offsets.push(item[0])
this._targets.push(item[1])
}
2018-09-26 10:39:01 +02:00
}
2015-05-13 22:43:56 +02:00
2018-09-26 10:39:01 +02:00
dispose() {
2017-09-25 09:09:01 +02:00
EventHandler.off(this._scrollElement, EVENT_KEY)
super.dispose()
2018-09-26 10:39:01 +02:00
}
2015-05-13 23:46:50 +02:00
2018-09-26 10:39:01 +02:00
// Private
2015-05-13 22:43:56 +02:00
2018-09-26 10:39:01 +02:00
_getConfig(config) {
config = {
...Default,
2021-04-06 17:28:10 +02:00
...Manipulator.getDataAttributes(this._element),
...(typeof config === 'object' && config ? config : {})
2015-05-11 21:05:35 +02:00
}
config.target = getElement(config.target) || document.documentElement
2015-05-11 21:05:35 +02:00
typeCheckConfig(NAME, config, DefaultType)
2018-09-26 10:39:01 +02:00
return config
}
2015-05-11 21:05:35 +02:00
2018-09-26 10:39:01 +02:00
_getScrollTop() {
2019-02-26 12:20:34 +01:00
return this._scrollElement === window ?
this._scrollElement.pageYOffset :
this._scrollElement.scrollTop
2018-09-26 10:39:01 +02:00
}
2015-05-11 21:05:35 +02:00
2018-09-26 10:39:01 +02:00
_getScrollHeight() {
return this._scrollElement.scrollHeight || Math.max(
document.body.scrollHeight,
document.documentElement.scrollHeight
)
}
2015-05-11 21:05:35 +02:00
2018-09-26 10:39:01 +02:00
_getOffsetHeight() {
2019-02-26 12:20:34 +01:00
return this._scrollElement === window ?
window.innerHeight :
this._scrollElement.getBoundingClientRect().height
2018-09-26 10:39:01 +02:00
}
2015-05-11 21:05:35 +02:00
2018-09-26 10:39:01 +02:00
_process() {
2019-02-26 12:20:34 +01:00
const scrollTop = this._getScrollTop() + this._config.offset
2018-09-26 10:39:01 +02:00
const scrollHeight = this._getScrollHeight()
2020-12-02 05:45:15 +01:00
const maxScroll = this._config.offset + scrollHeight - this._getOffsetHeight()
2015-05-11 21:05:35 +02:00
2018-09-26 10:39:01 +02:00
if (this._scrollHeight !== scrollHeight) {
this.refresh()
}
2015-05-11 21:05:35 +02:00
2018-09-26 10:39:01 +02:00
if (scrollTop >= maxScroll) {
const target = this._targets[this._targets.length - 1]
if (this._activeTarget !== target) {
this._activate(target)
2015-05-11 21:05:35 +02:00
}
2019-02-26 12:20:34 +01:00
2018-09-26 10:39:01 +02:00
return
2015-05-11 21:05:35 +02:00
}
2018-09-26 10:39:01 +02:00
if (this._activeTarget && scrollTop < this._offsets[0] && this._offsets[0] > 0) {
this._activeTarget = null
2015-05-11 21:05:35 +02:00
this._clear()
2018-09-26 10:39:01 +02:00
return
}
2015-05-11 21:05:35 +02:00
for (let i = this._offsets.length; i--;) {
2018-09-26 10:39:01 +02:00
const isActiveTarget = this._activeTarget !== this._targets[i] &&
scrollTop >= this._offsets[i] &&
2020-12-02 05:45:15 +01:00
(typeof this._offsets[i + 1] === 'undefined' || scrollTop < this._offsets[i + 1])
2015-05-11 21:05:35 +02:00
2018-09-26 10:39:01 +02:00
if (isActiveTarget) {
this._activate(this._targets[i])
2015-05-11 21:05:35 +02:00
}
}
2018-09-26 10:39:01 +02:00
}
2015-05-11 21:05:35 +02:00
2018-09-26 10:39:01 +02:00
_activate(target) {
this._activeTarget = target
this._clear()
const queries = SELECTOR_LINK_ITEMS.split(',')
.map(selector => `${selector}[data-bs-target="${target}"],${selector}[href="${target}"]`)
2018-09-26 10:39:01 +02:00
const link = SelectorEngine.findOne(queries.join(','), this._config.target)
2017-09-25 09:09:01 +02:00
link.classList.add(CLASS_NAME_ACTIVE)
if (link.classList.contains(CLASS_NAME_DROPDOWN_ITEM)) {
2020-06-20 18:00:53 +02:00
SelectorEngine.findOne(SELECTOR_DROPDOWN_TOGGLE, link.closest(SELECTOR_DROPDOWN))
.classList.add(CLASS_NAME_ACTIVE)
2018-09-26 10:39:01 +02:00
} else {
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)
}
}
}
2015-05-11 21:05:35 +02:00
}
EventHandler.trigger(this._scrollElement, EVENT_ACTIVATE, {
2018-09-26 10:39:01 +02:00
relatedTarget: target
})
}
2015-05-11 21:05:35 +02:00
2018-09-26 10:39:01 +02:00
_clear() {
const activeNodes = SelectorEngine.find(SELECTOR_LINK_ITEMS, this._config.target)
.filter(node => node.classList.contains(CLASS_NAME_ACTIVE))
for (const node of activeNodes) {
node.classList.remove(CLASS_NAME_ACTIVE)
}
2018-09-26 10:39:01 +02:00
}
2015-05-11 21:05:35 +02:00
2018-09-26 10:39:01 +02:00
// Static
2015-05-11 21:05:35 +02:00
2019-07-28 15:24:46 +02:00
static jQueryInterface(config) {
2018-09-26 10:39:01 +02:00
return this.each(function () {
const data = ScrollSpy.getOrCreateInstance(this, config)
2018-09-26 10:39:01 +02:00
2021-04-06 17:28:10 +02:00
if (typeof config !== 'string') {
return
2018-09-26 10:39:01 +02:00
}
2021-04-06 17:28:10 +02:00
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
2018-09-26 10:39:01 +02:00
}
2021-04-06 17:28:10 +02:00
data[config]()
2018-09-26 10:39:01 +02:00
})
2015-05-11 21:05:35 +02:00
}
2018-09-26 10:39:01 +02:00
}
2015-05-11 21:05:35 +02:00
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
2015-05-11 21:05:35 +02:00
EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
for (const spy of SelectorEngine.find(SELECTOR_DATA_SPY)) {
new ScrollSpy(spy) // eslint-disable-line no-new
}
2018-09-26 10:39:01 +02:00
})
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2020-11-01 14:49:51 +01:00
* add .ScrollSpy to jQuery only if jQuery is present
2018-09-26 10:39:01 +02:00
*/
defineJQueryPlugin(ScrollSpy)
2015-05-11 21:05:35 +02:00
export default ScrollSpy