2 import Util from './util'
5 * --------------------------------------------------------------------------
6 * Bootstrap (v4.1.3): scrollspy.js
7 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
8 * --------------------------------------------------------------------------
11 const ScrollSpy = (($) => {
13 * ------------------------------------------------------------------------
15 * ------------------------------------------------------------------------
18 const NAME = 'scrollspy'
19 const VERSION = '4.1.3'
20 const DATA_KEY = 'bs.scrollspy'
21 const EVENT_KEY = `.${DATA_KEY}`
22 const DATA_API_KEY = '.data-api'
23 const JQUERY_NO_CONFLICT = $.fn[NAME]
34 target : '(string|element)'
38 ACTIVATE : `activate${EVENT_KEY}`,
39 SCROLL : `scroll${EVENT_KEY}`,
40 LOAD_DATA_API : `load${EVENT_KEY}${DATA_API_KEY}`
44 DROPDOWN_ITEM : 'dropdown-item',
45 DROPDOWN_MENU : 'dropdown-menu',
50 DATA_SPY : '[data-spy="scroll"]',
52 NAV_LIST_GROUP : '.nav, .list-group',
53 NAV_LINKS : '.nav-link',
54 NAV_ITEMS : '.nav-item',
55 LIST_ITEMS : '.list-group-item',
56 DROPDOWN : '.dropdown',
57 DROPDOWN_ITEMS : '.dropdown-item',
58 DROPDOWN_TOGGLE : '.dropdown-toggle'
61 const OffsetMethod = {
67 * ------------------------------------------------------------------------
69 * ------------------------------------------------------------------------
73 constructor(element, config) {
74 this._element = element
75 this._scrollElement = element.tagName === 'BODY' ? window : element
76 this._config = this._getConfig(config)
77 this._selector = `${this._config.target} ${Selector.NAV_LINKS},` +
78 `${this._config.target} ${Selector.LIST_ITEMS},` +
79 `${this._config.target} ${Selector.DROPDOWN_ITEMS}`
82 this._activeTarget = null
83 this._scrollHeight = 0
85 $(this._scrollElement).on(Event.SCROLL, (event) => this._process(event))
93 static get VERSION() {
97 static get Default() {
104 const autoMethod = this._scrollElement === this._scrollElement.window
105 ? OffsetMethod.OFFSET : OffsetMethod.POSITION
107 const offsetMethod = this._config.method === 'auto'
108 ? autoMethod : this._config.method
110 const offsetBase = offsetMethod === OffsetMethod.POSITION
111 ? this._getScrollTop() : 0
116 this._scrollHeight = this._getScrollHeight()
118 const targets = [].slice.call(document.querySelectorAll(this._selector))
123 const targetSelector = Util.getSelectorFromElement(element)
125 if (targetSelector) {
126 target = document.querySelector(targetSelector)
130 const targetBCR = target.getBoundingClientRect()
131 if (targetBCR.width || targetBCR.height) {
132 // TODO (fat): remove sketch reliance on jQuery position/offset
134 $(target)[offsetMethod]().top + offsetBase,
141 .filter((item) => item)
142 .sort((a, b) => a[0] - b[0])
144 this._offsets.push(item[0])
145 this._targets.push(item[1])
150 $.removeData(this._element, DATA_KEY)
151 $(this._scrollElement).off(EVENT_KEY)
154 this._scrollElement = null
156 this._selector = null
159 this._activeTarget = null
160 this._scrollHeight = null
168 ...typeof config === 'object' && config ? config : {}
171 if (typeof config.target !== 'string') {
172 let id = $(config.target).attr('id')
174 id = Util.getUID(NAME)
175 $(config.target).attr('id', id)
177 config.target = `#${id}`
180 Util.typeCheckConfig(NAME, config, DefaultType)
186 return this._scrollElement === window
187 ? this._scrollElement.pageYOffset : this._scrollElement.scrollTop
191 return this._scrollElement.scrollHeight || Math.max(
192 document.body.scrollHeight,
193 document.documentElement.scrollHeight
198 return this._scrollElement === window
199 ? window.innerHeight : this._scrollElement.getBoundingClientRect().height
203 const scrollTop = this._getScrollTop() + this._config.offset
204 const scrollHeight = this._getScrollHeight()
205 const maxScroll = this._config.offset +
207 this._getOffsetHeight()
209 if (this._scrollHeight !== scrollHeight) {
213 if (scrollTop >= maxScroll) {
214 const target = this._targets[this._targets.length - 1]
216 if (this._activeTarget !== target) {
217 this._activate(target)
222 if (this._activeTarget && scrollTop < this._offsets[0] && this._offsets[0] > 0) {
223 this._activeTarget = null
228 const offsetLength = this._offsets.length
229 for (let i = offsetLength; i--;) {
230 const isActiveTarget = this._activeTarget !== this._targets[i] &&
231 scrollTop >= this._offsets[i] &&
232 (typeof this._offsets[i + 1] === 'undefined' ||
233 scrollTop < this._offsets[i + 1])
235 if (isActiveTarget) {
236 this._activate(this._targets[i])
242 this._activeTarget = target
246 let queries = this._selector.split(',')
247 // eslint-disable-next-line arrow-body-style
248 queries = queries.map((selector) => {
249 return `${selector}[data-target="${target}"],` +
250 `${selector}[href="${target}"]`
253 const $link = $([].slice.call(document.querySelectorAll(queries.join(','))))
255 if ($link.hasClass(ClassName.DROPDOWN_ITEM)) {
256 $link.closest(Selector.DROPDOWN).find(Selector.DROPDOWN_TOGGLE).addClass(ClassName.ACTIVE)
257 $link.addClass(ClassName.ACTIVE)
259 // Set triggered link as active
260 $link.addClass(ClassName.ACTIVE)
261 // Set triggered links parents as active
262 // With both <ul> and <nav> markup a parent is the previous sibling of any nav ancestor
263 $link.parents(Selector.NAV_LIST_GROUP).prev(`${Selector.NAV_LINKS}, ${Selector.LIST_ITEMS}`).addClass(ClassName.ACTIVE)
264 // Handle special case when .nav-link is inside .nav-item
265 $link.parents(Selector.NAV_LIST_GROUP).prev(Selector.NAV_ITEMS).children(Selector.NAV_LINKS).addClass(ClassName.ACTIVE)
268 $(this._scrollElement).trigger(Event.ACTIVATE, {
269 relatedTarget: target
274 const nodes = [].slice.call(document.querySelectorAll(this._selector))
275 $(nodes).filter(Selector.ACTIVE).removeClass(ClassName.ACTIVE)
280 static _jQueryInterface(config) {
281 return this.each(function () {
282 let data = $(this).data(DATA_KEY)
283 const _config = typeof config === 'object' && config
286 data = new ScrollSpy(this, _config)
287 $(this).data(DATA_KEY, data)
290 if (typeof config === 'string') {
291 if (typeof data[config] === 'undefined') {
292 throw new TypeError(`No method named "${config}"`)
301 * ------------------------------------------------------------------------
302 * Data Api implementation
303 * ------------------------------------------------------------------------
306 $(window).on(Event.LOAD_DATA_API, () => {
307 const scrollSpys = [].slice.call(document.querySelectorAll(Selector.DATA_SPY))
309 const scrollSpysLength = scrollSpys.length
310 for (let i = scrollSpysLength; i--;) {
311 const $spy = $(scrollSpys[i])
312 ScrollSpy._jQueryInterface.call($spy, $spy.data())
317 * ------------------------------------------------------------------------
319 * ------------------------------------------------------------------------
322 $.fn[NAME] = ScrollSpy._jQueryInterface
323 $.fn[NAME].Constructor = ScrollSpy
324 $.fn[NAME].noConflict = function () {
325 $.fn[NAME] = JQUERY_NO_CONFLICT
326 return ScrollSpy._jQueryInterface
332 export default ScrollSpy