* --------------------------------------------------------------------------
*/
-import $ from 'jquery'
+import Data from './dom/data'
+import EventHandler from './dom/eventHandler'
+import Manipulator from './dom/manipulator'
+import SelectorEngine from './dom/selectorEngine'
import Util from './util'
/**
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,
this._activeTarget = null
this._scrollHeight = 0
- $(this._scrollElement).on(Event.SCROLL, (event) => this._process(event))
+ EventHandler.on(this._scrollElement, Event.SCROLL, (event) => this._process(event))
this.refresh()
this._process()
+
+ Data.setData(element, DATA_KEY, this)
}
// Getters
this._scrollHeight = this._getScrollHeight()
- const targets = [].slice.call(document.querySelectorAll(this._selector))
+ const targets = Util.makeArray(document.querySelectorAll(this._selector))
targets
.map((element) => {
if (targetBCR.width || targetBCR.height) {
// TODO (fat): remove sketch reliance on jQuery position/offset
return [
- $(target)[offsetMethod]().top + offsetBase,
+ Manipulator[offsetMethod](target).top + offsetBase,
targetSelector
]
}
}
dispose() {
- $.removeData(this._element, DATA_KEY)
- $(this._scrollElement).off(EVENT_KEY)
+ Data.removeData(this._element, DATA_KEY)
+ EventHandler.off(this._scrollElement, EVENT_KEY)
this._element = null
this._scrollElement = null
}
if (typeof config.target !== 'string') {
- let id = $(config.target).attr('id')
+ let id = config.target.id
if (!id) {
id = Util.getUID(NAME)
- $(config.target).attr('id', id)
+ config.target.id = id
}
config.target = `#${id}`
}
this._clear()
- const queries = this._selector
- .split(',')
+ const queries = this._selector.split(',')
.map((selector) => `${selector}[data-target="${target}"],${selector}[href="${target}"]`)
- const $link = $([].slice.call(document.querySelectorAll(queries.join(','))))
+ 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)
- if ($link.hasClass(ClassName.DROPDOWN_ITEM)) {
- $link.closest(Selector.DROPDOWN).find(Selector.DROPDOWN_TOGGLE).addClass(ClassName.ACTIVE)
- $link.addClass(ClassName.ACTIVE)
+ link.classList.add(ClassName.ACTIVE)
} else {
// 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)
- // 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)
+ link.classList.add(ClassName.ACTIVE)
+
+ SelectorEngine
+ .parents(link, Selector.NAV_LIST_GROUP)
+ .forEach((listGroup) => {
+ // 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}`)
+ .forEach((item) => item.classList.add(ClassName.ACTIVE))
+
+ // Handle special case when .nav-link is inside .nav-item
+ SelectorEngine.prev(listGroup, Selector.NAV_ITEMS)
+ .forEach((navItem) => {
+ SelectorEngine.children(navItem, Selector.NAV_LINKS)
+ .forEach((item) => item.classList.add(ClassName.ACTIVE))
+ })
+ })
}
- $(this._scrollElement).trigger(Event.ACTIVATE, {
+ EventHandler.trigger(this._scrollElement, Event.ACTIVATE, {
relatedTarget: target
})
}
_clear() {
- [].slice.call(document.querySelectorAll(this._selector))
+ Util.makeArray(document.querySelectorAll(this._selector))
.filter((node) => node.classList.contains(ClassName.ACTIVE))
.forEach((node) => node.classList.remove(ClassName.ACTIVE))
}
static _jQueryInterface(config) {
return this.each(function () {
- let data = $(this).data(DATA_KEY)
+ let data = Data.getData(this, DATA_KEY)
const _config = typeof config === 'object' && config
if (!data) {
data = new ScrollSpy(this, _config)
- $(this).data(DATA_KEY, data)
}
if (typeof config === 'string') {
* ------------------------------------------------------------------------
*/
-$(window).on(Event.LOAD_DATA_API, () => {
- const scrollSpys = [].slice.call(document.querySelectorAll(Selector.DATA_SPY))
- const scrollSpysLength = scrollSpys.length
-
- for (let i = scrollSpysLength; i--;) {
- const $spy = $(scrollSpys[i])
- ScrollSpy._jQueryInterface.call($spy, $spy.data())
- }
+EventHandler.on(window, Event.LOAD_DATA_API, () => {
+ Util.makeArray(SelectorEngine.find(Selector.DATA_SPY))
+ .forEach((spy) => new ScrollSpy(spy, Util.getDataAttributes(spy)))
})
/**
* ------------------------------------------------------------------------
*/
-$.fn[NAME] = ScrollSpy._jQueryInterface
-$.fn[NAME].Constructor = ScrollSpy
-$.fn[NAME].noConflict = () => {
- $.fn[NAME] = JQUERY_NO_CONFLICT
- return ScrollSpy._jQueryInterface
+const $ = Util.jQuery
+if (typeof $ !== 'undefined') {
+ const JQUERY_NO_CONFLICT = $.fn[NAME]
+ $.fn[NAME] = ScrollSpy._jQueryInterface
+ $.fn[NAME].Constructor = ScrollSpy
+ $.fn[NAME].noConflict = () => {
+ $.fn[NAME] = JQUERY_NO_CONFLICT
+ return ScrollSpy._jQueryInterface
+ }
}
export default ScrollSpy
.show()
.find('#scrollspy-example')
.bootstrapScrollspy({
- target: '#ss-target'
+ target: 'ss-target'
})
$scrollspy.one('scroll', function () {
.show()
.find('#scrollspy-example')
.bootstrapScrollspy({
- target: document.getElementById('#ss-target')
+ target: document.getElementById('ss-target')
})
$scrollspy.one('scroll', function () {
$scrollspy
.bootstrapScrollspy({
target: '#navigation',
- offset: $scrollspy.position().top
+ offset: $scrollspy[0].offsetTop
})
.one('scroll', function () {
assert.strictEqual($('.active').length, 1, '"active" class on only one element present')
method: 'offset'
})
} else if (type === 'data') {
- $(window).trigger('load')
+ EventHandler.trigger(window, 'load')
}
var $target = $('#div-' + type + 'm-2')
- var scrollspy = $content.data('bs.scrollspy')
+ var scrollspy = Data.getData($content[0], 'bs.scrollspy')
assert.ok(scrollspy._offsets[1] === $target.offset().top, 'offset method with ' + type + ' option')
assert.ok(scrollspy._offsets[1] !== $target.position().top, 'position method with ' + type + ' option')
method: 'position'
})
} else if (type === 'data') {
- $(window).trigger('load')
+ EventHandler.trigger(window, 'load')
}
var $target = $('#div-' + type + 'm-2')
- var scrollspy = $content.data('bs.scrollspy')
+ var scrollspy = Data.getData($content[0], 'bs.scrollspy')
assert.ok(scrollspy._offsets[1] !== $target.offset().top, 'offset method with ' + type + ' option')
assert.ok(scrollspy._offsets[1] === $target.position().top, 'position method with ' + type + ' option')