1
0
mirror of https://github.com/webslides/WebSlides.git synced 2025-08-21 20:35:31 +02:00

Adding custom event support

This commit is contained in:
Antonio Laguna
2017-01-28 15:38:33 +01:00
parent 7b495cccf1
commit ea164b6d69
2 changed files with 53 additions and 0 deletions

38
src/utils/custom-event.js Normal file
View File

@@ -0,0 +1,38 @@
const NativeCustomEvent = window.CustomEvent;
/**
* Check for the usage of native support for CustomEvents which is lacking
* completely on IE.
* @return {boolean} Whether it can be used or not.
*/
function canIuseNativeCustom () {
try {
const p = new NativeCustomEvent('t', { detail: { a: 'b' } });
return 't' === p.type && 'b' === p.detail.a;
} catch (e) {
}
return false;
}
/**
* Lousy polyfill for the Custom Event constructor for IE.
* @param {!string} type The type of the event.
* @param {?Object} params Additional information for the event.
* @return {Event}
* @constructor
*/
const IECustomEvent = function CustomEvent(type, params) {
const e = document.createEvent('CustomEvent');
if (params) {
e.initCustomEvent(type, params.bubbles, params.cancelable, params.detail);
} else {
e.initCustomEvent(type, false, false, undefined);
}
return e;
};
const WSCustomEvent = canIuseNativeCustom() ? NativeCustomEvent : IECustomEvent;
export default WSCustomEvent;

View File

@@ -1,3 +1,6 @@
import WSCustomEvent from './custom-event';
/**
* Static class for DOM helper.
*/
@@ -54,4 +57,16 @@ export default class DOM {
static unlockScroll() {
document.documentElement.style.overflow = 'auto';
}
/**
* Fires a custom event on the given target.
* @param {Element} target The target of the event.
* @param {string} eventType The event type.
* @param {Object} eventInfo Optional parameter to provide additional data
* to the event.
*/
static fireEvent(target, eventType, eventInfo = { detail: '' }) {
const event = new WSCustomEvent(eventType, eventInfo);
target.dispatchEvent(event);
}
}