diff options
author | Johann-S <johann.servoire@gmail.com> | 2018-06-07 22:43:04 +0300 |
---|---|---|
committer | XhmikosR <xhmikosr@gmail.com> | 2019-02-20 23:05:45 +0300 |
commit | 2b780787797da2bed2af0f95963be61e2b8e94a4 (patch) | |
tree | c5efa177458d3a64b4a072137b6c171ac65bfcfd /js/src | |
parent | 7eddee286eb76da4d057a59706e4c512206dab45 (diff) |
fix(data): do not use data object in our unit tests
Diffstat (limited to 'js/src')
-rw-r--r-- | js/src/alert.js | 4 | ||||
-rw-r--r-- | js/src/button.js | 4 | ||||
-rw-r--r-- | js/src/carousel.js | 5 | ||||
-rw-r--r-- | js/src/collapse.js | 4 | ||||
-rw-r--r-- | js/src/dom/polyfill.js | 94 | ||||
-rw-r--r-- | js/src/dropdown.js | 4 | ||||
-rw-r--r-- | js/src/index.js | 4 | ||||
-rw-r--r-- | js/src/modal.js | 4 | ||||
-rw-r--r-- | js/src/popover.js | 4 | ||||
-rw-r--r-- | js/src/scrollspy.js | 4 | ||||
-rw-r--r-- | js/src/tab.js | 4 | ||||
-rw-r--r-- | js/src/tooltip.js | 4 |
12 files changed, 136 insertions, 3 deletions
diff --git a/js/src/alert.js b/js/src/alert.js index 9332aa891d..9d8a865d1c 100644 --- a/js/src/alert.js +++ b/js/src/alert.js @@ -137,6 +137,10 @@ class Alert { } }) } + + static _getInstance(element) { + return Data.getData(element, DATA_KEY) + } } /** diff --git a/js/src/button.js b/js/src/button.js index 0e3b8a9df5..ce69253e44 100644 --- a/js/src/button.js +++ b/js/src/button.js @@ -112,6 +112,10 @@ class Button { if (triggerChangeEvent) { this._element.classList.toggle(ClassName.ACTIVE) } + + static _getInstance(element) { + return Data.getData(element, DATA_KEY) + } } dispose() { diff --git a/js/src/carousel.js b/js/src/carousel.js index fd6d5bf802..15a56bd76a 100644 --- a/js/src/carousel.js +++ b/js/src/carousel.js @@ -579,6 +579,10 @@ class Carousel { event.preventDefault() } + + static _getInstance(element) { + return Data.getData(element, DATA_KEY) + } } /** @@ -597,7 +601,6 @@ EventHandler.on(window, Event.LOAD_DATA_API, () => { } }) - /** * ------------------------------------------------------------------------ * jQuery diff --git a/js/src/collapse.js b/js/src/collapse.js index 7d6aa30ed8..d04743d039 100644 --- a/js/src/collapse.js +++ b/js/src/collapse.js @@ -373,6 +373,10 @@ class Collapse { Collapse._collapseInterface(this, config) }) } + + static _getInstance(element) { + return Data.getData(element, DATA_KEY) + } } /** diff --git a/js/src/dom/polyfill.js b/js/src/dom/polyfill.js new file mode 100644 index 0000000000..644e6025b1 --- /dev/null +++ b/js/src/dom/polyfill.js @@ -0,0 +1,94 @@ +import EventHandler from './eventHandler' + +const Polyfill = (() => { + // 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 + })() + + let defaultPreventedPreservedOnDispatch = true + + // CustomEvent polyfill for IE (see: https://mzl.la/2v76Zvn) + if (typeof window.CustomEvent !== 'function') { + window.CustomEvent = (event, params) => { + params = params || { + bubbles: false, + cancelable: false, + detail: null + } + 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 + } else { + // MSEdge resets defaultPrevented flag upon dispatchEvent call if at least one listener is attached + defaultPreventedPreservedOnDispatch = (() => { + const e = new CustomEvent('Bootstrap', { + cancelable: true + }) + + const element = document.createElement('div') + element.addEventListener('Bootstrap', () => null) + + e.preventDefault() + element.dispatchEvent(e) + return e.defaultPrevented + })() + } + + // 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 + } + + // focusin and focusout polyfill + if (typeof window.onfocusin === 'undefined') { + (() => { + function listenerFocus(event) { + EventHandler.trigger(event.target, 'focusin') + } + function listenerBlur(event) { + EventHandler.trigger(event.target, 'focusout') + } + EventHandler.on(document, 'focus', 'input', listenerFocus) + EventHandler.on(document, 'blur', 'input', listenerBlur) + })() + } + + return { + get defaultPreventedPreservedOnDispatch() { + return defaultPreventedPreservedOnDispatch + } + } +})() + +export default Polyfill diff --git a/js/src/dropdown.js b/js/src/dropdown.js index ddc783c0dd..282e7645ff 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -505,6 +505,10 @@ class Dropdown { items[index].focus() } + + static _getInstance(element) { + return Data.getData(element, DATA_KEY) + } } /** diff --git a/js/src/index.js b/js/src/index.js index 327de242b2..aa35ed07b3 100644 --- a/js/src/index.js +++ b/js/src/index.js @@ -5,7 +5,7 @@ import Collapse from './collapse' import Dropdown from './dropdown' import Modal from './modal' import Popover from './popover' -import Scrollspy from './scrollspy' +import ScrollSpy from './scrollspy' import Tab from './tab' import Toast from './toast' import Tooltip from './tooltip' @@ -27,7 +27,7 @@ export { Dropdown, Modal, Popover, - Scrollspy, + ScrollSpy, Tab, Toast, Tooltip diff --git a/js/src/modal.js b/js/src/modal.js index 6da3d25aa8..4f23fff741 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -498,6 +498,10 @@ class Modal { } else { document.body.style.paddingRight = '' } + + static _getInstance(element) { + return Data.getData(element, DATA_KEY) + } } _getScrollbarWidth() { // thx d.walsh diff --git a/js/src/popover.js b/js/src/popover.js index e7c00a4cd2..b462326190 100644 --- a/js/src/popover.js +++ b/js/src/popover.js @@ -165,6 +165,10 @@ class Popover extends Tooltip { } }) } + + static _getInstance(element) { + return Data.getData(element, DATA_KEY) + } } /** diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js index ea6d528157..458f5170e9 100644 --- a/js/src/scrollspy.js +++ b/js/src/scrollspy.js @@ -308,6 +308,10 @@ class ScrollSpy { } }) } + + static _getInstance(element) { + return Data.getData(element, DATA_KEY) + } } /** diff --git a/js/src/tab.js b/js/src/tab.js index 9343413480..121669aad5 100644 --- a/js/src/tab.js +++ b/js/src/tab.js @@ -219,6 +219,10 @@ class Tab { } }) } + + static _getInstance(element) { + return Data.getData(element, DATA_KEY) + } } /** diff --git a/js/src/tooltip.js b/js/src/tooltip.js index 164f6e990f..9b8b8263a9 100644 --- a/js/src/tooltip.js +++ b/js/src/tooltip.js @@ -741,6 +741,10 @@ class Tooltip { .map((token) => token.trim()) .forEach((tClass) => tip.classList.remove(tClass)) } + + static _getInstance(element) { + return Data.getData(element, DATA_KEY) + } } _handlePopperPlacementChange(popperData) { |