Simplify ScrollSpy config (#33250)

This commit is contained in:
GeoSot 2021-04-06 18:28:10 +03:00 committed by GitHub
parent 4fc9386696
commit 0b34ff2fae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 12 deletions

View File

@ -12,7 +12,6 @@ import {
isElement, isElement,
typeCheckConfig typeCheckConfig
} from './util/index' } from './util/index'
import Data from './dom/data'
import EventHandler from './dom/event-handler' import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator' import Manipulator from './dom/manipulator'
import SelectorEngine from './dom/selector-engine' import SelectorEngine from './dom/selector-engine'
@ -155,6 +154,7 @@ class ScrollSpy extends BaseComponent {
_getConfig(config) { _getConfig(config) {
config = { config = {
...Default, ...Default,
...Manipulator.getDataAttributes(this._element),
...(typeof config === 'object' && config ? config : {}) ...(typeof config === 'object' && config ? config : {})
} }
@ -278,20 +278,17 @@ class ScrollSpy extends BaseComponent {
static jQueryInterface(config) { static jQueryInterface(config) {
return this.each(function () { return this.each(function () {
let data = Data.get(this, DATA_KEY) const data = ScrollSpy.getInstance(this) || new ScrollSpy(this, typeof config === 'object' ? config : {})
const _config = typeof config === 'object' && config
if (!data) { if (typeof config !== 'string') {
data = new ScrollSpy(this, _config) return
} }
if (typeof config === 'string') { if (typeof data[config] === 'undefined') {
if (typeof data[config] === 'undefined') { throw new TypeError(`No method named "${config}"`)
throw new TypeError(`No method named "${config}"`)
}
data[config]()
} }
data[config]()
}) })
} }
} }
@ -304,7 +301,7 @@ class ScrollSpy extends BaseComponent {
EventHandler.on(window, EVENT_LOAD_DATA_API, () => { EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
SelectorEngine.find(SELECTOR_DATA_SPY) SelectorEngine.find(SELECTOR_DATA_SPY)
.forEach(spy => new ScrollSpy(spy, Manipulator.getDataAttributes(spy))) .forEach(spy => new ScrollSpy(spy))
}) })
/** /**

View File

@ -605,6 +605,23 @@ describe('ScrollSpy', () => {
expect(ScrollSpy.getInstance(div)).toBeDefined() expect(ScrollSpy.getInstance(div)).toBeDefined()
}) })
it('should create a scrollspy with given config', () => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
jQueryMock.fn.scrollspy = ScrollSpy.jQueryInterface
jQueryMock.elements = [div]
jQueryMock.fn.scrollspy.call(jQueryMock, { offset: 15 })
spyOn(ScrollSpy.prototype, 'constructor')
expect(ScrollSpy.prototype.constructor).not.toHaveBeenCalledWith(div, { offset: 15 })
const scrollspy = ScrollSpy.getInstance(div)
expect(scrollspy).toBeDefined()
expect(scrollspy._config.offset).toBe(15)
})
it('should not re create a scrollspy', () => { it('should not re create a scrollspy', () => {
fixtureEl.innerHTML = '<div></div>' fixtureEl.innerHTML = '<div></div>'