0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-17 09:52:29 +01:00

Add CustomEvent polyfill and a working preventDefault for IE

This commit is contained in:
Johann-S 2017-08-21 17:43:04 +02:00 committed by XhmikosR
parent d6560bbc81
commit 744071040e
2 changed files with 57 additions and 1 deletions

View File

@ -85,7 +85,8 @@ class Alert {
let parent = false
if (selector) {
parent = document.querySelector(selector)
const tmpSelected = SelectorEngine.find(selector)
parent = tmpSelected[0]
}
if (!parent) {

View File

@ -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 = {
on(element, event, handler) {
if (typeof event !== 'string' || typeof element === 'undefined') {
@ -31,6 +83,9 @@ const EventHandler = {
bubbles: true,
cancelable: true
})
// Add a function 'isDefaultPrevented'
eventToDispatch.isDefaultPrevented = () => eventToDispatch.defaultPrevented
element.dispatchEvent(eventToDispatch)
return eventToDispatch