From 744071040eeaa6785d558f7a11c469d1a7674b32 Mon Sep 17 00:00:00 2001 From: Johann-S Date: Mon, 21 Aug 2017 17:43:04 +0200 Subject: [PATCH] Add CustomEvent polyfill and a working preventDefault for IE --- js/src/alert.js | 3 ++- js/src/dom/eventHandler.js | 55 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/js/src/alert.js b/js/src/alert.js index 87209a8608..4a63b1737d 100644 --- a/js/src/alert.js +++ b/js/src/alert.js @@ -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) { diff --git a/js/src/dom/eventHandler.js b/js/src/dom/eventHandler.js index 0627ccd268..9b9d369028 100644 --- a/js/src/dom/eventHandler.js +++ b/js/src/dom/eventHandler.js @@ -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