2015-05-11 21:05:35 +02:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2019-02-13 17:01:40 +01:00
|
|
|
* Bootstrap (v4.3.1): scrollspy.js
|
2015-05-11 21:05:35 +02:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2019-02-22 23:37:55 +01:00
|
|
|
import {
|
2019-08-02 15:51:05 +02:00
|
|
|
getjQuery,
|
2019-02-22 23:37:55 +01:00
|
|
|
getSelectorFromElement,
|
|
|
|
getUID,
|
|
|
|
makeArray,
|
|
|
|
typeCheckConfig
|
2019-10-02 11:43:54 +02:00
|
|
|
} 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'
|
2018-11-14 10:16:56 +01:00
|
|
|
|
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 VERSION = '4.3.1'
|
|
|
|
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 = {
|
2019-02-26 12:20:34 +01:00
|
|
|
ACTIVATE: `activate${EVENT_KEY}`,
|
|
|
|
SCROLL: `scroll${EVENT_KEY}`,
|
|
|
|
LOAD_DATA_API: `load${EVENT_KEY}${DATA_API_KEY}`
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const ClassName = {
|
2019-02-26 12:20:34 +01:00
|
|
|
DROPDOWN_ITEM: 'dropdown-item',
|
|
|
|
ACTIVE: 'active'
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Selector = {
|
2019-02-26 12:20:34 +01:00
|
|
|
DATA_SPY: '[data-spy="scroll"]',
|
|
|
|
NAV_LIST_GROUP: '.nav, .list-group',
|
|
|
|
NAV_LINKS: '.nav-link',
|
|
|
|
NAV_ITEMS: '.nav-item',
|
|
|
|
LIST_ITEMS: '.list-group-item',
|
|
|
|
DROPDOWN: '.dropdown',
|
|
|
|
DROPDOWN_TOGGLE: '.dropdown-toggle'
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const OffsetMethod = {
|
2019-02-26 12:20:34 +01:00
|
|
|
OFFSET: 'offset',
|
|
|
|
POSITION: 'position'
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
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
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
class ScrollSpy {
|
|
|
|
constructor(element, config) {
|
2019-02-26 12:20:34 +01:00
|
|
|
this._element = element
|
2018-09-26 10:39:01 +02:00
|
|
|
this._scrollElement = element.tagName === 'BODY' ? window : element
|
2019-02-26 12:20:34 +01:00
|
|
|
this._config = this._getConfig(config)
|
|
|
|
this._selector = `${this._config.target} ${Selector.NAV_LINKS},` +
|
2018-09-26 10:39:01 +02:00
|
|
|
`${this._config.target} ${Selector.LIST_ITEMS},` +
|
2019-02-26 14:48:27 +01:00
|
|
|
`${this._config.target} .${ClassName.DROPDOWN_ITEM}`
|
2019-02-26 12:20:34 +01:00
|
|
|
this._offsets = []
|
|
|
|
this._targets = []
|
|
|
|
this._activeTarget = null
|
|
|
|
this._scrollHeight = 0
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2019-02-26 12:20:34 +01:00
|
|
|
EventHandler.on(this._scrollElement, Event.SCROLL, event => this._process(event))
|
2018-09-26 10:39:01 +02:00
|
|
|
|
|
|
|
this.refresh()
|
|
|
|
this._process()
|
2017-09-25 09:09:01 +02:00
|
|
|
|
|
|
|
Data.setData(element, DATA_KEY, this)
|
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 VERSION() {
|
|
|
|
return VERSION
|
2015-05-13 21:55:11 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
static get Default() {
|
|
|
|
return Default
|
|
|
|
}
|
2015-05-11 21:05:35 +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 ?
|
|
|
|
OffsetMethod.OFFSET :
|
|
|
|
OffsetMethod.POSITION
|
2015-05-13 21:55:11 +02:00
|
|
|
|
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
|
|
|
|
2019-02-26 12:20:34 +01:00
|
|
|
const offsetBase = offsetMethod === OffsetMethod.POSITION ?
|
|
|
|
this._getScrollTop() :
|
|
|
|
0
|
2015-05-11 21:05:35 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
this._offsets = []
|
|
|
|
this._targets = []
|
2015-05-11 21:05:35 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
this._scrollHeight = this._getScrollHeight()
|
2015-05-11 21:05:35 +02:00
|
|
|
|
2019-02-22 23:37:55 +01:00
|
|
|
const targets = makeArray(SelectorEngine.find(this._selector))
|
2015-05-11 21:05:35 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
targets
|
2019-02-26 12:20:34 +01:00
|
|
|
.map(element => {
|
2018-09-26 10:39:01 +02:00
|
|
|
let target
|
2019-02-22 23:37:55 +01:00
|
|
|
const targetSelector = getSelectorFromElement(element)
|
2015-05-11 21:05:35 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (targetSelector) {
|
2019-02-20 09:45:46 +01:00
|
|
|
target = SelectorEngine.findOne(targetSelector)
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-11 21:05:35 +02:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (target) {
|
|
|
|
const targetBCR = target.getBoundingClientRect()
|
|
|
|
if (targetBCR.width || targetBCR.height) {
|
|
|
|
return [
|
2017-09-25 09:09:01 +02:00
|
|
|
Manipulator[offsetMethod](target).top + offsetBase,
|
2018-09-26 10:39:01 +02:00
|
|
|
targetSelector
|
|
|
|
]
|
2015-05-11 21:05:35 +02:00
|
|
|
}
|
2015-05-13 22:43:56 +02:00
|
|
|
}
|
2019-02-26 12:20:34 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
return null
|
|
|
|
})
|
2019-02-26 12:20:34 +01:00
|
|
|
.filter(item => item)
|
2018-09-26 10:39:01 +02:00
|
|
|
.sort((a, b) => a[0] - b[0])
|
2019-02-26 12:20:34 +01:00
|
|
|
.forEach(item => {
|
2018-09-26 10:39:01 +02:00
|
|
|
this._offsets.push(item[0])
|
|
|
|
this._targets.push(item[1])
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
Data.removeData(this._element, DATA_KEY)
|
|
|
|
EventHandler.off(this._scrollElement, EVENT_KEY)
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2019-02-26 12:20:34 +01:00
|
|
|
this._element = null
|
2018-09-26 10:39:01 +02:00
|
|
|
this._scrollElement = null
|
2019-02-26 12:20:34 +01:00
|
|
|
this._config = null
|
|
|
|
this._selector = null
|
|
|
|
this._offsets = null
|
|
|
|
this._targets = null
|
|
|
|
this._activeTarget = null
|
|
|
|
this._scrollHeight = null
|
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,
|
|
|
|
...typeof config === 'object' && config ? config : {}
|
2015-05-11 21:05:35 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
if (typeof config.target !== 'string') {
|
2019-02-26 12:20:34 +01:00
|
|
|
let { id } = config.target
|
2018-09-26 10:39:01 +02:00
|
|
|
if (!id) {
|
2019-02-22 23:37:55 +01:00
|
|
|
id = getUID(NAME)
|
2017-09-25 09:09:01 +02:00
|
|
|
config.target.id = id
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2019-02-26 12:20:34 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
config.target = `#${id}`
|
2015-05-11 21:05:35 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 23:37:55 +01:00
|
|
|
typeCheckConfig(NAME, config, DefaultType)
|
2016-01-08 20:20:40 +01:00
|
|
|
|
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()
|
2019-02-26 12:20:34 +01:00
|
|
|
const maxScroll = this._config.offset +
|
2018-09-26 10:39:01 +02:00
|
|
|
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
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
const offsetLength = this._offsets.length
|
|
|
|
for (let i = offsetLength; i--;) {
|
|
|
|
const isActiveTarget = this._activeTarget !== this._targets[i] &&
|
|
|
|
scrollTop >= this._offsets[i] &&
|
|
|
|
(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()
|
|
|
|
|
2017-09-25 09:09:01 +02:00
|
|
|
const queries = this._selector.split(',')
|
2019-02-26 12:20:34 +01:00
|
|
|
.map(selector => `${selector}[data-target="${target}"],${selector}[href="${target}"]`)
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2017-09-25 09:09:01 +02:00
|
|
|
const link = SelectorEngine.findOne(queries.join(','))
|
|
|
|
|
|
|
|
if (link.classList.contains(ClassName.DROPDOWN_ITEM)) {
|
|
|
|
SelectorEngine
|
|
|
|
.findOne(Selector.DROPDOWN_TOGGLE, SelectorEngine.closest(link, Selector.DROPDOWN))
|
|
|
|
.classList.add(ClassName.ACTIVE)
|
2018-09-26 10:39:01 +02:00
|
|
|
|
2017-09-25 09:09:01 +02:00
|
|
|
link.classList.add(ClassName.ACTIVE)
|
2018-09-26 10:39:01 +02:00
|
|
|
} else {
|
|
|
|
// Set triggered link as active
|
2017-09-25 09:09:01 +02:00
|
|
|
link.classList.add(ClassName.ACTIVE)
|
|
|
|
|
|
|
|
SelectorEngine
|
|
|
|
.parents(link, Selector.NAV_LIST_GROUP)
|
2019-02-26 12:20:34 +01:00
|
|
|
.forEach(listGroup => {
|
2017-09-25 09:09:01 +02:00
|
|
|
// 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}`)
|
2019-02-26 12:20:34 +01:00
|
|
|
.forEach(item => item.classList.add(ClassName.ACTIVE))
|
2017-09-25 09:09:01 +02:00
|
|
|
|
|
|
|
// Handle special case when .nav-link is inside .nav-item
|
|
|
|
SelectorEngine.prev(listGroup, Selector.NAV_ITEMS)
|
2019-02-26 12:20:34 +01:00
|
|
|
.forEach(navItem => {
|
2017-09-25 09:09:01 +02:00
|
|
|
SelectorEngine.children(navItem, Selector.NAV_LINKS)
|
2019-02-26 12:20:34 +01:00
|
|
|
.forEach(item => item.classList.add(ClassName.ACTIVE))
|
2017-09-25 09:09:01 +02:00
|
|
|
})
|
|
|
|
})
|
2015-05-11 21:05:35 +02:00
|
|
|
}
|
|
|
|
|
2017-09-25 09:09:01 +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() {
|
2019-02-22 23:37:55 +01:00
|
|
|
makeArray(SelectorEngine.find(this._selector))
|
2019-02-26 12:20:34 +01:00
|
|
|
.filter(node => node.classList.contains(ClassName.ACTIVE))
|
|
|
|
.forEach(node => node.classList.remove(ClassName.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 () {
|
2017-09-25 09:09:01 +02:00
|
|
|
let data = Data.getData(this, DATA_KEY)
|
2018-09-26 10:39:01 +02:00
|
|
|
const _config = typeof config === 'object' && config
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
data = new ScrollSpy(this, _config)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof config === 'string') {
|
|
|
|
if (typeof data[config] === 'undefined') {
|
|
|
|
throw new TypeError(`No method named "${config}"`)
|
2015-05-11 21:05:35 +02:00
|
|
|
}
|
2019-02-26 12:20:34 +01:00
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
data[config]()
|
|
|
|
}
|
|
|
|
})
|
2015-05-11 21:05:35 +02:00
|
|
|
}
|
2018-06-07 21:43:04 +02:00
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
static getInstance(element) {
|
2018-06-07 21:43:04 +02:00
|
|
|
return Data.getData(element, DATA_KEY)
|
|
|
|
}
|
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
|
|
|
|
2017-09-25 09:09:01 +02:00
|
|
|
EventHandler.on(window, Event.LOAD_DATA_API, () => {
|
2019-02-22 23:37:55 +01:00
|
|
|
makeArray(SelectorEngine.find(Selector.DATA_SPY))
|
2019-02-26 12:20:34 +01:00
|
|
|
.forEach(spy => new ScrollSpy(spy, Manipulator.getDataAttributes(spy)))
|
2018-09-26 10:39:01 +02:00
|
|
|
})
|
|
|
|
|
2019-08-02 15:51:05 +02:00
|
|
|
const $ = getjQuery()
|
|
|
|
|
2018-09-26 10:39:01 +02:00
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* jQuery
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
2019-06-30 11:37:25 +02:00
|
|
|
/* istanbul ignore if */
|
2019-08-02 15:51:05 +02:00
|
|
|
if ($) {
|
2017-09-25 09:09:01 +02:00
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
2019-07-28 15:24:46 +02:00
|
|
|
$.fn[NAME] = ScrollSpy.jQueryInterface
|
2019-02-26 12:20:34 +01:00
|
|
|
$.fn[NAME].Constructor = ScrollSpy
|
|
|
|
$.fn[NAME].noConflict = () => {
|
2017-09-25 09:09:01 +02:00
|
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
2019-07-28 15:24:46 +02:00
|
|
|
return ScrollSpy.jQueryInterface
|
2017-09-25 09:09:01 +02:00
|
|
|
}
|
2018-09-26 10:39:01 +02:00
|
|
|
}
|
2015-05-11 21:05:35 +02:00
|
|
|
|
|
|
|
export default ScrollSpy
|