1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-26 23:04:32 +02:00

Comply to the new rules.

This commit is contained in:
XhmikosR
2017-12-16 14:00:38 +02:00
parent 6d336502c7
commit 80d0943b95
32 changed files with 1798 additions and 1760 deletions

View File

@@ -1,7 +1,6 @@
import $ from 'jquery'
import Util from './util'
/**
* --------------------------------------------------------------------------
* Bootstrap (v4.0.0-beta.3): scrollspy.js
@@ -10,8 +9,6 @@ import Util from './util'
*/
const ScrollSpy = (($) => {
/**
* ------------------------------------------------------------------------
* Constants
@@ -66,7 +63,6 @@ const ScrollSpy = (($) => {
POSITION : 'position'
}
/**
* ------------------------------------------------------------------------
* Class Definition
@@ -74,14 +70,13 @@ const ScrollSpy = (($) => {
*/
class ScrollSpy {
constructor(element, config) {
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.target} ${Selector.LIST_ITEMS},`
+ `${this._config.target} ${Selector.DROPDOWN_ITEMS}`
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
@@ -93,8 +88,7 @@ const ScrollSpy = (($) => {
this._process()
}
// getters
// Getters
static get VERSION() {
return VERSION
@@ -104,18 +98,17 @@ const ScrollSpy = (($) => {
return Default
}
// public
// Public
refresh() {
const autoMethod = this._scrollElement !== this._scrollElement.window ?
OffsetMethod.POSITION : OffsetMethod.OFFSET
const autoMethod = this._scrollElement === this._scrollElement.window
? OffsetMethod.OFFSET : OffsetMethod.POSITION
const offsetMethod = this._config.method === 'auto' ?
autoMethod : this._config.method
const offsetMethod = this._config.method === 'auto'
? autoMethod : this._config.method
const offsetBase = offsetMethod === OffsetMethod.POSITION ?
this._getScrollTop() : 0
const offsetBase = offsetMethod === OffsetMethod.POSITION
? this._getScrollTop() : 0
this._offsets = []
this._targets = []
@@ -136,7 +129,7 @@ const ScrollSpy = (($) => {
if (target) {
const targetBCR = target.getBoundingClientRect()
if (targetBCR.width || targetBCR.height) {
// todo (fat): remove sketch reliance on jQuery position/offset
// TODO (fat): remove sketch reliance on jQuery position/offset
return [
$(target)[offsetMethod]().top + offsetBase,
targetSelector
@@ -145,8 +138,8 @@ const ScrollSpy = (($) => {
}
return null
})
.filter((item) => item)
.sort((a, b) => a[0] - b[0])
.filter((item) => item)
.sort((a, b) => a[0] - b[0])
.forEach((item) => {
this._offsets.push(item[0])
this._targets.push(item[1])
@@ -167,8 +160,7 @@ const ScrollSpy = (($) => {
this._scrollHeight = null
}
// private
// Private
_getConfig(config) {
config = {
@@ -191,8 +183,8 @@ const ScrollSpy = (($) => {
}
_getScrollTop() {
return this._scrollElement === window ?
this._scrollElement.pageYOffset : this._scrollElement.scrollTop
return this._scrollElement === window
? this._scrollElement.pageYOffset : this._scrollElement.scrollTop
}
_getScrollHeight() {
@@ -203,16 +195,16 @@ const 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 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()
@@ -234,9 +226,9 @@ const ScrollSpy = (($) => {
}
for (let i = this._offsets.length; i--;) {
const isActiveTarget = this._activeTarget !== this._targets[i]
&& scrollTop >= this._offsets[i]
&& (typeof this._offsets[i + 1] === 'undefined' ||
const isActiveTarget = this._activeTarget !== this._targets[i] &&
scrollTop >= this._offsets[i] &&
(typeof this._offsets[i + 1] === 'undefined' ||
scrollTop < this._offsets[i + 1])
if (isActiveTarget) {
@@ -252,7 +244,7 @@ const ScrollSpy = (($) => {
let queries = this._selector.split(',')
// eslint-disable-next-line arrow-body-style
queries = queries.map((selector) => {
queries = queries.map((selector) => {
return `${selector}[data-target="${target}"],` +
`${selector}[href="${target}"]`
})
@@ -281,12 +273,11 @@ const ScrollSpy = (($) => {
$(this._selector).filter(Selector.ACTIVE).removeClass(ClassName.ACTIVE)
}
// static
// Static
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
let data = $(this).data(DATA_KEY)
const _config = typeof config === 'object' && config
if (!data) {
@@ -296,17 +287,14 @@ const ScrollSpy = (($) => {
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new Error(`No method named "${config}"`)
throw new TypeError(`No method named "${config}"`)
}
data[config]()
}
})
}
}
/**
* ------------------------------------------------------------------------
* Data Api implementation
@@ -322,22 +310,20 @@ const ScrollSpy = (($) => {
}
})
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
$.fn[NAME] = ScrollSpy._jQueryInterface
$.fn[NAME] = ScrollSpy._jQueryInterface
$.fn[NAME].Constructor = ScrollSpy
$.fn[NAME].noConflict = function () {
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT
return ScrollSpy._jQueryInterface
}
return ScrollSpy
})($)
export default ScrollSpy