Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/bootstrap.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorGeoSot <geo.sotis@gmail.com>2021-04-06 18:28:10 +0300
committerGitHub <noreply@github.com>2021-04-06 18:28:10 +0300
commit0b34ff2faedb0f5f13d91d279dbe3f8b3ae1fda7 (patch)
tree9a35459b22fe2be01a62adb8118fd907d1456344 /js
parent4fc93866961722fe727b1f206645f7cf5c1dee7e (diff)
Simplify ScrollSpy config (#33250)
Diffstat (limited to 'js')
-rw-r--r--js/src/scrollspy.js21
-rw-r--r--js/tests/unit/scrollspy.spec.js17
2 files changed, 26 insertions, 12 deletions
diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js
index 3667b9a12d..4e830b530b 100644
--- a/js/src/scrollspy.js
+++ b/js/src/scrollspy.js
@@ -12,7 +12,6 @@ import {
isElement,
typeCheckConfig
} from './util/index'
-import Data from './dom/data'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
import SelectorEngine from './dom/selector-engine'
@@ -155,6 +154,7 @@ class ScrollSpy extends BaseComponent {
_getConfig(config) {
config = {
...Default,
+ ...Manipulator.getDataAttributes(this._element),
...(typeof config === 'object' && config ? config : {})
}
@@ -278,20 +278,17 @@ class ScrollSpy extends BaseComponent {
static jQueryInterface(config) {
return this.each(function () {
- let data = Data.get(this, DATA_KEY)
- const _config = typeof config === 'object' && config
+ const data = ScrollSpy.getInstance(this) || new ScrollSpy(this, typeof config === 'object' ? config : {})
- if (!data) {
- data = new ScrollSpy(this, _config)
+ if (typeof config !== 'string') {
+ return
}
- if (typeof config === 'string') {
- if (typeof data[config] === 'undefined') {
- throw new TypeError(`No method named "${config}"`)
- }
-
- data[config]()
+ if (typeof data[config] === 'undefined') {
+ throw new TypeError(`No method named "${config}"`)
}
+
+ data[config]()
})
}
}
@@ -304,7 +301,7 @@ class ScrollSpy extends BaseComponent {
EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
SelectorEngine.find(SELECTOR_DATA_SPY)
- .forEach(spy => new ScrollSpy(spy, Manipulator.getDataAttributes(spy)))
+ .forEach(spy => new ScrollSpy(spy))
})
/**
diff --git a/js/tests/unit/scrollspy.spec.js b/js/tests/unit/scrollspy.spec.js
index f7258ba355..c4130a1aa7 100644
--- a/js/tests/unit/scrollspy.spec.js
+++ b/js/tests/unit/scrollspy.spec.js
@@ -605,6 +605,23 @@ describe('ScrollSpy', () => {
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', () => {
fixtureEl.innerHTML = '<div></div>'