index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. var NativeCustomEvent = global.CustomEvent;
  2. function useNative () {
  3. try {
  4. var p = new NativeCustomEvent('cat', { detail: { foo: 'bar' } });
  5. return 'cat' === p.type && 'bar' === p.detail.foo;
  6. } catch (e) {
  7. }
  8. return false;
  9. }
  10. /**
  11. * Cross-browser `CustomEvent` constructor.
  12. *
  13. * https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent.CustomEvent
  14. *
  15. * @public
  16. */
  17. module.exports = useNative() ? NativeCustomEvent :
  18. // IE >= 9
  19. 'undefined' !== typeof document && 'function' === typeof document.createEvent ? function CustomEvent (type, params) {
  20. var e = document.createEvent('CustomEvent');
  21. if (params) {
  22. e.initCustomEvent(type, params.bubbles, params.cancelable, params.detail);
  23. } else {
  24. e.initCustomEvent(type, false, false, void 0);
  25. }
  26. return e;
  27. } :
  28. // IE <= 8
  29. function CustomEvent (type, params) {
  30. var e = document.createEventObject();
  31. e.type = type;
  32. if (params) {
  33. e.bubbles = Boolean(params.bubbles);
  34. e.cancelable = Boolean(params.cancelable);
  35. e.detail = params.detail;
  36. } else {
  37. e.bubbles = false;
  38. e.cancelable = false;
  39. e.detail = void 0;
  40. }
  41. return e;
  42. }