1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-10-05 01:31:39 +02:00

Comply to the new rules

This commit is contained in:
XhmikosR
2020-06-12 21:50:30 +03:00
parent 1c37a2ba77
commit 3be585990c
35 changed files with 606 additions and 574 deletions

View File

@@ -14,42 +14,42 @@ import Util from './util'
* ------------------------------------------------------------------------
*/
const NAME = 'scrollspy'
const VERSION = '4.5.2'
const DATA_KEY = 'bs.scrollspy'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
const NAME = 'scrollspy'
const VERSION = '4.5.2'
const DATA_KEY = 'bs.scrollspy'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
const JQUERY_NO_CONFLICT = $.fn[NAME]
const Default = {
offset : 10,
method : 'auto',
target : ''
offset: 10,
method: 'auto',
target: ''
}
const DefaultType = {
offset : 'number',
method : 'string',
target : '(string|element)'
offset: 'number',
method: 'string',
target: '(string|element)'
}
const EVENT_ACTIVATE = `activate${EVENT_KEY}`
const EVENT_SCROLL = `scroll${EVENT_KEY}`
const EVENT_ACTIVATE = `activate${EVENT_KEY}`
const EVENT_SCROLL = `scroll${EVENT_KEY}`
const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`
const CLASS_NAME_DROPDOWN_ITEM = 'dropdown-item'
const CLASS_NAME_ACTIVE = 'active'
const CLASS_NAME_ACTIVE = 'active'
const SELECTOR_DATA_SPY = '[data-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_DROPDOWN = '.dropdown'
const SELECTOR_DROPDOWN_ITEMS = '.dropdown-item'
const SELECTOR_DATA_SPY = '[data-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_DROPDOWN = '.dropdown'
const SELECTOR_DROPDOWN_ITEMS = '.dropdown-item'
const SELECTOR_DROPDOWN_TOGGLE = '.dropdown-toggle'
const METHOD_OFFSET = 'offset'
const METHOD_OFFSET = 'offset'
const METHOD_POSITION = 'position'
/**
@@ -60,18 +60,18 @@ const METHOD_POSITION = 'position'
class ScrollSpy {
constructor(element, config) {
this._element = element
this._element = element
this._scrollElement = element.tagName === 'BODY' ? window : element
this._config = this._getConfig(config)
this._selector = `${this._config.target} ${SELECTOR_NAV_LINKS},` +
this._config = this._getConfig(config)
this._selector = `${this._config.target} ${SELECTOR_NAV_LINKS},` +
`${this._config.target} ${SELECTOR_LIST_ITEMS},` +
`${this._config.target} ${SELECTOR_DROPDOWN_ITEMS}`
this._offsets = []
this._targets = []
this._activeTarget = null
this._scrollHeight = 0
this._offsets = []
this._targets = []
this._activeTarget = null
this._scrollHeight = 0
$(this._scrollElement).on(EVENT_SCROLL, (event) => this._process(event))
$(this._scrollElement).on(EVENT_SCROLL, event => this._process(event))
this.refresh()
this._process()
@@ -90,14 +90,14 @@ class ScrollSpy {
// Public
refresh() {
const autoMethod = this._scrollElement === this._scrollElement.window
? METHOD_OFFSET : METHOD_POSITION
const autoMethod = this._scrollElement === this._scrollElement.window ?
METHOD_OFFSET : METHOD_POSITION
const offsetMethod = this._config.method === 'auto'
? autoMethod : this._config.method
const offsetMethod = this._config.method === 'auto' ?
autoMethod : this._config.method
const offsetBase = offsetMethod === METHOD_POSITION
? this._getScrollTop() : 0
const offsetBase = offsetMethod === METHOD_POSITION ?
this._getScrollTop() : 0
this._offsets = []
this._targets = []
@@ -107,7 +107,7 @@ class ScrollSpy {
const targets = [].slice.call(document.querySelectorAll(this._selector))
targets
.map((element) => {
.map(element => {
let target
const targetSelector = Util.getSelectorFromElement(element)
@@ -125,11 +125,12 @@ class ScrollSpy {
]
}
}
return null
})
.filter((item) => item)
.filter(item => item)
.sort((a, b) => a[0] - b[0])
.forEach((item) => {
.forEach(item => {
this._offsets.push(item[0])
this._targets.push(item[1])
})
@@ -139,14 +140,14 @@ class ScrollSpy {
$.removeData(this._element, DATA_KEY)
$(this._scrollElement).off(EVENT_KEY)
this._element = null
this._element = null
this._scrollElement = null
this._config = null
this._selector = null
this._offsets = null
this._targets = null
this._activeTarget = null
this._scrollHeight = null
this._config = null
this._selector = null
this._offsets = null
this._targets = null
this._activeTarget = null
this._scrollHeight = null
}
// Private
@@ -163,6 +164,7 @@ class ScrollSpy {
id = Util.getUID(NAME)
$(config.target).attr('id', id)
}
config.target = `#${id}`
}
@@ -172,8 +174,8 @@ class ScrollSpy {
}
_getScrollTop() {
return this._scrollElement === window
? this._scrollElement.pageYOffset : this._scrollElement.scrollTop
return this._scrollElement === window ?
this._scrollElement.pageYOffset : this._scrollElement.scrollTop
}
_getScrollHeight() {
@@ -184,14 +186,14 @@ class ScrollSpy {
}
_getOffsetHeight() {
return this._scrollElement === window
? window.innerHeight : this._scrollElement.getBoundingClientRect().height
return this._scrollElement === window ?
window.innerHeight : this._scrollElement.getBoundingClientRect().height
}
_process() {
const scrollTop = this._getScrollTop() + this._config.offset
const scrollTop = this._getScrollTop() + this._config.offset
const scrollHeight = this._getScrollHeight()
const maxScroll = this._config.offset + scrollHeight - this._getOffsetHeight()
const maxScroll = this._config.offset + scrollHeight - this._getOffsetHeight()
if (this._scrollHeight !== scrollHeight) {
this.refresh()
@@ -203,6 +205,7 @@ class ScrollSpy {
if (this._activeTarget !== target) {
this._activate(target)
}
return
}
@@ -231,7 +234,7 @@ class ScrollSpy {
const queries = this._selector
.split(',')
.map((selector) => `${selector}[data-target="${target}"],${selector}[href="${target}"]`)
.map(selector => `${selector}[data-target="${target}"],${selector}[href="${target}"]`)
const $link = $([].slice.call(document.querySelectorAll(queries.join(','))))
@@ -262,8 +265,8 @@ class ScrollSpy {
_clear() {
[].slice.call(document.querySelectorAll(this._selector))
.filter((node) => node.classList.contains(CLASS_NAME_ACTIVE))
.forEach((node) => node.classList.remove(CLASS_NAME_ACTIVE))
.filter(node => node.classList.contains(CLASS_NAME_ACTIVE))
.forEach(node => node.classList.remove(CLASS_NAME_ACTIVE))
}
// Static
@@ -282,6 +285,7 @@ class ScrollSpy {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
data[config]()
}
})