Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 752b001

Browse files
GeoSotXhmikosR
authored andcommitted
Simplify Modal config
1 parent 0b34ff2 commit 752b001

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

js/src/modal.js

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import {
1010
emulateTransitionEnd,
1111
getElementFromSelector,
1212
getTransitionDurationFromElement,
13-
isVisible,
1413
isRTL,
14+
isVisible,
1515
reflow,
1616
typeCheckConfig
1717
} from './util/index'
18-
import Data from './dom/data'
1918
import EventHandler from './dom/event-handler'
2019
import Manipulator from './dom/manipulator'
2120
import SelectorEngine from './dom/selector-engine'
@@ -222,6 +221,7 @@ class Modal extends BaseComponent {
222221
_getConfig(config) {
223222
config = {
224223
...Default,
224+
...Manipulator.getDataAttributes(this._element),
225225
...config
226226
}
227227
typeCheckConfig(NAME, config, DefaultType)
@@ -509,24 +509,17 @@ class Modal extends BaseComponent {
509509

510510
static jQueryInterface(config, relatedTarget) {
511511
return this.each(function () {
512-
let data = Data.get(this, DATA_KEY)
513-
const _config = {
514-
...Default,
515-
...Manipulator.getDataAttributes(this),
516-
...(typeof config === 'object' && config ? config : {})
517-
}
512+
const data = Modal.getInstance(this) || new Modal(this, typeof config === 'object' ? config : {})
518513

519-
if (!data) {
520-
data = new Modal(this, _config)
514+
if (typeof config !== 'string') {
515+
return
521516
}
522517

523-
if (typeof config === 'string') {
524-
if (typeof data[config] === 'undefined') {
525-
throw new TypeError(`No method named "${config}"`)
526-
}
527-
528-
data[config](relatedTarget)
518+
if (typeof data[config] === 'undefined') {
519+
throw new TypeError(`No method named "${config}"`)
529520
}
521+
522+
data[config](relatedTarget)
530523
})
531524
}
532525
}
@@ -540,7 +533,7 @@ class Modal extends BaseComponent {
540533
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
541534
const target = getElementFromSelector(this)
542535

543-
if (this.tagName === 'A' || this.tagName === 'AREA') {
536+
if (['A', 'AREA'].includes(this.tagName)) {
544537
event.preventDefault()
545538
}
546539

@@ -557,15 +550,7 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (
557550
})
558551
})
559552

560-
let data = Data.get(target, DATA_KEY)
561-
if (!data) {
562-
const config = {
563-
...Manipulator.getDataAttributes(target),
564-
...Manipulator.getDataAttributes(this)
565-
}
566-
567-
data = new Modal(target, config)
568-
}
553+
const data = Modal.getInstance(target) || new Modal(target, Manipulator.getDataAttributes(this))
569554

570555
data.toggle(this)
571556
})

js/tests/unit/modal.spec.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Modal from '../../src/modal'
22
import EventHandler from '../../src/dom/event-handler'
33

44
/** Test helpers */
5-
import { getFixture, clearFixture, createEvent, jQueryMock } from '../helpers/fixture'
5+
import { clearFixture, createEvent, getFixture, jQueryMock } from '../helpers/fixture'
66

77
describe('Modal', () => {
88
let fixtureEl
@@ -1090,6 +1090,23 @@ describe('Modal', () => {
10901090
expect(Modal.getInstance(div)).toBeDefined()
10911091
})
10921092

1093+
it('should create a modal with given config', () => {
1094+
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
1095+
1096+
const div = fixtureEl.querySelector('div')
1097+
1098+
jQueryMock.fn.modal = Modal.jQueryInterface
1099+
jQueryMock.elements = [div]
1100+
1101+
jQueryMock.fn.modal.call(jQueryMock, { keyboard: false })
1102+
spyOn(Modal.prototype, 'constructor')
1103+
expect(Modal.prototype.constructor).not.toHaveBeenCalledWith(div, { keyboard: false })
1104+
1105+
const modal = Modal.getInstance(div)
1106+
expect(modal).toBeDefined()
1107+
expect(modal._config.keyboard).toBe(false)
1108+
})
1109+
10931110
it('should not re create a modal', () => {
10941111
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
10951112

0 commit comments

Comments
 (0)