mirror of
https://github.com/twbs/bootstrap.git
synced 2025-01-29 21:52:22 +01:00
Add CustomEvent polyfill and a working preventDefault for IE
This commit is contained in:
parent
d6560bbc81
commit
744071040e
@ -85,7 +85,8 @@ class Alert {
|
|||||||
let parent = false
|
let parent = false
|
||||||
|
|
||||||
if (selector) {
|
if (selector) {
|
||||||
parent = document.querySelector(selector)
|
const tmpSelected = SelectorEngine.find(selector)
|
||||||
|
parent = tmpSelected[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!parent) {
|
if (!parent) {
|
||||||
|
@ -5,6 +5,58 @@
|
|||||||
* --------------------------------------------------------------------------
|
* --------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// defaultPrevented is broken in IE.
|
||||||
|
// https://connect.microsoft.com/IE/feedback/details/790389/event-defaultprevented-returns-false-after-preventdefault-was-called
|
||||||
|
const workingDefaultPrevented = (() => {
|
||||||
|
const e = document.createEvent('CustomEvent')
|
||||||
|
e.initEvent('Bootstrap', true, true)
|
||||||
|
e.preventDefault()
|
||||||
|
return e.defaultPrevented
|
||||||
|
})()
|
||||||
|
|
||||||
|
// CustomEvent polyfill for IE (see: https://mzl.la/2v76Zvn)
|
||||||
|
if (typeof window.CustomEvent !== 'function') {
|
||||||
|
window.CustomEvent = (event, params) => {
|
||||||
|
params = params || {
|
||||||
|
bubbles: false,
|
||||||
|
cancelable: false
|
||||||
|
}
|
||||||
|
const evt = document.createEvent('CustomEvent')
|
||||||
|
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail)
|
||||||
|
if (!workingDefaultPrevented) {
|
||||||
|
const origPreventDefault = Event.prototype.preventDefault
|
||||||
|
evt.preventDefault = () => {
|
||||||
|
if (!evt.cancelable) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
origPreventDefault.call(evt)
|
||||||
|
Object.defineProperty(evt, 'defaultPrevented', {
|
||||||
|
get() {
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
configurable: true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return evt
|
||||||
|
}
|
||||||
|
|
||||||
|
window.CustomEvent.prototype = window.Event.prototype
|
||||||
|
}
|
||||||
|
|
||||||
|
// Event constructor shim
|
||||||
|
if (!window.Event || typeof window.Event !== 'function') {
|
||||||
|
const origEvent = window.Event
|
||||||
|
window.Event = (inType, params) => {
|
||||||
|
params = params || {}
|
||||||
|
const e = document.createEvent('Event')
|
||||||
|
e.initEvent(inType, Boolean(params.bubbles), Boolean(params.cancelable))
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
window.Event.prototype = origEvent.prototype
|
||||||
|
}
|
||||||
|
|
||||||
const EventHandler = {
|
const EventHandler = {
|
||||||
on(element, event, handler) {
|
on(element, event, handler) {
|
||||||
if (typeof event !== 'string' || typeof element === 'undefined') {
|
if (typeof event !== 'string' || typeof element === 'undefined') {
|
||||||
@ -31,6 +83,9 @@ const EventHandler = {
|
|||||||
bubbles: true,
|
bubbles: true,
|
||||||
cancelable: true
|
cancelable: true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Add a function 'isDefaultPrevented'
|
||||||
|
eventToDispatch.isDefaultPrevented = () => eventToDispatch.defaultPrevented
|
||||||
element.dispatchEvent(eventToDispatch)
|
element.dispatchEvent(eventToDispatch)
|
||||||
|
|
||||||
return eventToDispatch
|
return eventToDispatch
|
||||||
|
Loading…
x
Reference in New Issue
Block a user