0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-06 04:08:22 +01:00

Remove jQuery from alert.js and add .alert only if jQuery is available

This commit is contained in:
Johann-S 2017-08-23 12:03:50 +02:00 committed by XhmikosR
parent a3398fffd6
commit 2970d14dd9
5 changed files with 30 additions and 37 deletions

View File

@ -85,8 +85,7 @@ class Alert {
let parent = false let parent = false
if (selector) { if (selector) {
const tmpSelected = SelectorEngine.find(selector) parent = SelectorEngine.find(selector)[0]
parent = tmpSelected[0]
} }
if (!parent) { if (!parent) {
@ -136,16 +135,6 @@ class Alert {
} }
}) })
} }
static _handleDismiss(alertInstance) {
return function (event) {
if (event) {
event.preventDefault()
}
alertInstance.close(this)
}
}
} }
/** /**
@ -159,13 +148,17 @@ EventHandler.on(document, Event.CLICK_DATA_API, Selector.DISMISS, Alert._handleD
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* jQuery * jQuery
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* add .alert to jQuery only if jQuery is present
*/ */
$.fn[NAME] = Alert._jQueryInterface if (typeof window.$ !== 'undefined' || typeof window.jQuery !== 'undefined') {
$.fn[NAME].Constructor = Alert const $ = window.$ || window.jQuery
$.fn[NAME].noConflict = () => { $.fn[NAME] = Alert._jQueryInterface
$.fn[NAME] = JQUERY_NO_CONFLICT $.fn[NAME].Constructor = Alert
return Alert._jQueryInterface $.fn[NAME].noConflict = () => {
$.fn[NAME] = JQUERY_NO_CONFLICT
return Alert._jQueryInterface
}
} }
export default Alert export default Alert

View File

@ -10,7 +10,7 @@ const mapData = (() => {
return { return {
set(element, key, data) { set(element, key, data) {
let id let id
if (element.key === undefined) { if (typeof element.key === 'undefined') {
element.key = { element.key = {
key, key,
id id
@ -20,14 +20,14 @@ const mapData = (() => {
storeData[id] = data storeData[id] = data
}, },
get(element, key) { get(element, key) {
if (element.key === undefined || element.key !== key) { if (typeof element.key === 'undefined' || element.key !== key) {
return null return null
} }
const keyProperties = element.key const keyProperties = element.key
return storeData[keyProperties.id] return storeData[keyProperties.id]
}, },
delete(element, key) { delete(element, key) {
if (element.key === undefined || element.key !== key) { if (typeof element.key === 'undefined' || element.key !== key) {
return return
} }
const keyProperties = element.key const keyProperties = element.key

View File

@ -19,7 +19,8 @@ if (typeof window.CustomEvent !== 'function') {
window.CustomEvent = (event, params) => { window.CustomEvent = (event, params) => {
params = params || { params = params || {
bubbles: false, bubbles: false,
cancelable: false cancelable: false,
detail: null
} }
const evt = document.createEvent('CustomEvent') const evt = document.createEvent('CustomEvent')
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail) evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail)
@ -101,6 +102,8 @@ function bootstrapDelegationHandler(selector, fn) {
} }
} }
} }
// To please ESLint
return null
} }
} }

View File

@ -16,9 +16,8 @@ if (!Element.prototype.matches) {
} }
// closest polyfill (see: https://mzl.la/2vXggaI) // closest polyfill (see: https://mzl.la/2vXggaI)
let fnClosest = null
if (!Element.prototype.closest) { if (!Element.prototype.closest) {
fnClosest = (element, selector) => { Element.prototype.closest = (element, selector) => {
let ancestor = element let ancestor = element
if (!document.documentElement.contains(element)) { if (!document.documentElement.contains(element)) {
return null return null
@ -34,12 +33,10 @@ if (!Element.prototype.closest) {
return null return null
} }
} else {
fnClosest = (element, selector) => {
return element.closest(selector)
}
} }
const fnClosest = Element.prototype.closest
const SelectorEngine = { const SelectorEngine = {
matches(element, selector) { matches(element, selector) {
return fnMatches.call(element, selector) return fnMatches.call(element, selector)
@ -59,7 +56,7 @@ const SelectorEngine = {
}, },
closest(element, selector) { closest(element, selector) {
return fnClosest(element, selector) return fnClosest.call(element, selector)
} }
} }

View File

@ -42,8 +42,8 @@ $(function () {
var $alert = $(alertHTML).bootstrapAlert().appendTo($('#qunit-fixture')) var $alert = $(alertHTML).bootstrapAlert().appendTo($('#qunit-fixture'))
$alert.find('.close').trigger('click') var closeBtn = $alert.find('.close')[0]
EventHandler.trigger(closeBtn, 'click')
assert.strictEqual($alert.hasClass('show'), false, 'remove .show class on .close click') assert.strictEqual($alert.hasClass('show'), false, 'remove .show class on .close click')
}) })
@ -58,13 +58,13 @@ $(function () {
assert.notEqual($('#qunit-fixture').find('.alert').length, 0, 'element added to dom') assert.notEqual($('#qunit-fixture').find('.alert').length, 0, 'element added to dom')
$alert EventHandler.on($alert[0], 'closed.bs.alert', function () {
.one('closed.bs.alert', function () { assert.strictEqual($('#qunit-fixture').find('.alert').length, 0, 'element removed from dom')
assert.strictEqual($('#qunit-fixture').find('.alert').length, 0, 'element removed from dom') done()
done() })
})
.find('.close') var closeBtn = $alert.find('.close')[0]
.trigger('click') EventHandler.trigger(closeBtn, 'click')
}) })
QUnit.test('should not fire closed when close is prevented', function (assert) { QUnit.test('should not fire closed when close is prevented', function (assert) {