1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-08 06:36:33 +02:00

Simplify Modal config

This commit is contained in:
GeoSot
2021-03-03 02:17:48 +02:00
committed by XhmikosR
parent 0b34ff2fae
commit 752b001b0a
2 changed files with 29 additions and 27 deletions

View File

@@ -10,12 +10,11 @@ import {
emulateTransitionEnd, emulateTransitionEnd,
getElementFromSelector, getElementFromSelector,
getTransitionDurationFromElement, getTransitionDurationFromElement,
isVisible,
isRTL, isRTL,
isVisible,
reflow, reflow,
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'
@@ -222,6 +221,7 @@ class Modal extends BaseComponent {
_getConfig(config) { _getConfig(config) {
config = { config = {
...Default, ...Default,
...Manipulator.getDataAttributes(this._element),
...config ...config
} }
typeCheckConfig(NAME, config, DefaultType) typeCheckConfig(NAME, config, DefaultType)
@@ -509,24 +509,17 @@ class Modal extends BaseComponent {
static jQueryInterface(config, relatedTarget) { static jQueryInterface(config, relatedTarget) {
return this.each(function () { return this.each(function () {
let data = Data.get(this, DATA_KEY) const data = Modal.getInstance(this) || new Modal(this, typeof config === 'object' ? config : {})
const _config = {
...Default, if (typeof config !== 'string') {
...Manipulator.getDataAttributes(this), return
...(typeof config === 'object' && config ? config : {})
} }
if (!data) { if (typeof data[config] === 'undefined') {
data = new Modal(this, _config) throw new TypeError(`No method named "${config}"`)
} }
if (typeof config === 'string') { data[config](relatedTarget)
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
data[config](relatedTarget)
}
}) })
} }
} }
@@ -540,7 +533,7 @@ class Modal extends BaseComponent {
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) { EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
const target = getElementFromSelector(this) const target = getElementFromSelector(this)
if (this.tagName === 'A' || this.tagName === 'AREA') { if (['A', 'AREA'].includes(this.tagName)) {
event.preventDefault() event.preventDefault()
} }
@@ -557,15 +550,7 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (
}) })
}) })
let data = Data.get(target, DATA_KEY) const data = Modal.getInstance(target) || new Modal(target, Manipulator.getDataAttributes(this))
if (!data) {
const config = {
...Manipulator.getDataAttributes(target),
...Manipulator.getDataAttributes(this)
}
data = new Modal(target, config)
}
data.toggle(this) data.toggle(this)
}) })

View File

@@ -2,7 +2,7 @@ import Modal from '../../src/modal'
import EventHandler from '../../src/dom/event-handler' import EventHandler from '../../src/dom/event-handler'
/** Test helpers */ /** Test helpers */
import { getFixture, clearFixture, createEvent, jQueryMock } from '../helpers/fixture' import { clearFixture, createEvent, getFixture, jQueryMock } from '../helpers/fixture'
describe('Modal', () => { describe('Modal', () => {
let fixtureEl let fixtureEl
@@ -1090,6 +1090,23 @@ describe('Modal', () => {
expect(Modal.getInstance(div)).toBeDefined() expect(Modal.getInstance(div)).toBeDefined()
}) })
it('should create a modal with given config', () => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const div = fixtureEl.querySelector('div')
jQueryMock.fn.modal = Modal.jQueryInterface
jQueryMock.elements = [div]
jQueryMock.fn.modal.call(jQueryMock, { keyboard: false })
spyOn(Modal.prototype, 'constructor')
expect(Modal.prototype.constructor).not.toHaveBeenCalledWith(div, { keyboard: false })
const modal = Modal.getInstance(div)
expect(modal).toBeDefined()
expect(modal._config.keyboard).toBe(false)
})
it('should not re create a modal', () => { it('should not re create a modal', () => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>' fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'