2017-08-29 21:16:00 +02:00
|
|
|
import $ from 'jquery'
|
2015-05-11 21:05:35 +02:00
|
|
|
import Util from './util'
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2017-08-11 05:56:35 +02:00
|
|
|
* Bootstrap (v4.0.0-beta): scrollspy.js
|
2015-05-11 21:05:35 +02:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2017-08-29 21:16:00 +02:00
|
|
|
const ScrollSpy = (() => {
|
2015-05-11 21:05:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Constants
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2015-05-11 21:29:06 +02:00
|
|
|
const NAME = 'scrollspy'
|
2017-08-11 05:56:35 +02:00
|
|
|
const VERSION = '4.0.0-beta'
|
2015-05-11 21:29:06 +02:00
|
|
|
const DATA_KEY = 'bs.scrollspy'
|
2015-05-13 21:48:34 +02:00
|
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
|
|
|
const DATA_API_KEY = '.data-api'
|
2015-05-11 21:29:06 +02:00
|
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
2015-05-11 21:05:35 +02:00
|
|
|
|
2015-05-12 08:32:37 +02:00
|
|
|
const Default = {
|
2015-05-13 21:55:11 +02:00
|
|
|
offset : 10,
|
2015-05-13 22:43:56 +02:00
|
|
|
method : 'auto',
|
|
|
|
target : ''
|
2015-05-11 21:05:35 +02:00
|
|
|
}
|
|
|
|
|
2015-05-13 23:46:50 +02:00
|
|
|
const DefaultType = {
|
|
|
|
offset : 'number',
|
|
|
|
method : 'string',
|
|
|
|
target : '(string|element)'
|
|
|
|
}
|
|
|
|
|
2015-05-11 21:05:35 +02:00
|
|
|
const Event = {
|
2015-05-13 21:48:34 +02:00
|
|
|
ACTIVATE : `activate${EVENT_KEY}`,
|
|
|
|
SCROLL : `scroll${EVENT_KEY}`,
|
|
|
|
LOAD_DATA_API : `load${EVENT_KEY}${DATA_API_KEY}`
|
2015-05-11 21:05:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const ClassName = {
|
2015-08-19 06:23:53 +02:00
|
|
|
DROPDOWN_ITEM : 'dropdown-item',
|
2015-05-11 21:05:35 +02:00
|
|
|
DROPDOWN_MENU : 'dropdown-menu',
|
|
|
|
ACTIVE : 'active'
|
|
|
|
}
|
|
|
|
|
|
|
|
const Selector = {
|
2015-08-19 06:23:53 +02:00
|
|
|
DATA_SPY : '[data-spy="scroll"]',
|
|
|
|
ACTIVE : '.active',
|
2017-04-02 11:21:04 +02:00
|
|
|
NAV_LIST_GROUP : '.nav, .list-group',
|
2015-08-19 06:23:53 +02:00
|
|
|
NAV_LINKS : '.nav-link',
|
2017-09-17 09:30:37 +02:00
|
|
|
NAV_ITEMS : '.nav-item',
|
2017-04-02 11:21:04 +02:00
|
|
|
LIST_ITEMS : '.list-group-item',
|
2015-08-19 06:23:53 +02:00
|
|
|
DROPDOWN : '.dropdown',
|
|
|
|
DROPDOWN_ITEMS : '.dropdown-item',
|
|
|
|
DROPDOWN_TOGGLE : '.dropdown-toggle'
|
2015-05-11 21:05:35 +02:00
|
|
|
}
|
|
|
|
|
2015-05-13 21:55:11 +02:00
|
|
|
const OffsetMethod = {
|
|
|
|
OFFSET : 'offset',
|
|
|
|
POSITION : 'position'
|
|
|
|
}
|
|
|
|
|
2015-05-11 21:05:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Class Definition
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ScrollSpy {
|
|
|
|
|
|
|
|
constructor(element, config) {
|
2015-05-13 21:48:34 +02:00
|
|
|
this._element = element
|
2015-05-11 21:05:35 +02:00
|
|
|
this._scrollElement = element.tagName === 'BODY' ? window : element
|
2015-05-13 22:43:56 +02:00
|
|
|
this._config = this._getConfig(config)
|
2015-08-19 05:33:57 +02:00
|
|
|
this._selector = `${this._config.target} ${Selector.NAV_LINKS},`
|
2017-04-02 11:21:04 +02:00
|
|
|
+ `${this._config.target} ${Selector.LIST_ITEMS},`
|
2015-08-19 05:33:57 +02:00
|
|
|
+ `${this._config.target} ${Selector.DROPDOWN_ITEMS}`
|
2015-05-11 21:05:35 +02:00
|
|
|
this._offsets = []
|
|
|
|
this._targets = []
|
|
|
|
this._activeTarget = null
|
|
|
|
this._scrollHeight = 0
|
|
|
|
|
2016-11-01 04:32:36 +01:00
|
|
|
$(this._scrollElement).on(Event.SCROLL, (event) => this._process(event))
|
2015-05-11 21:05:35 +02:00
|
|
|
|
|
|
|
this.refresh()
|
|
|
|
this._process()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// getters
|
|
|
|
|
|
|
|
static get VERSION() {
|
|
|
|
return VERSION
|
|
|
|
}
|
|
|
|
|
|
|
|
static get Default() {
|
|
|
|
return Default
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// public
|
|
|
|
|
|
|
|
refresh() {
|
2016-11-21 15:36:00 +01:00
|
|
|
const autoMethod = this._scrollElement !== this._scrollElement.window ?
|
2015-05-13 21:55:11 +02:00
|
|
|
OffsetMethod.POSITION : OffsetMethod.OFFSET
|
|
|
|
|
2016-11-21 15:36:00 +01:00
|
|
|
const offsetMethod = this._config.method === 'auto' ?
|
2015-05-13 21:55:11 +02:00
|
|
|
autoMethod : this._config.method
|
2015-05-11 21:05:35 +02:00
|
|
|
|
2016-11-21 15:36:00 +01:00
|
|
|
const offsetBase = offsetMethod === OffsetMethod.POSITION ?
|
2015-05-13 21:55:11 +02:00
|
|
|
this._getScrollTop() : 0
|
2015-05-11 21:05:35 +02:00
|
|
|
|
|
|
|
this._offsets = []
|
|
|
|
this._targets = []
|
|
|
|
|
|
|
|
this._scrollHeight = this._getScrollHeight()
|
|
|
|
|
2016-11-21 15:36:00 +01:00
|
|
|
const targets = $.makeArray($(this._selector))
|
2015-05-11 21:05:35 +02:00
|
|
|
|
|
|
|
targets
|
|
|
|
.map((element) => {
|
|
|
|
let target
|
2016-11-21 15:36:00 +01:00
|
|
|
const targetSelector = Util.getSelectorFromElement(element)
|
2015-05-11 21:05:35 +02:00
|
|
|
|
|
|
|
if (targetSelector) {
|
|
|
|
target = $(targetSelector)[0]
|
|
|
|
}
|
|
|
|
|
2017-03-19 02:24:54 +01:00
|
|
|
if (target) {
|
|
|
|
const targetBCR = target.getBoundingClientRect()
|
|
|
|
if (targetBCR.width || targetBCR.height) {
|
|
|
|
// todo (fat): remove sketch reliance on jQuery position/offset
|
|
|
|
return [
|
|
|
|
$(target)[offsetMethod]().top + offsetBase,
|
|
|
|
targetSelector
|
|
|
|
]
|
|
|
|
}
|
2015-05-11 21:05:35 +02:00
|
|
|
}
|
2016-05-31 09:45:32 +02:00
|
|
|
return null
|
2015-05-11 21:05:35 +02:00
|
|
|
})
|
|
|
|
.filter((item) => item)
|
|
|
|
.sort((a, b) => a[0] - b[0])
|
|
|
|
.forEach((item) => {
|
|
|
|
this._offsets.push(item[0])
|
|
|
|
this._targets.push(item[1])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-13 21:48:34 +02:00
|
|
|
dispose() {
|
|
|
|
$.removeData(this._element, DATA_KEY)
|
|
|
|
$(this._scrollElement).off(EVENT_KEY)
|
|
|
|
|
|
|
|
this._element = null
|
|
|
|
this._scrollElement = null
|
|
|
|
this._config = null
|
|
|
|
this._selector = null
|
|
|
|
this._offsets = null
|
|
|
|
this._targets = null
|
|
|
|
this._activeTarget = null
|
|
|
|
this._scrollHeight = null
|
|
|
|
}
|
|
|
|
|
2015-05-11 21:05:35 +02:00
|
|
|
|
|
|
|
// private
|
|
|
|
|
2015-05-13 22:43:56 +02:00
|
|
|
_getConfig(config) {
|
|
|
|
config = $.extend({}, Default, config)
|
|
|
|
|
|
|
|
if (typeof config.target !== 'string') {
|
|
|
|
let id = $(config.target).attr('id')
|
|
|
|
if (!id) {
|
|
|
|
id = Util.getUID(NAME)
|
|
|
|
$(config.target).attr('id', id)
|
|
|
|
}
|
|
|
|
config.target = `#${id}`
|
|
|
|
}
|
|
|
|
|
2015-05-13 23:46:50 +02:00
|
|
|
Util.typeCheckConfig(NAME, config, DefaultType)
|
|
|
|
|
2015-05-13 22:43:56 +02:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2015-05-11 21:05:35 +02:00
|
|
|
_getScrollTop() {
|
|
|
|
return this._scrollElement === window ?
|
2017-01-04 18:44:50 +01:00
|
|
|
this._scrollElement.pageYOffset : this._scrollElement.scrollTop
|
2015-05-11 21:05:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_getScrollHeight() {
|
|
|
|
return this._scrollElement.scrollHeight || Math.max(
|
|
|
|
document.body.scrollHeight,
|
|
|
|
document.documentElement.scrollHeight
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-01-08 20:20:40 +01:00
|
|
|
_getOffsetHeight() {
|
|
|
|
return this._scrollElement === window ?
|
2017-03-19 02:24:54 +01:00
|
|
|
window.innerHeight : this._scrollElement.getBoundingClientRect().height
|
2016-01-08 20:20:40 +01:00
|
|
|
}
|
|
|
|
|
2015-05-11 21:05:35 +02:00
|
|
|
_process() {
|
2016-11-21 15:36:00 +01:00
|
|
|
const scrollTop = this._getScrollTop() + this._config.offset
|
|
|
|
const scrollHeight = this._getScrollHeight()
|
|
|
|
const maxScroll = this._config.offset
|
2015-05-11 21:05:35 +02:00
|
|
|
+ scrollHeight
|
2016-01-08 20:20:40 +01:00
|
|
|
- this._getOffsetHeight()
|
2015-05-11 21:05:35 +02:00
|
|
|
|
|
|
|
if (this._scrollHeight !== scrollHeight) {
|
|
|
|
this.refresh()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scrollTop >= maxScroll) {
|
2016-11-21 15:36:00 +01:00
|
|
|
const target = this._targets[this._targets.length - 1]
|
2015-05-11 21:05:35 +02:00
|
|
|
|
|
|
|
if (this._activeTarget !== target) {
|
|
|
|
this._activate(target)
|
|
|
|
}
|
2016-01-08 20:20:40 +01:00
|
|
|
return
|
2015-05-11 21:05:35 +02:00
|
|
|
}
|
|
|
|
|
2016-11-28 01:20:33 +01:00
|
|
|
if (this._activeTarget && scrollTop < this._offsets[0] && this._offsets[0] > 0) {
|
2015-05-11 21:05:35 +02:00
|
|
|
this._activeTarget = null
|
|
|
|
this._clear()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = this._offsets.length; i--;) {
|
2016-11-21 15:36:00 +01:00
|
|
|
const isActiveTarget = this._activeTarget !== this._targets[i]
|
2015-05-11 21:05:35 +02:00
|
|
|
&& scrollTop >= this._offsets[i]
|
2017-07-27 12:39:55 +02:00
|
|
|
&& (typeof this._offsets[i + 1] === 'undefined' ||
|
2015-05-11 21:05:35 +02:00
|
|
|
scrollTop < this._offsets[i + 1])
|
|
|
|
|
|
|
|
if (isActiveTarget) {
|
|
|
|
this._activate(this._targets[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_activate(target) {
|
|
|
|
this._activeTarget = target
|
|
|
|
|
|
|
|
this._clear()
|
|
|
|
|
2015-08-19 05:33:57 +02:00
|
|
|
let queries = this._selector.split(',')
|
2017-07-27 12:39:55 +02:00
|
|
|
// eslint-disable-next-line arrow-body-style
|
2015-08-19 05:33:57 +02:00
|
|
|
queries = queries.map((selector) => {
|
|
|
|
return `${selector}[data-target="${target}"],` +
|
|
|
|
`${selector}[href="${target}"]`
|
|
|
|
})
|
2015-05-11 21:05:35 +02:00
|
|
|
|
2016-11-21 15:36:00 +01:00
|
|
|
const $link = $(queries.join(','))
|
2015-05-11 21:05:35 +02:00
|
|
|
|
2015-08-19 05:33:57 +02:00
|
|
|
if ($link.hasClass(ClassName.DROPDOWN_ITEM)) {
|
2015-08-19 06:23:53 +02:00
|
|
|
$link.closest(Selector.DROPDOWN).find(Selector.DROPDOWN_TOGGLE).addClass(ClassName.ACTIVE)
|
|
|
|
$link.addClass(ClassName.ACTIVE)
|
|
|
|
} else {
|
2017-04-02 11:21:04 +02:00
|
|
|
// Set triggered link as active
|
|
|
|
$link.addClass(ClassName.ACTIVE)
|
|
|
|
// Set triggered links parents as active
|
|
|
|
// With both <ul> and <nav> markup a parent is the previous sibling of any nav ancestor
|
|
|
|
$link.parents(Selector.NAV_LIST_GROUP).prev(`${Selector.NAV_LINKS}, ${Selector.LIST_ITEMS}`).addClass(ClassName.ACTIVE)
|
2017-09-17 09:30:37 +02:00
|
|
|
// Handle special case when .nav-link is inside .nav-item
|
|
|
|
$link.parents(Selector.NAV_LIST_GROUP).prev(Selector.NAV_ITEMS).children(Selector.NAV_LINKS).addClass(ClassName.ACTIVE)
|
2015-05-11 21:05:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$(this._scrollElement).trigger(Event.ACTIVATE, {
|
|
|
|
relatedTarget: target
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_clear() {
|
2015-08-19 06:23:53 +02:00
|
|
|
$(this._selector).filter(Selector.ACTIVE).removeClass(ClassName.ACTIVE)
|
2015-05-11 21:05:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// static
|
|
|
|
|
|
|
|
static _jQueryInterface(config) {
|
|
|
|
return this.each(function () {
|
2016-11-21 15:36:00 +01:00
|
|
|
let data = $(this).data(DATA_KEY)
|
|
|
|
const _config = typeof config === 'object' && config
|
2015-05-11 21:05:35 +02:00
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
data = new ScrollSpy(this, _config)
|
|
|
|
$(this).data(DATA_KEY, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof config === 'string') {
|
2017-07-27 12:39:55 +02:00
|
|
|
if (typeof data[config] === 'undefined') {
|
2015-09-16 10:35:29 +02:00
|
|
|
throw new Error(`No method named "${config}"`)
|
|
|
|
}
|
2015-05-11 21:05:35 +02:00
|
|
|
data[config]()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* Data Api implementation
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2015-08-19 04:22:46 +02:00
|
|
|
$(window).on(Event.LOAD_DATA_API, () => {
|
2016-11-21 15:36:00 +01:00
|
|
|
const scrollSpys = $.makeArray($(Selector.DATA_SPY))
|
2015-05-11 21:05:35 +02:00
|
|
|
|
|
|
|
for (let i = scrollSpys.length; i--;) {
|
2016-11-21 15:36:00 +01:00
|
|
|
const $spy = $(scrollSpys[i])
|
2015-05-11 21:05:35 +02:00
|
|
|
ScrollSpy._jQueryInterface.call($spy, $spy.data())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
* jQuery
|
|
|
|
* ------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
$.fn[NAME] = ScrollSpy._jQueryInterface
|
|
|
|
$.fn[NAME].Constructor = ScrollSpy
|
|
|
|
$.fn[NAME].noConflict = function () {
|
|
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
|
|
|
return ScrollSpy._jQueryInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
return ScrollSpy
|
|
|
|
|
2017-10-03 14:27:36 +02:00
|
|
|
})($)
|
2015-05-11 21:05:35 +02:00
|
|
|
|
|
|
|
export default ScrollSpy
|